Dragonfly 4.19
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/// Count of number of modified attribute categories.
33const int ATTR_COUNT = 18;
34
35/// Categories of attributes that indicate modification.
37 ID,
38 ACTIVE,
39 VISIBLE,
40 EVENTS,
41 BOX,
42 POSITION,
43 TYPE,
44 ANIMATION,
45 ALTITUDE,
46 SOLIDNESS,
47 NO_SOFT,
48 SPEED,
49 DIRECTION,
50 ACCELERATION,
51 SHAPE,
52 PATH,
53 FOLLOW_PATH,
54 AVOID_COLLISIONS,
55};
56
57class PathFind; // Forward reference.
58
59class Object {
60
61 public:
62 static int max_id; ///< Monotonically increasing identifier.
63
64 private:
65 int m_id; ///< Unique game engine defined identifier.
66 std::string m_type; ///< Game programmer defined type.
67 bool m_is_active; ///< If false, Object not acted upon.
68 bool m_is_visible; ///< If true, Object gets drawn.
69 Box m_box; ///< Box for sprite boundary & collisions.
70 Vector m_position; ///< Position in game world.
71 Vector m_direction; ///< Direction vector.
72 float m_speed; ///< Object speed in direction.
73 Vector m_acceleration; ///< Acceleration vector.
74 int m_altitude; ///< 0 to MAX_ALTITUDE (higher drawn on top).
75 Solidness m_solidness; ///< Solidness state of Object.
76 Animation m_animation; ///< Animation associated with Object.
77 bool m_no_soft; ///< True if won't move on SOFT Objects.
78 int m_event_count; ///< Number of events of interest.
79 std::string m_event_name[MAX_OBJ_EVENTS]; ///< Names of events interested in.
80 bool m_modified[ATTR_COUNT]; ///< Mod. attrib. since last serialize().
81 Shape m_shape; ///< Basic geometric shape to draw.
82 PathFind *m_p_path_find; ///< Object performing pathfinding.
83 Path m_path; ///< If following path, holds path information.
84 bool m_follow_path; ///< True if following path.
85 bool m_avoid_collisions; ///< True if should avoid collisions with HARD.
86
87 public:
88
89 /// Construct Object.
90 /// Set default parameters and add to game world (WorldManager).
92
93 /// Destroy Object.
94 /// Unregister for any interested events.
95 /// Remove from game world (WorldManager).
96 virtual ~Object();
97
98 /// Set Object id.
99 void setId(int new_id);
100
101 /// Get Object id.
102 int getId() const;
103
104 /// Set type identifier of Object.
105 void setType(std::string new_type);
106
107 /// Get type identifier of Object.
108 std::string getType() const;
109
110 /// Set position of Object.
111 /// Return 0 if ok, else -1.
112 virtual int setPosition(Vector new_position);
113
114 /// Get position of Object.
116
117 /// Handle event (default only handles pathfinding).
118 /// Return 0 if ignored, else 1.
119 virtual int eventHandler(const Event *p_event);
120
121 /// Return True if Object is HARD or SOFT, else false.
122 bool isSolid() const;
123
124 /// Set solidness of Object, with checks for consistency.
125 /// Return 0 if ok, else -1.
126 int setSolidness(Solidness new_solid);
127
128 /// Return solidness of Object.
130
131 /// Set "no soft" setting of Object (true - cannot move onto SOFT Objects).
132 void setNoSoft(bool new_no_soft=true);
133
134 /// Get "no soft" setting of Object (true - cannot move onto SOFT Objects).
135 bool getNoSoft() const;
136
137 /// Set "avoid collisions" (true - avoid collisions with HARD when moving).
138 void setAvoidCollisions(bool new_avoid_collisions=true);
139
140 /// Get "avoid collisions" (true - avoid collisions with HARD when moving).
141 bool getAvoidCollisions() const;
142
143 /// Set altitude of Object, with checks for range [0, MAX_ALTITUDE].
144 /// Return 0 if ok, else -1.
145 int setAltitude(int new_altitude);
146
147 /// Return altitude of Object.
148 int getAltitude() const;
149
150 /// Set speed of Object.
151 void setSpeed(float speed);
152
153 /// Get speed of Object.
154 float getSpeed() const;
155
156 /// Set direction of Object.
157 void setDirection(Vector new_direction);
158
159 /// Get direction of Object.
161
162 /// Set direction and speed of Object.
163 void setVelocity(Vector velocity);
164
165 /// Get velocity of Object based on direction and speed.
167
168 /// Set acceleration of Object.
169 void setAcceleration(Vector new_acceleration);
170
171 /// Get acceleration of Object.
173
174 /// Add acceleration to Object velocity.
175 /// (Typically done once per step by World Manager).
177
178 /// Predict Object position based on speed and direction.
179 /// Return predicted position.
181
182 /// Navigate Object from current position to target position
183 /// using pathfinding.
184 /// Turns on avoid collisions.
185 void pathTo(Vector position);
186
187 /// Navigate Object from current position to current position
188 /// of target Object using pathfinding.
189 /// Turns on avoid collisions.
190 void pathTo(Object *p_o_target);
191
192 /// Set direction of Object towards position.
193 /// (Called when pathfinding).
194 void moveTo(Vector new_position);
195
196 /// If following a path, update path progress.
198
199 /// Set visibility of Object. Objects not visible are not drawn.
200 /// Return 0 if ok, else -1.
201 int setVisible(bool new_visible=true);
202
203 /// Return visibility of Object. Objects not visible are not drawn.
204 bool isVisible() const;
205
206 /// Set activeness of Object. Objects not active are not acted upon
207 /// by engine.
208 /// Return 0 if ok, else -1.
209 int setActive(bool new_active=true);
210
211 /// Return activeness of Object. Objects not active are not acted upon
212 /// by engine.
213 bool isActive() const;
214
215 /// Set bounding box of Object.
216 void setBox(Box new_box);
217
218 /// Get bounding box of Object.
219 Box getBox() const;
220
221 /// Register for interest in event.
222 /// Keeps track of manager and event.
223 /// Return 0 if ok, else -1.
224 int registerInterest(std::string event_type);
225
226 /// Unregister for interest in event.
227 /// Return 0 if ok, else -1.
228 int unregisterInterest(std::string event_type);
229
230 /// Set Sprite for this Object to animate.
231 /// Return 0 if ok, else -1.
232 int setSprite(std::string sprite_label);
233
234 /// Set Animation for this Object to new one.
235 /// If set_box is true, set bounding box to size of associated Sprite.
236 void setAnimation(Animation new_animation, bool set_box=true);
237
238 /// Get Animation for this Object.
240
241 /// Draw Object: Animation and/or Shape.
242 /// Return 0 if ok, else -1.
243 virtual int draw();
244
245 /// Serialize attributes to string. e.g., "id:4,pos-x:2.2,pos-y:0.9...".
246 /// Default ("") is only attributes modified since last serialize().
247 /// Can specify specific attribute(s) to serialize (modified or not).
248 /// separated by commas.
249 /// If attr is "ALL" then serialize all attributes (modified or not).
250 /// Clear m_modified[] array for attributes serialized.
251 virtual std::string serialize(std::string attr="");
252
253 /// Deserialize string to become attributes and apply.
254 /// Return 0 if no errors, else -1.
255 virtual int deserialize(std::string s);
256
257 /// Return true if attribute modified since last serialize.
258 virtual bool isModified(enum ObjectAttribute attribute) const;
259
260 /// Return true if any attribute modified since last serialize.
261 virtual bool isModified() const;
262
263 /// Set drawing shape.
264 void setShape(Shape new_shape);
265
266 /// Get drawing shape.
268
269 /// Set follow_path (note: is also set in pathTo() method).
270 /// If path defined, start moving to first node.
271 void setFollowPath(bool new_follow_path=true);
272
273 /// Returns true if following a path (note: is set in pathTo() method).
274 bool getFollowPath() const;
275
276 /// Set Path (note: is also set in pathTo() method).
277 void setPath(Path new_path);
278
279 /// Get Path (note: is set in pathTo() method).
280 Path getPath() const;
281
282 /// Get object doing pathfinding.
284
285 /// Return base attributes as string.
286 std::string toString() const;
287};
288
289} // end of namespace df
290#endif // __OBJECT_H__
Definition: Animation.h:17
Definition: Box.h:12
Definition: Event.h:15
Definition: Object.h:59
Vector getPosition() const
Get position of Object.
int setAltitude(int new_altitude)
Set altitude of Object, with checks for range [0, MAX_ALTITUDE].
int getAltitude() const
Return altitude of Object.
virtual int deserialize(std::string s)
Deserialize string to become attributes and apply.
int m_altitude
0 to MAX_ALTITUDE (higher drawn on top).
Definition: Object.h:74
void setType(std::string new_type)
Set type identifier of Object.
void setFollowPath(bool new_follow_path=true)
Set follow_path (note: is also set in pathTo() method).
void doPathFollowing()
If following a path, update path progress.
void setSpeed(float speed)
Set speed of Object.
std::string m_type
Game programmer defined type.
Definition: Object.h:66
void setDirection(Vector new_direction)
Set direction of Object.
int setVisible(bool new_visible=true)
Set visibility of Object.
int setSprite(std::string sprite_label)
Set Sprite for this Object to animate.
Vector m_direction
Direction vector.
Definition: Object.h:71
bool m_avoid_collisions
True if should avoid collisions with HARD.
Definition: Object.h:85
int unregisterInterest(std::string event_type)
Unregister for interest in event.
std::string getType() const
Get type identifier of Object.
void setBox(Box new_box)
Set bounding box of Object.
bool isActive() const
Return activeness of Object.
Vector m_acceleration
Acceleration vector.
Definition: Object.h:73
Shape getShape() const
Get drawing shape.
void accelerate()
Add acceleration to Object velocity.
Vector getDirection() const
Get direction of Object.
static int max_id
Monotonically increasing identifier.
Definition: Object.h:62
void setVelocity(Vector velocity)
Set direction and speed of Object.
bool getFollowPath() const
Returns true if following a path (note: is set in pathTo() method).
float m_speed
Object speed in direction.
Definition: Object.h:72
virtual int draw()
Draw Object: Animation and/or Shape.
void setPath(Path new_path)
Set Path (note: is also set in pathTo() method).
bool isSolid() const
Return True if Object is HARD or SOFT, else false.
int setActive(bool new_active=true)
Set activeness of Object.
Solidness m_solidness
Solidness state of Object.
Definition: Object.h:75
Object()
Construct Object.
Animation m_animation
Animation associated with Object.
Definition: Object.h:76
bool m_follow_path
True if following path.
Definition: Object.h:84
void moveTo(Vector new_position)
Set direction of Object towards position.
void setAvoidCollisions(bool new_avoid_collisions=true)
Set "avoid collisions" (true - avoid collisions with HARD when moving).
Shape m_shape
Basic geometric shape to draw.
Definition: Object.h:81
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:65
std::string m_event_name[MAX_OBJ_EVENTS]
Names of events interested in.
Definition: Object.h:79
bool m_is_active
If false, Object not acted upon.
Definition: Object.h:67
PathFind * getPathFind()
Get object doing pathfinding.
virtual bool isModified() const
Return true if any attribute modified since last serialize.
int getId() const
Get Object id.
virtual bool isModified(enum ObjectAttribute attribute) const
Return true if attribute modified since last serialize.
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.
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:83
void setNoSoft(bool new_no_soft=true)
Set "no soft" setting of Object (true - cannot move onto SOFT Objects).
void setAnimation(Animation new_animation, bool set_box=true)
Set Animation for this Object to new one.
virtual std::string serialize(std::string attr="")
Serialize attributes to string.
bool isVisible() const
Return visibility of Object. Objects not visible are not drawn.
bool m_no_soft
True if won't move on SOFT Objects.
Definition: Object.h:77
bool m_is_visible
If true, Object gets drawn.
Definition: Object.h:68
virtual int eventHandler(const Event *p_event)
Handle event (default only handles pathfinding).
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.
void setShape(Shape new_shape)
Set drawing shape.
PathFind * m_p_path_find
Object performing pathfinding.
Definition: Object.h:82
Vector m_position
Position in game world.
Definition: Object.h:70
Vector getAcceleration() const
Get acceleration of Object.
bool m_modified[ATTR_COUNT]
Mod. attrib. since last serialize().
Definition: Object.h:80
Box getBox() const
Get bounding box of Object.
int m_event_count
Number of events of interest.
Definition: Object.h:78
int setSolidness(Solidness new_solid)
Set solidness of Object, with checks for consistency.
void setId(int new_id)
Set Object id.
void setAcceleration(Vector new_acceleration)
Set acceleration of Object.
Vector predictPosition()
Predict Object position based on speed and direction.
Box m_box
Box for sprite boundary & collisions.
Definition: Object.h:69
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 attributes that indicate modification.
Definition: Object.h:36
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
const int ATTR_COUNT
Count of number of modified attribute categories.
Definition: Object.h:33