Dragonfly 4.20
A text-based game engine
Object.h
1///
2/// The base game world object
3///
4
5#ifndef __OBJECT_H__
6#define __OBJECT_H__
7
8// System includes.
9#include <string>
10
11// Engine includes.
12#include "Animation.h" ///< Objects (often) have animated sprites.
13#include "Box.h" ///< Objects have a bounding box.
14#include "Event.h" ///< Objects can handle events.
15#include "Path.h" ///< Objects can seek along path.
16#include "PathFind.h" ///< Objects can do pathfinding.
17#include "Shape.h" ///< Objects can have simple drawing shapes.
18#include "Vector.h" ///< Objects need a float location.
19
20namespace df {
21
22/// Max number of events Object can be interested in.
23const int MAX_OBJ_EVENTS = 100;
24
25/// Types of solidness of Object.
27 HARD, ///< Object causes collisions and impedes.
28 SOFT, ///< Object causes collision, but doesn't impede.
29 SPECTRAL, ///< Object doesn't cause collisions.
30};
31
32/// Categories of Object attributes that indicate modification.
33enum class ObjectAttribute : unsigned int {
34 ID = 1 << 0, // 0001
35 ACTIVE = 1 << 1, // 0010
36 VISIBLE = 1 << 2, // 0100
37 EVENTS = 1 << 3, // ...
38 BOX = 1 << 4,
39 POSITION = 1 << 5,
40 TYPE = 1 << 6,
41 ANIMATION = 1 << 7,
42 ALTITUDE = 1 << 8,
43 SOLIDNESS = 1 << 9,
44 NO_SOFT = 1 << 10,
45 SPEED = 1 << 11,
46 DIRECTION = 1 << 12,
47 ACCELERATION = 1 << 13,
48 SHAPE = 1 << 14,
49 PATH = 1 << 15,
50 FOLLOW_PATH = 1 << 16,
51 AVOID_COLLISIONS = 1 << 17,
52};
53const int ObjectAttributeMax = 18;
54
55class PathFind; // Forward reference.
56
57class Object {
58
59 public:
60 static int max_id; ///< Monotonically increasing identifier.
61
62 private:
63 int m_id; ///< Unique game engine defined identifier.
64 std::string m_type; ///< Game programmer defined type.
65 bool m_is_active; ///< If false, Object not acted upon.
66 bool m_is_visible; ///< If true, Object gets drawn.
67 Box m_box; ///< Box for sprite boundary & collisions.
68 Vector m_position; ///< Position in game world.
69 Vector m_direction; ///< Direction vector.
70 float m_speed; ///< Object speed in direction.
71 Vector m_acceleration; ///< Acceleration vector.
72 int m_altitude; ///< 0 to MAX_ALTITUDE (higher drawn on top).
73 Solidness m_solidness; ///< Solidness state of Object.
74 Animation m_animation; ///< Animation associated with Object.
75 bool m_no_soft; ///< True if won't move on SOFT Objects.
76 int m_event_count; ///< Number of events of interest.
77 std::string m_event_name[MAX_OBJ_EVENTS]; ///< Names of events interested in.
78 unsigned int m_modified; ///< Mod. attributes mask since last serialize().
79 Shape m_shape; ///< Basic geometric shape to draw.
80 PathFind *m_p_path_find; ///< Object performing pathfinding.
81 Path m_path; ///< If following path, holds path information.
82 bool m_follow_path; ///< True if following path.
83 bool m_avoid_collisions; ///< True if should avoid collisions with HARD.
84
85 public:
86
87 /// Construct Object.
88 /// Set default parameters and add to game world (WorldManager).
90
91 /// Destroy Object.
92 /// Unregister for any interested events.
93 /// Remove from game world (WorldManager).
94 virtual ~Object();
95
96 /// Set Object id.
97 void setId(int new_id);
98
99 /// Get Object id.
100 int getId() const;
101
102 /// Set type identifier of Object.
103 virtual void setType(std::string new_type);
104
105 /// Get type identifier of Object.
106 std::string getType() const;
107
108 /// Set position of Object.
109 /// Return 0 if ok, else -1.
110 virtual int setPosition(Vector new_position);
111
112 /// Get position of Object.
114
115 /// Handle event (default only handles pathfinding).
116 /// Return 0 if ignored, else 1.
117 virtual int eventHandler(const Event *p_event);
118
119 /// Return True if Object is HARD or SOFT, else false.
120 bool isSolid() const;
121
122 /// Set solidness of Object, with checks for consistency.
123 /// Return 0 if ok, else -1.
124 virtual int setSolidness(Solidness new_solid);
125
126 /// Return solidness of Object.
128
129 /// Set "no soft" setting of Object (true - cannot move onto SOFT Objects).
130 void setNoSoft(bool new_no_soft=true);
131
132 /// Get "no soft" setting of Object (true - cannot move onto SOFT Objects).
133 bool getNoSoft() const;
134
135 /// Set "avoid collisions" (true - avoid collisions with HARD when moving).
136 virtual void setAvoidCollisions(bool new_avoid_collisions=true);
137
138 /// Get "avoid collisions" (true - avoid collisions with HARD when moving).
139 bool getAvoidCollisions() const;
140
141 /// Set altitude of Object, with checks for range [0, MAX_ALTITUDE].
142 /// Return 0 if ok, else -1.
143 virtual int setAltitude(int new_altitude);
144
145 /// Return altitude of Object.
146 int getAltitude() const;
147
148 /// Set speed of Object.
149 void setSpeed(float speed);
150
151 /// Get speed of Object.
152 float getSpeed() const;
153
154 /// Set direction of Object.
155 virtual void setDirection(Vector new_direction);
156
157 /// Get direction of Object.
159
160 /// Set direction and speed of Object.
161 virtual void setVelocity(Vector velocity);
162
163 /// Get velocity of Object based on direction and speed.
165
166 /// Set acceleration of Object.
167 virtual void setAcceleration(Vector new_acceleration);
168
169 /// Get acceleration of Object.
171
172 /// Add acceleration to Object velocity.
173 /// (Typically done once per step by World Manager).
175
176 /// Predict Object position based on speed and direction.
177 /// Return predicted position.
179
180 /// Navigate Object from current position to target position
181 /// using pathfinding.
182 /// Turns on avoid collisions.
183 void pathTo(Vector position);
184
185 /// Navigate Object from current position to current position
186 /// of target Object using pathfinding.
187 /// Turns on avoid collisions.
188 void pathTo(Object *p_o_target);
189
190 /// Set direction of Object towards position.
191 /// (Called when pathfinding).
192 void moveTo(Vector new_position);
193
194 /// If following a path, update path progress.
196
197 /// Set visibility of Object. Objects not visible are not drawn.
198 /// Return 0 if ok, else -1.
199 int setVisible(bool new_visible=true);
200
201 /// Return visibility of Object. Objects not visible are not drawn.
202 virtual bool isVisible() const;
203
204 /// Set activeness of Object. Objects not active are not acted upon
205 /// by engine.
206 /// Return 0 if ok, else -1.
207 virtual int setActive(bool new_active=true);
208
209 /// Return activeness of Object. Objects not active are not acted upon
210 /// by engine.
211 bool isActive() const;
212
213 /// Set bounding box of Object.
214 virtual void setBox(Box new_box);
215
216 /// Get bounding box of Object.
217 Box getBox() const;
218
219 /// Register for interest in event.
220 /// Keeps track of manager and event.
221 /// Return 0 if ok, else -1.
222 int registerInterest(std::string event_type);
223
224 /// Unregister for interest in event.
225 /// Return 0 if ok, else -1.
226 int unregisterInterest(std::string event_type);
227
228 /// Get registered interest event count.
229 int getEventCount() const;
230
231 /// Set Sprite for this Object to animate.
232 /// Return 0 if ok, else -1.
233 virtual int setSprite(std::string sprite_label);
234
235 /// Set Animation for this Object to new one.
236 /// If set_box is true, set bounding box to size of associated Sprite.
237 virtual void setAnimation(Animation new_animation, bool set_box=true);
238
239 /// Get Animation for this Object.
241
242 /// Draw Object: Animation and/or Shape.
243 /// Return 0 if ok, else -1.
244 virtual int draw();
245
246 /// Serialize attributes to stream.
247 /// Can specify individual attribute(s) to force (modified or not).
248 /// Default is only modified attributes.
249 /// Clears modified bits for attributes serialized.
250 /// Return 0 if ok, else -1.
251 virtual int serialize(std::stringstream *p_ss, unsigned int attr=0);
252
253 /// Deserialize stream to attributes and apply.
254 /// p_ss - incoming stream to deserialize.
255 /// p_a - outgoing bitmask of attributes modified (NULL means no outgoing).
256 /// Return 0 if ok, else -1.
257 virtual int deserialize(std::stringstream *p_ss, unsigned int *p_a=NULL);
258
259 /// Return true if attribute modified since last serialize.
260 bool isModified(enum ObjectAttribute attribute) const;
261
262 /// Return true if any attribute modified since last serialize.
263 bool isModified() const;
264
265 /// Set modified attribute mask.
266 virtual void setModified(unsigned int new_modified);
267
268 /// Return modified attribute mask.
269 virtual unsigned int getModified() const;
270
271 /// Set drawing shape.
272 virtual void setShape(Shape new_shape);
273
274 /// Get drawing shape.
276
277 /// Set follow_path (note: is also set in pathTo() method).
278 /// If path defined, start moving to first node.
279 virtual void setFollowPath(bool new_follow_path=true);
280
281 /// Returns true if following a path (note: is set in pathTo() method).
282 bool getFollowPath() const;
283
284 /// Set Path (note: is also set in pathTo() method).
285 void setPath(Path new_path);
286
287 /// Get Path (note: is set in pathTo() method).
288 Path getPath() const;
289
290 /// Get object doing pathfinding.
292
293 /// Return base attributes as string.
294 std::string toString() const;
295
296 /// Compare two Objects.
297 bool operator==(const Object &o);
298 bool operator!=(const Object &o);
299};
300
301} // end of namespace df
302#endif // __OBJECT_H__
Definition: Animation.h:17
Definition: Box.h:16
Definition: Event.h:15
Definition: Object.h:57
virtual int deserialize(std::stringstream *p_ss, unsigned int *p_a=NULL)
Deserialize stream to attributes and apply.
Vector getPosition() const
Get position of Object.
unsigned int m_modified
Mod. attributes mask since last serialize().
Definition: Object.h:78
bool operator==(const Object &o)
Compare two Objects.
int getAltitude() const
Return altitude of Object.
int m_altitude
0 to MAX_ALTITUDE (higher drawn on top).
Definition: Object.h:72
virtual bool isVisible() const
Return visibility of Object. Objects not visible are not drawn.
void doPathFollowing()
If following a path, update path progress.
bool isModified() const
Return true if any attribute modified since last serialize.
void setSpeed(float speed)
Set speed of Object.
virtual int setActive(bool new_active=true)
Set activeness of Object.
std::string m_type
Game programmer defined type.
Definition: Object.h:64
int setVisible(bool new_visible=true)
Set visibility of Object.
Vector m_direction
Direction vector.
Definition: Object.h:69
bool m_avoid_collisions
True if should avoid collisions with HARD.
Definition: Object.h:83
int unregisterInterest(std::string event_type)
Unregister for interest in event.
std::string getType() const
Get type identifier of Object.
bool isActive() const
Return activeness of Object.
Vector m_acceleration
Acceleration vector.
Definition: Object.h:71
Shape getShape() const
Get drawing shape.
void accelerate()
Add acceleration to Object velocity.
Vector getDirection() const
Get direction of Object.
virtual void setDirection(Vector new_direction)
Set direction of Object.
static int max_id
Monotonically increasing identifier.
Definition: Object.h:60
bool getFollowPath() const
Returns true if following a path (note: is set in pathTo() method).
virtual void setAnimation(Animation new_animation, bool set_box=true)
Set Animation for this Object to new one.
float m_speed
Object speed in direction.
Definition: Object.h:70
virtual int draw()
Draw Object: Animation and/or Shape.
virtual void setBox(Box new_box)
Set bounding box of Object.
void setPath(Path new_path)
Set Path (note: is also set in pathTo() method).
virtual int setSprite(std::string sprite_label)
Set Sprite for this Object to animate.
virtual int serialize(std::stringstream *p_ss, unsigned int attr=0)
Serialize attributes to stream.
bool isSolid() const
Return True if Object is HARD or SOFT, else false.
bool isModified(enum ObjectAttribute attribute) const
Return true if attribute modified since last serialize.
Solidness m_solidness
Solidness state of Object.
Definition: Object.h:73
Object()
Construct Object.
Animation m_animation
Animation associated with Object.
Definition: Object.h:74
bool m_follow_path
True if following path.
Definition: Object.h:82
virtual int setSolidness(Solidness new_solid)
Set solidness of Object, with checks for consistency.
void moveTo(Vector new_position)
Set direction of Object towards position.
Shape m_shape
Basic geometric shape to draw.
Definition: Object.h:79
virtual ~Object()
Destroy Object.
bool getAvoidCollisions() const
Get "avoid collisions" (true - avoid collisions with HARD when moving).
int m_id
Unique game engine defined identifier.
Definition: Object.h:63
virtual void setAvoidCollisions(bool new_avoid_collisions=true)
Set "avoid collisions" (true - avoid collisions with HARD when moving).
virtual void setFollowPath(bool new_follow_path=true)
Set follow_path (note: is also set in pathTo() method).
std::string m_event_name[MAX_OBJ_EVENTS]
Names of events interested in.
Definition: Object.h:77
bool m_is_active
If false, Object not acted upon.
Definition: Object.h:65
int getEventCount() const
Get registered interest event count.
virtual void setShape(Shape new_shape)
Set drawing shape.
PathFind * getPathFind()
Get object doing pathfinding.
int getId() const
Get Object id.
float getSpeed() const
Get speed of Object.
void pathTo(Vector position)
Navigate Object from current position to target position using pathfinding.
Animation getAnimation() const
Get Animation for this Object.
Vector getVelocity() const
Get velocity of Object based on direction and speed.
virtual int setAltitude(int new_altitude)
Set altitude of Object, with checks for range [0, MAX_ALTITUDE].
bool getNoSoft() const
Get "no soft" setting of Object (true - cannot move onto SOFT Objects).
Path getPath() const
Get Path (note: is set in pathTo() method).
Solidness getSolidness() const
Return solidness of Object.
virtual int setPosition(Vector new_position)
Set position of Object.
Path m_path
If following path, holds path information.
Definition: Object.h:81
virtual unsigned int getModified() const
Return modified attribute mask.
void setNoSoft(bool new_no_soft=true)
Set "no soft" setting of Object (true - cannot move onto SOFT Objects).
bool m_no_soft
True if won't move on SOFT Objects.
Definition: Object.h:75
bool m_is_visible
If true, Object gets drawn.
Definition: Object.h:66
virtual int eventHandler(const Event *p_event)
Handle event (default only handles pathfinding).
virtual void setType(std::string new_type)
Set type identifier of Object.
void pathTo(Object *p_o_target)
Navigate Object from current position to current position of target Object using pathfinding.
int registerInterest(std::string event_type)
Register for interest in event.
std::string toString() const
Return base attributes as string.
PathFind * m_p_path_find
Object performing pathfinding.
Definition: Object.h:80
Vector m_position
Position in game world.
Definition: Object.h:68
Vector getAcceleration() const
Get acceleration of Object.
virtual void setVelocity(Vector velocity)
Set direction and speed of Object.
Box getBox() const
Get bounding box of Object.
virtual void setAcceleration(Vector new_acceleration)
Set acceleration of Object.
int m_event_count
Number of events of interest.
Definition: Object.h:76
virtual void setModified(unsigned int new_modified)
Set modified attribute mask.
void setId(int new_id)
Set Object id.
Vector predictPosition()
Predict Object position based on speed and direction.
Box m_box
Box for sprite boundary & collisions.
Definition: Object.h:67
Definition: PathFind.h:31
Definition: Path.h:17
Definition: Shape.h:24
Definition: Vector.h:12
An animation for a sprite.
Definition: Animation.h:15
ObjectAttribute
Categories of Object attributes that indicate modification.
Definition: Object.h:33
const int MAX_OBJ_EVENTS
Max number of events Object can be interested in.
Definition: Object.h:23
Solidness
Types of solidness of Object.
Definition: Object.h:26
@ SOFT
Object causes collision, but doesn't impede.
Definition: Object.h:28
@ HARD
Object causes collisions and impedes.
Definition: Object.h:27
@ SPECTRAL
Object doesn't cause collisions.
Definition: Object.h:29