From 76284d80b9b5fdbb2fc24dcb5ed64ca09b79931f Mon Sep 17 00:00:00 2001 From: Rendo Date: Wed, 11 Mar 2026 14:25:42 +0500 Subject: [PATCH] World working --- src/components.cpp | 18 +++++++++++++++--- src/main.cpp | 14 +++++++------- src/world.cpp | 16 +++++++++++----- src/world.h | 3 ++- 4 files changed, 35 insertions(+), 16 deletions(-) diff --git a/src/components.cpp b/src/components.cpp index 9ef9ba5..b3e44e7 100644 --- a/src/components.cpp +++ b/src/components.cpp @@ -1,10 +1,10 @@ -#include #include #include #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 diff --git a/src/main.cpp b/src/main.cpp index 983897d..9616590 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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(); diff --git a/src/world.cpp b/src/world.cpp index 16f4228..641330d 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -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(); } } diff --git a/src/world.h b/src/world.h index 582a04c..5c2fbe9 100644 --- a/src/world.h +++ b/src/world.h @@ -8,8 +8,9 @@ class World { public: World(); + ~World(); - std::vector entities; /// Main subjects of game world. + std::vector entities; /// Main subjects of game world. float seconds_per_tick; /// Internal clock speed void setup(); /// Sets up game world.