SPARK  0.1.0
A general purpose game engine written in C++.
Loading...
Searching...
No Matches
AbstractGameObject.h
1#pragma once
2
3#include "experimental/ser/SerializerScheme.h"
4#include "spark/patterns/Composite.h"
5
6namespace spark::core
7{
8 class GameObject;
9}
10
11namespace spark::core::details
12{
13 template <typename Impl>
14 struct GameObjectDeleter;
15
20 template <typename Impl>
21 class AbstractGameObject : public patterns::Composite<GameObject, GameObjectDeleter>
22 {
23 friend class spark::core::GameObject;
24 SPARK_ALLOW_PRIVATE_SERIALIZATION
25
26 public:
27 explicit AbstractGameObject(GameObject* parent = nullptr);
28 ~AbstractGameObject() override;
29
30 AbstractGameObject(const AbstractGameObject& other) = delete;
31 AbstractGameObject(AbstractGameObject&& other) noexcept = default;
32 AbstractGameObject& operator=(const AbstractGameObject& other) = delete;
33 AbstractGameObject& operator=(AbstractGameObject&& other) noexcept = default;
34
38 void onSpawn();
39
44 void onUpdate(float dt);
45
49 void onDestroyed();
50
51 private:
52 bool m_initialized = false;
53 std::unordered_map<rtti::RttiBase*, std::pair<Component*, bool>> m_components;
54 };
55
60 template <typename Impl>
62 {
63 inline static std::vector<AbstractGameObject<GameObject>*> objectsToDestroy;
64
65 static void DeleteMarkedObjects()
66 {
67 for (std::size_t i = 0; i < objectsToDestroy.size(); i++)
68 delete objectsToDestroy[i];
69 objectsToDestroy.clear();
70 }
71
72 void operator()(Impl* ptr, const bool immediate) const
73 {
74 static_cast<AbstractGameObject<GameObject>*>(ptr)->onDestroyed();
75 if (immediate)
76 delete ptr;
77 else
78 objectsToDestroy.push_back(ptr);
79 }
80
81 void operator()(Impl* ptr) const
82 {
83 operator()(ptr, true);
84 }
85 };
86}
87
88#include "spark/core/impl/AbstractGameObject.h"
A GameObject is any object in the game. It contains a list of components that provides functionality ...
Definition GameObject.h:26
A CRTP class to implement a GameObject. It is used to wrap the onSpawn, onUpdate and onDestroyed meth...
Definition AbstractGameObject.h:22
void onSpawn()
Method calling the corresponding method on the implementation, the children and components.
Definition AbstractGameObject.h:24
void onDestroyed()
Method calling the corresponding method on the implementation, the children and components.
Definition AbstractGameObject.h:50
void onUpdate(float dt)
Method calling the corresponding method on the implementation, the children and components.
Definition AbstractGameObject.h:39
Definition Composite.h:10
Deleter used to call the onDestroyed method on the implementation of a GameObject.
Definition AbstractGameObject.h:62