SPARK  0.1.0
A general purpose game engine written in C++.
Loading...
Searching...
No Matches
AbstractGameObject.h
1#pragma once
2
3#include <algorithm>
4#include <ranges>
5
6namespace spark::core::details
7{
8 template <typename Impl>
9 AbstractGameObject<Impl>::AbstractGameObject(GameObject* parent)
10 : Composite(parent) {}
11
12 template <typename Impl>
13 AbstractGameObject<Impl>::~AbstractGameObject()
14 {
15 // Ensure onDestroyed() was called
16 SPARK_CORE_ASSERT(!m_initialized)
17
18 for (const auto& [component, managed] : m_components | std::views::values)
19 if (managed)
20 delete component;
21 }
22
23 template <typename Impl>
25 {
26 if (m_initialized)
27 return;
28
29 static_cast<Impl*>(this)->onSpawn();
30 std::ranges::for_each(m_components | std::views::values | std::views::keys,
31 [](Component* component)
32 {
33 component->onAttach();
34 });
35 m_initialized = true;
36 }
37
38 template <typename Impl>
40 {
41 static_cast<Impl*>(this)->onUpdate(dt);
42 std::ranges::for_each(m_components | std::views::values | std::views::keys,
43 [&dt](Component* component)
44 {
45 component->onUpdate(dt);
46 });
47 }
48
49 template <typename Impl>
51 {
52 if (!m_initialized)
53 return;
54
55 std::ranges::for_each(m_components | std::views::values | std::views::keys,
56 [](Component* component)
57 {
58 component->onDetach();
59 });
60 static_cast<Impl*>(this)->onDestroyed();
61 m_initialized = false;
62 }
63}
A component that can be attached to a GameObject to provide additional functionality.
Definition Component.h:20
virtual void onDetach()
Method called when the component is detached from a GameObject.
Definition Component.h:75
virtual void onAttach()
Method called when the component is attached to a GameObject.
Definition Component.h:64
virtual void onUpdate(float dt)
Method called on every frame.
Definition Component.h:70
A CRTP class to implement a GameObject. It is used to wrap the onSpawn, onUpdate and onDestroyed meth...
Definition AbstractGameObject.h:22