diff --git a/source/application/App/Application.h b/source/application/App/Application.h index d6ae14236bddd3dda82d322b418b43cf478c77f4..e2ad97f039730e116cbd8a4784f69f9954e46852 100644 --- a/source/application/App/Application.h +++ b/source/application/App/Application.h @@ -12,7 +12,7 @@ public: int run(); private: - void set_next_state(std::unique_ptr<IState>&& ptr_state); + void set_next_state(std::unique_ptr<IState>&& ptr_state) override; void apply_deffer_state_change() noexcept; private: diff --git a/source/application/BobBuilder/BobBuilderDirector.cpp b/source/application/BobBuilder/BobBuilderDirector.cpp index 5aa8b66fa0047106b438e27b4278b22ac1e3dcc1..09d49f1591d56ffe9f4e455a9e2560f1b49524e8 100644 --- a/source/application/BobBuilder/BobBuilderDirector.cpp +++ b/source/application/BobBuilder/BobBuilderDirector.cpp @@ -1,11 +1,13 @@ #include "BobBuilderDirector.h" +#include <utility> + GameBuilderDirector::GameBuilderDirector(std::unique_ptr<IGameBuilder> &&ptr_builder, sf::VideoMode mode, - const std::string &window_title, + std::string window_title, float dynamic_objects_ratio) : m_dynamic_object_ratio(dynamic_objects_ratio), - m_window_title(window_title), + m_window_title(std::move(window_title)), m_mode(mode), m_ptr_builder(std::move(ptr_builder)) { } diff --git a/source/application/BobBuilder/BobBuilderDirector.h b/source/application/BobBuilder/BobBuilderDirector.h index 6b066481ecf2a7d486cee40b7d747feeb883042f..f52a9998652240fa41c20cf149c4deacd930e2c1 100644 --- a/source/application/BobBuilder/BobBuilderDirector.h +++ b/source/application/BobBuilder/BobBuilderDirector.h @@ -3,7 +3,7 @@ class GameBuilderDirector { public: - GameBuilderDirector(std::unique_ptr<IGameBuilder>&& ptr_builder, sf::VideoMode mode, const std::string& window_title, float dynamic_objects_ratio); + GameBuilderDirector(std::unique_ptr<IGameBuilder>&& ptr_builder, sf::VideoMode mode, std::string window_title, float dynamic_objects_ratio); std::unique_ptr<GameState> build(IStateManager& state_manager); private: diff --git a/source/application/BobBuilder/GameBuilders.cpp b/source/application/BobBuilder/GameBuilders.cpp index 55b3eee8f03a522cc0f9bfecc93c4a738169767e..d2232a1bfd13c27e2542f6145357288f52446648 100644 --- a/source/application/BobBuilder/GameBuilders.cpp +++ b/source/application/BobBuilder/GameBuilders.cpp @@ -1,43 +1,44 @@ #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){ +} + void CommonBuilder::create_context(float dynamic_objects_ratio) { -// GameContext context; -// std::vector<Room*> buffer; -// //РїСЂРѕС…РѕРґРёРјСЃСЏ РїРѕ комнатам, если РѕРЅР° пустая то добавляем РІ buffer -// for (auto& row : m_rooms) { -// for (auto& room : row) { -// if (room != nullptr) -// buffer.emplace_back(room); -// } -// } -// -// int id = rand() % buffer.size(); -// //помещаем пакмана РІ рандомную комнату Рё удаляем ее РёР· буфера -// m_context.pacman.set_location(buffer[id]); -// auto it = buffer.begin(); -// for (int i = 0; i < id; i++, it++); -// buffer.erase(it); -// -// for (auto it = buffer.begin(); it != buffer.end(); it++) { -// auto food = std::make_unique<Food>(); -//// food->set_location(); -// context.static_objects.emplace_back(std::move(food)); -// } -// -// buffer.clear(); -// m_context = std::move(context); + GameContext context; + std::vector<Room*> empty_rooms_buffer; + for (auto& row : m_rooms) + for (auto& room : row) + empty_rooms_buffer.emplace_back(room); + + float id = std::rand() % empty_rooms_buffer.size(); + context.pacman.set_location(empty_rooms_buffer[id]); + auto it = std::next(empty_rooms_buffer.begin(), id); + empty_rooms_buffer.erase(it); + + + for (auto it : empty_rooms_buffer) { + auto food = std::make_unique<Food>(); + food->set_location(it); + context.static_objects.emplace_back(std::move(food)); + } + empty_rooms_buffer.clear(); + m_context = std::move(context); } -void CommonBuilder::create_state(IStateManager &state_manager, sf::VideoMode &mode, std::string &window_title) { +void CommonBuilder::create_state(IStateManager &state_manager, const sf::VideoMode &mode, const std::string &window_title) { m_game_state = std::make_unique<GameState>(state_manager, mode, window_title); } void CommonBuilder::set_all_to_state() { - std::vector<std::unique_ptr<Room>> rooms; + std::vector<Room*> rooms; for (auto& row : m_rooms) for (auto& col : row) - rooms.emplace_back(std::move(col)); - m_game_state->set_maze(Maze(std::move(rooms))); + rooms.push_back(col); + Maze maze(rooms); + m_game_state->set_maze(std::move(maze)); m_game_state->set_context(std::move(m_context)); } @@ -45,53 +46,53 @@ std::unique_ptr<GameState> CommonBuilder::get_game() { return std::move(m_game_state); } -void SimpleBuilder::create_context(float dynamic_objects_ratio) { - GameContext context; - std::vector<Room*> buffer; - //РїСЂРѕС…РѕРґРёРјСЃСЏ РїРѕ комнатам, если РѕРЅР° пустая то добавляем РІ buffer - for (auto& row : m_rooms) { - for (auto& room : row) { - if (room != nullptr) - buffer.emplace_back(room); - } - } - - int id = rand() % buffer.size(); - //помещаем пакмана РІ рандомную комнату Рё удаляем ее РёР· буфера - m_context.pacman.set_location(buffer[id]); - auto it = buffer.begin(); - for (int i = 0; i < id; i++, it++); - buffer.erase(it); - - for (auto it = buffer.begin(); it != buffer.end(); it++) { - auto food = std::make_unique<Food>(); - food->set_location(buffer[id]); - context.static_objects.emplace_back(std::move(food)); - } - - buffer.clear(); - m_context = std::move(context); -} - void SimpleBuilder::create_rooms() { - size_t room_col = m_width / m_room_size; - size_t room_row = m_height / m_room_size; - + size_t rows = m_height / m_room_size; + size_t cols = 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_col)/2 + m_room_size/2, (m_height - m_room_size*room_row)/2 + + m_room_size/2}; - - for (int row = 0; row < room_row * room_size; row += room_size) { - std::vector<Room*> vector_row; - for (int col = 0; col < room_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(room); + sf::Vector2f left_pos = sf::Vector2f{ (m_width - m_room_size * cols + m_room_size)/2, (m_height - m_room_size * rows + m_room_size)/2}; + for (int row = 0; row < rows * room_size; row += room_size) { + std::vector<Room*> row_vec; + for (size_t col = 0; col < cols * room_size; col += room_size) { + Room* room = new Room(m_room_size); + room->set_position(sf::Vector2f {col + left_pos.x, row + left_pos.y}); + row_vec.push_back(std::move(room)); } - m_rooms.emplace_back(std::move(vector_row)); + m_rooms.push_back(std::move(row_vec)); } } void SimpleBuilder::set_rooms_sides() { + for (size_t i = 0; i < m_rooms.size(); i++) { + for (size_t j = 0; j < m_rooms.begin()->size(); j++) { + if (i == 0) { + auto up_wall = std::make_shared<Wall>(*m_rooms[i][j]); + m_rooms[i][j]->set_side(Room::UP, std::move(up_wall)); + } + else { + auto pass = std::make_shared<Pass>(*m_rooms[i - 1][j], *m_rooms[i][j]); + m_rooms[i][j]->set_side(Room::UP, pass); + m_rooms[i - 1][j]->set_side(Room::DOWN, pass); + } + if (j == 0) { + auto left_wall = std::make_shared<Wall>(*m_rooms[i][j]); + m_rooms[i][j]->set_side(Room::LEFT, std::move(left_wall)); + } + else { + auto pass = std::make_shared<Pass>(*m_rooms[i][j - 1], *m_rooms[i][j]); + m_rooms[i][j]->set_side(Room::LEFT, pass); + m_rooms[i][j - 1]->set_side(Room::RIGHT, pass); + } + if (i == m_rooms.size() - 1) { + auto down_wall = std::make_shared<Wall>(*m_rooms[i][j]); + m_rooms[i][j]->set_side(Room::DOWN, std::move(down_wall)); + } + if (j == m_rooms.begin()->size() - 1) { + auto right_wall = std::make_shared<Wall>(*m_rooms[i][j]); + m_rooms[i][j]->set_side(Room::RIGHT, std::move(right_wall)); + } + } + } } diff --git a/source/application/BobBuilder/GameBuilders.h b/source/application/BobBuilder/GameBuilders.h index cef221acae31e756a7c4fe02ab97d5e86cbcd9db..fe8965a36c7741494c1bc6c37f5c1b9cef03721a 100644 --- a/source/application/BobBuilder/GameBuilders.h +++ b/source/application/BobBuilder/GameBuilders.h @@ -5,9 +5,10 @@ class CommonBuilder : public IGameBuilder { public: - void create_context(float dynamic_objects_ratio); - void create_state(IStateManager& state_manager, sf::VideoMode& mode, std::string& window_title); - void set_all_to_state(); + CommonBuilder(float width, float height, float room_size); + void create_context(float dynamic_objects_ratio) override; + void create_state(IStateManager& state_manager, const sf::VideoMode& mode, const std::string& window_title) override; + void set_all_to_state() override; std::unique_ptr<GameState> get_game() override; protected: @@ -21,21 +22,14 @@ protected: class SimpleBuilder : public CommonBuilder { public: - void create_context(float dynamic_objects_ratio); - SimpleBuilder(float width, float height, float room_size) : m_width(width), m_height(height), m_room_size(room_size) {}; + using CommonBuilder::CommonBuilder; void create_rooms() override; void set_rooms_sides() override; -private: - float m_width; - float m_height; - float m_room_size; - std::vector<std::vector<Room*>> m_rooms; - GameContext m_context; - std::unique_ptr<GameState> m_game_state; }; class ComplexBuilder : public CommonBuilder { public: + using CommonBuilder::CommonBuilder; void create_rooms() override; void set_rooms_sides() override; }; \ No newline at end of file diff --git a/source/application/BobBuilder/IGameBuilder.h b/source/application/BobBuilder/IGameBuilder.h index 6865be2cb232bcd6b61620e4d38384999152c79b..4f7b9d6b3fd027d83f3bc22ade5e6dcfeea4f965 100644 --- a/source/application/BobBuilder/IGameBuilder.h +++ b/source/application/BobBuilder/IGameBuilder.h @@ -7,10 +7,8 @@ public: virtual void create_rooms() = 0; virtual void set_rooms_sides() = 0; virtual void create_context(float dynamic_objects_ratio) = 0; - virtual void create_state(IStateManager& state_manager, sf::VideoMode& mode, std::string& window_title) = 0; + virtual void create_state(IStateManager& state_manager, const sf::VideoMode& mode, const std::string& window_title) = 0; virtual void set_all_to_state() = 0; virtual std::unique_ptr<GameState> get_game() = 0; virtual ~IGameBuilder() = default; }; - -///@todo created CommonBuilder + edited Simple and Complex Builder \ No newline at end of file diff --git a/source/application/Context/Context.cpp b/source/application/Context/Context.cpp index b0ce4b0e0fd98fc19b3d29179a5e1a06b5906e91..2757e87538cb91183250ac7a0e3e00455f54fef3 100644 --- a/source/application/Context/Context.cpp +++ b/source/application/Context/Context.cpp @@ -2,9 +2,9 @@ GameContext GameContext::clone() { GameContext context; - context.pacman = std::move(pacman); + context.pacman = pacman; context.state = state; - for (auto& obj : static_objects) { + for (auto& obj: static_objects) { context.static_objects.emplace_back(obj->clone()); } for (auto& obj : dynamic_objects) { diff --git a/source/application/Drawable/DrawMenu/Button/Button.cpp b/source/application/Drawable/DrawMenu/Button/Button.cpp index f1e334f5b5e14eae921fb93a0820ea773b158440..2cd23ae91b7fb9a3ffd8ee2303e0730e6bc08a26 100644 --- a/source/application/Drawable/DrawMenu/Button/Button.cpp +++ b/source/application/Drawable/DrawMenu/Button/Button.cpp @@ -32,7 +32,7 @@ bool Button::is_position_in(sf::Vector2f pos) const noexcept { void Button::push() { m_ptr_command->execute(); } -void Button::draw_into(sf::RenderWindow& window) const { +void Button::draw_into(sf::RenderWindow& window) { window.draw(m_rectangle); window.draw(m_text); } \ No newline at end of file diff --git a/source/application/Drawable/DrawMenu/Button/Button.h b/source/application/Drawable/DrawMenu/Button/Button.h index eab1a54b0a1c0bd941f2af301cbaee44cb2492fc..eb39b81802f95bb3e16aa4443b5b3b60dc24394a 100644 --- a/source/application/Drawable/DrawMenu/Button/Button.h +++ b/source/application/Drawable/DrawMenu/Button/Button.h @@ -16,7 +16,7 @@ public: bool is_selected() const noexcept; bool is_position_in(sf::Vector2f pos) const noexcept; void push(); - void draw_into(sf::RenderWindow& window) const override; + void draw_into(sf::RenderWindow& window) override; private: sf::Text m_text; diff --git a/source/application/Drawable/DrawMenu/Menu/Menu.cpp b/source/application/Drawable/DrawMenu/Menu/Menu.cpp index 03640828c265b3c9c4e827ce43d6dba6ce1113f4..833f2c6b9a1c75805588919740debc4799afbbc2 100644 --- a/source/application/Drawable/DrawMenu/Menu/Menu.cpp +++ b/source/application/Drawable/DrawMenu/Menu/Menu.cpp @@ -39,7 +39,7 @@ Menu::Menu(IStateManager& state_manager) { m_buttons.emplace_back(std::make_unique<Button>(sf::Vector2f{starting_position.x, starting_position.y + 4 * config::BUTTON_SIZE.y + 3 * delta}, config::BUTTON_SIZE, config::BUTTON_TEXT_EXIT, config::BUTTON_FONT_SIZE, std::move(exit_command))); } -void Menu::draw_into(sf::RenderWindow &window) const { +void Menu::draw_into(sf::RenderWindow &window) { //@todo change to const Button& for (auto& ptr_button : m_buttons) ptr_button->draw_into(window); diff --git a/source/application/Drawable/DrawMenu/Menu/Menu.h b/source/application/Drawable/DrawMenu/Menu/Menu.h index 57282882c037db6109c26dec091929d97d356567..9cf8ced9c80f6ba38bc0a87f38898f970bf6cddc 100644 --- a/source/application/Drawable/DrawMenu/Menu/Menu.h +++ b/source/application/Drawable/DrawMenu/Menu/Menu.h @@ -7,7 +7,7 @@ class Menu : public IDrawable { public: Menu(IStateManager& state_manager); - void draw_into(sf::RenderWindow& window) const override; + void draw_into(sf::RenderWindow& window) override; void process_mouse(const sf::Vector2f pos, bool is_pressed); private: diff --git a/source/application/Drawable/Entity/IEntity.cpp b/source/application/Drawable/Entity/IEntity.cpp index 603aec96a982ae514a939f8f7e1cf4a606d6f348..966fcad5aef7e35f4cb6ba2e07ab967d512d3f91 100644 --- a/source/application/Drawable/Entity/IEntity.cpp +++ b/source/application/Drawable/Entity/IEntity.cpp @@ -7,41 +7,49 @@ void IEntity::set_location(Room* ptr_room) { } Food::Food() { - //@todo - m_shape = sf::CircleShape(config::GAME_FOOD_SIZE, 30); + m_shape = sf::CircleShape(config::GAME_FOOD_SIZE); + 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 { return std::make_unique<Food>(*this); } -void Food::draw_into(sf::RenderWindow &window) const { - //@todo - window.draw(m_shape); +void Food::prepare_for_drawing() { + m_shape.setPosition(m_ptr_room->get_position()); } -void Food::prepare_for_drawing() { - //@todo +void Food::draw_into(sf::RenderWindow &window) { + prepare_for_drawing(); + window.draw(m_shape); } Enemy::Enemy() { - //@todo - m_shape = sf::CircleShape(config::GAME_ENEMY_SIZE, 10); + m_shape = sf::CircleShape(config::GAME_ENEMY_SIZE); + m_shape.setFillColor(config::GAME_ENEMY_COLOR); + m_shape.setOrigin({config::GAME_ENEMY_SIZE / 2, config::GAME_ENEMY_SIZE / 2}); } std::unique_ptr<IDynamicEntity> Enemy::clone() const { return std::make_unique<Enemy>(*this); } -void Enemy::draw_into(sf::RenderWindow &window) const { - //@todo - window.draw(m_shape); +void Enemy::prepare_for_drawing() { + m_shape.setPosition(m_ptr_room->get_position()); } -void Enemy::prepare_for_drawing() { - //@todo +void Enemy::draw_into(sf::RenderWindow &window) { + prepare_for_drawing(); + window.draw(m_shape); } void Enemy::action() { - //@todo pdf + auto miliseconds = static_cast<size_t>(m_clock.getElapsedTime().asMilliseconds()); + if (miliseconds < rand() % 10000) + return; + auto direction = static_cast<Room::Direction>(rand() % 4); + m_ptr_room->get_side(direction)->enter(this); + m_clock.restart(); + } \ No newline at end of file diff --git a/source/application/Drawable/Entity/IEntity.h b/source/application/Drawable/Entity/IEntity.h index a57bf617b2341379e711494fd6a4f04de7ad4683..f833764fda5a5fdc600c7ecafb47f35330772e3b 100644 --- a/source/application/Drawable/Entity/IEntity.h +++ b/source/application/Drawable/Entity/IEntity.h @@ -26,21 +26,21 @@ public: class Enemy : public IDynamicEntity { public: Enemy(); - void draw_into(sf::RenderWindow& window) const override; + void draw_into(sf::RenderWindow& window) override; void action() override; void prepare_for_drawing() override; std::unique_ptr<IDynamicEntity> clone() const override; private: sf::CircleShape m_shape; + sf::Clock m_clock; }; class Food: public IStaticEntity { public: Food(); - Food(const Food& food) = default; - void draw_into(sf::RenderWindow& window) const override; + void draw_into(sf::RenderWindow& window) override; void prepare_for_drawing() override; - std::unique_ptr<IStaticEntity> clone() const override; + std::unique_ptr<IStaticEntity> clone() const override; private: sf::CircleShape m_shape; }; \ No newline at end of file diff --git a/source/application/Drawable/Entity/Pacman.cpp b/source/application/Drawable/Entity/Pacman.cpp index 79bc4bd2fff86232867be1017faad8d3892fde03..582276067760fdbc59a239f953446256a4608b83 100644 --- a/source/application/Drawable/Entity/Pacman.cpp +++ b/source/application/Drawable/Entity/Pacman.cpp @@ -2,20 +2,20 @@ #include "../../../../workdir/config.h" Pacman::Pacman() { - //@todo - m_shape = sf::CircleShape(config::GAME_PACMAN_SIZE, 10); + 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}); } void Pacman::move(Room::Direction direction) { - //@todo move pacman m_ptr_room->get_side(direction)->enter(this); } -void Pacman::draw_into(sf::RenderWindow &window) const { - //@todo - window.draw(m_shape); +void Pacman::prepare_for_drawing() { + m_shape.setPosition(m_ptr_room->get_position()); } -void Pacman::prepare_for_drawing() { - //@todo -} \ No newline at end of file +void Pacman::draw_into(sf::RenderWindow &window) { + prepare_for_drawing(); + window.draw(m_shape); +} diff --git a/source/application/Drawable/Entity/Pacman.h b/source/application/Drawable/Entity/Pacman.h index d481813ce9c201c2717d9f7e918e016ed170136b..445d516a46d152994ec0d98f352a6ac57e319bdb 100644 --- a/source/application/Drawable/Entity/Pacman.h +++ b/source/application/Drawable/Entity/Pacman.h @@ -8,7 +8,7 @@ public: Pacman(); void move(Room::Direction direction); void prepare_for_drawing() override; - void draw_into(sf::RenderWindow& window) const override; + void draw_into(sf::RenderWindow& window) override; private: sf::CircleShape m_shape; }; \ No newline at end of file diff --git a/source/application/Drawable/IDrawable.h b/source/application/Drawable/IDrawable.h index 60d0f650b3d2fbfa33be8326887459e1577770d6..8a4a28599fdfa2854fa40e8842f9497eecb35a92 100644 --- a/source/application/Drawable/IDrawable.h +++ b/source/application/Drawable/IDrawable.h @@ -3,7 +3,7 @@ #include "SFML/Graphics.hpp" struct IDrawable { - virtual void draw_into(sf::RenderWindow& window) const = 0; + virtual void draw_into(sf::RenderWindow& window) = 0; virtual ~IDrawable() = default; }; diff --git a/source/application/Drawable/Maze/Maze.cpp b/source/application/Drawable/Maze/Maze.cpp index d758a2cf944cad447d4896edf9d3f77e9af59032..74ab45842e8f60ba3b7bc3963457331335bf7f6f 100644 --- a/source/application/Drawable/Maze/Maze.cpp +++ b/source/application/Drawable/Maze/Maze.cpp @@ -12,31 +12,64 @@ void Room::set_side(Direction side, std::shared_ptr<IRoomSide> ptr_side) { } } -Room::Direction Room::get_direction(std::shared_ptr<IRoomSide> ptr_side) { - if (m_sides[LEFT] == ptr_side) return LEFT; - if (m_sides[RIGHT] == ptr_side) return RIGHT; - if (m_sides[UP] == ptr_side) return UP; - if (m_sides[DOWN] == ptr_side) return DOWN; +Room::Direction Room::get_direction(IRoomSide* ptr_side) { + if (m_sides[LEFT].get() == ptr_side) return LEFT; + if (m_sides[RIGHT].get() == ptr_side) return RIGHT; + if (m_sides[UP].get() == ptr_side) return UP; + if (m_sides[DOWN].get() == ptr_side) return DOWN; else return INVALID; } -void Room::draw_into(sf::RenderWindow& m_window) const { - for (auto& side : m_sides) { - side->draw_into(m_window); +void Room::draw_into(sf::RenderWindow& window) { + for (const auto& side : m_sides) { + side->draw_into(window); } } -void Maze::draw_into(sf::RenderWindow& m_window) const { +void Maze::draw_into(sf::RenderWindow& m_window) { for (auto& room : m_rooms) { room->draw_into(m_window); } } -void Wall::draw_into(sf::RenderWindow& m_window) const { - //@todo +void Wall::prepare_for_drawing() { + Room::Direction direct = m_room.get_direction(this); + sf::Vector2f pos = m_room.get_position(); + float size = m_room.get_size(); + std::array<sf::Vector2f, 4> corners = { sf::Vector2f{pos.x - size / 2.f, pos.y - size / 2.f}, + sf::Vector2f{pos.x + size / 2.f, pos.y - size / 2.f}, + sf::Vector2f{pos.x + size / 2.f, pos.y + size / 2.f}, + sf::Vector2f{pos.x - size / 2.f, pos.y + size / 2.f} }; + + switch (direct) { + case Room::LEFT: + m_line[0] = sf::Vertex(corners[0], config::GAME_COLOR_WALL); + m_line[1] = sf::Vertex(corners[3], config::GAME_COLOR_WALL); + break; + case Room::RIGHT: + m_line[0] = sf::Vertex(corners[1], config::GAME_COLOR_WALL); + m_line[1] = sf::Vertex(corners[2], config::GAME_COLOR_WALL); + break; + case Room::UP: + m_line[0] = sf::Vertex(corners[0], config::GAME_COLOR_WALL); + m_line[1] = sf::Vertex(corners[1], config::GAME_COLOR_WALL); + break; + case Room::DOWN: + m_line[0] = sf::Vertex(corners[2], config::GAME_COLOR_WALL); + m_line[1] = sf::Vertex(corners[3], config::GAME_COLOR_WALL); + break; + case Room::INVALID: + throw std::runtime_error("Invalid side"); + } + +} + +void Wall::draw_into(sf::RenderWindow& m_window) { + prepare_for_drawing(); m_window.draw(m_line, 2, sf::Lines); } + void Pass::enter(IEntity* entity) { if (entity->get_location() == &m_room2) { entity->set_location(&m_room1); diff --git a/source/application/Drawable/Maze/Maze.h b/source/application/Drawable/Maze/Maze.h index 76d71b7818d15b8ca99c3dfe2e60daee7f1e9614..967923d9d6ff952785b196d4f3ffb4034b1ce043 100644 --- a/source/application/Drawable/Maze/Maze.h +++ b/source/application/Drawable/Maze/Maze.h @@ -14,14 +14,14 @@ class Room: public IDrawable { public: enum Direction { INVALID = -1, LEFT, RIGHT, UP, DOWN }; - Room(float size); + explicit Room(float size); float get_size() { return m_rectangle.getSize().x; } void set_position(sf::Vector2f pos) { m_rectangle.setPosition(pos); } sf::Vector2f get_position() { return m_rectangle.getPosition(); } void set_side(Direction side, std::shared_ptr<IRoomSide> ptr_side); - std::shared_ptr<IRoomSide> get_side(Direction side) { return m_sides[side]; } - Direction get_direction(std::shared_ptr<IRoomSide> ptr_side); - void draw_into(sf::RenderWindow& m_window) const override; + IRoomSide* get_side(Direction side) { return m_sides[side].get(); } + Direction get_direction(IRoomSide* ptr_side); + void draw_into(sf::RenderWindow& m_window) override; public: std::array<std::shared_ptr<IRoomSide>, 4> m_sides; @@ -31,17 +31,18 @@ public: class Maze: public IDrawable { public: Maze() = default; - Maze(std::vector<std::unique_ptr<Room>> rooms) : m_rooms(std::move(rooms)) {}; - void draw_into(sf::RenderWindow& m_window) const override; + Maze(std::vector<Room*>& rooms) : m_rooms(rooms) {}; + void draw_into(sf::RenderWindow& m_window) override; private: - std::vector<std::unique_ptr<Room>> m_rooms; + std::vector<Room*> m_rooms; }; ///стена, которая хранит РІ себе информацию Рѕ том РІ какой комнате находится class Wall: public IRoomSide { public: - Wall(Room& room) : m_room(room) {}; - void draw_into(sf::RenderWindow& m_window) const override; + explicit Wall(Room& room) : m_room(room) {}; + void prepare_for_drawing() override; + void draw_into(sf::RenderWindow& m_window) override; void enter(IEntity* entity) override {}; private: Room& m_room; @@ -52,8 +53,9 @@ private: class Pass: public IRoomSide { public: Pass(Room& room1, Room& room2) : m_room1(room1), m_room2(room2) {}; - void draw_into(sf::RenderWindow& m_window) const override {}; + void draw_into(sf::RenderWindow& m_window) override {}; void enter(IEntity* entity) override; + void prepare_for_drawing() override {}; private: Room& m_room1; Room& m_room2; diff --git a/source/application/Event/IGameEvent.cpp b/source/application/Event/IGameEvent.cpp index 7f25b9cc3324c3dd4ef26f5b55bc026770d14426..acd31bc397a06bb5d4dd3d688c8a48c2c71ed653 100644 --- a/source/application/Event/IGameEvent.cpp +++ b/source/application/Event/IGameEvent.cpp @@ -1,14 +1,14 @@ #include "IGameEvent.h" #include "../Context/Context.h" -void LostGame::handle(GameContext* context) { - context->state = GameContext::LOST; +void LostGame::handle(GameContext context) { + context.state = GameContext::LOST; } -void DeleteStaticEntity::handle(GameContext* context) { - //@todo +void DeleteStaticEntity::handle(GameContext context) { + context.static_objects.erase(m_ptr_entity); } -void WinGame::handle(GameContext *context) { - context->state = GameContext::WIN; +void WinGame::handle(GameContext context) { + context.state = GameContext::WIN; } \ No newline at end of file diff --git a/source/application/Event/IGameEvent.h b/source/application/Event/IGameEvent.h index b43e28c5d0dc972d05baf63ef1198a9f3ebfd236..326eaabaa36197e0d9d49bc05be92df688b7b434 100644 --- a/source/application/Event/IGameEvent.h +++ b/source/application/Event/IGameEvent.h @@ -1,30 +1,31 @@ #pragma once #include <iostream> +#include <list> class GameContext; class IStaticEntity; class IGameEvent { public: - virtual void handle(GameContext* context) = 0; + virtual void handle(GameContext context) = 0; virtual ~IGameEvent() = default; }; class WinGame : public IGameEvent { public: - void handle(GameContext* context) override; + void handle(GameContext context) override; }; class LostGame : public IGameEvent { public: - void handle(GameContext* context) override; + void handle(GameContext context) override; }; class DeleteStaticEntity : public IGameEvent { public: - //@todo DeleteStaticEntity(std::unique_ptr<IStaticEntity> ptr_entity) : m_ptr_entity(std::move(ptr_entity)) {}; - void handle(GameContext* context) override; + DeleteStaticEntity(std::list<std::unique_ptr<IStaticEntity>>::iterator ptr_entity) : m_ptr_entity(ptr_entity) {}; + void handle(GameContext context) override; private: - std::unique_ptr<IStaticEntity> m_ptr_entity; + std::list<std::unique_ptr<IStaticEntity>>::iterator m_ptr_entity; }; diff --git a/source/application/SelectCommand/ISelectCommand.h b/source/application/SelectCommand/ISelectCommand.h index 8edfbffc97c1de779f989228bc4b102db94136c8..844b111eda10dafea5a70156ec487e1ade8746b6 100644 --- a/source/application/SelectCommand/ISelectCommand.h +++ b/source/application/SelectCommand/ISelectCommand.h @@ -12,14 +12,14 @@ public: class ChangeStateCommand : public ISelectCommand { public: - ChangeStateCommand(IStateManager& state_manager) : m_state_manager(state_manager) {} + explicit ChangeStateCommand(IStateManager& state_manager) : m_state_manager(state_manager) {} protected: IStateManager& m_state_manager; }; class ExitCommand : public ChangeStateCommand { public: - ExitCommand(IStateManager& state_manager) : ChangeStateCommand(state_manager) {} + explicit ExitCommand(IStateManager& state_manager) : ChangeStateCommand(state_manager) {} void execute() override { m_state_manager.set_next_state(std::make_unique<ExitState>(m_state_manager)); } diff --git a/source/application/State/GameState.cpp b/source/application/State/GameState.cpp index 318c381a8e3adc81ae3ccc98e9663c7ca4a28249..d87550d3831bc803f6243e7f57766a28a9b0ec5d 100644 --- a/source/application/State/GameState.cpp +++ b/source/application/State/GameState.cpp @@ -6,7 +6,7 @@ GameState::GameState(IStateManager &state_manager, const sf::VideoMode &video_mo IState(state_manager), IWindowKeeper(config::GAME_VIDEO_MODE, window_title){ } void GameState::event_handling() { - sf::Event event; + sf::Event event{}; while (m_window.pollEvent(event)) { if (event.type == sf::Event::Closed) { m_state_manager.set_next_state(std::make_unique<SelectState>(m_state_manager, config::GAME_VIDEO_MODE, config::SELECT_LEVEL_TITLE)); @@ -47,9 +47,21 @@ void GameState::update() { GameContext& context = m_context_manager.get_current_context(); for (auto& entity : context.dynamic_objects) entity->action(); + for (auto it = context.static_objects.begin(); it != context.static_objects.end(); ++it) { + if (context.pacman.get_location() == (*it)->get_location()) { + m_events.emplace_back(std::move(std::make_unique<DeleteStaticEntity>(it))); + } + } - //@todo + for (auto it = context.dynamic_objects.begin(); it != context.dynamic_objects.end(); ++it) { + if (context.pacman.get_location() == (*it)->get_location()) { + m_events.emplace_back(std::move(std::make_unique<LostGame>())); + } + } + if (context.static_objects.empty()) { + m_events.emplace_back(std::move(std::make_unique<WinGame>())); + } } void GameState::render() { @@ -64,12 +76,14 @@ void GameState::render() { default: m_window.clear(config::GAME_COLOR_BACKGROUND_WIN); } - m_maze.draw_into(m_window); - for (auto& el: context.static_objects) + for (auto& el: context.static_objects) { el->draw_into(m_window); + } + context.pacman.draw_into(m_window); + for (auto& el : context.dynamic_objects) el->draw_into(m_window); m_window.display(); @@ -83,6 +97,6 @@ bool GameState::do_step() { } void GameState::set_context(GameContext&& context) { - m_context_manager.get_current_context(); m_context_manager.save_current_context(); + m_context_manager.reset(std::move(context)); } \ No newline at end of file diff --git a/source/application/State/GameState.h b/source/application/State/GameState.h index bfdc249d6467b87d18ab3494f0fbb91c7ac9ae58..39472377dfede4cd8afbbec9832355e941fb55a2 100644 --- a/source/application/State/GameState.h +++ b/source/application/State/GameState.h @@ -2,6 +2,7 @@ #include "IState.h" #include "../Drawable/Maze/Maze.h" #include "../Context/Context.h" +#include "../Event/IGameEvent.h" class GameState: public IState, public IWindowKeeper { public: @@ -17,4 +18,5 @@ public: private: Maze m_maze; ContextManager m_context_manager; + std::vector<std::unique_ptr<IGameEvent>> m_events; }; diff --git a/source/application/State/IState.h b/source/application/State/IState.h index 7642442859c7c2bc6cde3c2604e30c310b030afa..1f28e4f92f36b3b732266ac3c7452a09ac9862f9 100644 --- a/source/application/State/IState.h +++ b/source/application/State/IState.h @@ -9,7 +9,7 @@ class IStateManager; class IState { public: - IState(IStateManager& state_manager) : m_state_manager(state_manager) {} + explicit IState(IStateManager& state_manager) : m_state_manager(state_manager) {} virtual ~IState() = default; virtual bool do_step() = 0; @@ -19,7 +19,7 @@ protected: class ExitState: public IState { public: - ExitState(IStateManager& state_manager) : IState(state_manager) {} + explicit ExitState(IStateManager& state_manager) : IState(state_manager) {} bool do_step() override {return false; } }; diff --git a/source/application/State/SelectState.cpp b/source/application/State/SelectState.cpp index d60ddcc98b96ae67922744ebef038190c16c6839..64aeff9f386adf1d7a11bca1ab9f814b497dcc11 100644 --- a/source/application/State/SelectState.cpp +++ b/source/application/State/SelectState.cpp @@ -7,7 +7,7 @@ SelectState::SelectState(IStateManager& state_manager, IState(state_manager), IWindowKeeper(video_mode, window_title), m_menu(state_manager) {} void SelectState::event_handling() { - sf::Event event; + sf::Event event{}; while (m_window.pollEvent(event)) { if (event.type == sf::Event::Closed) { m_state_manager.set_next_state(std::make_unique<ExitState>(m_state_manager)); diff --git a/source/main.cpp b/source/main.cpp index 1a36e9473a5540a363ea00a4d73fe805431493f6..688c439b38debafaaa03bf924b7f5379ae17a91e 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -1,6 +1,7 @@ -#include "application/app/Application.h" +#include "application/App/Application.h" int main() { Application app; return app.run(); -} \ No newline at end of file +} + diff --git a/workdir/config.h b/workdir/config.h index f23f852405566e9383b3ed0eea0db79bc046d018..487db35bb2334776d86a0eb959b868a74d8857b3 100644 --- a/workdir/config.h +++ b/workdir/config.h @@ -16,7 +16,7 @@ namespace config { const char BUTTON_TEXT_HARD[] = "Hard"; const char BUTTON_TEXT_EXIT[] = "Exit"; // РРіСЂР°: - const sf::VideoMode GAME_VIDEO_MODE{ 1080, 720 }; + const sf::VideoMode GAME_VIDEO_MODE{ 880, 620 }; const char EASY_GAME_TITLE[] = "Level: Easy"; const char MEDIUM_GAME_TITLE[] = "Level: Medium"; const char HARD_GAME_TITLE[] = "Level: Hard";