Compare commits

..

2 commits

Author SHA1 Message Date
63e458ca68 segf 2026-03-14 00:27:31 +05:00
efcbf48299 src/pacman.cpp 2026-03-13 15:28:56 +05:00
5 changed files with 37 additions and 3 deletions

View file

@ -15,6 +15,10 @@ class Entity{
virtual void process() {} virtual void process() {}
virtual void draw() const {} virtual void draw() const {}
virtual void tick() {} 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 process() override;
void animation_tick(); void animation_tick();
void draw() const override; void draw() const override;
void collision(Entity* with) override;
}; };
class Wall : public Entity { class Wall : public Entity {

17
src/entity.cpp Normal file
View file

@ -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;
}
}

View file

@ -37,7 +37,7 @@ void Pacman::tick() {
World& world = get_world(); World& world = get_world();
if (world.grid[indexify_position(new_position)] == nullptr){ if (dynamic_cast<Wall*>(world.grid[indexify_position(new_position)]) == nullptr){
this->position=new_position; this->position=new_position;
} }
@ -73,5 +73,12 @@ void Pacman::draw() const {
DrawTextureRec(this->texture.get_texture(), this->getTextureRect(), this->position, WHITE); DrawTextureRec(this->texture.get_texture(), this->getTextureRect(), this->position, WHITE);
} }
void Pacman::collision(Entity* with){
Scorepoint* score = dynamic_cast<Scorepoint*>(with);
if (score != nullptr) {
score->queue_free();
}
}

View file

@ -1,10 +1,8 @@
#include "components.h" #include "components.h"
#include "world.h" #include "world.h"
#include <iostream>
#include <raylib.h> #include <raylib.h>
Wall::Wall() { Wall::Wall() {
std::cout << get_world().get_atlas() <<'\n';
this->texture = TextureAtlas(get_world().get_atlas(),0,64,16,16); this->texture = TextureAtlas(get_world().get_atlas(),0,64,16,16);
} }

View file

@ -92,6 +92,13 @@ void World::update_grid() {
int indexified_position = indexify_position(this->entities[i]->position); int indexified_position = indexify_position(this->entities[i]->position);
if (indexified_position < 0 || indexified_position >= GRID_CAPACITY) if (indexified_position < 0 || indexified_position >= GRID_CAPACITY)
continue; 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]; grid[indexified_position] = this->entities[i];
} }
} }