VisuTwin Canvas
C++ 3D Engine — Metal Backend
Loading...
Searching...
No Matches
entity.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2025-2026 Arnis Lektauers
3//
4// Created by Arnis Lektauers on 05.09.2025.
5//
6#pragma once
7
8#include <memory>
9#include <type_traits>
10#include <unordered_map>
11#include <vector>
12
15#include "engine.h"
17#include "scene/graphNode.h"
18
19namespace visutwin::canvas
20{
21 class ScriptComponent;
22
31 class Entity : public GraphNode
32 {
33 public:
34 virtual ~Entity();
35
36 Component* addComponentInstance(std::unique_ptr<Component> component, ComponentTypeID typeId)
37 {
38 if (!component) {
39 return nullptr;
40 }
41 if (const auto it = _components.find(typeId); it != _components.end()) {
42 return it->second;
43 }
44
45 auto* raw = component.get();
46 _components[typeId] = raw;
47 _componentStorage.push_back(std::move(component));
48
49 if (typeId == componentTypeID<ScriptComponent>()) {
50 _script = static_cast<ScriptComponent*>(raw);
51 }
52
53 return raw;
54 }
55
56 template <class ComponentType>
58 {
60 if (const auto it = _components.find(typeId); it != _components.end()) {
61 return it->second;
62 }
63
64 if (!_engine || !_engine->systems()) {
65 return nullptr;
66 }
67
68 auto* system = _engine->systems()->template getByComponentType<ComponentType>();
69 if (!system) {
70 return nullptr;
71 }
72
73 auto component = system->addComponent(this);
74 if (!component) {
75 return nullptr;
76 }
77
78 auto* raw = component.get();
79 _components[typeId] = raw;
80 _componentStorage.push_back(std::move(component));
81
82 if constexpr (std::is_same_v<ComponentType, ScriptComponent>) {
83 _script = static_cast<ScriptComponent*>(raw);
84 }
85
86 return raw;
87 }
88
93 template <class T>
94 requires std::derived_from<T, Component>
96 {
97 auto it = _components.find(componentTypeID<T>());
98 if (it == _components.end()) {
99 return nullptr;
100 }
101 return static_cast<T*>(it->second);
102 }
103
107 template <class T>
108 requires std::derived_from<T, Component>
109 std::vector<T*> findComponents()
110 {
111 std::vector<T*> result;
112 auto it = _components.find(componentTypeID<T>());
113 if (it == _components.end())
114 {
115 return result;
116 }
117 result.push_back(static_cast<T*>(it->second));
118 return result;
119 }
120
121 ScriptComponent* script() const { return _script; }
122
123 Engine* engine() const { return _engine; }
124 void setEngine(Engine* engine) { _engine = engine; }
125
131 void onHierarchyStateChanged(bool enabled) override;
132
140 Entity* clone() const;
141
145 const std::unordered_map<ComponentTypeID, Component*>& components() const { return _components; }
146
152 Engine* findEngine() const;
153
154 private:
155 Engine* _engine = nullptr;
156
157 // Component map for generic access
158 std::unordered_map<ComponentTypeID, Component*> _components;
159 std::vector<std::unique_ptr<Component>> _componentStorage;
160
161 ScriptComponent* _script = nullptr;
162 };
163}
Base class for ECS components that attach functionality to entities.
Definition component.h:40
Central application orchestrator managing scenes, rendering, input, and resource loading.
Definition engine.h:38
ECS entity — a GraphNode that hosts components defining its behavior.
Definition entity.h:32
void setEngine(Engine *engine)
Definition entity.h:124
Component * addComponent()
Definition entity.h:57
ScriptComponent * script() const
Definition entity.h:121
Engine * engine() const
Definition entity.h:123
void onHierarchyStateChanged(bool enabled) override
Definition entity.cpp:18
const std::unordered_map< ComponentTypeID, Component * > & components() const
Definition entity.h:145
Component * addComponentInstance(std::unique_ptr< Component > component, ComponentTypeID typeId)
Definition entity.h:36
Engine * findEngine() const
Definition entity.cpp:40
Entity * clone() const
Definition entity.cpp:56
std::vector< T * > findComponents()
Definition entity.h:109
GraphNode(const std::string &name="Untitled")
Definition graphNode.h:30
ComponentTypeID componentTypeID()
Definition component.h:25
std::size_t ComponentTypeID
Definition component.h:17