CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
EntityTemplate.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
10#include "Component.hpp"
11
13
16
17namespace CeresEngine {
18
20 private:
27
29 public:
31
32 public:
33 template<typename... Args> explicit TComponentData(Args&&... args) : component(std::forward<Args>(args)...) {}
34
35 public:
37 [[nodiscard]] const ComponentType& getComponentType() const noexcept final { return ComponentType::get<C>(); }
38
40 Any getComponent() noexcept final { return std::ref(component); }
41
43 [[nodiscard]] Any getComponent() const noexcept final { return std::cref(component); }
44 };
45
48
49 public:
51 template<CComponent C, typename... Args> inline C& add(Args&&... args) noexcept(std::is_nothrow_constructible_v<C, Args...>) {
52 const auto found = mComponents.find(ComponentType::get<C>().componentID);
53 if(found != mComponents.end()) {
54 // TODO Throw an exception here!
55 }
56
57 mComponents[ComponentType::get<C>().componentID] = ce_unique_new<TComponentData<C>>(std::forward<Args>(args)...);
58 }
59
68 template<CComponent C, typename Block, typename... Args>
69 inline C& addWith(Block&& block, Args&&... args) noexcept(std::is_nothrow_constructible_v<C, Args...>);
70
77 template<CComponent C, typename... Args> inline C& set(Args&&... args) noexcept(std::is_nothrow_constructible_v<C, Args...>);
78
81 template<CComponent C> inline void remove() noexcept(std::is_nothrow_destructible_v<C>) {
82 const auto found = mComponents.find(ComponentType::get<C>().componentID);
83 if(found == mComponents.end()) {
84 // TODO Throw an exception here!
85 }
86
87 mComponents.erase(found);
88 }
89
94 template<CComponent C> [[nodiscard]] inline C& get() noexcept {
95 const auto found = mComponents.find(ComponentType::get<C>().componentID);
96 if(found == mComponents.end()) {
97 // TODO Throw an exception here!
98 }
99
100 TComponentData<C>& componentData = static_cast<TComponentData<C>&>(*found->second);
102 }
103
108 template<CComponent C> [[nodiscard]] inline const C& get() const noexcept {
109 const auto found = mComponents.find(ComponentType::get<C>().componentID);
110 if(found == mComponents.end()) {
111 // TODO Throw an exception here!
112 }
113
114 const TComponentData<C>& componentData = static_cast<const TComponentData<C>&>(*found->second);
116 }
117
124 template<CComponent C0, CComponent C1, CComponent... Cs> [[nodiscard]] inline Tuple<C0&, C1&, Cs&...> get() noexcept {
125 return {get<C0>(), get<C1>(), get<Cs>()...};
126 }
127
134 template<CComponent C0, CComponent C1, CComponent... Cs> [[nodiscard]] inline Tuple<const C0&, const C1&, const Cs&...> get() const noexcept {
135 return {get<C0>(), get<C1>(), get<Cs>()...};
136 }
137
142 template<CComponent C> inline C& getOr() noexcept(std::is_nothrow_constructible_v<C>) {}
143
150 template<CComponent C0, CComponent C1, CComponent... Cs> inline Tuple<C0&, C1&, Cs&...> getOr() noexcept;
151
157
163
171
179
184
188 void clear();
189
194 };
195
196} // namespace CeresEngine
#define CE_SCRIPT_EXPORT(...)
The CE_SCRIPT_EXPORT macro marks a class or method as exportable and available in scripting environme...
Definition Macros.hpp:247
Definition Any.hpp:13
A type that describes and provides type-erased operations on a component.
Definition Component.hpp:456
Definition EntityTemplate.hpp:21
virtual const ComponentType & getComponentType() const noexcept=0
Definition EntityTemplate.hpp:28
C component
Definition EntityTemplate.hpp:30
TComponentData(Args &&... args)
Definition EntityTemplate.hpp:33
Any getComponent() const noexcept final
Definition EntityTemplate.hpp:43
const ComponentType & getComponentType() const noexcept final
Definition EntityTemplate.hpp:37
Any getComponent() noexcept final
Definition EntityTemplate.hpp:40
Definition EntityTemplate.hpp:19
C & getOr() noexcept(std::is_nothrow_constructible_v< C >)
Gets a component of type C.
Definition EntityTemplate.hpp:142
bool has() const noexcept
Checks if the entity has all components of types Cs.
FlatMap< ComponentID, UPtr< ComponentData > > mComponents
Definition EntityTemplate.hpp:46
Tuple< C0 &, C1 &, Cs &... > getOr() noexcept
Gets a set of components of types C0, C1 and Cs.
C & addWith(Block &&block, Args &&... args) noexcept(std::is_nothrow_constructible_v< C, Args... >)
Adds a new component and calls block with the added component.
bool empty() noexcept
Checks if the entity is empty (i.e.
Tuple< const C0 &, const C1 &, const Cs &... > get() const noexcept
Gets a set of components of types C0, C1 and Cs.
Definition EntityTemplate.hpp:134
void remove() noexcept(std::is_nothrow_destructible_v< C >)
Removes a component of type C from the entity.
Definition EntityTemplate.hpp:81
void clear()
Removes all components attached to the entity, without destroying the entity itself.
String mName
Definition EntityTemplate.hpp:47
const C & get() const noexcept
Gets a component of type C.
Definition EntityTemplate.hpp:108
C & set(Args &&... args) noexcept(std::is_nothrow_constructible_v< C, Args... >)
Sets a component.
C & get() noexcept
Gets a component of type C.
Definition EntityTemplate.hpp:94
Tuple< C0 &, C1 &, Cs &... > get() noexcept
Gets a set of components of types C0, C1 and Cs.
Definition EntityTemplate.hpp:124
C & add(Args &&... args) noexcept(std::is_nothrow_constructible_v< C, Args... >)
Adds a new component of the given type.
Definition EntityTemplate.hpp:51
C * getIf() noexcept
Gets a component of type C.
FlatMap is an almsot drop-in replacement of Map.
Definition FlatMap.hpp:53
Tuple is a fixed-size collection of heterogeneous values.
Definition Tuple.hpp:15
Definition Component.hpp:117
Definition Application.hpp:19
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
decltype(&*std::declval< I >() found)(const I &it, C &container)
Returns a pointer to the value if found, otherwise nullptr.
Definition Iterator.hpp:572
Definition Span.hpp:668