Tramway SDK v0.1.1
controller.h
Go to the documentation of this file.
1// Tramway Drifting and Dungeon Exploration Simulator SDK Runtime
2
3#ifndef TRAM_SDK_COMPONENTS_CONTROLLERCOMPONENT_H
4#define TRAM_SDK_COMPONENTS_CONTROLLERCOMPONENT_H
5
6#include <framework/core.h>
8#include <physics/physics.h>
9
10namespace tram {
11
12class PhysicsComponent;
13class TriggerComponent;
14
16public:
17 virtual void Move(vec3 local_direction) = 0;
18
19 virtual void Run() = 0;
20 virtual void Crouch() = 0;
21 virtual void Fly() = 0;
22 virtual void Jump() = 0;
23 virtual void TurnLeft() = 0;
24 virtual void TurnRight() = 0;
25
26 virtual void Push(vec3 direction) = 0;
27
28 inline void SetCollisions(bool collide) { this->collide = collide; }
29
30 inline void SetLookDirection(quat direction) { look_direction = direction; }
31 inline quat GetLookDirection() const { return look_direction; }
32
33 static bool IsDebugInfoDraw();
34 static void SetDebugInfoDraw(bool);
35
36 static void Update();
37protected:
38 quat look_direction = {1.0f, 0.0f, 0.0f, 0.0f};
39 bool collide = true;
40};
41
43public:
44 void Start() override;
45 void EventHandler(Event &event) override {}
46
47 void Move(vec3 local_direction) override;
48
49 void Run() override;
50 void Crouch() override;
51 void Fly() override;
52 void Jump() override;
53 void TurnLeft() override;
54 void TurnRight() override;
55
56 void Push(vec3 direction) override;
57
58 inline void SetCollisionSize(float width, float height, float crouch_height) {
59 collision_width = width;
60 collision_height = height;
61 collision_height_crouch = crouch_height;
62 }
63
64 inline void SetStepHeight(float height, float crouch_height) {
65 step_height = height;
66 step_height_crouch = crouch_height;
67 }
68
70 wallbonk_callback = callback;
71 }
72
73 inline void SetWalkSpeed(float speed) { this->walk_speed = speed; }
74 inline void SetRunSpeed(float speed) { this->run_speed = speed; }
75 inline void SetCrouchSpeed(float speed) { this->crouch_speed = speed; }
76 inline void SetFlySpeed(float speed) { this->fly_speed = speed; }
77 inline void SetFriction(float friction) { this->friction = friction; }
78 inline void SetCollisionGroup(uint32_t group) { this->collision_group = group; }
79protected:
82
85
86 void ApplyDynamics();
88 void ResetMove();
89
90 vec3 velocity = {0.0f, 0.0f, 0.0f};
91 vec3 move_direction = {0.0f, 0.0f, 0.0f};
92
93
94 bool is_in_air = false;
95
96 float collision_width = 0.35f;
97 float collision_height = 1.85f;
99
100 float step_height = 0.35f;
101 float step_height_crouch = 0.15f;
102
103 float walk_speed = 0.1f;
104 float run_speed = 0.2f;
105 float crouch_speed = 0.05f;
106 float fly_speed = 0.2f;
107
108 float friction = 0.89f;
109
110 bool crouching = false;
111 bool running = false;
112 bool flying = false;
113
118
120
122
124 template <typename> friend class Pool;
125};
126
128public:
129 void Start() override;
130 void EventHandler(Event &event) override {}
131
132 void Move(vec3 local_direction) override;
133
134 void Run() override;
135 void Crouch() override;
136 void Fly() override;
137 void Jump() override;
138 void TurnLeft() override;
139 void TurnRight() override;
140
141 void Push(vec3 direction) override;
142
143 inline void SetCollisionSize(float width, float height, float crouch_height) {
144 collision_width = width;
145 collision_height = height;
146 //collision_height_crouch = crouch_height;
147 }
148
149 inline void SetStepHeight(float height, float crouch_height) {
150 step_height = height;
151 //step_height_crouch = crouch_height;
152 }
153
155 wallbonk_callback = callback;
156 }
157
158 inline void SetWalkSpeed(float speed) { this->walk_speed = speed; }
159 inline void SetRunSpeed(float speed) { this->run_speed = speed; }
160 inline void SetCrouchSpeed(float speed) { this->crouch_speed = speed; }
161 inline void SetFlySpeed(float speed) { this->fly_speed = speed; }
162 inline void SetFriction(float speed) { }
163 inline void SetCollisionGroup(uint32_t group) { this->collision_group = group; }
164protected:
167
169
170 void ApplyDynamics();
172 void ResetMove();
173
174 vec3 move_magnitude = {0.0f, 0.0f, 0.0f};
175 vec3 move_direction = {0.0f, 0.0f, 0.0f};
176
177 vec3 air_velocity = {0.0f, 0.0f, 0.0f};
178
179 bool enabled = false;
180
181 bool is_in_air = false;
182
183 float collision_width = 0.35f;
184 float collision_height = 1.85f;
185
186 float step_height = 0.35f;
187
188 float walk_speed = 0.1f;
189 float run_speed = 0.2f;
190 float crouch_speed = 0.05f;
191 float fly_speed = 0.2f;
192
193 bool crouching = false;
194 bool running = false;
195 bool flying = false;
196
197 int jump_frames = 0;
198
202
204
206
208 template <typename> friend class Pool;
209};
210
211}
212
213#endif // TRAM_SDK_COMPONENTS_CONTROLLERCOMPONENT_H
Wrapper for an EntityComponent pointer.
Definition: entitycomponent.h:51
Provides an API for character controllers.
Definition: controller.h:15
void SetCollisions(bool collide)
Definition: controller.h:28
virtual void Run()=0
virtual void TurnRight()=0
virtual void Move(vec3 local_direction)=0
static bool IsDebugInfoDraw()
Definition: controller.cpp:120
virtual void Push(vec3 direction)=0
void SetLookDirection(quat direction)
Definition: controller.h:30
bool collide
Definition: controller.h:39
quat GetLookDirection() const
Definition: controller.h:31
quat look_direction
Definition: controller.h:38
virtual void TurnLeft()=0
virtual void Jump()=0
static void Update()
Updates the ControllerComponents.
Definition: controller.cpp:61
virtual void Fly()=0
virtual void Crouch()=0
static void SetDebugInfoDraw(bool)
Definition: controller.cpp:124
Component base class.
Definition: entitycomponent.h:16
Implementation of an FPS-style character controller.
Definition: controller.h:42
float collision_width
Definition: controller.h:96
void Jump() override
Definition: controller.cpp:173
float walk_speed
Definition: controller.h:103
void RecoverFromCollisions()
Definition: controller.cpp:234
void Push(vec3 direction) override
Definition: controller.cpp:153
void(* wallbonk_callback)(ControllerComponent *, Physics::Collision)
Definition: controller.h:121
void SetCollisionGroup(uint32_t group)
Definition: controller.h:78
id_t standing_on
Definition: controller.h:114
bool crouching
Definition: controller.h:110
void TurnLeft() override
Definition: controller.cpp:180
void SetWallCollisionCallback(void(*callback)(ControllerComponent *, Physics::Collision))
Definition: controller.h:69
uint32_t collision_group
Definition: controller.h:119
void SetRunSpeed(float speed)
Definition: controller.h:74
vec3 standing_pos
Definition: controller.h:116
void ApplyDynamics()
Definition: controller.cpp:189
float step_height_crouch
Definition: controller.h:101
void Crouch() override
Definition: controller.cpp:165
float friction
Definition: controller.h:108
void SetFriction(float friction)
Definition: controller.h:77
float step_height
Definition: controller.h:100
void ResetMove()
Definition: controller.cpp:387
void SetCrouchSpeed(float speed)
Definition: controller.h:75
float fly_speed
Definition: controller.h:106
void SetWalkSpeed(float speed)
Definition: controller.h:73
void EventHandler(Event &event) override
Definition: controller.h:45
float run_speed
Definition: controller.h:104
void SetStepHeight(float height, float crouch_height)
Definition: controller.h:64
vec3 velocity
Definition: controller.h:90
void SetCollisionSize(float width, float height, float crouch_height)
Definition: controller.h:58
vec3 move_direction
Definition: controller.h:91
bool is_in_air
Definition: controller.h:94
float collision_height
Definition: controller.h:97
bool running
Definition: controller.h:111
void Run() override
Definition: controller.cpp:161
void TurnRight() override
Definition: controller.cpp:184
void Start() override
Definition: controller.cpp:135
void SetFlySpeed(float speed)
Definition: controller.h:76
quat standing_rot
Definition: controller.h:117
void Fly() override
Definition: controller.cpp:169
void Move(vec3 local_direction) override
Definition: controller.cpp:157
float collision_height_crouch
Definition: controller.h:98
bool flying
Definition: controller.h:112
id_t standing_on_prev
Definition: controller.h:115
float crouch_speed
Definition: controller.h:105
Component< TriggerComponent > crouch_collision
Definition: controller.h:84
Component< TriggerComponent > walk_collision
Definition: controller.h:83
Definition: pool.h:21
Implementation of a simple character controller.
Definition: controller.h:127
vec3 move_direction
Definition: controller.h:175
bool running
Definition: controller.h:194
vec3 move_magnitude
Definition: controller.h:174
void EventHandler(Event &event) override
Definition: controller.h:130
id_t standing_on
Definition: controller.h:199
Component< TriggerComponent > wall_collision
Definition: controller.h:168
void Move(vec3 local_direction) override
Definition: controller.cpp:417
bool flying
Definition: controller.h:195
void Run() override
Definition: controller.cpp:421
uint32_t collision_group
Definition: controller.h:203
void Start() override
Definition: controller.cpp:402
vec3 standing_pos
Definition: controller.h:201
void ApplyDynamics()
Definition: controller.cpp:456
void SetCollisionGroup(uint32_t group)
Definition: controller.h:163
float collision_width
Definition: controller.h:183
void Crouch() override
Definition: controller.cpp:425
vec3 air_velocity
Definition: controller.h:177
float collision_height
Definition: controller.h:184
void Fly() override
Definition: controller.cpp:429
void TurnRight() override
Definition: controller.cpp:451
void SetStepHeight(float height, float crouch_height)
Definition: controller.h:149
void SetCollisionSize(float width, float height, float crouch_height)
Definition: controller.h:143
void TurnLeft() override
Definition: controller.cpp:447
void Jump() override
Definition: controller.cpp:433
void Push(vec3 direction) override
Definition: controller.cpp:413
int jump_frames
Definition: controller.h:197
void SetFriction(float speed)
Definition: controller.h:162
void ResetMove()
Definition: controller.cpp:564
void(* wallbonk_callback)(ControllerComponent *, Physics::Collision)
Definition: controller.h:205
bool enabled
Definition: controller.h:179
bool crouching
Definition: controller.h:193
bool is_in_air
Definition: controller.h:181
void SetFlySpeed(float speed)
Definition: controller.h:161
void SetCrouchSpeed(float speed)
Definition: controller.h:160
float fly_speed
Definition: controller.h:191
float crouch_speed
Definition: controller.h:190
float run_speed
Definition: controller.h:189
float walk_speed
Definition: controller.h:188
void RecoverFromCollisions()
Definition: controller.cpp:495
float step_height
Definition: controller.h:186
void SetWalkSpeed(float speed)
Definition: controller.h:158
id_t standing_on_prev
Definition: controller.h:200
void SetWallCollisionCallback(void(*callback)(ControllerComponent *, Physics::Collision))
Definition: controller.h:154
void SetRunSpeed(float speed)
Definition: controller.h:159
@ COLL_PLAYER
Definition: physics.h:29
Serialization, i.e.
uint32_t id_t
Definition: core.h:10
glm::vec3 vec3
Definition: math.h:11
glm::quat quat
Definition: math.h:12
Event data.
Definition: event.h:24
Physics collision data.
Definition: physics.h:34