Repository navigation
Expand file tree
/
Copy pathobject.h
More file actions
190 lines (157 loc) · 8.93 KB
/
Copy pathobject.h
File metadata and controls
190 lines (157 loc) · 8.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#ifndef SRC_ENGINE_OBJECT_H_
#define SRC_ENGINE_OBJECT_H_
#include <memory>
#include <array>
#include <stack>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iostream>
#include "spatial/defaults.h"
#include "shader.h"
#include "spatial/vec.h"
#include "spatial/quaternion.h"
#include "mesh.h"
namespace Engine {
class Object {
static std::unordered_set<const Object *> invalid;
static std::set<Object *> marked;
bool display = true;
Mesh *mesh = nullptr, *collider = nullptr;
std::list<Object *> children;
Object *parent = nullptr;
Shader::Program *shader = nullptr;
float_max_t
mass = 1.0,
min_speed = 0.0,
max_speed = std::numeric_limits<float_max_t>::infinity(),
min_acceleration = 0.0,
max_acceleration = std::numeric_limits<float_max_t>::infinity(),
max_force = std::numeric_limits<float_max_t>::infinity();
Spatial::Vec<3> position, speed, acceleration;
Spatial::Quaternion orientation;
static void delayedDestroy(void);
public:
inline static bool isValid (const Object *obj, bool is_marked = true) {
return Object::invalid.find(obj) == Object::invalid.end() || (is_marked && Object::marked.count(const_cast<Object *>(obj)));
}
inline Object (
const Spatial::Vec<3> &_position = Spatial::Vec<3>::origin,
const Spatial::Quaternion &_orientation = Spatial::Quaternion::identity,
bool _display = true,
Mesh *_mesh = nullptr,
Mesh *_collider = nullptr,
const Spatial::Vec<3> &_speed = Spatial::Vec<3>::zero,
const Spatial::Vec<3> &_acceleration = Spatial::Vec<3>::zero,
float_max_t _mass = 1.0,
float_max_t _min_speed = 0.0,
float_max_t _max_speed = std::numeric_limits<float_max_t>::infinity(),
float_max_t _min_acceleration = 0.0,
float_max_t _max_acceleration = std::numeric_limits<float_max_t>::infinity(),
float_max_t _max_force = std::numeric_limits<float_max_t>::infinity()
) : display(_display), mass(_mass), min_speed(_min_speed), max_speed(_max_speed), max_force(_max_force), position(_position), orientation(_orientation) {
this->setMesh(_mesh);
this->setCollider(_collider);
this->setAcceleration(_acceleration);
this->setSpeed(_speed);
Object::invalid.erase(this);
};
inline virtual ~Object (void) { Object::invalid.insert(this); }
inline bool detectCollision (const Object *other, const Spatial::Vec<3> &my_speed, const Spatial::Vec<3> &other_speed, Spatial::Vec<3> &point) const {
return this->getCollider()->detectCollision(other->getCollider(), this->getPosition(), my_speed, other->getPosition(), other_speed, point);
}
inline bool collides (void) const { return this->collider != nullptr; }
inline bool isMoving (void) const { return this->getSpeed(); }
inline void addChild (Object *obj) {
if (Object::isValid(this) && Object::isValid(obj)) {
obj->parent = this;
obj->onSetParent(this);
this->children.push_back(obj);
this->onAddChild(obj);
}
}
inline void removeChild (Object *obj) {
if (Object::isValid(this)) {
if (Object::isValid(obj)) {
obj->parent = nullptr;
obj->onRemoveParent(this);
}
this->children.remove(obj);
this->onRemoveChild(obj);
}
}
inline void setParent (Object *obj) {
if (Object::isValid(this) && Object::isValid(obj)) {
this->parent->addChild(obj);
}
}
inline void removeParent (void) {
if (Object::isValid(this)) {
if (Object::isValid(this->parent)) {
this->parent->removeChild(this);
} else {
Object *parent = this->parent;
this->parent = nullptr;
this->onRemoveParent(parent);
}
}
}
inline Object *getParent (void) const { return this->parent; }
inline const std::list<Object *> &getChildren (void) const { return this->children; }
virtual void move(float_max_t delta_time, bool collision_detect) final;
virtual void update(float_max_t now, float_max_t delta_time, unsigned tick, bool collision_detect) final;
virtual void alwaysUpdate(float_max_t now, float_max_t delta_time, unsigned tick, bool collision_detect) final;
virtual void draw(bool only_border = false) const final;
inline Shader::Program *getShader (void) const { return this->shader; }
inline virtual void destroy (void) final {
if (Object::isValid(this)) {
this->display = false;
this->collider = nullptr;
Object::marked.insert(this);
}
}
inline void setShader (Shader::Program *program) { this->shader = program; }
inline float_max_t getMinSpeed (void) const { return this->min_speed; }
inline float_max_t getMaxSpeed (void) const { return this->max_speed; }
inline float_max_t getMinAcceleration (void) const { return this->min_acceleration; }
inline float_max_t getMaxAcceleration (void) const { return this->max_acceleration; }
inline void setMinSpeed (float_max_t _min_speed) { this->min_speed = _min_speed; this->setSpeed(this->getSpeed()); }
inline void setMaxSpeed (float_max_t _max_speed) { this->max_speed = _max_speed; this->setSpeed(this->getSpeed()); }
inline void setMinAcceleration (float_max_t _min_acceleration) { this->min_acceleration = _min_acceleration; this->setAcceleration(this->getAcceleration()); }
inline void setMaxAcceleration (float_max_t _max_acceleration) { this->max_acceleration = _max_acceleration; this->setAcceleration(this->getAcceleration()); }
inline float_max_t getMaxForce (void) const { return this->max_force; }
inline void setMaxForce (float_max_t _max_force) { this->max_force = _max_force; }
inline const Spatial::Vec<3> &getPosition (void) const { return this->position; }
inline const Spatial::Quaternion &getOrientation (void) const { return this->orientation; }
inline const Spatial::Vec<3> &getSpeed (void) const { return this->speed; }
inline const Spatial::Vec<3> &getAcceleration (void) const { return this->acceleration; }
inline float_max_t getMass (void) const { return this->mass; }
inline void setPosition (const Spatial::Vec<3> &_position) { this->position = _position; }
inline void setOrientation (const Spatial::Quaternion &_orientation) { this->orientation = _orientation; }
inline void setSpeed (const Spatial::Vec<3> &_speed) { this->speed = _speed.clamped(this->getMinSpeed(), this->getMaxSpeed()); }
inline void setAcceleration (const Spatial::Vec<3> &_acceleration) { this->acceleration = _acceleration.clamped(this->getMinAcceleration(), this->getMaxAcceleration()); }
inline void setMass (float_max_t _mass) { this->mass = _mass; }
inline void applyForce (const Spatial::Vec<3> &_force) { this->setAcceleration(this->getAcceleration() + (_force / this->getMass()).clamped(0.0, this->getMaxForce())); }
inline Mesh *getMesh (void) const { return this->mesh; }
inline Mesh *getCollider (void) const { return this->collider; }
inline void setMesh (Mesh *_mesh) { this->mesh = _mesh; }
inline void setCollider (Mesh *_collider) { this->collider = _collider; }
inline operator bool () const { return Object::isValid(this); }
virtual inline void onCollision (const Object *other, const Spatial::Vec<3> &point) {}
virtual inline void beforeDestroy () {}
virtual inline void afterDestroy () {}
virtual inline void beforeUpdate (float_max_t now, float_max_t delta_time, unsigned tick) {}
virtual inline void afterUpdate (float_max_t now, float_max_t delta_time, unsigned tick) {}
virtual inline void beforeAlwaysUpdate (float_max_t now, float_max_t delta_time, unsigned tick) {}
virtual inline void afterAlwaysUpdate (float_max_t now, float_max_t delta_time, unsigned tick) {}
virtual inline void beforeDraw (bool only_border) const {}
virtual inline void afterDraw (bool only_border) const {}
virtual inline void onAddChild (Object *child) {}
virtual inline void onRemoveChild (Object *child) {}
virtual inline void onSetParent (Object *parent) {}
virtual inline void onRemoveParent (Object *parent) {}
virtual void debugInfo (std::ostream &out, const std::string shift = "") const;
virtual inline std::string getType () const { return "object"; }
};
};
#endif