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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
/*
core/entity.h
This file is part of the Osirion project and is distributed under
the terms of the GNU General Public License version 2.
*/
#ifndef __INCLUDED_CORE_ENTITY_H__
#define __INCLUDED_CORE_ENTITY_H__
namespace core
{
class Entity;
class EntityControlable;
}
#include "core/model.h"
#include "core/player.h"
#include "math/mathlib.h"
#include <iostream>
#include <string>
#include <map>
namespace core
{
/// The base world entity. All gameworld entities must derive from this class.
class Entity
{
public:
/// Entity flags
enum Flags {Static=1, Solid=2, Bright=4};
/// Entity type constants
enum Type {Default=0, Dynamic=1, Controlable=2};
/// Entity shape constants
enum Shape {Diamond=0, Sphere=1, Cube=2, Axis=3};
/// create a new entity and add it to the registry
Entity(unsigned int flags = 0);
/// create an entity froms stream data
Entity(std::istream & is);
/// destroy an entity
virtual ~Entity();
/*----- inspectors ------------------------------------------------ */
/// entity id
inline unsigned int id() const { return entity_id; }
/// module type id
inline unsigned int moduletype() const { return entity_moduletypeid; }
/// core type id
virtual inline unsigned int type() const { return Default; }
/// entity flags
inline unsigned int flags() const { return entity_flags; }
/// entity name (can not contain double qoutes ")
inline std::string const & name() { return entity_name; }
/// entity model name
inline std::string const & modelname() { return entity_modelname; }
/// pointer to the model, is used client-side
inline Model * model() { return entity_model; }
/// dirty flag
inline bool dirty() const { return entity_dirty; }
/// entity location
inline math::Vector3f const & location() const { return entity_location; }
/// direction the entity is facing, in degrees.
inline float direction() const { return entity_direction; }
/// base color of the entity
inline math::Color const & color() const { return entity_color; }
/// base shape of the entity
inline Shape shape() const { return entity_shape; }
/// base radius of the entity
inline float radius() const { return entity_radius; }
/// serialize the entity to a stream
virtual void serialize(std::ostream & os) const;
/// serialize a client-to-server update on a stream
virtual void serialize_client_update(std::ostream & os) const;
/// serialize a server-to-client update on a stream
virtual void serialize_server_update(std::ostream & os) const;
/*----- mutators -------------------------------------------------- */
/// receive a client-to-server update from a stream
virtual void recieve_client_update(std::istream &is);
/// receive a server-to-client update from a stream
virtual void recieve_server_update(std::istream &is);
/// mark the entity as destroyed
inline void die() { entity_destroyed = true; }
/// runs one game frame for the entity
/**
* The default implementation does nothing
*/
virtual void frame(float seconds);
/*----- static ---------------------------------------------------- */
/// the entity registry
static std::map<unsigned int, Entity*> registry;
/// find an entity in the registry
static Entity *find(unsigned int id);
/// remove one entity from the registry and deletes it
static void remove(unsigned int entity_id);
/// list the entity registry
static void list();
/* entity_ variables can be set by the module */
float entity_radius;
std::string entity_name;
std::string entity_modelname;
Model *entity_model;
Shape entity_shape;
math::Vector3f entity_location;
math::Color entity_color;
/*
* A direction of 0 degrees means the entity is looking
* along the positive X-axis. Positive angle is along the negative Z-axis.
*/
float entity_direction;
unsigned int entity_moduletypeid;
unsigned int entity_flags;
bool entity_dirty;
bool entity_created;
bool entity_destroyed;
private:
/// add an entity to the registry
static void add(Entity *ent);
/// add an entity with id to the registry
void add(Entity *ent, unsigned int id);
/// the id is set by add()
unsigned int entity_id;
};
/// an entity that can move around in the game world
class EntityDynamic : public Entity
{
public:
/// create a dynamic entity
EntityDynamic(unsigned int flags = 0);
/// create a dynamic entity from stream data
EntityDynamic(std::istream & is);
virtual ~EntityDynamic();
/*----- inspectors ------------------------------------------------ */
/// core type id
virtual inline unsigned int type() const { return Entity::Dynamic; }
/// current speed of the entity in game units per second
inline float speed() const { return entity_speed; }
/// serialize the entity to a stream
virtual void serialize(std::ostream & os) const;
/// serialize a client-to-server update on a stream
virtual void serialize_client_update(std::ostream & os) const ;
/// serialize a server-to-client update on a stream
virtual void serialize_server_update(std::ostream & os) const;
/*----- mutators -------------------------------------------------- */
/// receive a client-to-server update from a stream
virtual void recieve_client_update(std::istream &is);
/// receive a server-to-client update from a stream
virtual void recieve_server_update(std::istream &is);
/// runs one game frame for the entity
/**
* The default implementation will update the position() of the entity,
* determined by its speed() and direction()
*/
virtual void frame(float seconds);
/// speed of the entity
float entity_speed;
};
/// an entity that can be controlled by a player
class EntityControlable : public EntityDynamic
{
public:
/// create a controlable entity
EntityControlable(Player *player, unsigned int flags = 0);
/// create a controlable entity from stream data
EntityControlable(std::istream & is);
virtual ~EntityControlable();
/*----- inspectors ------------------------------------------------ */
/// core type id
virtual inline unsigned int type() const { return Entity::Controlable; }
/// owner of this entity
inline Player *owner() const { return entity_owner; }
/// thrust
inline float thrust() const { return entity_thrust; }
/// serialize the entity to a stream
virtual void serialize(std::ostream & os) const;
/// serialize a client-to-server update on a stream
virtual void serialize_client_update(std::ostream & os) const;
/// serialize a server-to-client update on a stream
virtual void serialize_server_update(std::ostream & os) const;
/*----- mutators -------------------------------------------------- */
/// receive a client-to-server update from a stream
virtual void recieve_client_update(std::istream &is);
/// receive a server-to-client update from a stream
virtual void recieve_server_update(std::istream &is);
/// set the target thrust
void set_thrust(float t);
/// set the target direction
void set_direction(float t);
/// runs one game frame for the entity
/**
* The default implementation will set direction() and thrust() to the desired targets
* and calls its parent frame() funcion.
*/
virtual void frame(float seconds);
/* entity_ variables can be set by the module */
/// owner of the entity
Player *entity_owner;
/// current thrust
float entity_thrust;
/* target_ variables can be set by the client */
/// target thrust as set by the client
float target_thrust;
/// target direction as set by the client
float target_direction;
};
}
#endif // __INCLUDED_CORE_ENTITY_H__
|