Tramway SDK
entitycomponent.h
Go to the documentation of this file.
1// TRAMWAY DRIFT AND DUNGEON EXPLORATION SIMULATOR 2022
2// All rights reserved.
3
4#ifndef TRAM_SDK_FRAMEWORK_ENTITYCOMPONENT_H
5#define TRAM_SDK_FRAMEWORK_ENTITYCOMPONENT_H
6
7#include <templates/pool.h>
8
9#include <framework/core.h>
10#include <framework/async.h>
11
12
13namespace tram {
14
15class Event;
16class Entity;
17
19public:
21 virtual ~EntityComponent();
22
23 virtual void Init() {
24 is_init = true;
25 if (resources_waiting == 0) Start();
26 }
27
28 virtual void EventHandler(Event &event) = 0;
29
30 inline bool IsReady() { return is_ready; }
31 inline bool IsInit() { return is_init; }
32
33 inline Entity* GetParent() { return parent; }
34 inline void SetParent(Entity* parent) { this->parent = parent; }
35
36protected:
38 bool is_ready = false;
39 bool is_init = false;
40 Entity* parent = nullptr;
41
43
44 virtual void Start() = 0;
46 template <typename T> friend class ResourceProxy;
47};
48
52template <typename T>
53class Component {
54public:
57 void make() { ptr = PoolProxy<T>::New(); }
58 void clear() { if (ptr) PoolProxy<T>::Delete(ptr); ptr = nullptr; }
59 T* get() { return ptr; }
60 T* operator->() { return ptr; }
61 T& operator*() { return ptr; }
62 operator T*() { return ptr; }
63 explicit operator bool() { return ptr != nullptr; }
64protected:
65 T* ptr = nullptr;
66};
67
68}
69
70#endif // TRAM_SDK_FRAMEWORK_ENTITYCOMPONENT_H
Wrapper for an EntityComponent pointer.
Definition: entitycomponent.h:53
T * get()
Definition: entitycomponent.h:59
Component()
Definition: entitycomponent.h:55
void make()
Definition: entitycomponent.h:57
T * ptr
Definition: entitycomponent.h:65
T * operator->()
Definition: entitycomponent.h:60
void clear()
Definition: entitycomponent.h:58
T & operator*()
Definition: entitycomponent.h:61
~Component()
Definition: entitycomponent.h:56
Definition: entitycomponent.h:18
bool is_init
Definition: entitycomponent.h:39
void SetParent(Entity *parent)
Definition: entitycomponent.h:34
EntityComponent()
Definition: entitycomponent.cpp:8
bool is_ready
Definition: entitycomponent.h:38
virtual void EventHandler(Event &event)=0
virtual void Start()=0
Entity * GetParent()
Definition: entitycomponent.h:33
size_t resources_waiting
Definition: entitycomponent.h:37
void ResourceReady()
Definition: entitycomponent.h:42
bool IsReady()
Definition: entitycomponent.h:30
bool IsInit()
Definition: entitycomponent.h:31
virtual ~EntityComponent()
Definition: entitycomponent.cpp:12
Entity * parent
Definition: entitycomponent.h:40
virtual void Init()
Definition: entitycomponent.h:23
Definition: entity.h:20
static void Delete(T *obj)
Definition: pool.h:124
static T * New(Args &&... args)
Definition: pool.h:123
Definition: resource.h:47
void FinishResources()
Notifies EntityComponents about finished resources.
Definition: async.cpp:131
Definition: api.h:9
Definition: event.h:24