World working

This commit is contained in:
Rendo 2026-03-11 14:25:42 +05:00
commit 76284d80b9
4 changed files with 35 additions and 16 deletions

View file

@ -1,10 +1,10 @@
#include <iostream>
#include <raylib.h>
#include <raymath.h>
#include "components.h"
// Pacman definitions
Rectangle Pacman::getTextureRect(){
Rectangle result;
result.x = 0;
@ -21,7 +21,7 @@ Pacman::Pacman() {
Pacman::Pacman(Vector2 position) {
this->texture = LoadTexture("assets/sprites/pacman.png");
this->facing = 0;
this->position = position;
this->position = {0.,0.};
}
Pacman::~Pacman() {
UnloadTexture(this->texture);
@ -38,12 +38,24 @@ void Pacman::tick() {
Vector2 direction = {(float)(cos(angle)),(float)(sin(-angle))};
this->position = Vector2Add(this->position,Vector2Scale(direction, (float)speed));
// Bound check
if (this->position.x < 0){
this->position.x += 320;
}
if (this->position.y < 0){
this->position.y += 320;
}
if (this->position.x >= 320){
this->position.x -= 320;
}
if (this->position.y >= 320){
this->position.y -= 320;
}
}
void Pacman::draw() {
// Drawing
DrawTextureRec(this->texture, this->getTextureRect(), this->position, WHITE);
DrawText(TextFormat("Position: %"), int posX, int posY, int fontSize, Color color)
}
// Wall definitions

View file

@ -3,17 +3,18 @@
#include "components.h"
#include "world.h"
int main() {
const int screen_height = 320;
const int screen_width = 320;
const int SCREEN_HEIGHT = 320;
const int SCREEN_WIDTH = 320;
InitWindow(screen_width, screen_height, "Test game");
int main() {
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Test game");
SetTargetFPS(60);
world = create_world_with(1.0);
world = create_world_with(.25);
world.entities.push_back(Pacman({screen_width/2,screen_height/2}));
world.entities.push_back(new Pacman({SCREEN_WIDTH/2,SCREEN_HEIGHT/2}));
world.setup();
@ -25,7 +26,6 @@ int main() {
ClearBackground(BLACK);
DrawText(TextFormat("Entities: %i",world.entities.size()),0,0, 14,WHITE);
world.draw();
EndDrawing();

View file

@ -7,18 +7,24 @@ World::World(){
this->clock = 0;
}
World::~World(){
for(int i = 0; i < this->entities.size();i++) {
delete this->entities[i];
}
}
void World::setup(){
for(int i = 0; i < this->entities.size(); i++)
this->entities[i].ready();
this->entities[i]->ready();
}
void World::process(){
this->clock += GetFrameTime();
for(int i = 0; i < this->entities.size(); i++) {
this->entities[i].process();
this->entities[i]->process();
if (this->clock>=this->seconds_per_tick) {
this->entities[i].tick();
this->entities[i]->tick();
}
}
@ -28,8 +34,8 @@ void World::process(){
}
void World::draw() {
for(int i = 0; i< this->entities.size(); i++) {
this->entities[i].draw();
for(int i = 0; i < this->entities.size(); i++) {
this->entities[i]->draw();
}
}

View file

@ -8,8 +8,9 @@
class World {
public:
World();
~World();
std::vector<Entity> entities; /// Main subjects of game world.
std::vector<Entity*> entities; /// Main subjects of game world.
float seconds_per_tick; /// Internal clock speed
void setup(); /// Sets up game world.