Skip to content

Commit

Permalink
Rework HierarchySystem -> NodeSystem + Add RenderingSystem
Browse files Browse the repository at this point in the history
  • Loading branch information
tristankpka committed Jul 26, 2024
1 parent badaaa4 commit 8cc56d8
Show file tree
Hide file tree
Showing 23 changed files with 425 additions and 337 deletions.
24 changes: 18 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,18 @@ add_executable(Engine main.cpp
include/World.tpp
src/Window.cpp
include/Window.h
include/components/Drawable.h
include/components/Shape.h
include/components/Node.h
include/EventDispatcher.h
include/HierarchySystem.h
src/HierarchySystem.cpp
include/NodeSystem.h
src/NodeSystem.cpp
src/EventDispatcher.cpp
src/RenderingSystem.cpp
include/RenderingSystem.h
include/EntityFactory.h
include/TransformManager.h
src/TransformManager.cpp
src/EntityFactory.cpp
)

target_include_directories(Engine PRIVATE include)
Expand Down Expand Up @@ -103,12 +109,18 @@ add_executable(EngineTests
include/World.tpp
src/Window.cpp
include/Window.h
include/components/Drawable.h
include/components/Shape.h
include/components/Node.h
include/EventDispatcher.h
include/HierarchySystem.h
src/HierarchySystem.cpp
include/NodeSystem.h
src/NodeSystem.cpp
src/EventDispatcher.cpp
src/RenderingSystem.cpp
include/RenderingSystem.h
include/EntityFactory.h
include/TransformManager.h
src/TransformManager.cpp
src/EntityFactory.cpp
)

target_include_directories(EngineTests PRIVATE include)
Expand Down
3 changes: 2 additions & 1 deletion include/Component.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <type_traits>
#include <utility>

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

Expand All @@ -33,6 +33,7 @@ class Component {

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


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

Expand Down Expand Up @@ -44,7 +44,7 @@ class ComponentManager {
using ComponentVariant = std::variant<
std::unique_ptr<Component<Velocity>>,
std::unique_ptr<Component<Node>>,
std::unique_ptr<Component<Drawable>>
std::unique_ptr<Component<Shape>>
>;
std::unordered_map<Entity::Id, std::unordered_map<std::type_index, ComponentVariant>> m_componentMaps;
};
Expand Down
8 changes: 7 additions & 1 deletion include/ComponentManager.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@

template<ComponentType T>
void ComponentManager::addComponent(const Entity::Id entityId, T component) {
m_componentMaps[entityId][typeid(T)] = std::make_unique<Component<T>>(std::move(component));
auto& entityComponents = m_componentMaps[entityId];
const std::type_index typeIndex = typeid(T);
// Check if the component type already exists
if (entityComponents.contains(typeIndex)) {
throw std::runtime_error("Component already exists for this entity");
}
entityComponents[typeIndex] = std::make_unique<Component<T>>(std::move(component));
}

template<ComponentType T>
Expand Down
30 changes: 30 additions & 0 deletions include/EntityFactory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// Created by Tristan Klempka on 25/07/2024.
//

#ifndef ENTITYFACTORY_H
#define ENTITYFACTORY_H

#include <SFML/Graphics/CircleShape.hpp> // for CircleShape
#include <SFML/Graphics/Color.hpp> // for Color
#include <SFML/Graphics/RectangleShape.hpp> // for RectangleShape
#include <SFML/System/Vector2.hpp> // for Vector2f
#include <memory> // for unique_ptr
#include "Entity.h" // for Entity
class World;

class EntityFactory {
public:
explicit EntityFactory(World& world);

[[nodiscard]] Entity::Id createCircleEntity(float radius, sf::Color fillColor, sf::Color outlineColor = sf::Color::Transparent, float outlineThickness = 0.0f) const;
[[nodiscard]] Entity::Id createRectangleEntity(const sf::Vector2f& size, const sf::Color& fillColor, const sf::Color& outlineColor = sf::Color::Transparent, float outlineThickness = 0.0f) const;

private:
World& world;

static std::unique_ptr<sf::CircleShape> createCircle(float radius, sf::Color fillColor, sf::Color outlineColor = sf::Color::Transparent, float outlineThickness = 0.0f);
static std::unique_ptr<sf::RectangleShape> createRectangle(const sf::Vector2f& size, const sf::Color& fillColor, const sf::Color& outlineColor = sf::Color::Transparent, float outlineThickness = 0.0f);
};

#endif // ENTITYFACTORY_H
2 changes: 1 addition & 1 deletion include/EntityManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class EntityManager {
[[nodiscard]] const std::unordered_set<Entity>& getEntities() const;

private:
Entity::Id m_nextId = 1;
Entity::Id m_nextId = 0;
std::unordered_set<Entity> m_entities;
};

Expand Down
3 changes: 1 addition & 2 deletions include/EventDispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ enum class EventType {
EntityCreated,
EntityDestroyed,

GlobalTransformChanged,
LocalTransformChanged
TransformChanged,
};

struct Event {
Expand Down
13 changes: 5 additions & 8 deletions include/HierarchySystem.h → include/NodeSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
#include "Entity.h" // for Entity
#include "System.h" // for System
#include "components/Node.h" // for Node
class EventDispatcher;
class EventDispatcher; // lines 18-18
class World;

class HierarchySystem : public System {
class NodeSystem : 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);
explicit NodeSystem(World& world, EventDispatcher& dispatcher);

void update() override;

Expand All @@ -32,11 +33,7 @@ class HierarchySystem : public System {
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);
void onEventEntityChildAdded(Entity::Id parentId, Entity::Id childId) const;
};

#endif //HIERARCHYSYSTEM_H
40 changes: 40 additions & 0 deletions include/PhysicSystem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// Created by Tristan Klempka on 25/07/2024.
//

#ifndef PHYSICSYSTEM_H
#define PHYSICSYSTEM_H

#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <iostream>

#include "System.h"
#include "Entity.h"
#include "EventDispatcher.h"
#include "World.h"
#include "components/Node.h"
#include "components/Velocity.h"

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

explicit PhysicsSystem(World& world, EventDispatcher& dispatcher)
: m_query(world.getComponentQuery<Node, Velocity>()), m_dispatcher(dispatcher) {}

void update() override {
m_query([this](const Entity::Id id, Node& node, const Velocity& velocity) {
node.transform = node.transform.rotate(velocity.dtheta).translate(velocity.dx, velocity.dy);
m_dispatcher.dispatch(EventBuilder(EventType::TransformChanged, id).build());
}, std::nullopt);
}

private:
QueryFunction m_query;
EventDispatcher& m_dispatcher;
};

