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

started pattern Observed

1 merge request!9added pattern Observer
Showing with 63 additions and 16 deletions
+63 -16
assets/pacman.png

1.1 KB

assets/pacmanDown.png

2.21 KB

assets/pacmanLeft.png

2.19 KB | W: | H:

assets/pacmanLeft.png

2.24 KB | W: | H:

assets/pacmanLeft.png
assets/pacmanLeft.png
assets/pacmanLeft.png
assets/pacmanLeft.png
  • 2-up
  • Swipe
  • Onion skin
assets/pacmanRight.png

2.17 KB | W: | H:

assets/pacmanRight.png

1.87 KB | W: | H:

assets/pacmanRight.png
assets/pacmanRight.png
assets/pacmanRight.png
assets/pacmanRight.png
  • 2-up
  • Swipe
  • Onion skin
assets/pacmanUp.png

2.18 KB

......@@ -22,6 +22,9 @@ namespace config {
const char BUTTON_TEXT_EXIT[] = "Exit";
// Игра:
const sf::VideoMode GAME_VIDEO_MODE{ 1138, 620 };
const float MENU_WIDTH = GAME_VIDEO_MODE.width / 4;
const float SCREEN_HEIGHT = GAME_VIDEO_MODE.height;
const float GAME_WIDTH = GAME_VIDEO_MODE.width - MENU_WIDTH;
const char BUTTON_START_GAME[] = "Start Game";
const char EASY_GAME_TITLE[] = "Level: Easy";
const char ONE_PLAYER_GAME_TITLE[] = "Player: One";
......
......@@ -63,14 +63,14 @@ std::unique_ptr<GameState> CommonBuilder::get_game() {
void SimpleBuilder::create_rooms() {
size_t rows = m_height / m_room_size;
size_t cols = m_width / m_room_size;
size_t cols = config::GAME_WIDTH / m_room_size;
int room_size = static_cast<int>(m_room_size);
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};
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) {
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({col + left_pos.x, row + left_pos.y});
room->set_position({config::MENU_WIDTH + col, row + left_pos.y});
row_vec.emplace_back(room);
}
m_rooms.emplace_back(std::move(row_vec));
......@@ -79,7 +79,7 @@ void SimpleBuilder::create_rooms() {
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++) {
for (size_t j = 0; j < m_rooms[i].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));
......@@ -113,10 +113,10 @@ void SimpleBuilder::set_rooms_sides() {
void ComplexBuilder::create_rooms() {
size_t room_quantity_row = (m_height / m_room_size);
size_t room_quantity_col = (m_width / m_room_size);
size_t room_quantity_col = config::GAME_WIDTH / m_room_size;
int room_size = static_cast<int>(m_room_size);
auto starting_point = sf::Vector2f{(m_width - m_room_size*room_quantity_col)/2 + m_room_size/2, (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) {
std::vector<Room*> vector_row;
......
......@@ -9,6 +9,7 @@ struct GameContext {
enum State { INGAME, WIN, LOST } state = INGAME;
std::list<std::unique_ptr<IDynamicEntity>> dynamic_objects;
std::list<std::unique_ptr<IStaticEntity>> static_objects;
Pacman pacman;
Pacman pacman2;
GameContext clone();
......
......@@ -9,6 +9,7 @@ void DeleteStaticEntity::handle(GameContext* context) {
auto it = std::find_if(context->static_objects.begin(), context->static_objects.end(),
[this](const std::unique_ptr<IStaticEntity>& ptr) { return ptr.get() == m_ptr_entity; });
context->static_objects.erase(it);
notify();
}
DeleteStaticEntity::DeleteStaticEntity(IStaticEntity* ptr_entity) :
......@@ -17,4 +18,13 @@ DeleteStaticEntity::DeleteStaticEntity(IStaticEntity* ptr_entity) :
void WinGame::handle(GameContext* context) {
context->state = GameContext::WIN;
}
\ No newline at end of file
}
void IGameEvent::add_observer(IObserver *observer) {
m_observers.push_back(observer);
}
void IGameEvent::notify() const {
for (auto observer : m_observers)
observer->on_notify(*this);
}
......@@ -2,6 +2,7 @@
#include <iostream>
#include <list>
#include "../Drawable/Maze/IObserver.h"
class GameContext;
class IStaticEntity;
......@@ -10,6 +11,11 @@ class IGameEvent {
public:
virtual void handle(GameContext* context) = 0;
virtual ~IGameEvent() = default;
void add_observer(IObserver* observer);
protected:
void notify() const;
private:
std::vector<IObserver*> m_observers;
};
class WinGame : public IGameEvent {
......@@ -26,6 +32,8 @@ class DeleteStaticEntity : public IGameEvent {
public:
DeleteStaticEntity(IStaticEntity* ptr_entity);
void handle(GameContext* context) override;
int get_player_id() const {return m_player_id; }
private:
IStaticEntity* m_ptr_entity;
int m_player_id;
};
#include "GameState.h"
#include "SelectState.h"
#include "../../../config.h"
#include "../Drawable/Entity/IEntity.h"
#include "ThemeManager.h"
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){ }
IState(state_manager), IWindowKeeper(config::GAME_VIDEO_MODE, window_title),
m_menu(),
m_score_count(std::make_unique<ScoreCount>()){ }
void GameState::event_handling() {
sf::Event event{};
......@@ -72,21 +72,30 @@ void GameState::update() {
for (auto& static_ptr : context.static_objects) {
IVisitor* ptr_visitor = &(context.pacman);
auto ptr_event = static_ptr->accept(ptr_visitor);
if (ptr_event) game_events.emplace_back(std::move(ptr_event));
if (auto ptr_event = static_ptr->accept(ptr_visitor)) {
ptr_event->add_observer(m_score_count.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 (ptr_event2) game_events.emplace_back(std::move(ptr_event2));
if (auto ptr_event2 = static_ptr->accept(ptr_visitor)) {
ptr_event2->add_observer(m_score_count.get());
game_events.emplace_back(std::move(ptr_event2));
}
}
}
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 << "выиграл первый";
else std::cout << "выиграл второй";
}
for (int i = 0; i < game_events.size(); i++)
game_events[i]->handle(&m_context_manager.get_current_context());
if (context.static_objects.empty()) {
game_events.emplace_back(std::move(std::make_unique<WinGame>()));
}
game_events.clear();
}
......@@ -100,11 +109,15 @@ void GameState::render() {
case GameContext::LOST:
backgroundColor = ThemeManager::Instance().getGameBackgroundLostColor();
break;
default:
case GameContext::WIN:
backgroundColor = ThemeManager::Instance().getGameBackgroundWinColor();
break;
default:
break;
}
m_window.clear(backgroundColor);
m_menu.draw_into(m_window, *m_score_count);
m_maze.draw_into(m_window);
context.pacman.draw_into(m_window);
if (context.pacman2.get_location() != nullptr)
......
#pragma once
#include "IState.h"
#include "../Drawable/Maze/Maze.h"
#include "../Drawable/Maze/MenuGame.h"
#include "../Context/Context.h"
#include "../Event/IGameEvent.h"
......@@ -14,8 +15,11 @@ 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; }
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;
};
......@@ -54,6 +54,7 @@ void ThemeManager::loadThemeColors() {
if (m_theme == Theme::LIGHT) {
m_currentColors = {
sf::Color::White, // button_text
sf::Color::Black,
sf::Color(21, 117, 217), // button_fill
sf::Color(0, 0, 0), // button_selection
sf::Color::White, // button_frame
......@@ -74,6 +75,7 @@ void ThemeManager::loadThemeColors() {
} else if (m_theme == Theme::DARK) {
m_currentColors = {
sf::Color::White, // button_text
sf::Color::Black,
sf::Color(21, 117, 217), // button_fill
sf::Color(80, 80, 80), // button_selection
sf::Color::White, // button_frame
......@@ -94,3 +96,7 @@ void ThemeManager::loadThemeColors() {
}
}
sf::Color ThemeManager::getButtonScoreColor() const {
return m_currentColors.button_text_score;
}
......@@ -3,6 +3,7 @@
struct ThemeColors {
sf::Color button_text;
sf::Color button_text_score;
sf::Color button_fill;
sf::Color button_selection;
sf::Color button_frame;
......@@ -35,6 +36,7 @@ public:
sf::Color getButtonFillColor() const;
sf::Color getButtonOutlineColor() const;
sf::Color getButtonTextColor() const;
sf::Color getButtonScoreColor() const;
sf::Color getButtonSelectionColor() const;
sf::Color getTitleTextColor() const;
......
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