Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
e2420a5
add TagFilter and tags to Entities
Mirong1707 Apr 21, 2022
7826ef0
add Mob class
Mirong1707 Apr 21, 2022
be3522a
fix TestCast.cpp
Mirong1707 Apr 21, 2022
557ef11
add RadiusFilter
Mirong1707 Apr 21, 2022
098cdf3
fix idea bug
Mirong1707 Apr 21, 2022
46d7778
add controller
Mirong1707 Apr 25, 2022
6631cb9
add m_storage field to controller
Mirong1707 Apr 25, 2022
e906149
set sandbox executable
Mirong1707 Apr 25, 2022
2c2134f
add attack to hero and enemy. Press Q
Mirong1707 Apr 25, 2022
55da899
fix glebs comments
Mirong1707 Apr 28, 2022
3e214d5
fix glebs comments
Mirong1707 Apr 28, 2022
6316e7a
fix glebs comments
Mirong1707 May 2, 2022
d72f49d
add TagFilter and tags to Entities
Mirong1707 Apr 21, 2022
51364d5
add Mob class
Mirong1707 Apr 21, 2022
90e5fd5
fix TestCast.cpp
Mirong1707 Apr 21, 2022
6798250
add RadiusFilter
Mirong1707 Apr 21, 2022
b62efca
fix idea bug
Mirong1707 Apr 21, 2022
d6dc251
fix glebs comments
Mirong1707 May 2, 2022
ef59a39
Merge branch 'main' into 82-create-mobs
Mirong1707 May 2, 2022
165cb34
fix glebs comments
Mirong1707 May 2, 2022
4caa4a0
add m_storage field to controller
Mirong1707 Apr 25, 2022
ccda133
set sandbox executable
Mirong1707 Apr 25, 2022
2ff3b1e
add attack to hero and enemy. Press Q
Mirong1707 Apr 25, 2022
3c52927
fix glebs comments
Mirong1707 Apr 28, 2022
46786d8
fix glebs comments
Mirong1707 Apr 28, 2022
9ab756e
fix glebs comments
Mirong1707 May 2, 2022
81d0faf
fix glebs comments
Mirong1707 May 2, 2022
d861bbb
fix glebs comments
Mirong1707 May 2, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ add_subdirectory(${DIRECTORY_GAME})
message(STATUS "Game executable: ${EXECUTABLE_GAME}")
message(STATUS "Game-runner executable ${EXECUTABLE_GAME_RUNNER}")


