diff --git a/builder/builderdirector.h b/builder/builderdirector.h new file mode 100644 index 0000000000000000000000000000000000000000..10f4fbd06f8cd446061fcadd321851904b7bf4b3 --- /dev/null +++ b/builder/builderdirector.h @@ -0,0 +1,15 @@ +#pragma once +#include "gamebuilder.h" +#include<string> +#include<SFML/Graphics.hpp> + +class GameBuilderDirector { +public: + GameBuilderDirector(IGameBuilder* ptr_builder, sf::VideoMode videomode, + std::string window_title, float dynamic_objects_ratio): + m_ptr_builder(ptr_builder), m_window_title(window_title), m_mode(videomode) {} +private: + IGameBuilder* m_ptr_builder; + std::string m_window_title; + sf::VideoMode m_mode; +}; \ No newline at end of file diff --git a/builder/builders.h b/builder/builders.h new file mode 100644 index 0000000000000000000000000000000000000000..fd4443b092694614932bd7076b85e32cd01c8798 --- /dev/null +++ b/builder/builders.h @@ -0,0 +1,28 @@ +#pragma once +#include <vector> +#include "gamebuilder.h" +#include "../context/gamecontext.h" + +class CommonBuilder: public IGameBuilder { +public: + virtual void create_context(float dynamic_objects_ratio) override {} + virtual void create_state(IStateManager& state_manager, sf::VideoMode videomode, const std::string window_title) override{} + virtual void set_all_to_state() override {} + virtual GameState* get_game() override {} +private: + float m_width; + float m_height; + float m_room_size; + std::vector <std::vector<Room*>> m_rooms; + GameContext m_context; + GameState* m_game_state; +}; + +class SimpleBuilder : CommonBuilder { + virtual void create_rooms() override {} + virtual void set_rooms_sides() override {} +}; +class ComplexBuilder : CommonBuilder { + virtual void create_rooms() override {} + virtual void set_rooms_sides() override {} +}; \ No newline at end of file diff --git a/builder/gamebuilder.h b/builder/gamebuilder.h new file mode 100644 index 0000000000000000000000000000000000000000..1d1df7562a2cc101489689e446d6ccc94c01e06c --- /dev/null +++ b/builder/gamebuilder.h @@ -0,0 +1,12 @@ +#pragma once +#include"../states/states.h" +#include"../states/istatemanager.h" + +struct IGameBuilder { + virtual void create_rooms() = 0; + virtual void set_rooms_sides() = 0; + virtual void create_context(float dynamic_objects_ratio) = 0; + virtual void create_state(IStateManager& state_manager, sf::VideoMode videomode, const std::string window_title) = 0; + virtual void set_all_to_state() = 0; + virtual GameState* get_game() = 0; +}; \ No newline at end of file diff --git a/context/contextmanager.cpp b/context/contextmanager.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b498f2eeccde52dd82a704466774e2a0d33eb40f --- /dev/null +++ b/context/contextmanager.cpp @@ -0,0 +1,8 @@ +#include "contextmanager.h" + +void ContextManager::restore_previous_context() { + if (m_contexts.size() > 1) + m_contexts.pop(); + /*else + m_contexts.top() = m_initial_context.clone();*/ +} \ No newline at end of file diff --git a/context/contextmanager.h b/context/contextmanager.h new file mode 100644 index 0000000000000000000000000000000000000000..3d264b83c3f3f36e9cf16581c93870f9c322e1ee --- /dev/null +++ b/context/contextmanager.h @@ -0,0 +1,14 @@ +#pragma once +#include "gamecontext.h" +#include <stack> + +class ContextManager { +public: + void reset (const GameContext&& context) {} + GameContext& get_current_context () {} + void save_current_context() {} + void restore_previous_context(); +private: + GameContext m_initial_context; + std::stack<GameContext> m_contexts; +}; \ No newline at end of file diff --git a/context/gamecontext.h b/context/gamecontext.h new file mode 100644 index 0000000000000000000000000000000000000000..c38bdbb1a47ad53e16cdf6e40e522459eb873256 --- /dev/null +++ b/context/gamecontext.h @@ -0,0 +1,15 @@ +#pragma once +#include "../entity/pacman.h" +#include "../entity/staticentity.h" +#include "../entity/dynamicentity.h" + +class IGameEvent; + +struct GameContext { + Pacman pacman; + std::vector<std::unique_ptr<IStaticEntity>> static_objects; + std::vector<std::unique_ptr<IDynamicEntity>> dynamic_objects; + enum State { INGAME, WIN, LOST } state = INGAME; + + GameContext clone() { } +}; \ No newline at end of file diff --git a/drawable/maze.cpp b/drawable/maze.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7997397f19bfeb1abb4205017e2be9beeb31cfd4 --- /dev/null +++ b/drawable/maze.cpp @@ -0,0 +1,13 @@ +#include "maze.h" + +void Maze::draw_into(sf::RenderWindow& window) const +{ + for (const Room& ptr_room : m_rooms) + ptr_room.draw_into(window); +} + +void Room::draw_into(sf::RenderWindow& window) const +{ + for (const IRoomSide* ptr_side : m_sides) + ptr_side->draw_into(window); +} \ No newline at end of file diff --git a/drawable/maze.h b/drawable/maze.h new file mode 100644 index 0000000000000000000000000000000000000000..1129334acd89d87762c6da180584abdf873069da --- /dev/null +++ b/drawable/maze.h @@ -0,0 +1,58 @@ +#pragma once +#include "drawable.h" +#include "../entity/ientity.h" +#include<array> +#include<vector> + + +class IRoomSide : public IPreparable { +public: + virtual void enter(IEntity* entity) = 0; + virtual ~IRoomSide() = 0 {} + +}; + +class Room :public IDrawable { +public: + enum Direction { INVALID = -1, LEFT, RIGHT, UP, DOWN }; + + Room(float size) {} + float get_size() {} + void set_position(sf::Vector2f pos) {} + sf::Vector2f get_position() {} + void set_side(Direction side, std::unique_ptr<IRoomSide> ptr_side) {} + IRoomSide* get_side(Direction side) { return m_sides[0]; } + Direction get_direction(std::unique_ptr<IRoomSide> ptr_side) {} + + void draw_into(sf::RenderWindow& window) const override; + + std::array<IRoomSide*, 4> m_sides; + +private: + sf::RectangleShape m_rectangle; + +}; + +class Maze : public IDrawable { +public: + Maze(std::vector<std::unique_ptr<Room>> rooms) {} + void draw_into(sf::RenderWindow& window) const override; + +private: + std::vector<Room> m_rooms; +}; + +class Pass : public IRoomSide { +public: + Pass(Room& room1, Room& room2) : m_room1(room1), m_room2(room2) {} + virtual void prepare_for_drawing() override {} + virtual void draw_into(sf::RenderWindow& window) const override {} +private: + Room& m_room1; + Room& m_room2; +}; + +class Wall : public IRoomSide { + + +}; \ No newline at end of file diff --git a/drawable/menu.cpp b/drawable/menu.cpp new file mode 100644 index 0000000000000000000000000000000000000000..64179808fc8b6e8bdddfcc97f335b8723376af04 --- /dev/null +++ b/drawable/menu.cpp @@ -0,0 +1,7 @@ +#include "menu.h" + + +void Menu::draw_into(sf::RenderWindow& window) const { + for (const Button& ptr_button : m_buttons) + ptr_button.draw_into(window); +} \ No newline at end of file diff --git a/entity/dynamicentity.cpp b/entity/dynamicentity.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1cf01b169590b97abdb7cf8656e8fdca111a7012 --- /dev/null +++ b/entity/dynamicentity.cpp @@ -0,0 +1,28 @@ +#include "dynamicentity.h" +#include "../drawable/maze.h" + +std::unique_ptr<IDynamicEntity> Enemy::clone() const { + return std::make_unique<Enemy>(*this); +} + +void Enemy::action() { + auto miliseconds = static_cast<size_t>(m_stopwatch.getElapsedTime().asMilliseconds()); + if (miliseconds < rand() % 10000) + return; + auto direction = static_cast<Room::Direction>(rand() % 4); + m_ptr_room->get_side(direction)->enter(this); + m_stopwatch.restart(); +} + +void Enemy::prepare_for_drawing() +{ + m_rectangle.setPosition(m_ptr_room->get_position()); +} + +void Enemy::draw_into(sf::RenderWindow& window) const +{ + window.draw(m_rectangle); +} + + + diff --git a/entity/dynamicentity.h b/entity/dynamicentity.h new file mode 100644 index 0000000000000000000000000000000000000000..5583f0f2bbe14b4a2a3054a5f84e9389f0f8a1e7 --- /dev/null +++ b/entity/dynamicentity.h @@ -0,0 +1,22 @@ +#pragma once +#include "ientity.h" + + +class IDynamicEntity : public IEntity, public IVisitable { +public: + virtual std::unique_ptr<IDynamicEntity> clone() const = 0; + void virtual action() = 0; + virtual ~IDynamicEntity() = 0 {} +}; + +class Enemy : public IDynamicEntity { + Enemy(); + void action() override; + void prepare_for_drawing() override; + void draw_into(sf::RenderWindow& window) const override; + virtual std::unique_ptr<IDynamicEntity> clone() const override {return std::make_unique<Enemy>(*this); } + private: + sf::RectangleShape m_rectangle; + sf::Clock m_stopwatch; +}; + diff --git a/entity/ientity.h b/entity/ientity.h new file mode 100644 index 0000000000000000000000000000000000000000..0a930ab92fa4f5c347f9ffb706ec032d58f7e4e0 --- /dev/null +++ b/entity/ientity.h @@ -0,0 +1,20 @@ +#pragma once +#include "../drawable/drawable.h" + +class Room; + +class IEntity : public IPreparable { +public: + void set_location(std::unique_ptr<Room> ptr_room) {} + Room* get_location() { + } +protected: + Room* m_ptr_room; +}; + +class IVisitor; + +class IVisitable { +public: + virtual std::unique_ptr<IGameEvent> accept(IVisitor* ptr_visitor) = 0; +}; diff --git a/entity/pacman.cpp b/entity/pacman.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6b4a13cc62469cbbf002ec83c9b93c9598cd8983 --- /dev/null +++ b/entity/pacman.cpp @@ -0,0 +1,5 @@ +#include "pacman.h" + +void Pacman::move(Room::Direction direction) { + m_ptr_room->get_side(direction)->enter(this); +} diff --git a/entity/pacman.h b/entity/pacman.h new file mode 100644 index 0000000000000000000000000000000000000000..0fd020cc9483bbdc806a16cd4d9b620e3d99d03f --- /dev/null +++ b/entity/pacman.h @@ -0,0 +1,20 @@ +#pragma once +#include "ientity.h" +#include <memory> +#include "../drawable/maze.h" + +class IVisitor { +public: + virtual std::unique_ptr<IGameEvent> visit(Food* ptr_food) = 0; + virtual std::unique_ptr<IGameEvent> visit(Enemy* ptr_enemy) = 0; + +}; + +class Pacman : public IEntity, public IVisitor { + Pacman() {} + virtual void prepare_for_drawing() override {} + virtual void draw_into(sf::RenderWindow& window) const override {} + void move(Room::Direction direction); + virtual std::unique_ptr<IGameEvent> visit(Food* ptr_food) override {} + virtual std::unique_ptr<IGameEvent> visit(Enemy* ptr_enemy) override {} +}; \ No newline at end of file diff --git a/entity/staticentity.h b/entity/staticentity.h new file mode 100644 index 0000000000000000000000000000000000000000..a32a3b1348ed28a02f074d9ddcc8fd0301004c18 --- /dev/null +++ b/entity/staticentity.h @@ -0,0 +1,13 @@ +#pragma once +#include "ientity.h" + +class IStaticEntity : public IEntity, public IVisitable { +public: + virtual std::unique_ptr<IStaticEntity> clone() const = 0; + virtual ~IStaticEntity() = 0 {} +}; + + +class Food : public IStaticEntity { + +}; \ No newline at end of file diff --git a/events/gameevent.h b/events/gameevent.h new file mode 100644 index 0000000000000000000000000000000000000000..dce047aec39fbe869b1148f6803a43cab1344fa9 --- /dev/null +++ b/events/gameevent.h @@ -0,0 +1,19 @@ +#pragma once +#include "../context/gamecontext.h" +#include "../entity/staticentity.h" + +class IGameEvent { + virtual void handle(GameContext& context) const = 0; + +}; + +class DeleteStaticEntity : public IGameEvent { +public: + DeleteStaticEntity(IStaticEntity* ptr_entity): m_ptr_entity(ptr_entity) {} +private: + std::unique_ptr<IStaticEntity> m_ptr_entity; +}; + +class LostGame: public IGameEvent { + +};