CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
EntityScript.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
11
13
14namespace CeresEngine {
15
17 friend class ScriptingSystem;
18
19 protected:
22
23 public:
24 virtual ~EntityScript() = default;
25
26 public:
28 virtual void onCreate() {}
29
32 virtual void update(double dt) {}
33
35 virtual void onDestroy() {}
36
37 public: // Component accessors
44 template<CComponent C, typename... Args> inline C& add(Args&&... args) noexcept(std::is_nothrow_constructible_v<C, Args...>) {
45 return mEntity.add<C>(std::forward<Args>(args)...);
46 }
47
56 template<CComponent C, typename Block, typename... Args>
57 inline C& addWith(Block&& block, Args&&... args) noexcept(std::is_nothrow_constructible_v<C, Args...>) {
58 return mEntity.addWith<C>(std::forward<Block>(block), std::forward<Args>(args)...);
59 }
60
67 template<CComponent C, typename... Args> inline C& set(Args&&... args) noexcept(std::is_nothrow_constructible_v<C, Args...>) {
68 return mEntity.set<C>(std::forward<Args>(args)...);
69 }
70
73 template<CComponent C> inline void remove() noexcept(std::is_nothrow_destructible_v<C>) { mEntity.remove<C>(); }
74
78 template<CComponent C> [[nodiscard]] inline C& get() noexcept { return mEntity.get<C>(); }
79
83 template<CComponent C> [[nodiscard]] inline const C& get() const noexcept { return mEntity.get<C>(); }
84
91 template<CComponent C0, CComponent C1, CComponent... Cs> [[nodiscard]] inline Tuple<C0&, C1&, Cs&...> get() noexcept {
92 return mEntity.get<C0, C1, Cs...>();
93 }
94
101 template<CComponent C0, CComponent C1, CComponent... Cs> [[nodiscard]] inline Tuple<const C0&, const C1&, const Cs&...> get() const noexcept {
102 return mEntity.get<C0, C1, Cs...>();
103 }
104
109 template<CComponent C> inline C& getOr() noexcept(std::is_nothrow_constructible_v<C>) { return mEntity.getOr<C>(); }
110
117 template<CComponent C0, CComponent C1, CComponent... Cs> inline Tuple<C0&, C1&, Cs&...> getOr() noexcept { return mEntity.getOr<C0, C1, Cs...>(); }
118
123 template<CComponent C> inline C* getIf() noexcept { return mEntity.getIf<C>(); }
124
129 template<CComponent C> inline const C* getIf() const noexcept { return mEntity.getIf<C>(); }
130
137 template<CComponent C0, CComponent C1, CComponent... Cs> [[nodiscard]] inline Tuple<C0*, C1*, Cs*...> getIf() noexcept {
138 return mEntity.getIf<C0, C1, Cs...>();
139 }
140
147 template<CComponent C0, CComponent C1, CComponent... Cs> [[nodiscard]] inline Tuple<const C0*, const C1*, const Cs*...> getIf() const noexcept {
148 return mEntity.getIf<C0, C1, Cs...>();
149 }
150
154 template<CComponent C, CComponent... Cs> [[nodiscard]] inline bool has() const noexcept { return mEntity.has<C>(); }
155 };
156
157} // namespace CeresEngine
The base entity class.
Definition Entity.hpp:41
AbstractComponent & add(const ComponentType &type, const Box &initialValue=nullptr) const
Adds a new component of the given type.
AbstractComponent * getIf(const ComponentType &type) const
Gets a component of the given type.
C & addWith(Block &&block, Args &&... args) const noexcept(std::is_nothrow_constructible_v< C, Args... >)
Adds a new component and calls block with the added component.
AbstractComponent & get(const ComponentType &type) const
Gets a component of the given type.
void remove(const ComponentType &type) const
Removes a component of the given type from the entity.
bool has(const ComponentType &type) const noexcept
Checks if the entity has all components of the given type.
C & set(Args &&... args) const noexcept(std::is_nothrow_constructible_v< C, Args... >)
Sets a component.
AbstractComponent & getOr(const ComponentType &type) const
Gets a component of the given type.
Definition EntityScript.hpp:16
C & add(Args &&... args) noexcept(std::is_nothrow_constructible_v< C, Args... >)
Adds a new component.
Definition EntityScript.hpp:44
const C * getIf() const noexcept
Gets a component of type C.
Definition EntityScript.hpp:129
virtual void onDestroy()
A function called by the runtime whenever the script is destroyed.
Definition EntityScript.hpp:35
Tuple< C0 &, C1 &, Cs &... > get() noexcept
Gets a set of components of types C0, C1 and Cs.
Definition EntityScript.hpp:91
C * getIf() noexcept
Gets a component of type C.
Definition EntityScript.hpp:123
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.
Definition EntityScript.hpp:57
C & get() noexcept
Gets a component of type C.
Definition EntityScript.hpp:78
Tuple< C0 &, C1 &, Cs &... > getOr() noexcept
Gets a set of components of types C0, C1 and Cs.
Definition EntityScript.hpp:117
void remove() noexcept(std::is_nothrow_destructible_v< C >)
Removes a component of type C from the entity.
Definition EntityScript.hpp:73
Tuple< const C0 &, const C1 &, const Cs &... > get() const noexcept
Gets a set of components of types C0, C1 and Cs.
Definition EntityScript.hpp:101
virtual void onCreate()
A function called by the runtime whenever the script is first created.
Definition EntityScript.hpp:28
C & getOr() noexcept(std::is_nothrow_constructible_v< C >)
Gets a component of type C.
Definition EntityScript.hpp:109
Tuple< C0 *, C1 *, Cs *... > getIf() noexcept
Gets a set of components of types C0, C1 and Cs.
Definition EntityScript.hpp:137
const C & get() const noexcept
Gets a component of type C.
Definition EntityScript.hpp:83
virtual void update(double dt)
A function called by the runtime every update iteration.
Definition EntityScript.hpp:32
Tuple< const C0 *, const C1 *, const Cs *... > getIf() const noexcept
Gets a set of components of types C0, C1 and Cs.
Definition EntityScript.hpp:147
Entity mEntity
The entity associated with this script instance.
Definition EntityScript.hpp:21
virtual ~EntityScript()=default
bool has() const noexcept
Checks if the entity has all components of types Cs.
Definition EntityScript.hpp:154
C & set(Args &&... args) noexcept(std::is_nothrow_constructible_v< C, Args... >)
Sets a component.
Definition EntityScript.hpp:67
Definition ScriptingSystem.hpp:18
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
Definition Span.hpp:668