# Tests
set(DIRECTORY_TEST ${PROJECT_SOURCE_DIR}/test)
set(EXECUTABLE_TEST test)
Expand Down
22 changes: 17 additions & 5 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ set(
event/management/producer/SystemListener.cpp event/management/producer/SystemListener.hpp
event/management/producer/EventProducer.hpp
event/management/dispatcher/DelayedDispatcher.cpp event/management/dispatcher/DelayedDispatcher.hpp
event/physics/Collision.hpp
event/physics/Collision.cpp
event/management/handler/controller/Controller.hpp
event/management/handler/controller/Controller.cpp
../game/mobs/MobController.hpp
../game/mobs/MobController.cpp
event/menu/MenuEvent.hpp event/menu/MenuEvent.cpp
event/menu/MainMenuEvent.hpp event/menu/MainMenuEvent.cpp
event/menu/PauseMenuEvent.hpp event/menu/PauseMenuEvent.cpp
Expand All @@ -35,30 +41,36 @@ set(

event/management/handler/CollisionHandler.hpp event/management/handler/CollisionHandler.cpp
event/physics/Collision.hpp event/physics/Collision.cpp
event/management/handler/controller/Controller.hpp
event/management/handler/controller/Controller.cpp
../game/mobs/MobController.hpp
../game/mobs/MobController.cpp

world/World.cpp world/World.hpp
world/LocalWorld.cpp world/LocalWorld.hpp
world/filter/Filter.hpp
world/filter/TagFilter.hpp
world/filter/TrueFilter.hpp
world/filter/IdFilter.hpp
world/entity/EntityStorage.cpp world/entity/EntityStorage.hpp
world/entity/ViewableEntity.hpp world/entity/ViewableEntity.cpp
world/entity/Entity.hpp world/entity/Entity.cpp
world/entity/mobs/Mob.hpp
world/entity/mobs/Mob.cpp
world/intent/Intent.cpp world/intent/Intent.hpp
world/intent/LambdaIntent.cpp world/intent/LambdaIntent.hpp
world/intent/Intent.cpp world/intent/Intent.hpp
world/entity/PhysicalEntity.hpp world/entity/PhysicalEntity.cpp
world/entity/ContactListener/ContactListener.hpp
world/entity/contactListener/ContactListener.hpp
world/filter/RadiusFilter.hpp
world/filter/TagRadiusFilter.hpp

menu/Menu.hpp menu/Menu.cpp
menu/MainMenu.hpp menu/MainMenu.cpp
menu/PauseMenu.hpp menu/PauseMenu.cpp

runner/Runner.hpp
runner/SequentialRunner.cpp runner/SequentialRunner.hpp
runner/LevelRunner.hpp runner/LevelRunner.cpp
runner/GameRunner.hpp runner/GameRunner.cpp
loader/LevelLoader.hpp

visual/image/Image.hpp
visual/image/static/RenderableStatic.cpp visual/image/static/RenderableStatic.hpp
Expand All @@ -83,7 +95,7 @@ add_library(
if (CMAKE_BUILD_TYPE MATCHES Debug)
target_compile_definitions(${LIBRARY_CORE} PUBLIC SPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_TRACE)
message(STATUS "Enable debug logs for ${LIBRARY_CORE}")
endif()
endif ()

target_include_directories(
${LIBRARY_CORE} PUBLIC
Expand Down
3 changes: 3 additions & 0 deletions core/common/FVec2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,7 @@ namespace mad::core {
Vec2d operator*(float k, Vec2d v) {
return v *= k;
}
float dist_sq(const Vec2d &v1, const Vec2d &v2) {
return (v1.get_x() - v2.get_x()) * (v1.get_x() - v2.get_x()) + (v1.get_y() - v2.get_y()) * (v1.get_y() - v2.get_y());
}
}
2 changes: 2 additions & 0 deletions core/common/FVec2D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ namespace mad::core {
Vec2d operator*(Vec2d v, float k);

Vec2d operator*(float k, Vec2d v);

float dist_sq(const Vec2d &v1, const Vec2d &v2);
}

#endif //MAD_CORE_COMMON_FVEC2D_HPP
21 changes: 21 additions & 0 deletions core/event/management/handler/controller/Controller.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "Controller.hpp"

#include <event/physics/Collision.hpp>
#include <event/Event.hpp>
#include <event/Event.hpp>
#include <event/physics/Collision.hpp>
#include <event/Event.hpp>
#include <world/entity/Entity.hpp>
#include <world/entity/PhysicalEntity.hpp>
#include <world/intent/LambdaIntent.hpp>

#include <common/Cast.hpp>

#include <spdlog/spdlog.h>

#include <utility>

mad::core::Controller::Controller(std::shared_ptr<EntityStorage> m_storage) : m_storage(std::move(m_storage)){}

void mad::core::Controller::handle(const mad::core::Event &event) {
}
21 changes: 21 additions & 0 deletions core/event/management/handler/controller/Controller.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef MAD_CONTROLLER_HPP
#define MAD_CONTROLLER_HPP

#include <world/entity/EntityStorage.hpp>
#include <event/management/handler/EventHandler.hpp>
namespace mad::core {

class Controller : public EventHandler {
public:
explicit Controller(std::shared_ptr<EntityStorage> m_storage);

void handle(const Event &event) override;

virtual void control() = 0;

protected:
std::shared_ptr<EntityStorage> m_storage;
};

}
#endif//MAD_CONTROLLER_HPP
35 changes: 23 additions & 12 deletions core/world/LocalWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
#include <world/filter/TrueFilter.hpp>
#include <common/Error.hpp>
#include <event/management/dispatcher/DelayedDispatcher.hpp>
#include <spdlog/spdlog.h>
#include <world/entity/Entity.hpp>
#include <world/filter/TrueFilter.hpp>
#include <world/intent/LambdaIntent.hpp>
#include <world/entity/Entity.hpp>
#include <world/entity/ContactListener/ContactListener.hpp>
#include <world/entity/contactListener/ContactListener.hpp>

#include <utility>
#include <vector>


Expand All @@ -18,15 +23,18 @@ mad::core::LocalWorld::LocalWorld(EventDispatcher &event_dispatcher, Vec2d gravi

m_contact_listener = std::make_shared<mad::core::MyContactListener>(event_dispatcher);
m_physical_world.SetContactListener(&*m_contact_listener);
m_storage = std::make_shared<mad::core::EntityStorage>();
m_controller = std::make_shared<MobController>(m_storage);
event_dispatcher.registry(m_controller);
}


bool mad::core::LocalWorld::manipulate(const mad::core::Filter &filter, const mad::core::Intent &intent) {
//CHECK_THROW(is_legal(validate_filter(f), IllegalManipulation, "Illegal filter");
//CHECK_THROW(is_legal(validate_intent(i), IllegalManipulation, "Illegal intent");

for (Entity::Id entity_id : m_storage.extract(filter)) {
m_storage.get_entity(entity_id).accept(*this, intent, *m_event_queue_dispatcher);
for (Entity::Id entity_id : m_storage->extract(filter)) {
m_storage->get_entity(entity_id).accept(*this, intent, *m_event_queue_dispatcher);
}

return true;
Expand All @@ -37,18 +45,18 @@ void mad::core::LocalWorld::produce(mad::core::EventDispatcher &dispatcher) {
// calculating fps + dt
sf::Time time = clock.getElapsedTime();
dt = time.asSeconds() - last_time;
sf::sleep(sf::seconds((1.0f/120) - dt));
sf::sleep(sf::seconds((1.0f / 120) - dt));
last_time = time.asSeconds();
float fact_dt = (1.0f/120);
float fact_dt = (1.0f / 120);

//double fps = 1 / (time.asSeconds() - l_old);
//SPDLOG_INFO("FPS {}", fps);

// simulating physics
m_physical_world.Step(fact_dt * render_scale, 3, 10);
for (Entity::Id entity_id : m_storage.extract(TrueFilter())) {
if (&m_storage.get_entity(entity_id) != nullptr && cast_to_or_null<PhysicalEntity>(m_storage.get_entity(entity_id)) != nullptr) {
auto physical_entity = cast_to_or_null<PhysicalEntity>(m_storage.get_entity(entity_id));
for (Entity::Id entity_id : m_storage->extract(TrueFilter())) {
if (cast_to_or_null<PhysicalEntity>(m_storage->get_entity(entity_id)) != nullptr) {
auto physical_entity = cast_to_or_null<PhysicalEntity>(m_storage->get_entity(entity_id));
physical_entity->synchronize_position_with_viewable();
}
}
Expand All @@ -74,12 +82,15 @@ void mad::core::LocalWorld::produce(mad::core::EventDispatcher &dispatcher) {
dispatcher.dispatch(m_step_events_queue->front());
m_step_events_queue->pop();
}

//control
m_controller->control();
}

mad::core::Entity::Id mad::core::LocalWorld::create_viewable_entity(int z_ind, mad::core::Vec2d initial_position, float initial_rotation,
mad::core::Entity::Id mad::core::LocalWorld::create_viewable_entity(std::unordered_set<std::string> tags, int z_ind, mad::core::Vec2d initial_position, float initial_rotation,
std::shared_ptr<Image> image) {
return m_storage.create_viewable_entity(z_ind, initial_position, initial_rotation, image);
return m_storage->create_viewable_entity(std::move(tags), z_ind, initial_position, initial_rotation, std::move(image));
}
mad::core::Entity::Id mad::core::LocalWorld::create_physical_entity(int z_ind, mad::core::Vec2d initial_position, float initial_rotation, std::shared_ptr<Image> image, bool is_Fixed) {
return m_storage.create_physical_entity(z_ind, initial_position, initial_rotation, image, m_physical_world, is_Fixed);
mad::core::Entity::Id mad::core::LocalWorld::create_physical_entity(std::unordered_set<std::string> tags, int z_ind, mad::core::Vec2d initial_position, float initial_rotation, std::shared_ptr<Image> image, bool is_Fixed) {
return m_storage->create_physical_entity(std::move(tags), z_ind, initial_position, initial_rotation, std::move(image), m_physical_world, is_Fixed);
}
27 changes: 14 additions & 13 deletions core/world/LocalWorld.hpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
#ifndef MAD_CORE_WORLD_LOCALWORLD_HPP
#define MAD_CORE_WORLD_LOCALWORLD_HPP

#include <world/World.hpp>
#include <world/entity/EntityStorage.hpp>
#include <event/management/dispatcher/ImmediateDispatcher.hpp>
#include <event/management/dispatcher/DelayedDispatcher.hpp>
#include <box2d/box2d.h>
#include <visual/image/shape/Shape.hpp>
#include <world/entity/ContactListener/ContactListener.hpp>
#include <game/mobs/MobController.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Graphics/CircleShape.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/System/Clock.hpp>
#include <SFML/Window/Event.hpp>

#include <box2d/box2d.h>
#include <event/management/dispatcher/DelayedDispatcher.hpp>
#include <event/management/dispatcher/ImmediateDispatcher.hpp>
#include <queue>
#include <visual/image/shape/Shape.hpp>
#include <world/World.hpp>
#include <world/entity/EntityStorage.hpp>
#include <world/entity/contactListener/ContactListener.hpp>


namespace mad::core {
Expand All @@ -28,23 +28,24 @@ namespace mad::core {

void produce(EventDispatcher &dispatcher) override;

Entity::Id create_viewable_entity(int z_ind, Vec2d initial_position, float initial_rotation, std::shared_ptr<Image> image) override;
Entity::Id create_viewable_entity(std::unordered_set<std::string> tags, int z_ind, Vec2d initial_position, float initial_rotation, std::shared_ptr<Image> image) override;

Entity::Id create_physical_entity(int z_ind, Vec2d initial_position, float initial_rotation, std::shared_ptr<Image> image, bool is_Fixed = false) override;
Entity::Id create_physical_entity(std::unordered_set<std::string> tags, int z_ind, Vec2d initial_position, float initial_rotation, std::shared_ptr<Image> image, bool is_Fixed = false) override;

private:
std::shared_ptr<std::queue<std::shared_ptr<Event>>> m_step_events_queue;
std::unique_ptr<DelayedDispatcher> m_event_queue_dispatcher;
EntityStorage m_storage;
std::shared_ptr<EntityStorage> m_storage;
b2World m_physical_world;
std::shared_ptr<MobController> m_controller;
float dt;
float render_scale = 3;
sf::Clock clock;
float last_time = 0;
std::shared_ptr<mad::core::MyContactListener> m_contact_listener;
};

}
}// namespace mad::core


#endif //MAD_CORE_WORLD_LOCALWORLD_HPP
#endif//MAD_CORE_WORLD_LOCALWORLD_HPP
8 changes: 4 additions & 4 deletions core/world/World.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
#define MAD_CORE_WORLD_WORLD_HPP


#include <event/management/producer/EventProducer.hpp>
#include <common/FVec2D.hpp>
#include <event/management/producer/EventProducer.hpp>
#include <world/entity/Entity.hpp>

#include <memory>
#include "visual/image/shape/Shape.hpp"
#include <memory>

namespace mad::core {
class Filter;
Expand All @@ -26,11 +26,11 @@ namespace mad::core {

bool manipulate_entity_id(Entity::Id entity_id, const Intent &intent);

virtual Entity::Id create_viewable_entity(int z_ind,
virtual Entity::Id create_viewable_entity(std::unordered_set<std::string> tags, int z_ind,
Vec2d initial_position, float initial_rotation,
std::shared_ptr<Image> image) = 0;

virtual Entity::Id create_physical_entity(int z_ind, Vec2d initial_position, float initial_rotation, std::shared_ptr<Image> image, bool is_Fixed) = 0;
virtual Entity::Id create_physical_entity(std::unordered_set<std::string> tags, int z_ind, Vec2d initial_position, float initial_rotation, std::shared_ptr<Image> image, bool is_Fixed) = 0;
};

}// namespace mad::core
Expand Down
2 changes: 2 additions & 0 deletions core/world/entity/Entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

#include <event/visual/PositionalAppearance.hpp>
#include <event/management/dispatcher/EventDispatcher.hpp>
#include <utility>


namespace mad::core {

mad::core::Entity::Id mad::core::Entity::get_id() const noexcept {
return m_id;
}
Entity::Entity(std::unordered_set<std::string> tags) : tags(std::move(tags)) {}

}
6 changes: 5 additions & 1 deletion core/world/entity/Entity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <common/Error.hpp>

#include <cstdint>
#include <unordered_set>


namespace mad::core {
Expand All @@ -21,7 +22,10 @@ namespace mad::core {
public:
using Id = std::int32_t;

public:
const std::unordered_set<std::string> tags;

explicit Entity(std::unordered_set<std::string> tags);

virtual void accept(World &world, const Intent &intent, EventDispatcher &dispatcher) = 0;

[[nodiscard]] Id get_id() const noexcept;
Expand Down
Loading