Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
Common.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
14#pragma once
15
16// =============================================================================
17// Includes
18// =============================================================================
19
20#include "../../core/Types.hpp"
21
22#include <glm/glm.hpp>
23#include <string>
24#include <unordered_map>
25#include <variant>
26#include <vector>
27
28namespace esengine::ecs {
29
30// =============================================================================
31// Tag Components
32// =============================================================================
33
40struct Active {};
41
48struct Visible {};
49
56struct Static {};
57
63struct MainEntity {};
64
71struct Folder {};
72
73// =============================================================================
74// Name Component
75// =============================================================================
76
88struct Name {
90 std::string value;
91
93 Name() = default;
94
99 explicit Name(const std::string& name) : value(name) {}
100
105 explicit Name(std::string&& name) : value(std::move(name)) {}
106
111 explicit Name(const char* name) : value(name) {}
112};
113
114// =============================================================================
115// UUID Component
116// =============================================================================
117
124struct UUID {
127
128 UUID() = default;
129 explicit UUID(u64 id) : value(id) {}
130
131 bool operator==(const UUID& other) const { return value == other.value; }
132 bool operator!=(const UUID& other) const { return value != other.value; }
133};
134
135// =============================================================================
136// Script Components
137// =============================================================================
138
140using ScriptFieldValue = std::variant<
141 f32,
142 i32,
143 bool,
144 std::string,
145 glm::vec2,
146 glm::vec3,
147 glm::vec4,
148 u32 // Entity reference
149>;
150
155 std::string componentName;
156 std::unordered_map<std::string, ScriptFieldValue> values;
157};
158
162struct Scripts {
163 std::vector<ScriptInstance> instances;
164
166 bool has(const std::string& name) const {
167 for (const auto& inst : instances) {
168 if (inst.componentName == name) return true;
169 }
170 return false;
171 }
172
174 ScriptInstance* get(const std::string& name) {
175 for (auto& inst : instances) {
176 if (inst.componentName == name) return &inst;
177 }
178 return nullptr;
179 }
180
181 const ScriptInstance* get(const std::string& name) const {
182 for (const auto& inst : instances) {
183 if (inst.componentName == name) return &inst;
184 }
185 return nullptr;
186 }
187
189 void add(ScriptInstance&& instance) {
190 instances.push_back(std::move(instance));
191 }
192
194 void remove(const std::string& name) {
195 instances.erase(
196 std::remove_if(instances.begin(), instances.end(),
197 [&name](const ScriptInstance& inst) { return inst.componentName == name; }),
198 instances.end());
199 }
200};
201
202} // namespace esengine::ecs
203
204// =============================================================================
205// Std Hash Support for UUID
206// =============================================================================
207
208namespace std {
209
210template<>
211struct hash<esengine::ecs::UUID> {
212 size_t operator()(const esengine::ecs::UUID& uuid) const noexcept {
213 return hash<esengine::u64>{}(uuid.value);
214 }
215};
216
217} // namespace std
Core type definitions and utilities for ESEngine.
std::variant< f32, i32, bool, std::string, glm::vec2, glm::vec3, glm::vec4, u32 > ScriptFieldValue
Value type for script component fields.
Definition Common.hpp:140
std::int32_t i32
Signed 32-bit integer.
Definition Types.hpp:54
std::uint64_t u64
Unsigned 64-bit integer.
Definition Types.hpp:47
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
float f32
32-bit floating point
Definition Types.hpp:59
Tag component marking an entity as active.
Definition Common.hpp:40
Tag component marking an entity as a folder for organization.
Definition Common.hpp:71
Tag component marking an entity as the main/primary entity.
Definition Common.hpp:63
Name(std::string &&name)
Constructs with name (move).
Definition Common.hpp:105
std::string value
The entity's name.
Definition Common.hpp:90
Name(const char *name)
Constructs with C-string.
Definition Common.hpp:111
Name(const std::string &name)
Constructs with name (copy).
Definition Common.hpp:99
Name()=default
Default constructor (empty name).
Instance of a script component attached to an entity.
Definition Common.hpp:154
std::unordered_map< std::string, ScriptFieldValue > values
Definition Common.hpp:156
Component storing all script instances for an entity.
Definition Common.hpp:162
bool has(const std::string &name) const
Check if entity has a specific script component.
Definition Common.hpp:166
const ScriptInstance * get(const std::string &name) const
Definition Common.hpp:181
ScriptInstance * get(const std::string &name)
Get script instance by name (nullptr if not found).
Definition Common.hpp:174
std::vector< ScriptInstance > instances
Definition Common.hpp:163
void add(ScriptInstance &&instance)
Add a script instance.
Definition Common.hpp:189
void remove(const std::string &name)
Remove script instance by name.
Definition Common.hpp:194
Tag component marking an entity as static (non-moving).
Definition Common.hpp:56
Universally Unique Identifier component.
Definition Common.hpp:124
bool operator!=(const UUID &other) const
Definition Common.hpp:132
u64 value
The unique identifier.
Definition Common.hpp:126
bool operator==(const UUID &other) const
Definition Common.hpp:131
Tag component marking an entity as visible.
Definition Common.hpp:48
size_t operator()(const esengine::ecs::UUID &uuid) const noexcept
Definition Common.hpp:212