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

added pattern - observer

1 merge request!9added pattern Observer
Showing with 31 additions and 32 deletions
+31 -32
......@@ -4,9 +4,9 @@
void ScoreCount::on_notify(const IGameEvent& event) {
if (const auto* food_event = dynamic_cast<const DeleteStaticEntity*>(&event)) {
if (food_event->get_player_id() == 1)
if (m_player == 1)
++m_player_one_food;
else if (food_event->get_player_id() == 2)
else
++m_player_two_food;
}
}
}
\ No newline at end of file
......@@ -11,11 +11,13 @@ public:
class ScoreCount : public IObserver {
public:
ScoreCount(int player) : m_player(player) {}
void on_notify(const IGameEvent& event) override;
int get_player_one() const {return m_player_one_food; }
int get_player_two() const {return m_player_two_food; }
private:
int m_player;
int m_player_one_food{0};
int m_player_two_food{0};
};
\ No newline at end of file
......@@ -2,24 +2,24 @@
#include "../../../../config.h"
#include "../../State/ThemeManager.h"
#include "../../State/GameState.h"
#include "../DrawMenu/Font/Font.h"
MenuGame::MenuGame() {
m_title.setString(config::SELECT_LEVEL_TITLE_WINDOW);
m_title.setCharacterSize(30);
}
void MenuGame::draw_into(sf::RenderWindow &window, const ScoreCount& scores_one, const ScoreCount& scores_two) {
sf::Text m_title(config::SELECT_LEVEL_TITLE_WINDOW, MyFont::Instance(), 40);
m_title.setFillColor(ThemeManager::Instance().getButtonScoreColor());
m_title.setPosition(10.f, 55.f);
m_player_one_score.setFillColor(ThemeManager::Instance().getButtonScoreColor());
m_player_one_score.setPosition(10.f, 95.f);
m_player_two_score.setFillColor(ThemeManager::Instance().getButtonScoreColor());
m_player_two_score.setPosition(10.f, 125.f);
}
window.draw(m_title);
sf::Text player_one("Player 1: " + std::to_string(scores_one.get_player_one()), MyFont::Instance(), 30);
player_one.setFillColor(ThemeManager::Instance().getButtonScoreColor());
player_one.setPosition(10.f, 95.f);
window.draw(player_one);
void MenuGame::draw_into(sf::RenderWindow &window, const ScoreCount& scores) {
m_player_one_score.setString("Player 1: " + std::to_string(scores.get_player_one()));
m_player_two_score.setString("Player 2: " + std::to_string(scores.get_player_two()));
window.draw(m_button);
window.draw(m_title);
window.draw(m_player_one_score);
window.draw(m_player_two_score);
sf::Text player_two("Player 2: " + std::to_string(scores_two.get_player_two()), MyFont::Instance(), 30);
player_two.setFillColor(ThemeManager::Instance().getButtonScoreColor());
player_two.setPosition(10.f, 125.f);
window.draw(player_two);
}
\ No newline at end of file
......@@ -5,12 +5,12 @@
class MenuGame {
public:
MenuGame();
void draw_into(sf::RenderWindow &window, const ScoreCount& scores);
void draw_into(sf::RenderWindow &window, const ScoreCount& scores_one, const ScoreCount& scores_two);
private:
sf::RenderWindow m_window;
sf::RectangleShape m_background;
sf::RectangleShape m_button;
sf::Text m_title;
sf::Text m_player_one_score;
sf::Text m_player_two_score;
};
\ No newline at end of file
......@@ -4,8 +4,7 @@
GameState::GameState(IStateManager &state_manager, const sf::VideoMode &video_mode, const std::string &window_title) :
IState(state_manager), IWindowKeeper(config::GAME_VIDEO_MODE, window_title),
m_menu(),
m_score_count(std::make_unique<ScoreCount>()){ }
m_menu(), m_score_count_one(std::make_unique<ScoreCount>(1)), m_score_count_two(std::make_unique<ScoreCount>(2)){ }
void GameState::event_handling() {
sf::Event event{};
......@@ -71,17 +70,15 @@ void GameState::update() {
}
for (auto& static_ptr : context.static_objects) {
IVisitor* ptr_visitor = &(context.pacman);
auto ptr_event = static_ptr->accept(ptr_visitor);
if (auto ptr_event = static_ptr->accept(ptr_visitor)) {
ptr_event->add_observer(m_score_count.get());
ptr_event->add_observer(m_score_count_one.get());
game_events.emplace_back(std::move(ptr_event));
}
if (context.pacman2.get_location() != nullptr) {
IVisitor* ptr_visitor2 = &(context.pacman2);
auto ptr_event2 = static_ptr->accept(ptr_visitor2);
if (auto ptr_event2 = static_ptr->accept(ptr_visitor)) {
ptr_event2->add_observer(m_score_count.get());
if (auto ptr_event2 = static_ptr->accept(ptr_visitor2)) {
ptr_event2->add_observer(m_score_count_two.get());
game_events.emplace_back(std::move(ptr_event2));
}
}
......@@ -89,7 +86,7 @@ void GameState::update() {
if (context.static_objects.empty()) {
game_events.emplace_back(std::make_unique<WinGame>());
if (m_score_count->get_player_one() > m_score_count->get_player_two()) std::cout << "выиграл первый";
if (m_score_count_one->get_player_one() > m_score_count_two->get_player_two()) std::cout << "выиграл первый";
else std::cout << "выиграл второй";
}
......@@ -117,7 +114,7 @@ void GameState::render() {
}
m_window.clear(backgroundColor);
m_menu.draw_into(m_window, *m_score_count);
m_menu.draw_into(m_window, *m_score_count_one, *m_score_count_two);
m_maze.draw_into(m_window);
context.pacman.draw_into(m_window);
if (context.pacman2.get_location() != nullptr)
......
......@@ -15,11 +15,12 @@ public:
void set_maze(Maze&& maze) {m_maze = std::move(maze); }
void set_context(GameContext&& context);
bool process_key_pressed(sf::Keyboard::Key code);
const ScoreCount& get_score_count() const {return *m_score_count; }
const ScoreCount& get_score_count() const {return *m_score_count_one, *m_score_count_two; }
private:
Maze m_maze;
MenuGame m_menu;
ContextManager m_context_manager;
std::vector<std::unique_ptr<IGameEvent>> m_events;
std::unique_ptr<ScoreCount> m_score_count;
std::unique_ptr<ScoreCount> m_score_count_one;
std::unique_ptr<ScoreCount> m_score_count_two;
};
......@@ -27,6 +27,7 @@ void SelectState::update() {
}
m_menu.process_mouse(pos, false);
}
void SelectState::render() {
update_theme();
m_window.draw(m_backgorund_sprite);
......
......@@ -26,7 +26,6 @@ struct ThemeColors {
class ThemeManager {
public:
enum class Theme { LIGHT, DARK };
ThemeManager(const ThemeManager&) = delete;
ThemeManager& operator=(const ThemeManager&) = delete;
static ThemeManager& Instance();
......@@ -47,7 +46,6 @@ public:
sf::Color getRoomColor() const;
sf::Color getWallColor() const;
private:
Theme m_theme = Theme::LIGHT;
ThemeColors m_currentColors;
......
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