From 63e458ca68e9220cbbb70d3729084f8d27a274ac Mon Sep 17 00:00:00 2001 From: Rendo Date: Sat, 14 Mar 2026 00:27:31 +0500 Subject: [PATCH] segf --- src/components.h | 5 +++++ src/entity.cpp | 17 +++++++++++++++++ src/pacman.cpp | 7 +++++++ src/wall.cpp | 2 -- src/world.cpp | 7 +++++++ 5 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 src/entity.cpp diff --git a/src/components.h b/src/components.h index d0c9d8c..23f3eeb 100644 --- a/src/components.h +++ b/src/components.h @@ -15,6 +15,10 @@ class Entity{ virtual void process() {} virtual void draw() const {} virtual void tick() {} + virtual void collision(Entity* with) {} + void queue_free(); + private: + bool free_queued; }; @@ -36,6 +40,7 @@ class Pacman : public Entity{ void process() override; void animation_tick(); void draw() const override; + void collision(Entity* with) override; }; class Wall : public Entity { diff --git a/src/entity.cpp b/src/entity.cpp new file mode 100644 index 0000000..7df294f --- /dev/null +++ b/src/entity.cpp @@ -0,0 +1,17 @@ +#include "components.h" +#include "world.h" + +void Entity::queue_free() { + if (this->free_queued) + return; + + this->free_queued = true; + World& world = get_world(); + + for(int i = 0; i < world.entities.size(); i++) + if(world.entities[i] == this) { + delete world.entities[i]; + world.entities.erase(world.entities.begin()+i); + break; + } +} diff --git a/src/pacman.cpp b/src/pacman.cpp index 867d544..c1b4755 100644 --- a/src/pacman.cpp +++ b/src/pacman.cpp @@ -73,5 +73,12 @@ void Pacman::draw() const { DrawTextureRec(this->texture.get_texture(), this->getTextureRect(), this->position, WHITE); } +void Pacman::collision(Entity* with){ + Scorepoint* score = dynamic_cast(with); + if (score != nullptr) { + score->queue_free(); + } + +} diff --git a/src/wall.cpp b/src/wall.cpp index e3c3f05..87f5db7 100644 --- a/src/wall.cpp +++ b/src/wall.cpp @@ -1,10 +1,8 @@ #include "components.h" #include "world.h" -#include #include Wall::Wall() { - std::cout << get_world().get_atlas() <<'\n'; this->texture = TextureAtlas(get_world().get_atlas(),0,64,16,16); } diff --git a/src/world.cpp b/src/world.cpp index 2577a32..e03690b 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -92,6 +92,13 @@ void World::update_grid() { int indexified_position = indexify_position(this->entities[i]->position); if (indexified_position < 0 || indexified_position >= GRID_CAPACITY) continue; + if(grid[indexified_position] != nullptr){ + Entity* entity = this->entities[i]; + grid[indexified_position]->collision(entity); + if(entity != nullptr && grid[indexified_position] != nullptr && grid[indexified_position] != entity){ + entity->collision(grid[indexified_position]); + } + } grid[indexified_position] = this->entities[i]; } }