Skip to content

Commit

Permalink
Add Nodes Hierarchy System
Browse files Browse the repository at this point in the history
Add missing Drawable.h

CI - Clean unused build archis

remove no needed hash table include

Remove WShadow not useful
  • Loading branch information
tristankpka committed Jul 25, 2024
1 parent 022a65f commit badaaa4
Show file tree
Hide file tree
Showing 22 changed files with 693 additions and 208 deletions.
17 changes: 0 additions & 17 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,6 @@ jobs:
echo "tools.system.package_manager:mode=install" >> ~/.conan2/profiles/default
echo "tools.system.package_manager:sudo=True" >> ~/.conan2/profiles/default
- name: Install dependencies on macOS
if: matrix.os == 'macos-latest'
run: |
brew update
brew install cmake conan
conan profile detect
conan config set tools.system.package_manager:mode=install
conan config set tools.system.package_manager:sudo=True
- name: Install dependencies on Windows
if: matrix.os == 'windows-latest'
run: |
choco install python3 cmake
python -m pip install --upgrade pip
pip install conan
conan profile detect
- name: Create build directory
run: mkdir build
working-directory: ${{ github.workspace }}
Expand Down
35 changes: 22 additions & 13 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clan
-Werror
-Wconversion
-Wsign-conversion
-Wshadow
-Wnon-virtual-dtor
-Wold-style-cast
-Wcast-align
Expand Down Expand Up @@ -59,18 +58,23 @@ add_executable(Engine main.cpp
include/Entity.h
src/Entity.cpp
include/System.h
src/MovementSystem.cpp
include/MovementSystem.h
include/components/Velocity.h
include/components/Position.h
src/EntityManager.cpp
include/EntityManager.h
src/SystemManager.cpp
include/SystemManager.h
include/SystemManager.tpp
include/Coordinator.h
src/Coordinator.cpp
include/Coordinator.tpp
include/World.h
src/World.cpp
include/World.tpp
src/Window.cpp
include/Window.h
include/components/Drawable.h
include/components/Node.h
include/EventDispatcher.h
include/HierarchySystem.h
src/HierarchySystem.cpp
src/EventDispatcher.cpp
)

target_include_directories(Engine PRIVATE include)
Expand All @@ -88,18 +92,23 @@ add_executable(EngineTests
include/Entity.h
src/Entity.cpp
include/System.h
src/MovementSystem.cpp
include/MovementSystem.h
include/components/Velocity.h
include/components/Position.h
src/EntityManager.cpp
include/EntityManager.h
src/SystemManager.cpp
include/SystemManager.h
include/SystemManager.tpp
include/Coordinator.h
src/Coordinator.cpp
include/Coordinator.tpp
include/World.h
src/World.cpp
include/World.tpp
src/Window.cpp
include/Window.h
include/components/Drawable.h
include/components/Node.h
include/EventDispatcher.h
include/HierarchySystem.h
src/HierarchySystem.cpp
src/EventDispatcher.cpp
)

target_include_directories(EngineTests PRIVATE include)
Expand Down
6 changes: 4 additions & 2 deletions include/Component.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

#include <type_traits>
#include <utility>
#include "components/Position.h"

#include "components/Drawable.h"
#include "components/Node.h"
#include "components/Velocity.h"

// Concept to check if a type is a valid component (i.e., is a plain data structure)
Expand All @@ -29,7 +31,7 @@ class Component {
explicit Component(T data) : m_value(std::move(data)) {}
};

static_assert(ValidComponent<Position>);
static_assert(ValidComponent<Node>);
static_assert(ValidComponent<Velocity>);


Expand Down
9 changes: 7 additions & 2 deletions include/ComponentManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
#include <variant> // for variant
#include "Component.h" // for Component, ValidComponent
#include "Entity.h" // for Entity
#include "components/Position.h" // for Position
#include "components/Drawable.h" // for Drawable
#include "components/Node.h" // for Node
#include "components/Velocity.h" // for Velocity

constexpr std::uint8_t MAX_COMPONENTS_PER_ENTITY = 128;
Expand Down Expand Up @@ -40,7 +41,11 @@ class ComponentManager {
void entityDestroyed(Entity::Id entityId);

private:
using ComponentVariant = std::variant<std::unique_ptr<Component<Position>>, std::unique_ptr<Component<Velocity>>>;
using ComponentVariant = std::variant<
std::unique_ptr<Component<Velocity>>,
std::unique_ptr<Component<Node>>,
std::unique_ptr<Component<Drawable>>
>;
std::unordered_map<Entity::Id, std::unordered_map<std::type_index, ComponentVariant>> m_componentMaps;
};

Expand Down
58 changes: 58 additions & 0 deletions include/EventDispatcher.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//
// Created by Tristan Klempka on 22/07/2024.
//

#ifndef EVENTDISPATCHER_H
#define EVENTDISPATCHER_H

#include <unordered_map>
#include <vector>
#include <functional>
#include <optional>
#include "Entity.h"

enum class EventType {
EntityUpdated,
EntityChildAdded,
ComponentAdded,
ComponentRemoved,
EntityCreated,
EntityDestroyed,

GlobalTransformChanged,
LocalTransformChanged
};

