From 581a115a8fb6e1c0f27881543b6640d17990a0ae Mon Sep 17 00:00:00 2001 From: Sulimov Igor Andreevich <igansulimov@edu.hse.ru> Date: Mon, 24 Mar 2025 03:21:05 +0300 Subject: [PATCH] Completed Room --- CMakeLists.txt | 1 + include/Commands/ExitCommand.h | 2 +- include/{config.h => Config.h} | 6 ++-- include/Draw/Button.h | 2 +- include/MazeContent/Maze.h | 2 +- include/MazeContent/Room.h | 14 ++++---- include/States/ExitState.h | 2 +- source/Draw/MyFont.cpp | 2 +- source/Maze_Content/Room.cpp | 38 ++++++++++++++++++++++ source/Maze_Content/Wall.cpp | 59 ++++++++++++++++++++++++++++++++++ 10 files changed, 113 insertions(+), 15 deletions(-) rename include/{config.h => Config.h} (93%) create mode 100644 source/Maze_Content/Room.cpp create mode 100644 source/Maze_Content/Wall.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 8524300..e3e750c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,7 @@ add_executable(pac-man "source/Draw/Button.cpp" "source/Draw/MyFont.cpp" "source/Commands/ExitCommand.cpp" + "source/Maze_Content/Room.cpp" ) diff --git a/include/Commands/ExitCommand.h b/include/Commands/ExitCommand.h index d51d0ed..bedffb3 100644 --- a/include/Commands/ExitCommand.h +++ b/include/Commands/ExitCommand.h @@ -4,7 +4,7 @@ class ExitCommand: public ChangeStateCommand { public: - void execute() override; ///@todo + void execute() override; public: ExitCommand(IStateManager& state_manager); }; \ No newline at end of file diff --git a/include/config.h b/include/Config.h similarity index 93% rename from include/config.h rename to include/Config.h index ffdca33..0c2cc14 100644 --- a/include/config.h +++ b/include/Config.h @@ -23,7 +23,7 @@ namespace config { // const float EASY_GAME_ENEMY_RATIO = 0.0f; // const float MEDIUM_GAME_ENEMY_RATIO = 0.03f; // const float HARD_GAME_ENEMY_RATIO = 0.07f; -// const float ROOM_SIZE = 50; + const float ROOM_SIZE = 50; // const float GAME_ENEMY_SIZE = ROOM_SIZE * 0.7; // const float GAME_FOOD_SIZE = ROOM_SIZE * 0.2; // Пакмэн: @@ -42,8 +42,8 @@ namespace config { // const sf::Color GAME_COLOR_BACKGROUND_WIN{ 0, 255, 0 }; // const sf::Color GAME_COLOR_BACKGROUND_LOST{ 255, 0, 0 }; // const sf::Color GAME_COLOR_PACMAN{ 250, 150, 0 }; -// const sf::Color GAME_COLOR_ROOM{ 255, 255, 255 }; -// const sf::Color GAME_COLOR_WALL{ 0, 0, 0 }; + const sf::Color GAME_COLOR_ROOM{ 255, 255, 255 }; + const sf::Color GAME_COLOR_WALL{ 0, 0, 0 }; // const sf::Color GAME_FOOD_COLOR{ 0, 200, 100 }; // const sf::Color GAME_ENEMY_COLOR{ 255, 50, 0 }; } \ No newline at end of file diff --git a/include/Draw/Button.h b/include/Draw/Button.h index 573031e..c908ba8 100644 --- a/include/Draw/Button.h +++ b/include/Draw/Button.h @@ -1,6 +1,6 @@ #pragma once -#include "config.h" +#include "Config.h" #include "IDrawable.h" #include "Commands/ISelectCommand.h" diff --git a/include/MazeContent/Maze.h b/include/MazeContent/Maze.h index 5fea69d..a7f099c 100644 --- a/include/MazeContent/Maze.h +++ b/include/MazeContent/Maze.h @@ -4,7 +4,7 @@ class Maze: public IDrawable { public: - void draw_into(sf::RenderWindow& window) const override; ///@todo + void draw_into(sf::RenderWindow& window) const override; public: Maze(std::vector<std::unique_ptr<Room>>&& rooms): m_rooms(std::move(rooms)) {} private: diff --git a/include/MazeContent/Room.h b/include/MazeContent/Room.h index 3a8a9de..5362f54 100644 --- a/include/MazeContent/Room.h +++ b/include/MazeContent/Room.h @@ -5,14 +5,14 @@ class Room: public IDrawable { public: enum Direction { INVALID = -1, LEFT, RIGHT, UP, DOWN }; - float get_size(); ///@todo - void set_position(sf::Vector2f); ///@todo - sf::Vector2f get_position(); ///@todo - void set_side(Direction side, IRoomSide prt_side); ///@todo - IRoomSide get_side(Direction side); ///@todo - Direction get_direction(IRoomSide prt_side); ///@todo + float get_size() const noexcept; + void set_position(const sf::Vector2f& pos) noexcept; + sf::Vector2f get_position() const noexcept; + void set_side(Direction side, std::shared_ptr<IRoomSide> ptr_side) noexcept; + std::shared_ptr<IRoomSide> get_side(Direction side); + Direction get_direction(IRoomSide* ptr_side); public: - Room(float size); ///@todo + Room(float size); public: std::array<std::shared_ptr<IRoomSide>, 4> m_sides; private: diff --git a/include/States/ExitState.h b/include/States/ExitState.h index dc88262..92ccdad 100644 --- a/include/States/ExitState.h +++ b/include/States/ExitState.h @@ -6,5 +6,5 @@ class ExitState: public IState { public: bool do_step() override { return false; } public: - ExitState(IStateManager& state_manager); + ExitState(IStateManager& state_manager): IState(state_manager) {}; }; \ No newline at end of file diff --git a/source/Draw/MyFont.cpp b/source/Draw/MyFont.cpp index 0389c99..118d214 100644 --- a/source/Draw/MyFont.cpp +++ b/source/Draw/MyFont.cpp @@ -1,4 +1,4 @@ -#include "config.h" +#include "Config.h" #include "Draw/MyFont.h" MyFont::MyFont() { diff --git a/source/Maze_Content/Room.cpp b/source/Maze_Content/Room.cpp new file mode 100644 index 0000000..78c3aad --- /dev/null +++ b/source/Maze_Content/Room.cpp @@ -0,0 +1,38 @@ +#include "MazeContent/Room.h" +#include "Config.h" + +Room::Room(float size) : m_rectangle{ {size, size} } { + m_rectangle.setOrigin(m_rectangle.getSize() / 2.f); + m_rectangle.setFillColor(config::GAME_COLOR_ROOM); +} + +float Room::get_size() const noexcept { + return m_rectangle.getSize().x; +} + +void Room::set_position(const sf::Vector2f& pos) noexcept { + m_rectangle.setPosition(pos); +} + +sf::Vector2f Room::get_position() const noexcept { + return m_rectangle.getPosition(); +} + +void Room::set_side(Direction side, std::shared_ptr<IRoomSide> ptr_side) noexcept { + if (side == INVALID) + return; + m_sides.at(side) = std::move(ptr_side); + m_sides.at(side)->prepare_for_drawing(); +} + +std::shared_ptr<IRoomSide> Room::get_side(Direction side) { + return m_sides.at(side); +} + +Room::Direction Room::get_direction(IRoomSide* ptr_side) { + if (m_sides.at(LEFT).get() == ptr_side) return LEFT; + else if (m_sides.at(RIGHT).get() == ptr_side) return RIGHT; + else if (m_sides.at(UP).get() == ptr_side) return UP; + else if (m_sides.at(DOWN).get() == ptr_side) return DOWN; + return INVALID; +} \ No newline at end of file diff --git a/source/Maze_Content/Wall.cpp b/source/Maze_Content/Wall.cpp new file mode 100644 index 0000000..cfd3010 --- /dev/null +++ b/source/Maze_Content/Wall.cpp @@ -0,0 +1,59 @@ +#include "MazeContent/Wall.h" +#include "Config.h" + +//void Wall::prepare_for_drawing() { +// sf::Vector2f pos = m_room.get_position(); +// float size = config::ROOM_SIZE; +// +// switch (m_room.get_direction(this)) { +// case Room::UP: +// m_line[0] = sf::Vertex({ pos.x - size / 2, pos.y - size / 2 }, config::GAME_COLOR_WALL); +// m_line[1] = sf::Vertex({ pos.x + size / 2, pos.y - size / 2 }, config::GAME_COLOR_WALL); +// break; +// +// case Room::RIGHT: +// m_line[0] = sf::Vertex({ pos.x + size / 2, pos.y - size / 2 }, config::GAME_COLOR_WALL); +// m_line[1] = sf::Vertex({ pos.x + size / 2, pos.y + size / 2 }, config::GAME_COLOR_WALL); +// break; +// +// case Room::DOWN: +// m_line[0] = sf::Vertex({ pos.x + size / 2, pos.y + size / 2 }, config::GAME_COLOR_WALL); +// m_line[1] = sf::Vertex({ pos.x - size / 2, pos.y + size / 2 }, config::GAME_COLOR_WALL); +// break; +// +// case Room::LEFT: +// m_line[0] = sf::Vertex({ pos.x - size / 2, pos.y + size / 2 }, config::GAME_COLOR_WALL); +// m_line[1] = sf::Vertex({ pos.x - size / 2, pos.y - size / 2 }, config::GAME_COLOR_WALL); +// break; +// +// default: +// throw std::runtime_error("There is invalid side in some room"); +// } +//} +// +//void Wall::prepare_for_drawing() { +// if (m_room.get_direction(this) == Room::LEFT) { +// m_line[0] = sf::Vertex{ sf::Vector2f(m_room.get_position().x - m_room.get_size() / 2, +// m_room.get_position().y - m_room.get_size() / 2), config::GAME_COLOR_WALL }; +// m_line[1] = sf::Vertex{ sf::Vector2f(m_room.get_position().x - m_room.get_size() / 2, +// m_room.get_position().y + m_room.get_size() / 2), config::GAME_COLOR_WALL }; +// } +// else if (m_room.get_direction(this) == Room::RIGHT) { +// m_line[0] = sf::Vertex{ sf::Vector2f(m_room.get_position().x + m_room.get_size() / 2, +// m_room.get_position().y - m_room.get_size() / 2), config::GAME_COLOR_WALL }; +// m_line[1] = sf::Vertex{ sf::Vector2f(m_room.get_position().x + m_room.get_size() / 2, +// m_room.get_position().y + m_room.get_size() / 2), config::GAME_COLOR_WALL }; +// } +// else if (m_room.get_direction(this) == Room::UP) { +// m_line[0] = sf::Vertex{ sf::Vector2f( m_room.get_position().x - m_room.get_size() / 2, +// m_room.get_position().y - m_room.get_size() / 2 ), config::GAME_COLOR_WALL }; +// m_line[1] = sf::Vertex{ sf::Vector2f( m_room.get_position().x + m_room.get_size() / 2, +// m_room.get_position().y - m_room.get_size() / 2), config::GAME_COLOR_WALL }; +// } +// else if (m_room.get_direction(this) == Room::DOWN) { +// m_line[0] = sf::Vertex{ sf::Vector2f(m_room.get_position().x - m_room.get_size() / 2, +// m_room.get_position().y + m_room.get_size() / 2), config::GAME_COLOR_WALL }; +// m_line[1] = sf::Vertex{ sf::Vector2f(m_room.get_position().x + m_room.get_size() / 2, +// m_room.get_position().y + m_room.get_size() / 2), config::GAME_COLOR_WALL }; +// } +//} \ No newline at end of file -- GitLab