diff --git a/CMakeLists.txt b/CMakeLists.txt index cc0696be081bf2025ba6bf5de65bac0eceaaccdb..6bb663850d9111b4e7f64ccd4908ab2c47efb146 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,7 +8,8 @@ FetchContent_Declare(SFML GIT_REPOSITORY https://github.com/SFML/SFML.git GIT_TA FetchContent_MakeAvailable(SFML) file(GLOB_RECURSE SOURCE_CPP source/*.cpp) -add_executable(pacman ${SOURCE_CPP}) +file(GLOB_RECURSE SOURCE_H source/*.h) +add_executable(pacman ${SOURCE_CPP} ${SOURCE_H}) target_link_libraries(pacman PRIVATE sfml-window sfml-graphics sfml-system) target_compile_definitions(pacman PRIVATE ASSETS_PATH="${CMAKE_CURRENT_SOURCE_DIR}/workdir/assets/") diff --git a/source/application/App/Application.cpp b/source/application/App/Application.cpp index 6bac2e1faa6241b57ba66a18d45216f3335a5c98..0c2126a0e16227983cebaa5a6cb5694852da6164 100644 --- a/source/application/App/Application.cpp +++ b/source/application/App/Application.cpp @@ -28,6 +28,5 @@ void Application::set_next_state(std::unique_ptr<IState>&& ptr_state) { void Application::apply_deffer_state_change() noexcept { if (m_ptr_state_next) - std::swap(m_ptr_state_current, m_ptr_state_next); - m_ptr_state_next.reset(); + m_ptr_state_current = std::exchange(m_ptr_state_next, nullptr); } \ No newline at end of file diff --git a/source/application/App/Application.h b/source/application/App/Application.h index d8c0124f5d12df093cefaa247696f0258b22f9ba..a08c9a4f4cb5633fbf7fceac6ba38cd0bdacaa4c 100644 --- a/source/application/App/Application.h +++ b/source/application/App/Application.h @@ -10,7 +10,7 @@ class Application : public IStateManager { public: Application(); int run(); - ~Application() = default; + ~Application() override = default; private: void set_next_state(std::unique_ptr<IState>&& ptr_state) override; diff --git a/source/application/BobBuilder/GameBuilders.cpp b/source/application/BobBuilder/GameBuilders.cpp index 653f125ccecbd8ef42beaec35b88bb91dbc45e19..5afea2db38217af24d7f3205f583c598b207f874 100644 --- a/source/application/BobBuilder/GameBuilders.cpp +++ b/source/application/BobBuilder/GameBuilders.cpp @@ -1,9 +1,7 @@ #include "GameBuilders.h" -#include <random> - CommonBuilder::CommonBuilder(float width, float height, float room_size) : - m_width(width), m_height(height), m_room_size(room_size){ + m_width(width), m_height(height), m_room_size(room_size){ } void CommonBuilder::create_context(float dynamic_objects_ratio) { @@ -12,16 +10,28 @@ void CommonBuilder::create_context(float dynamic_objects_ratio) { for (auto& room : row) if (room != nullptr) empty_rooms_buffer.emplace_back(room); + float id = std::rand() % empty_rooms_buffer.size(); + //в рандомное место располагаем пакмана m_context.pacman.set_location(empty_rooms_buffer[id]); auto it = std::next(empty_rooms_buffer.begin(), id); empty_rooms_buffer.erase(it); - //@todo располагать врагов + + //располагаем врагов + size_t rooms_row = static_cast<size_t>(m_width / m_room_size); + size_t rooms_col = static_cast<size_t>(m_height / m_room_size); + for (size_t i = 0; i < rooms_row * rooms_col * dynamic_objects_ratio; i++) { + id = std::rand() % empty_rooms_buffer.size(); + auto enemy = std::make_unique<Enemy>(); + enemy->set_location(empty_rooms_buffer.at(id)); + m_context.dynamic_objects.emplace_back(std::move(enemy)); + } //располагаем еду for (auto it : empty_rooms_buffer) { auto food = std::make_unique<Food>(); food->set_location(it); + food->set_texture(&FoodTexture::instance()); m_context.static_objects.emplace_back(std::move(food)); } empty_rooms_buffer.clear(); @@ -96,9 +106,84 @@ void SimpleBuilder::set_rooms_sides() { } void ComplexBuilder::create_rooms() { + size_t room_quantity_row = (m_height / m_room_size); + size_t room_quantity_col = (m_width / m_room_size); + int room_size = static_cast<int>(m_room_size); + auto starting_point = sf::Vector2f{(m_width - m_room_size*room_quantity_col)/2 + m_room_size/2, (m_height - m_room_size*room_quantity_row)/2 + + m_room_size/2}; + + for (int row = 0; row < room_quantity_row * room_size; row += room_size) { + std::vector<Room*> vector_row; + for (int col = 0; col < room_quantity_col * room_size; col += room_size) { + auto room = new Room(m_room_size); + room->set_position(sf::Vector2f {(col + starting_point.x), (row + starting_point.y)}); + vector_row.emplace_back(std::move(room)); + } + m_rooms.emplace_back(std::move(vector_row)); + } } void ComplexBuilder::set_rooms_sides() { + size_t cols = m_rooms[0].size(); + size_t rows = m_rooms.size(); + for (int j = 0; j < cols; j++) { + m_rooms[0][j]->set_side(Room::Direction::UP, std::make_unique<Wall>(*m_rooms[0][j])); + } + for (int j = 0; j < cols; j++) { + m_rooms[rows - 1][j]->set_side(Room::Direction::DOWN, std::make_unique<Wall>(*m_rooms[rows - 1][j])); + if (j != cols - 1) { + m_rooms[rows - 1][j]->set_side(Room::Direction::RIGHT, std::make_unique<Pass>(*m_rooms[rows - 1][j], *m_rooms[rows - 1][j+1])); + m_rooms[rows - 1][j+1]->set_side(Room::Direction::LEFT, std::make_unique<Pass>(*m_rooms[rows - 1][j+1], *m_rooms[rows - 1][j])); + } + } + for (int i = 0; i < rows ; i++) { + m_rooms[i][0]->set_side(Room::Direction::LEFT, std::make_unique<Wall>(*m_rooms[i][0])); + } + for (int i = 0; i < rows; i++) { + m_rooms[i][cols - 1]->set_side(Room::Direction::RIGHT, std::make_unique<Wall>(*m_rooms[i][cols - 1])); + } -} \ No newline at end of file + std::vector<int> row(cols); + int count = 0; + for (; count < cols; row[count] = count, count++); + for (int i = 0; i < rows - 1; i++) { + for (int j = 0; j < cols - 1; j++) { + if (rand() % 2) { + m_rooms[i][j]->set_side(Room::Direction::RIGHT, std::make_unique<Wall>(*m_rooms[i][j])); + m_rooms[i][j+1]->set_side(Room::Direction::LEFT, std::make_unique<Wall>(*m_rooms[i][j+1])); + } + else { + m_rooms[i][j]->set_side(Room::Direction::RIGHT, std::make_unique<Pass>(*m_rooms[i][j], *m_rooms[i][j+1])); + m_rooms[i][j+1]->set_side(Room::Direction::LEFT, std::make_unique<Pass>(*m_rooms[i][j+1], *m_rooms[i][j])); + int temp = row[j + 1]; + for (int k = 0; k < cols; k++) { + if (row[k] == temp) row[k] = row[j]; + } + } + } + for (int j = 0; j < cols; j++) { + if (rand() % 2) { + int temp = 0; + for (int k = 0; k < cols; k++) { + if (row[k] == row[j] && !m_rooms[i][k]->get_side(Room::Direction::DOWN)) temp++; + } + if (temp > 1) { + m_rooms[i][j]->set_side(Room::Direction::DOWN, std::make_unique<Wall>(*m_rooms[i][j])); + m_rooms[i+1][j]->set_side(Room::Direction::UP, std::make_unique<Wall>(*m_rooms[i+1][j])); + } + else { + m_rooms[i][j]->set_side(Room::Direction::DOWN, std::make_unique<Pass>(*m_rooms[i][j], *m_rooms[i+1][j])); + m_rooms[i+1][j]->set_side(Room::Direction::UP, std::make_unique<Pass>(*m_rooms[i+1][j], *m_rooms[i][j])); + } + } + else { + m_rooms[i][j]->set_side(Room::Direction::DOWN, std::make_unique<Pass>(*m_rooms[i][j], *m_rooms[i+1][j])); + m_rooms[i+1][j]->set_side(Room::Direction::UP, std::make_unique<Pass>(*m_rooms[i+1][j], *m_rooms[i][j])); + } + } + for (int j = 0; j < cols; j++) { + if (m_rooms[i][j]->get_side(Room::Direction::DOWN)) row[j] = count++; + } + } + +} diff --git a/source/application/Context/Context.cpp b/source/application/Context/Context.cpp index fe23398a7d68299620dc18bd3ca36791015ef4e1..fe3ae599f9e558926c7652f6cb1a70481142f97f 100644 --- a/source/application/Context/Context.cpp +++ b/source/application/Context/Context.cpp @@ -14,13 +14,13 @@ GameContext GameContext::clone() { return context; } -//@todo void ContextManager::restore_previous_context() { if (m_history.size() == 1) { m_initial_context = m_history.top().clone(); return; } m_initial_context = std::move(m_history.top()); + m_history.pop(); } void ContextManager::reset(GameContext &&context) { diff --git a/source/application/Context/Context.h b/source/application/Context/Context.h index 456d2e396e8c3f5a4c071a57b7eaa194f52746c4..785b9857a1144357468f388d1505e673af3ee91c 100644 --- a/source/application/Context/Context.h +++ b/source/application/Context/Context.h @@ -4,13 +4,14 @@ #include <istream> #include <list> -class GameContext { -public: +struct GameContext { enum State { INGAME, WIN, LOST } state = INGAME; std::list<std::unique_ptr<IDynamicEntity>> dynamic_objects; std::list<std::unique_ptr<IStaticEntity>> static_objects; + std::vector<MyTexture*> m_textures; + MyTexture* m_pacman_texture; + MyTexture* m_food_texture; Pacman pacman; -public: GameContext clone(); }; diff --git a/source/application/Drawable/DrawMenu/Font/Font.h b/source/application/Drawable/DrawMenu/Font/Font.h index 240f722f038b399a76860c620e5056a35e9bbbb1..689eb11db0c741bc10fbd8b5f7b004121e7dc66e 100644 --- a/source/application/Drawable/DrawMenu/Font/Font.h +++ b/source/application/Drawable/DrawMenu/Font/Font.h @@ -7,7 +7,7 @@ public: MyFont(const MyFont&) = delete; MyFont& operator=(const MyFont&) = delete; static MyFont& Instance(); - operator const sf::Font&() const; //@todo why is this necessary + operator const sf::Font&() const; //приведение типа private: MyFont(); diff --git a/source/application/Drawable/DrawMenu/Menu/Menu.cpp b/source/application/Drawable/DrawMenu/Menu/Menu.cpp index 833f2c6b9a1c75805588919740debc4799afbbc2..6ffe566019c7f5ebe2e2bfa7d4f62604001a44b0 100644 --- a/source/application/Drawable/DrawMenu/Menu/Menu.cpp +++ b/source/application/Drawable/DrawMenu/Menu/Menu.cpp @@ -21,7 +21,7 @@ Menu::Menu(IStateManager& state_manager) { config::MEDIUM_GAME_TITLE, config::MEDIUM_GAME_ENEMY_RATIO); auto hard_director = std::make_unique<GameBuilderDirector>( - std::make_unique<SimpleBuilder>( + std::make_unique<ComplexBuilder>( config::GAME_VIDEO_MODE.width, config::GAME_VIDEO_MODE.height, config::ROOM_SIZE), config::GAME_VIDEO_MODE, diff --git a/source/application/Drawable/Entity/IEntity.cpp b/source/application/Drawable/Entity/IEntity.cpp index ab32bca22cc688da21afa05afcfce018067ffbc4..8bb10e298708aa5038a22eca1f2c53f34b6f6958 100644 --- a/source/application/Drawable/Entity/IEntity.cpp +++ b/source/application/Drawable/Entity/IEntity.cpp @@ -7,9 +7,10 @@ void IEntity::set_location(Room* ptr_room) { } Food::Food() { - m_shape = sf::CircleShape(config::GAME_FOOD_SIZE); + m_shape = sf::CircleShape(config::GAME_FOOD_SIZE/2); + m_shape.setOrigin({ config::GAME_FOOD_SIZE/2, config::GAME_FOOD_SIZE/2 }); + set_texture(&FoodTexture::instance()); m_shape.setFillColor(config::GAME_FOOD_COLOR); - m_shape.setOrigin({config::GAME_FOOD_SIZE/2, config::GAME_FOOD_SIZE/2}); } std::unique_ptr<IStaticEntity> Food::clone() const { @@ -17,7 +18,8 @@ std::unique_ptr<IStaticEntity> Food::clone() const { } void Food::prepare_for_drawing() { - m_shape.setPosition(m_ptr_room->get_position()); + m_sprite.setTexture(m_ptr_texture->get_texture()); + m_sprite.setPosition(m_ptr_room->get_position()+sf::Vector2f(-10, -10)); } std::unique_ptr<IGameEvent> Enemy::accept(IVisitor* ptr_visitor) { @@ -28,15 +30,13 @@ std::unique_ptr<IGameEvent> Food::accept(IVisitor* ptr_visitor) { return ptr_visitor->visit(this); } - void Food::draw_into(sf::RenderWindow& window) { - m_shape.setPosition(m_ptr_room->get_position()); - //из класса Room: sf::Vector2f get_position() { return m_rectangle.getPosition(); } - window.draw(m_shape); + prepare_for_drawing(); + window.draw(m_sprite); } Enemy::Enemy() { - m_shape = sf::CircleShape(config::GAME_ENEMY_SIZE); + m_shape = sf::CircleShape(config::GAME_ENEMY_SIZE/2); m_shape.setFillColor(config::GAME_ENEMY_COLOR); m_shape.setOrigin({config::GAME_ENEMY_SIZE / 2, config::GAME_ENEMY_SIZE / 2}); } @@ -49,7 +49,7 @@ void Enemy::prepare_for_drawing() { m_shape.setPosition(m_ptr_room->get_position()); } -void Enemy::draw_into(sf::RenderWindow &window) { +void Enemy::draw_into(sf::RenderWindow& window) { prepare_for_drawing(); window.draw(m_shape); } diff --git a/source/application/Drawable/Entity/IEntity.h b/source/application/Drawable/Entity/IEntity.h index 6a83d0970b5d9a76df4b1d088ed9c45e8ac6a69a..f0348c7883af7ad56e084a41434ded3fc8ab59c2 100644 --- a/source/application/Drawable/Entity/IEntity.h +++ b/source/application/Drawable/Entity/IEntity.h @@ -2,6 +2,7 @@ #include "../IDrawable.h" #include "../../Event/IGameEvent.h" +#include "MyTexture.h" class Room; class Food; @@ -30,6 +31,9 @@ public: class IStaticEntity : public IEntity, public IVisitable { public: virtual std::unique_ptr<IStaticEntity> clone() const = 0; + virtual void set_texture(MyTexture* ptr_texture) {m_ptr_texture = ptr_texture; } +protected: + MyTexture* m_ptr_texture; }; class IDynamicEntity: public IEntity, public IVisitable { @@ -52,14 +56,17 @@ private: sf::Clock m_clock; }; -class Food: public IStaticEntity { +class Food: public IStaticEntity{ public: Food(); void draw_into(sf::RenderWindow& window) override; void prepare_for_drawing() override; + void set_texture(MyTexture* ptr_texture) override { m_ptr_texture = ptr_texture; } std::unique_ptr<IStaticEntity> clone() const override; std::unique_ptr<IGameEvent> accept(IVisitor* ptr_visitor) override; ~Food() override {}; private: sf::CircleShape m_shape; + MyTexture* m_ptr_texture; + sf::Sprite m_sprite; }; \ No newline at end of file diff --git a/source/application/Drawable/Entity/MyTexture.cpp b/source/application/Drawable/Entity/MyTexture.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a7d42784465d95c9dc9e7cfc5875b1457886375e --- /dev/null +++ b/source/application/Drawable/Entity/MyTexture.cpp @@ -0,0 +1,28 @@ +#include "MyTexture.h" + +PacmanTexture::PacmanTexture() { + m_texture_up.loadFromFile(std::string(ASSETS_PATH) + "pacmanUp.png"); + m_texture_down.loadFromFile(std::string(ASSETS_PATH) + "pacmanDown.png"); + m_texture_right.loadFromFile(std::string(ASSETS_PATH) + "pacmanRight.png"); + m_texture_left.loadFromFile(std::string(ASSETS_PATH) + "pacmanLeft.png"); +} + +//в зависимости от направления - разные текстуры +sf::Texture& PacmanTexture::get_texture() { + switch (m_direction) { + case Direction::UP: + return m_texture_left; + case Direction::DOWN: + return m_texture_right; + case Direction::RIGHT: + return m_texture_up; + case Direction::LEFT: + return m_texture_down; + } +} + +FoodTexture::FoodTexture() { + if (!m_texture_food.loadFromFile(std::string(ASSETS_PATH) + "apple.png")) + throw std::runtime_error("Can't added Texture for food"); +} + diff --git a/source/application/Drawable/Entity/MyTexture.h b/source/application/Drawable/Entity/MyTexture.h new file mode 100644 index 0000000000000000000000000000000000000000..4af54f3586d8c2b21bc9700ec5b5ad4f1f8fe431 --- /dev/null +++ b/source/application/Drawable/Entity/MyTexture.h @@ -0,0 +1,35 @@ +#pragma once + +#include "SFML/Graphics.hpp" + +class MyTexture { +public: + MyTexture() = default; + enum Direction { UP, DOWN, RIGHT, LEFT }; + virtual sf::Texture& get_texture() = 0; + MyTexture(const MyTexture&) = delete; + MyTexture& operator=(const MyTexture&) = delete; + void set_direction(Direction direction) { m_direction = direction; } +protected: + Direction m_direction = UP; +}; + +//texture for Pacman +class PacmanTexture : public MyTexture { +public: + static PacmanTexture& instance() { static PacmanTexture instance; return instance; } + sf::Texture& get_texture() override; +private: + PacmanTexture(); + sf::Texture m_texture_up, m_texture_down, m_texture_right, m_texture_left; +}; + +//texture for Food +class FoodTexture : public MyTexture { +public: + static FoodTexture& instance() { static FoodTexture instance; return instance; } + sf::Texture& get_texture() override { return m_texture_food; } +private: + FoodTexture(); + sf::Texture m_texture_food; +}; diff --git a/source/application/Drawable/Entity/Pacman.cpp b/source/application/Drawable/Entity/Pacman.cpp index e20ab2d1667b382f7c7750d37a7e311ee6dc24cf..b47166fa9e9b3ebeebefa048ae51e10084766d74 100644 --- a/source/application/Drawable/Entity/Pacman.cpp +++ b/source/application/Drawable/Entity/Pacman.cpp @@ -2,30 +2,33 @@ #include "../../../../workdir/config.h" Pacman::Pacman() { - m_shape = sf::CircleShape(config::GAME_PACMAN_SIZE/2); + m_shape = sf::CircleShape(config::GAME_PACMAN_SIZE); m_shape.setFillColor(config::GAME_COLOR_PACMAN); - m_shape.setOrigin({config::GAME_PACMAN_SIZE/2, config::GAME_PACMAN_SIZE/2}); + set_texture(&PacmanTexture::instance()); + m_shape.setOrigin({config::GAME_PACMAN_SIZE, config::GAME_PACMAN_SIZE}); } void Pacman::move(Room::Direction direction) { m_ptr_room->get_side(direction)->enter(this); + m_ptr_texture->set_direction(static_cast<MyTexture::Direction>(direction)); } void Pacman::prepare_for_drawing() { - m_shape.setPosition(m_ptr_room->get_position()); + m_sprite.setTexture(m_ptr_texture->get_texture()); + m_sprite.setPosition(m_ptr_room->get_position()+sf::Vector2f(-20, -20)); } -std::unique_ptr<IGameEvent> Pacman::visit(Food *ptr_food) { +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) { +std::unique_ptr<IGameEvent> Pacman::visit(Enemy* ptr_enemy) { if (ptr_enemy->get_location() != this->get_location()) return {}; return std::make_unique<LostGame>(); } -void Pacman::draw_into(sf::RenderWindow &window) { +void Pacman::draw_into(sf::RenderWindow& window) { prepare_for_drawing(); - window.draw(m_shape); -} + window.draw(m_sprite); +} \ No newline at end of file diff --git a/source/application/Drawable/Entity/Pacman.h b/source/application/Drawable/Entity/Pacman.h index 8e5f5f693bb16e60581e4449327a33b80d86e7fd..3f38479312c90df75f8bde2d6441ca5ec1fea691 100644 --- a/source/application/Drawable/Entity/Pacman.h +++ b/source/application/Drawable/Entity/Pacman.h @@ -2,16 +2,20 @@ #include "IEntity.h" #include "../Maze/Maze.h" +#include "MyTexture.h" class Pacman : public IEntity, public IVisitor { public: Pacman(); void move(Room::Direction direction); void prepare_for_drawing() override; + void set_texture(MyTexture* ptr_texture) { m_ptr_texture = ptr_texture; } void draw_into(sf::RenderWindow& window) override; std::unique_ptr<IGameEvent> visit(Food* ptr_entity) override; std::unique_ptr<IGameEvent> visit(Enemy* ptr_entity) override; ~Pacman() override {}; private: sf::CircleShape m_shape; -}; \ No newline at end of file + sf::Sprite m_sprite; + MyTexture* m_ptr_texture; +}; diff --git a/source/application/Drawable/Maze/Maze.h b/source/application/Drawable/Maze/Maze.h index 967923d9d6ff952785b196d4f3ffb4034b1ce043..1b7219a076e1171a89de373dec25f46379bf4467 100644 --- a/source/application/Drawable/Maze/Maze.h +++ b/source/application/Drawable/Maze/Maze.h @@ -13,7 +13,6 @@ public: class Room: public IDrawable { public: enum Direction { INVALID = -1, LEFT, RIGHT, UP, DOWN }; - explicit Room(float size); float get_size() { return m_rectangle.getSize().x; } void set_position(sf::Vector2f pos) { m_rectangle.setPosition(pos); } @@ -31,7 +30,7 @@ public: class Maze: public IDrawable { public: Maze() = default; - Maze(std::vector<Room*>& rooms) : m_rooms(rooms) {}; + explicit Maze(std::vector<Room*>& rooms) : m_rooms(rooms) {}; void draw_into(sf::RenderWindow& m_window) override; private: std::vector<Room*> m_rooms; diff --git a/source/application/SelectCommand/ISelectCommand.cpp b/source/application/SelectCommand/ISelectCommand.cpp deleted file mode 100644 index 4669ecd5600111ce32e422e24a24859925035ffa..0000000000000000000000000000000000000000 --- a/source/application/SelectCommand/ISelectCommand.cpp +++ /dev/null @@ -1,2 +0,0 @@ -#include "ISelectCommand.h" - diff --git a/source/application/State/GameState.cpp b/source/application/State/GameState.cpp index bab1573444a092463d3fd4b422d16a50b986b09b..db705acdc429214b113b9101521efbc9c4a58033 100644 --- a/source/application/State/GameState.cpp +++ b/source/application/State/GameState.cpp @@ -23,15 +23,19 @@ void GameState::event_handling() { void GameState::process_key_pressed(sf::Keyboard::Key code) { switch (code) { case sf::Keyboard::W: - m_context_manager.get_current_context().pacman.move(Room::UP); + m_context_manager.save_current_context(); //сохраняем состояние + m_context_manager.get_current_context().pacman.move(Room::UP); //выполнили действие break; case sf::Keyboard::S: + m_context_manager.save_current_context(); m_context_manager.get_current_context().pacman.move(Room::DOWN); break; case sf::Keyboard::A: + m_context_manager.save_current_context(); m_context_manager.get_current_context().pacman.move(Room::LEFT); break; case sf::Keyboard::D: + m_context_manager.save_current_context(); m_context_manager.get_current_context().pacman.move(Room::RIGHT); break; } @@ -41,25 +45,21 @@ void GameState::update() { std::vector<std::unique_ptr<IGameEvent>> game_events; if (m_context_manager.get_current_context().state != GameContext::INGAME) return; GameContext& context = m_context_manager.get_current_context(); - for (auto& entity_ptr : context.dynamic_objects) { - entity_ptr->action(); + for (auto& ptr_entity : context.dynamic_objects) { + ptr_entity->action(); IVisitor* ptr_visitor = &(context.pacman); - game_events.emplace_back(entity_ptr->accept(ptr_visitor)); + auto ptr_event = ptr_entity->accept(ptr_visitor); + if (ptr_event) game_events.emplace_back(std::move(ptr_event)); } for (auto& static_ptr : context.static_objects) { IVisitor* ptr_visitor = &(context.pacman); auto ptr_event = static_ptr->accept(ptr_visitor); - if (ptr_event) - game_events.emplace_back(std::move(ptr_event)); + if (ptr_event) game_events.emplace_back(std::move(ptr_event)); } - for (auto it = context.dynamic_objects.begin(); it != context.dynamic_objects.end(); ++it) { - if (context.pacman.get_location() == (*it)->get_location()) { - game_events.emplace_back(std::move(std::make_unique<LostGame>())); - } - } - for (int i = 0; i < game_events.size(); i++) { + + for (int i = 0; i < game_events.size(); i++) game_events[i]->handle(&m_context_manager.get_current_context()); - } + if (context.static_objects.empty()) { game_events.emplace_back(std::move(std::make_unique<WinGame>())); } @@ -80,10 +80,8 @@ void GameState::render() { } m_maze.draw_into(m_window); context.pacman.draw_into(m_window); - for (auto& obj : context.static_objects) { + for (auto& obj : context.static_objects) obj->draw_into(m_window); - } - for (auto& el : context.dynamic_objects) el->draw_into(m_window); @@ -92,7 +90,8 @@ void GameState::render() { bool GameState::do_step() { event_handling(); - update(); + if (m_context_manager.get_current_context().state == GameContext::INGAME) + update(); render(); return true; } diff --git a/source/application/State/IState.cpp b/source/application/State/IState.cpp deleted file mode 100644 index 528669cdbe61d7714a0b7ef95e7a4c2c8c82e657..0000000000000000000000000000000000000000 --- a/source/application/State/IState.cpp +++ /dev/null @@ -1,2 +0,0 @@ -#include "IState.h" - diff --git a/workdir/assets/apple.png b/workdir/assets/apple.png new file mode 100644 index 0000000000000000000000000000000000000000..bc27ccc400f05db3ddff79abddf562bfd8949f70 Binary files /dev/null and b/workdir/assets/apple.png differ diff --git a/workdir/assets/pacman.png b/workdir/assets/pacman.png new file mode 100644 index 0000000000000000000000000000000000000000..9dfa35d9d504f7e413ad1e5180b9821dfab11f3b Binary files /dev/null and b/workdir/assets/pacman.png differ diff --git a/workdir/assets/pacmanDown.png b/workdir/assets/pacmanDown.png new file mode 100644 index 0000000000000000000000000000000000000000..18d63290bb82e97f70b629c745b29e4f8f443d5b Binary files /dev/null and b/workdir/assets/pacmanDown.png differ diff --git a/workdir/assets/pacmanLeft.png b/workdir/assets/pacmanLeft.png new file mode 100644 index 0000000000000000000000000000000000000000..cfad9cd3a0718cf3fe8a89621fe84d0254911c10 Binary files /dev/null and b/workdir/assets/pacmanLeft.png differ diff --git a/workdir/assets/pacmanRight.png b/workdir/assets/pacmanRight.png new file mode 100644 index 0000000000000000000000000000000000000000..9613050a23c47f95d5e51ed3bcf44514e85e68ad Binary files /dev/null and b/workdir/assets/pacmanRight.png differ diff --git a/workdir/assets/pacmanUp.png b/workdir/assets/pacmanUp.png new file mode 100644 index 0000000000000000000000000000000000000000..62ba3dcdcaed1efe5e18561316b57dd2f7b39b95 Binary files /dev/null and b/workdir/assets/pacmanUp.png differ