struct Event {
EventType type;
Entity::Id entityId;
std::optional<Entity::Id> parentId;
std::optional<Entity::Id> childId;

Event(EventType type, Entity::Id entityId, std::optional<Entity::Id> parentId = std::nullopt);
};

class EventBuilder {
public:
explicit EventBuilder(EventType type, Entity::Id entityId);

EventBuilder& withParentId(Entity::Id parentId);
EventBuilder& withChildId(Entity::Id childId);
[[nodiscard]] Event build() const;

private:
Event event;
};

class EventDispatcher {
public:
using EventCallback = std::function<void(const Event&)>;

void subscribe(EventType eventType, EventCallback callback);
void dispatch(const Event& event) const;

private:
std::unordered_map<EventType, std::vector<EventCallback>> listeners;
};

#endif //EVENTDISPATCHER_H
42 changes: 42 additions & 0 deletions include/HierarchySystem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// Created by Tristan Klempka on 24/07/2024.
//

#ifndef HIERARCHYSYSTEM_H
#define HIERARCHYSYSTEM_H

#include <SFML/Graphics/Transform.hpp> // for Transform
#include <functional> // for function
#include <optional> // for optional
#include <unordered_map> // for unordered_map
#include <unordered_set> // for unordered_set
#include <vector> // for vector
#include "Entity.h" // for Entity
#include "System.h" // for System
#include "components/Node.h" // for Node
class EventDispatcher;

class HierarchySystem : public System {
public:
using QueryFunction = std::function<void(std::function<void(Entity::Id, Node&)>,
std::optional<std::unordered_set<Entity::Id>>)>;

explicit HierarchySystem(QueryFunction query, EventDispatcher& dispatcher);

void update() override;

private:
QueryFunction m_query;
std::unordered_set<Entity::Id> m_rootNodes;
std::unordered_map<Entity::Id, sf::Transform> m_nodeTransforms;
std::unordered_map<Entity::Id, Entity::Id> m_parentMap;
std::unordered_map<Entity::Id, std::vector<Entity::Id>> m_childNodes;

void onEventEntityCreated(Entity::Id entityId);
void onEventEntityChildAdded(Entity::Id parentId, Entity::Id childId);
void propagateTransform(Entity::Id parentId, const sf::Transform& parentTransform);
void onEventGlobalTransformChanged(Entity::Id entityId);
void onEventLocalTransformChanged(Entity::Id entityId);
};

#endif //HIERARCHYSYSTEM_H
30 changes: 0 additions & 30 deletions include/MovementSystem.h

This file was deleted.

35 changes: 35 additions & 0 deletions include/Window.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// Created by Tristan Klempka on 22/07/2024.
//

#ifndef WINDOW_H
#define WINDOW_H

#include <SFML/Graphics/RenderWindow.hpp> // for RenderWindow
#include <SFML/System/String.hpp> // for String
#include <SFML/Window/VideoMode.hpp> // for VideoMode
#include <string> // for string
namespace sf { class Event; }

class Window {
public:
Window(const sf::VideoMode& mode, const std::string& title)
: m_window(mode, title) {}

Window(const Window&) = delete;
Window& operator=(const Window&) = delete;
Window(Window&&) = delete;
Window& operator=(Window&&) = delete;

bool isOpen() const { return m_window.isOpen(); }
bool pollEvent(sf::Event& event) { return m_window.pollEvent(event); }
void close() { m_window.close(); }
sf::RenderWindow& getRenderWindow() {
return m_window;
}

private:
sf::RenderWindow m_window;
};

#endif //WINDOW_H
17 changes: 14 additions & 3 deletions include/Coordinator.h → include/World.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,26 @@
#ifndef COORDINATOR_H
#define COORDINATOR_H

#include <functional> // for function
#include <memory> // for unique_ptr, shared_ptr
#include <optional> // for optional
#include <unordered_set> // for unordered_set
#include "ComponentManager.h" // for ComponentType, ComponentManager
#include "Entity.h" // for Entity
#include "EntityManager.h" // for EntityManager
#include "EventDispatcher.h" // for EventDispatcher
#include "SystemManager.h" // for SystemManager

class Coordinator {
class World {
public:
void init();

[[nodiscard]] Entity::Id createEntity() const;
[[nodiscard]] Entity::Id createEntity();

void destroyEntity(Entity::Id entityId) const;

void addChild(Entity::Id parentId, Entity::Id childId) const;

template<ComponentType T>
void addComponent(Entity::Id entityId, T component);

Expand All @@ -40,12 +46,17 @@ class Coordinator {
template<typename... Components>
auto getComponentQuery();

[[nodiscard]] EventDispatcher& getEventDispatcher() const {
return *m_eventDispatcher;
}

private:
std::unique_ptr<ComponentManager> m_componentManager;
std::unique_ptr<EntityManager> m_entityManager;
std::unique_ptr<SystemManager> m_systemManager;
std::unique_ptr<EventDispatcher> m_eventDispatcher;
};

#include "Coordinator.tpp"
#include "World.tpp"

#endif //COORDINATOR_H
Loading

0 comments on commit badaaa4

Please sign in to comment.