Tramway SDK
entity.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_ENTITY_H
5#define TRAM_SDK_FRAMEWORK_ENTITY_H
6
7#include <framework/uid.h>
8#include <framework/core.h>
9#include <framework/math.h>
10#include <framework/value.h>
11#include <framework/signal.h>
12
13namespace tram {
14class WorldCell;
15class Message;
16struct SharedEntityData;
17struct ValueArray;
18class File;
19
20class Entity {
21public:
22 virtual void Load() = 0;
23
24 virtual void Unload() = 0;
25
26 virtual void Serialize() = 0;
27
28 Entity();
31
32 virtual ~Entity();
33
34 inline name_t GetName() const { return name; }
35 inline id_t GetID() const { return id; }
36 inline WorldCell* GetCell() { return cell; }
37 inline bool IsLoaded() const { return is_loaded; }
38 inline bool IsAutoLoad() const { return auto_load; }
39 inline bool IsInInterior() const { return in_interior; }
40 inline bool IsPersistent() const { return is_persistent; }
41 inline bool IsChanged() const { return changed; }
42
43 void virtual UpdateParameters() = 0;
44 void virtual SetParameters() = 0;
45
46 void virtual Update() {}
47
48 inline void SetAutoLoad(bool auto_load) { this->auto_load = auto_load; }
49 inline void SetPersistent(bool persistent) { this->is_persistent = persistent; }
50
52 void SetRotation(quat rot) { rotation = rot; SetParameters(); }
53
54 inline void UpdateTransform(const vec3& loc, const quat& rot){
55 location = loc;
56 rotation = rot;
59 }
60
61 inline const vec3& GetLocation() { return location; }
62 inline const quat& GetRotation() { return rotation; }
63
64 virtual void MessageHandler(Message& msg) = 0;
65 virtual void EventHandler(Event &event) {};
66
67 virtual name_t GetType() = 0;
68
69 void CheckTransition();
70
71 struct FieldInfo {
72 uint32_t field_id;
73 uint32_t field_type;
74 uint32_t field_flags;
75 };
76
77 enum : uint32_t {
79 };
80
81 static void RegisterType(name_t name, Entity* (*constr_func)(const SharedEntityData&, const ValueArray&), void (*destr_func)(Entity*), const uint32_t* fields, size_t fieldcount);
82 static void RegisterType(name_t name, Entity* (*constr_func)(const SharedEntityData&, const ValueArray&), void (*destr_func)(Entity*), std::initializer_list<FieldInfo> fields);
83
84 inline SignalTable* GetSignalTable() { return signals; }
85
86 static void UpdateFromList();
87 static Entity* Make (name_t type, File* file);
88 static Entity* Find (id_t entity_id);
89 static Entity* Find (name_t entity_name);
90protected:
91 id_t id = 0;
93
94 uint32_t flags = 0;
95
96 bool is_persistent = true;
97 bool is_loaded = false;
98 bool auto_load = true;
99 bool in_interior = false;
100
101 bool changed = false;
102 bool is_serializable = true;
103
104 WorldCell* cell = nullptr;
106
107 quat rotation = {1.0f, 0.0f, 0.0f, 0.0f};
108 vec3 location = {0.0f, 0.0f, 0.0f};
109
110 void Register();
111 inline void FireSignal(signal_t type) { if (signals) signals->Fire(type, this->id); }
112 inline void FireSignal(signal_t type, Value value) { if (signals) signals->Fire(type, this->id, value); }
113 void AddUpdate();
114 void RemoveUpdate();
115
116 friend class WorldCell;
117};
118
119}
120
121#endif // TRAM_SDK_FRAMEWORK_ENTITY_H
Definition: entity.h:20
virtual void Serialize()=0
virtual void UpdateParameters()=0
quat rotation
Definition: entity.h:107
id_t id
Definition: entity.h:91
virtual void Unload()=0
void CheckTransition()
Definition: entity.cpp:81
virtual void Update()
Definition: entity.h:46
void Register()
Definition: entity.cpp:111
bool is_persistent
Definition: entity.h:96
bool IsPersistent() const
Definition: entity.h:40
bool is_serializable
Definition: entity.h:102
SignalTable * signals
Definition: entity.h:105
void UpdateTransform(const vec3 &loc, const quat &rot)
Definition: entity.h:54
bool in_interior
Definition: entity.h:99
static Entity * Make(name_t type, File *file)
Loads an Entity from a File.
Definition: entity.cpp:151
Entity()
Definition: entity.cpp:38
id_t GetID() const
Definition: entity.h:35
bool IsAutoLoad() const
Definition: entity.h:38
bool auto_load
Definition: entity.h:98
void SetPersistent(bool persistent)
Definition: entity.h:49
static void RegisterType(name_t name, Entity *(*constr_func)(const SharedEntityData &, const ValueArray &), void(*destr_func)(Entity *), const uint32_t *fields, size_t fieldcount)
Definition: entity.cpp:97
virtual void Load()=0
void SetLocation(vec3 loc)
Definition: entity.h:51
virtual ~Entity()
Definition: entity.cpp:64
const vec3 & GetLocation()
Definition: entity.h:61
SignalTable * GetSignalTable()
Definition: entity.h:84
static void UpdateFromList()
Updates all of the entities in the update list.
Definition: entity.cpp:144
@ FIELD_SERIALIZE
Definition: entity.h:78
bool IsLoaded() const
Definition: entity.h:37
virtual void EventHandler(Event &event)
Definition: entity.h:65
virtual name_t GetType()=0
virtual void MessageHandler(Message &msg)=0
void SetRotation(quat rot)
Definition: entity.h:52
WorldCell * GetCell()
Definition: entity.h:36
vec3 location
Definition: entity.h:108
virtual void SetParameters()=0
void FireSignal(signal_t type)
Definition: entity.h:111
bool IsChanged() const
Definition: entity.h:41
name_t name
Definition: entity.h:92
void SetAutoLoad(bool auto_load)
Definition: entity.h:48
bool is_loaded
Definition: entity.h:97
bool changed
Definition: entity.h:101
WorldCell * cell
Definition: entity.h:104
void AddUpdate()
Adds entity to update list.
Definition: entity.cpp:134
const quat & GetRotation()
Definition: entity.h:62
bool IsInInterior() const
Definition: entity.h:39
void RemoveUpdate()
Remove entity from the update list.
Definition: entity.cpp:139
uint32_t flags
Definition: entity.h:94
static Entity * Find(id_t entity_id)
Definition: entity.cpp:121
void FireSignal(signal_t type, Value value)
Definition: entity.h:112
name_t GetName() const
Definition: entity.h:34
Definition: file.h:23
Definition: signal.h:51
void Fire(signal_t signal, id_t sender)
Definition: signal.cpp:63
Definition: value.h:336
Generic, type-safe union.
Definition: value.h:18
Definition: worldcell.h:18
Definition: api.h:9
uint32_t id_t
Definition: core.h:11
uint32_t signal_t
Definition: signal.h:13
glm::vec3 vec3
Definition: math.h:12
glm::quat quat
Definition: math.h:13
Definition: entity.h:71
uint32_t field_type
Definition: entity.h:73
uint32_t field_flags
Definition: entity.h:74
uint32_t field_id
Definition: entity.h:72
Definition: event.h:24
Definition: message.h:16
Definition: entity.cpp:26
Definition: uid.h:11