From f86ffafaf1af6fae33b0bf4a299df598b0da9b71 Mon Sep 17 00:00:00 2001 From: Sulimov Igor Andreevich <igansulimov@edu.hse.ru> Date: Tue, 25 Mar 2025 18:29:22 +0300 Subject: [PATCH] Made GameState::render(); and partly GameState::event_handling(); --- include/Config.h | 6 ++-- include/States/GameState.h | 6 ++-- source/States/GameState.cpp | 61 +++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 6 deletions(-) diff --git a/include/Config.h b/include/Config.h index 4e718ec..359ee23 100644 --- a/include/Config.h +++ b/include/Config.h @@ -38,9 +38,9 @@ namespace config { const sf::Color BUTTON_COLOR_SELECTION{ 255, 180, 180 }; const sf::Color BUTTON_COLOR_FRAME{ 0, 0, 0 }; const sf::Color SELECT_LEVEL_BACKGROUND_COLOR{ 230,230,230 }; -// const sf::Color GAME_COLOR_BACKGROUND_INGAME{ 230,230,230 }; -// 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_BACKGROUND_INGAME{ 230,230,230 }; + 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 }; diff --git a/include/States/GameState.h b/include/States/GameState.h index 4837e87..28b9766 100644 --- a/include/States/GameState.h +++ b/include/States/GameState.h @@ -9,11 +9,11 @@ class GameState: public IState, public IWindowKeeper { public: void set_maze(Maze&& maze); void set_context(GameContext&& context); - bool do_step() override {}; ///@todo + bool do_step() override; protected: - void event_handling() override {}; ///@todo + void event_handling() override; ///@todo void update() override {}; ///@todo - void render() override {}; ///@todo + void render() override; ///@todo public: GameState(IStateManager& state_manager, const sf::VideoMode& video_mode, const std::string& window_title); diff --git a/source/States/GameState.cpp b/source/States/GameState.cpp index 89f3b14..eb03833 100644 --- a/source/States/GameState.cpp +++ b/source/States/GameState.cpp @@ -1,4 +1,5 @@ #include "States/GameState.h" +#include "States/SelectState.h" GameState::GameState(IStateManager& state_manager, const sf::VideoMode& video_mode, const std::string& window_title): IWindowKeeper(video_mode, window_title), IState(state_manager) {} @@ -9,4 +10,64 @@ void GameState::set_maze(Maze&& maze) { void GameState::set_context(GameContext&& context) { m_context_manager.reset(std::move(context)); +} + +bool GameState::do_step() { + while (m_window.isOpen()) { + event_handling(); + update(); + render(); + return true; + } +} + +void GameState::event_handling() { + 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::SELECT_LEVEL_VIDEO_MODE, config::SELECT_LEVEL_TITLE)); + } + if (event.type == sf::Event::Resized) { + sf::View view = m_window.getView(); + view.setSize(event.size.width, event.size.height); + m_window.setView(view); + } +// if (event.type == sf::Event::KeyPressed && m_context_manager.get_current_context().state == GameContext::INGAME) { +// process_key_pressed(event.key.code); +// break; +// } +// if (sf::Keyboard::isKeyPressed(sf::Keyboard::LControl) && +// sf::Keyboard::isKeyPressed(sf::Keyboard::Z)) { +// auto cur_state = m_context_manager.get_current_context().state; +// m_context_manager.restore_previous_context(); +// if (cur_state) { +// m_state_sounds.first.stop(); +// m_state_sounds.second.stop(); +// m_ptr_music_player->play_music(); +// } +// break; +// } + } +} + +void GameState::render() { + if (m_context_manager.get_current_context().state == GameContext::INGAME) { + m_window.clear(config::GAME_COLOR_BACKGROUND_INGAME); + } + else if (m_context_manager.get_current_context().state == GameContext::WIN) { + m_window.clear(config::GAME_COLOR_BACKGROUND_WIN); + } + else if (m_context_manager.get_current_context().state == GameContext::LOST) { + m_window.clear(config::GAME_COLOR_BACKGROUND_LOST); + } + m_maze.draw_into(m_window); + m_context_manager.get_current_context().pacman.draw_into(m_window); + for (auto& obj : m_context_manager.get_current_context().static_objects) { + obj->draw_into(m_window); + } + for (auto& obj : m_context_manager.get_current_context().dynamic_objects) { + obj->draw_into(m_window); + } + m_window.display(); } \ No newline at end of file -- GitLab