Tramway SDK
entitycomponent.h
Go to the documentation of this file.
1// Tramway Drifting and Dungeon Exploration Simulator SDK Runtime
2
3#ifndef TRAM_SDK_FRAMEWORK_ENTITYCOMPONENT_H
4#define TRAM_SDK_FRAMEWORK_ENTITYCOMPONENT_H
5
6#include <templates/pool.h>
7
8#include <framework/core.h>
9#include <framework/async.h>
10
11namespace tram {
12
13class Event;
14class Entity;
15
17public:
18 EntityComponent() = default;
19 virtual ~EntityComponent() = default;
20
21 virtual void Init();
22
23 virtual void EventHandler(Event &event) = 0;
24
25 inline bool IsReady() { return is_ready; }
26 inline bool IsInit() { return is_init; }
27
28 inline Entity* GetParent() { return parent; }
29 inline void SetParent(Entity* parent) { this->parent = parent; }
30
31protected:
33 bool is_ready = false;
34 bool is_init = false;
35 Entity* parent = nullptr;
36
37 // we could make resources_waiting a uint16_t and then we could even turn
38 // is_ready and is_init into a single bitmask, saving memory
39
40 void ResourceReady();
41
42 virtual void Start() = 0;
44 template <typename T> friend class ResourceProxy;
45};
46
50template <typename T>
51class Component {
52public:
55 void make() { if (ptr) yeet(); init(); }
56 void clear() { if (ptr) yeet(); ptr = nullptr; }
57 T* get() { return ptr; }
58 T* operator->() { return ptr; }
59 T& operator*() { return ptr; }
60 operator T*() { return ptr; }
61 explicit operator bool() { return ptr != nullptr; }
62protected:
63 void init();
64 void yeet();
65
66 T* ptr = nullptr;
67};
68
69}
70
71#endif // TRAM_SDK_FRAMEWORK_ENTITYCOMPONENT_H
Wrapper for an EntityComponent pointer.
Definition: entitycomponent.h:51
T * get()
Definition: entitycomponent.h:57
Component()
Definition: entitycomponent.h:53
void make()
Definition: entitycomponent.h:55
T * ptr
Definition: entitycomponent.h:66
T * operator->()
Definition: entitycomponent.h:58
void clear()
Definition: entitycomponent.h:56
T & operator*()
Definition: entitycomponent.h:59
~Component()
Definition: entitycomponent.h:54
Component base class.
Definition: entitycomponent.h:16
bool is_init
Definition: entitycomponent.h:34
void SetParent(Entity *parent)
Definition: entitycomponent.h:29
bool IsReady()
Returns the component's readiness.
Definition: entitycomponent.h:25
virtual ~EntityComponent()=default
bool is_ready
Definition: entitycomponent.h:33
virtual void EventHandler(Event &event)=0
virtual void Start()=0
Entity * GetParent()
Definition: entitycomponent.h:28
size_t resources_waiting
Definition: entitycomponent.h:32
void ResourceReady()
Notifies the component of a streamed-in resource.
Definition: entitycomponent.cpp:53
bool IsInit()
Returns the component's init status.
Definition: entitycomponent.h:26
virtual void Init()
Initializes an entity component.
Definition: entitycomponent.cpp:39
Entity * parent
Definition: entitycomponent.h:35
Entity base class.
Definition: entity.h:23
Definition: resource.h:46
void FinishResources()
Notifies EntityComponents about finished resources.
Definition: async.cpp:231
Serialization, i.e.
Event data.
Definition: event.h:24