From 06c0f077e954ff9f5ba17cd0e43de55485121452 Mon Sep 17 00:00:00 2001 From: Sulimov Igor Andreevich <igansulimov@edu.hse.ru> Date: Wed, 26 Mar 2025 01:03:38 +0300 Subject: [PATCH] Made GameState::process_entities_interactions --- .../Entities/DynamicEntities/IDynamicEntity.h | 3 +-- include/MazeContent/Entities/Pacman.h | 5 ++++- .../Entities/StaticEntities/IStaticEntity.h | 3 +-- source/Events/DeleteStaticEntity.cpp | 2 ++ .../Entities/DynamicEntities/Enemy.cpp | 1 + source/MazeContent/Entities/Pacman.cpp | 18 ++++++++++++++++++ .../Entities/StaticEntities/Food.cpp | 1 + source/States/GameState.cpp | 19 +++++++++---------- 8 files changed, 37 insertions(+), 15 deletions(-) diff --git a/include/MazeContent/Entities/DynamicEntities/IDynamicEntity.h b/include/MazeContent/Entities/DynamicEntities/IDynamicEntity.h index acecc9c..f2b22d2 100644 --- a/include/MazeContent/Entities/DynamicEntities/IDynamicEntity.h +++ b/include/MazeContent/Entities/DynamicEntities/IDynamicEntity.h @@ -2,8 +2,7 @@ #include "MazeContent/Entities/IEntity.h" #include "Visit/IVisitable.h" -#include "Visit/IVisitor.h" -#include <Events/IGameEvent.h> +//#include "Visit/IVisitor.h" ///@todo нужно ли? class IDynamicEntity: public IEntity, public IVisitable { public: diff --git a/include/MazeContent/Entities/Pacman.h b/include/MazeContent/Entities/Pacman.h index 91f9f33..d1f6f03 100644 --- a/include/MazeContent/Entities/Pacman.h +++ b/include/MazeContent/Entities/Pacman.h @@ -1,12 +1,15 @@ #pragma once #include "IEntity.h" +#include <Visit/IVisitor.h> -class Pacman: public IEntity { +class Pacman: public IEntity, public IVisitor { public: void prepare_for_drawing() override; void draw_into(sf::RenderWindow& window) const override; void move(Room::Direction direction); + std::unique_ptr<IGameEvent> visit(Food* ptr_food) override; + std::unique_ptr<IGameEvent> visit(Enemy* ptr_enemy) override; public: Pacman(); private: diff --git a/include/MazeContent/Entities/StaticEntities/IStaticEntity.h b/include/MazeContent/Entities/StaticEntities/IStaticEntity.h index 3c41572..e6ca290 100644 --- a/include/MazeContent/Entities/StaticEntities/IStaticEntity.h +++ b/include/MazeContent/Entities/StaticEntities/IStaticEntity.h @@ -2,8 +2,7 @@ #include "MazeContent/Entities/IEntity.h" #include "Visit/IVisitable.h" -#include "Visit/IVisitor.h" -#include <Events/IGameEvent.h> +//#include "Visit/IVisitor.h" ///@todo нужно ли? class IStaticEntity: public IEntity, public IVisitable { public: diff --git a/source/Events/DeleteStaticEntity.cpp b/source/Events/DeleteStaticEntity.cpp index 24f046e..0b5e18c 100644 --- a/source/Events/DeleteStaticEntity.cpp +++ b/source/Events/DeleteStaticEntity.cpp @@ -1,5 +1,7 @@ #include "Events/DeleteStaticEntity.hpp" +DeleteStaticEntity::DeleteStaticEntity(IStaticEntity* ptr_entity) : m_ptr_entity(ptr_entity) {} + void DeleteStaticEntity::handle(GameContext& context) const { for (size_t i = 0; i < context.static_objects.size(); ++i) { if (context.static_objects[i].get() == m_ptr_entity) { diff --git a/source/MazeContent/Entities/DynamicEntities/Enemy.cpp b/source/MazeContent/Entities/DynamicEntities/Enemy.cpp index 8bb059d..b4c21ff 100644 --- a/source/MazeContent/Entities/DynamicEntities/Enemy.cpp +++ b/source/MazeContent/Entities/DynamicEntities/Enemy.cpp @@ -1,5 +1,6 @@ #include "MazeContent/Entities/DynamicEntities/Enemy.h" #include "Config.h" +#include <Events/IGameEvent.h> Enemy::Enemy() : m_rectangle({ config::GAME_ENEMY_SIZE / 2, config::GAME_ENEMY_SIZE / 2 }) { m_rectangle.setOrigin(m_rectangle.getSize() / 2.f); diff --git a/source/MazeContent/Entities/Pacman.cpp b/source/MazeContent/Entities/Pacman.cpp index 867174f..729fa1b 100644 --- a/source/MazeContent/Entities/Pacman.cpp +++ b/source/MazeContent/Entities/Pacman.cpp @@ -1,5 +1,9 @@ #include "MazeContent/Entities/Pacman.h" #include <Config.h> +#include "Events/DeleteStaticEntity.hpp" +#include "Events/LostGame.hpp" +#include "MazeContent/Entities/StaticEntities/Food.h" +#include "MazeContent/Entities/DynamicEntities/Enemy.h" Pacman::Pacman() :m_circle(config::GAME_PACMAN_SIZE / 2) { m_circle.setOrigin(m_circle.getRadius(), m_circle.getRadius()); @@ -16,4 +20,18 @@ void Pacman::draw_into(sf::RenderWindow& window) const { void Pacman::move(Room::Direction direction) { m_ptr_room->get_side(direction)->enter(this); +} + +std::unique_ptr<IGameEvent> Pacman::visit(Food* ptr_food) { + if (ptr_food->get_location() != this->get_location()) { + return {}; + } + return std::make_unique<DeleteStaticEntity>(ptr_food); +} + +std::unique_ptr<IGameEvent> Pacman::visit(Enemy* ptr_enemy) { + if (ptr_enemy->get_location() != this->get_location()) { + return {}; + } + return std::make_unique<LostGame>(); } \ No newline at end of file diff --git a/source/MazeContent/Entities/StaticEntities/Food.cpp b/source/MazeContent/Entities/StaticEntities/Food.cpp index a135a5c..1532839 100644 --- a/source/MazeContent/Entities/StaticEntities/Food.cpp +++ b/source/MazeContent/Entities/StaticEntities/Food.cpp @@ -1,5 +1,6 @@ #include "MazeContent/Entities/StaticEntities/Food.h" #include "Config.h" +#include <Events/IGameEvent.h> Food::Food() : m_circle{config::GAME_FOOD_SIZE / 2} { m_circle.setOrigin(m_circle.getRadius(), m_circle.getRadius()); diff --git a/source/States/GameState.cpp b/source/States/GameState.cpp index 0595783..a3a1dbe 100644 --- a/source/States/GameState.cpp +++ b/source/States/GameState.cpp @@ -1,5 +1,8 @@ #include "States/GameState.h" #include "States/SelectState.h" +#include "Events/IGameEvent.h" +//#include "Events/LostGame.hpp" ///@todo убрать +//#include "Events/DeleteStaticEntity.hpp" ///@todo убрать GameState::GameState(IStateManager& state_manager, const sf::VideoMode& video_mode, const std::string& window_title, float width, float height): IWindowKeeper(video_mode, window_title), IState(state_manager), m_maze_width(width), m_maze_height(height) {} @@ -45,22 +48,18 @@ void GameState::process_entities_interactions(IVisitor* visitor) { std::unique_ptr<IGameEvent> dynamic_event; for (auto& obj : m_context_manager.get_current_context().static_objects) { static_event = std::move(obj->accept(visitor)); - if (static_event) { + if (static_event) break; - } } for (auto& obj : m_context_manager.get_current_context().dynamic_objects) { dynamic_event = std::move(obj->accept(visitor)); - if (dynamic_event) { + if (dynamic_event) break; - } - } - if (static_event) { - static_event->handle(&m_context_manager.get_current_context()); - } - if (dynamic_event) { - dynamic_event->handle(&m_context_manager.get_current_context()); } + if (static_event) + static_event->handle(m_context_manager.get_current_context()); + if (dynamic_event) + dynamic_event->handle(m_context_manager.get_current_context()); } -- GitLab