#endif //PHYSICSYSTEM_H
38 changes: 38 additions & 0 deletions include/RenderingSystem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// Created by Tristan Klempka on 25/07/2024.
//

#ifndef RENDERINGSYSTEM_H
#define RENDERINGSYSTEM_H

#include <functional> // for function
#include <optional> // for optional
#include <unordered_map> // for unordered_map
#include <unordered_set> // for unordered_set
#include "Entity.h" // for Entity
#include "System.h" // for System
#include "components/Node.h" // for Node
#include "components/Shape.h" // for Shape
class World;
namespace sf { class Drawable; }
namespace sf { class RenderWindow; }
namespace sf { class Transform; }

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

explicit RenderingSystem(World& world, sf::RenderWindow& window);

void update() override;

private:
QueryFunction m_query;
sf::RenderWindow& m_window;
std::unordered_map<Entity::Id, sf::Drawable*> m_renderables;

void doDraw(Entity::Id id, const sf::Transform& parentTransform);
};

#endif // RENDERINGSYSTEM_H
2 changes: 1 addition & 1 deletion include/SystemManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <memory> // for shared_ptr
#include <typeindex> // for hash, type_index
#include <unordered_map> // for unordered_map
class System;
class System; // lines 12-12

class SystemManager {
public:
Expand Down
27 changes: 27 additions & 0 deletions include/TransformManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// Created by Tristan Klempka on 26/07/2024.
//

#ifndef TRANSFORMMANAGER_H
#define TRANSFORMMANAGER_H

#include <functional> // for function
#include <optional> // for optional
#include <unordered_set> // for unordered_set
#include "Entity.h" // for Entity
class World;
struct Node;

class TransformManager {
public:
explicit TransformManager(World& world);

void applyRotation(Entity::Id entityId, float angle) const;
void applyTranslation(Entity::Id entityId, float x, float y) const;
void applyTransformation(Entity::Id entityId, float angle, float x, float y) const;

private:
std::function<void(std::function<void(Entity::Id, Node&)>, std::optional<std::unordered_set<Entity::Id>>)> m_query;
};

#endif // TRANSFORMMANAGER_H
5 changes: 1 addition & 4 deletions include/World.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
#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
Expand All @@ -19,7 +16,7 @@ class World {
public:
void init();

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

void destroyEntity(Entity::Id entityId) const;

Expand Down
8 changes: 6 additions & 2 deletions include/components/Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
#ifndef NODE_H
#define NODE_H

#include <unordered_set>
#include <SFML/Graphics.hpp>

#include "Entity.h"

struct Node {
sf::Transform global_transform = sf::Transform::Identity;
sf::Transform local_transform = sf::Transform::Identity;
sf::Transform transform = sf::Transform::Identity;
Entity::Id parent = 0;
std::unordered_set<Entity::Id> children = {};
};

#endif //NODE_H
6 changes: 3 additions & 3 deletions include/components/Drawable.h → include/components/Shape.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
#ifndef DRAWABLE_H
#define DRAWABLE_H

#include <SFML/Graphics/Drawable.hpp>
#include <SFML/Graphics/Shape.hpp>

struct Drawable {
std::unique_ptr<sf::Drawable> drawable;
struct Shape {
std::unique_ptr<sf::Shape> shape;
};

#endif //DRAWABLE_H
Loading

0 comments on commit 8cc56d8

Please sign in to comment.