Commit 299422de authored by Ушкова Диана Петровна's avatar Ушкова Диана Петровна
Browse files

final version

parent 7afb6eaf
No related merge requests found
Showing with 18 additions and 15 deletions
+18 -15
...@@ -16,7 +16,7 @@ void CommonBuilder::create_context(float static_objects_ratio, float dynamic_obj ...@@ -16,7 +16,7 @@ void CommonBuilder::create_context(float static_objects_ratio, float dynamic_obj
for (auto& row : m_rooms) for (auto& row : m_rooms)
for (auto& room : row) for (auto& room : row)
if (room != nullptr) if (room != nullptr)
empty_rooms_buffer.emplace_back(room); empty_rooms_buffer.emplace_back(room.get());
///> в рандомное место располагаем пакмана ///> в рандомное место располагаем пакмана
float id = std::rand() % empty_rooms_buffer.size(); float id = std::rand() % empty_rooms_buffer.size();
m_context.pacman.set_location(empty_rooms_buffer[id]); m_context.pacman.set_location(empty_rooms_buffer[id]);
...@@ -70,12 +70,11 @@ void CommonBuilder::create_state(IStateManager &state_manager, const sf::VideoMo ...@@ -70,12 +70,11 @@ void CommonBuilder::create_state(IStateManager &state_manager, const sf::VideoMo
* @brief Созданный лабиринт и контекст назначаем созданному состоянию игры GameState * @brief Созданный лабиринт и контекст назначаем созданному состоянию игры GameState
*/ */
void CommonBuilder::set_all_to_state() { void CommonBuilder::set_all_to_state() {
std::vector<Room*> rooms; std::vector<std::unique_ptr<Room>> rooms;
for (auto& row : m_rooms) for (auto& row : m_rooms)
for (auto& col : row) for (auto& col : row)
rooms.emplace_back(col); rooms.emplace_back(std::move(col));
Maze maze(rooms); m_game_state->set_maze(Maze(rooms)); ///> назначаем лабиринт
m_game_state->set_maze(std::move(maze)); ///> назначаем лабиринт
m_game_state->set_context(std::move(m_context)); m_game_state->set_context(std::move(m_context));
} }
...@@ -99,11 +98,11 @@ void SimpleBuilder::create_rooms() { ...@@ -99,11 +98,11 @@ void SimpleBuilder::create_rooms() {
int room_size = static_cast<int>(m_room_size); int room_size = static_cast<int>(m_room_size);
sf::Vector2f left_pos = sf::Vector2f{ config::MENU_WIDTH, (m_height - m_room_size * rows + m_room_size)/2}; sf::Vector2f left_pos = sf::Vector2f{ config::MENU_WIDTH, (m_height - m_room_size * rows + m_room_size)/2};
for (int row = 0; row < rows * room_size; row += room_size) { for (int row = 0; row < rows * room_size; row += room_size) {
std::vector<Room*> row_vec; std::vector<std::unique_ptr<Room>> row_vec;
for (size_t col = 0; col < cols * room_size; col += room_size) { for (size_t col = 0; col < cols * room_size; col += room_size) {
Room* room = new Room(m_room_size); std::unique_ptr<Room> room = std::make_unique<Room>(m_room_size);
room->set_position({config::MENU_WIDTH + col, row + left_pos.y}); room->set_position({config::MENU_WIDTH + col, row + left_pos.y});
row_vec.emplace_back(room); row_vec.emplace_back(std::move(room));
} }
m_rooms.emplace_back(std::move(row_vec)); m_rooms.emplace_back(std::move(row_vec));
} }
...@@ -144,9 +143,9 @@ void ComplexBuilder::create_rooms() { ...@@ -144,9 +143,9 @@ void ComplexBuilder::create_rooms() {
auto starting_point = sf::Vector2f{config::MENU_WIDTH, (m_height - m_room_size*room_quantity_row)/2 + + m_room_size/2}; auto starting_point = sf::Vector2f{config::MENU_WIDTH, (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) { for (int row = 0; row < room_quantity_row * room_size; row += room_size) {
std::vector<Room*> vector_row; std::vector<std::unique_ptr<Room>> vector_row;
for (int col = 0; col < room_quantity_col * room_size; col += room_size) { for (int col = 0; col < room_quantity_col * room_size; col += room_size) {
auto room = new Room(m_room_size); auto room = std::make_unique<Room>(m_room_size);
room->set_position(sf::Vector2f {(col + starting_point.x), (row + starting_point.y)}); room->set_position(sf::Vector2f {(col + starting_point.x), (row + starting_point.y)});
vector_row.emplace_back(std::move(room)); vector_row.emplace_back(std::move(room));
} }
......
...@@ -19,7 +19,7 @@ protected: ...@@ -19,7 +19,7 @@ protected:
float m_width; float m_width;
float m_height; float m_height;
float m_room_size; float m_room_size;
std::vector<std::vector<Room*>> m_rooms; std::vector<std::vector<std::unique_ptr<Room>>> m_rooms;
GameContext m_context; GameContext m_context;
std::unique_ptr<GameState> m_game_state; std::unique_ptr<GameState> m_game_state;
}; };
......
...@@ -16,6 +16,8 @@ public: ...@@ -16,6 +16,8 @@ public:
void move(Room::Direction direction); void move(Room::Direction direction);
void prepare_for_drawing() override; void prepare_for_drawing() override;
void set_lives() { m_lives = 3;} void set_lives() { m_lives = 3;}
bool is_active() { return is_second;}
void set_active() { is_second = true;}
void draw_into(sf::RenderWindow& window) override; void draw_into(sf::RenderWindow& window) override;
std::unique_ptr<IGameEvent> visit(Food* ptr_entity) override; std::unique_ptr<IGameEvent> visit(Food* ptr_entity) override;
std::unique_ptr<IGameEvent> visit(Enemy* ptr_entity) override; std::unique_ptr<IGameEvent> visit(Enemy* ptr_entity) override;
...@@ -30,4 +32,5 @@ private: ...@@ -30,4 +32,5 @@ private:
sf::Sound m_sound; sf::Sound m_sound;
MyTexture* m_ptr_texture; MyTexture* m_ptr_texture;
bool m_second_player = false; bool m_second_player = false;
bool is_second = false;
}; };
...@@ -30,10 +30,10 @@ public: ...@@ -30,10 +30,10 @@ public:
class Maze: public IDrawable { class Maze: public IDrawable {
public: public:
Maze() = default; Maze() = default;
explicit Maze(std::vector<Room*>& rooms) : m_rooms(rooms) {}; explicit Maze(std::vector<std::unique_ptr<Room>>& rooms) : m_rooms(std::move(rooms)) {};
void draw_into(sf::RenderWindow& m_window) override; void draw_into(sf::RenderWindow& m_window) override;
private: private:
std::vector<Room*> m_rooms; std::vector<std::unique_ptr<Room>> m_rooms;
}; };
///стена, которая хранит в себе информацию о том в какой комнате находится ///стена, которая хранит в себе информацию о том в какой комнате находится
......
...@@ -44,7 +44,7 @@ void PacmanLoseLifeEvent::handle(GameContext *context) { ...@@ -44,7 +44,7 @@ void PacmanLoseLifeEvent::handle(GameContext *context) {
pacman_lose_life.lose_life(); pacman_lose_life.lose_life();
if (pacman_lose_life.died()) { if (pacman_lose_life.died()) {
if (context->pacman.died() && !m_second_player && context->pacman2.get_lives() == 3) if (context->pacman.died() && !context->pacman2.is_active())
context->state = GameContext::LOST; context->state = GameContext::LOST;
if (context->pacman.died() && context->pacman2.died()) if (context->pacman.died() && context->pacman2.died())
context->state = GameContext::LOST; context->state = GameContext::LOST;
...@@ -69,6 +69,6 @@ SwampEvent::SwampEvent(Room* swamp_location) : m_swamp_location(swamp_location) ...@@ -69,6 +69,6 @@ SwampEvent::SwampEvent(Room* swamp_location) : m_swamp_location(swamp_location)
void SwampEvent::handle(GameContext* context) { void SwampEvent::handle(GameContext* context) {
if (context->pacman.get_lives() > 0) if (context->pacman.get_lives() > 0)
context->pacman.set_lives(); context->pacman.set_lives();
if (context->pacman2.get_lives()) if (context->pacman2.get_lives() > 0)
context->pacman2.set_lives(); context->pacman2.set_lives();
} }
...@@ -106,6 +106,7 @@ void GameState::update() { ...@@ -106,6 +106,7 @@ void GameState::update() {
if (ptr_event) game_events.emplace_back(std::move(ptr_event)); if (ptr_event) game_events.emplace_back(std::move(ptr_event));
if (context.pacman2.get_location() != nullptr) { if (context.pacman2.get_location() != nullptr) {
context.pacman2.set_active();
IVisitor* ptr_visitor2 = &(context.pacman2); IVisitor* ptr_visitor2 = &(context.pacman2);
auto ptr_event2 = ptr_entity->accept(ptr_visitor2); auto ptr_event2 = ptr_entity->accept(ptr_visitor2);
if (ptr_event2) game_events.emplace_back(std::move(ptr_event2)); if (ptr_event2) game_events.emplace_back(std::move(ptr_event2));
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment