Dragonfly  4.3
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 "Box.h" ///< Objects have a bounding box.
13 #include "Event.h" ///< Objects can handle events.
14 #include "Path.h" ///< Objects can seek along path.
15 #include "PathFind.h" ///< Objects can do pathfinding.
16 #include "Shape.h" ///< Objects can have simple drawing shapes.
17 #include "Sprite.h" ///< Objects (often) have sprites.
18 #include "Vector.h" ///< Objects need a float location.
19 
20 namespace df {
21 
22 /// Max number of events Object can be interested in.
23 const int MAX_OBJ_EVENTS = 100;
24 
25 /// Types of solidness of Object.
26 enum Solidness {
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.
33 const 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  SPRITE,
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 
57 class PathFind; // Forward reference.
58 
59 class Object {
60 
61  private:
62  int m_id; ///< Unique game engine defined identifier.
63  std::string m_type; ///< Game-programmer defined identification.
64  bool m_is_active; ///< If false, Object not acted upon.
65  bool m_is_visible; ///< If true, Object gets drawn.
66  Box m_box; ///< Box for sprite boundary & collisions.
67  Vector m_position; ///< Position in game world.
68  Vector m_direction; ///< Direction vector.
69  float m_speed; ///< Object speed in direction.
70  Vector m_acceleration; ///< Acceleration vector.
71  std::string sprite_name; ///< Sprite name in ResourceManager.
72  Sprite *m_p_sprite; ///< Sprite associated with Object.
73  bool m_sprite_center; ///< True if sprite centered on position.
74  char m_sprite_transparency; ///< Sprite transparent character (0 if none).
75  int m_sprite_index; ///< Current index frame for sprite.
76  int m_sprite_slowdown; ///< Slowdown rate (1 = no slowdown, 0 = stop).
77  int m_sprite_slowdown_count; ///< Slowdown counter.
78  int m_altitude; ///< 0 to MAX_ALTITUDE (higher drawn on top).
79  Solidness m_solidness; ///< Solidness state of Object.
80  bool m_no_soft; ///< True if won't move on SOFT Objects.
81  int m_event_count; ///< Number of events of interest.
82  std::string m_event_name[MAX_OBJ_EVENTS]; ///< Names of events interested in.
83  bool m_modified[ATTR_COUNT]; ///< Mod. attrib. since last serialize().
84  Shape m_shape; ///< Basic geometric shape to draw.
85  PathFind *m_p_path_find; ///< Object performing pathfinding.
86  Path m_path; ///< If following path, holds path information.
87  bool m_follow_path; ///< True if following path.
88  bool m_avoid_collisions; ///< True if should avoid collisions with HARD.
89 
90  public:
91 
92  /// Construct Object.
93  /// Set default parameters and add to game world (WorldManager).
94  Object();
95 
96  /// Destroy Object.
97  /// Unregister for any interested events.
98  /// Remove from game world (WorldManager).
99  virtual ~Object();
100 
101  /// Set Object id.
102  void setId(int new_id);
103 
104  /// Get Object id.
105  int getId() const;
106 
107  /// Set type identifier of Object.
108  void setType(std::string new_type);
109 
110  /// Get type identifier of Object.
111  std::string getType() const;
112 
113  /// Set position of Object.
114  virtual void setPosition(Vector new_position);
115 
116  /// Get position of Object.
117  Vector getPosition() const;
118 
119  /// Handle event (default only handles pathfinding).
120  /// Return 0 if ignored, else 1.
121  virtual int eventHandler(const Event *p_event);
122 
123  /// Return True if Object is HARD or SOFT, else false.
124  bool isSolid() const;
125 
126  /// Set solidness of Object, with checks for consistency.
127  /// Return 0 if ok, else -1.
128  int setSolidness(Solidness new_solid);
129 
130  /// Return solidness of Object.
131  Solidness getSolidness() const;
132 
133  /// Set "no soft" setting of Object (true - cannot move onto SOFT Objects).
134  void setNoSoft(bool new_no_soft=true);
135 
136  /// Get "no soft" setting of Object (true - cannot move onto SOFT Objects).
137  bool getNoSoft() const;
138 
139  /// Set "avoid collisions" (true - avoid collisions with HARD when moving).
140  void setAvoidCollisions(bool new_avoid_collisions=true);
141 
142  /// Get "avoid collisions" (true - avoid collisions with HARD when moving).
143  bool getAvoidCollisions() const;
144 
145  /// Set altitude of Object, with checks for range [0, MAX_ALTITUDE].
146  /// Return 0 if ok, else -1.
147  int setAltitude(int new_altitude);
148 
149  /// Return altitude of Object.
150  int getAltitude() const;
151 
152  /// Set speed of Object.
153  void setSpeed(float speed);
154 
155  /// Get speed of Object.
156  float getSpeed() const;
157 
158  /// Set direction of Object.
159  void setDirection(Vector new_direction);
160 
161  /// Get direction of Object.
162  Vector getDirection() const;
163 
164  /// Set direction and speed of Object.
165  void setVelocity(Vector velocity);
166 
167  /// Get velocity of Object based on direction and speed.
168  Vector getVelocity() const;
169 
170  /// Set acceleration of Object.
171  void setAcceleration(Vector new_acceleration);
172 
173  /// Get acceleration of Object.
174  Vector getAcceleration() const;
175 
176  /// Add acceleration to Object velocity.
177  /// (Typically done once per step by World Manager).
178  void accelerate();
179 
180  /// Predict Object position based on speed and direction.
181  /// Return predicted position.
183 
184  /// Navigate Object from current position to target position
185  /// using pathfinding.
186  /// Turns on avoid collisions.
187  void pathTo(Vector position);
188 
189  /// Navigate Object from current position to current position
190  /// of target Object using pathfinding.
191  /// Turns on avoid collisions.
192  void pathTo(Object *p_o_target);
193 
194  /// Set direction of Object towards position.
195  /// (Called when pathfinding).
196  void moveTo(Vector position);
197 
198  /// If following a path, update path progress.
199  void doPathFollowing();
200 
201  /// Set visibility of Object. Objects not visible are not drawn.
202  /// Return 0 if ok, else -1.
203  int setVisible(bool visible=true);
204 
205  /// Return visibility of Object. Objects not visible are not drawn.
206  bool isVisible() const;
207 
208  /// Set activeness of Object. Objects not active are not acted upon
209  /// by engine.
210  /// Return 0 if ok, else -1.
211  int setActive(bool active=true);
212 
213  /// Return activeness of Object. Objects not active are not acted upon
214  /// by engine.
215  bool isActive() const;
216 
217  /// Set bounding box of Object.
218  void setBox(Box new_box);
219 
220  /// Get bounding box of Object.
221  Box getBox() const;
222 
223  /// Register for interest in event.
224  /// Keeps track of manager and event.
225  /// Return 0 if ok, else -1.
226  int registerInterest(std::string event_type);
227 
228  /// Unregister for interest in event.
229  /// Return 0 if ok, else -1.
230  int unregisterInterest(std::string event_type);
231 
232  /// Draw single Sprite Frame.
233  /// Drawing accounts for centering & slowdown, and advances Sprite Frame.
234  virtual void draw();
235 
236  /// Set Sprite name (in ResourceManager) for Object.
237  void setSpriteName(std::string new_name);
238 
239  /// Get Sprite name (in ResourceManager) for Object.
240  std::string getSpriteName() const;
241 
242  /// Set Object Sprite to new one.
243  /// If set_box is true, set bounding box to size of Sprite.
244  /// Set sprite index to 0 (first frame).
245  void setSprite(Sprite *p_new_sprite, bool set_box=true);
246 
247  /// Return pointer to Sprite associated with this Object.
248  Sprite *getSprite() const;
249 
250  /// Set Sprite to be centered at Object position.
251  /// Adjust bounding box of Sprite.
252  void setCentered(bool centered=true);
253 
254  /// Indicates if Sprite is centered at Object position.
255  bool isCentered() const;
256 
257  /// Set Sprite transparency character (0 means none).
258  void setTransparency(char transparent=' ');
259 
260  /// Get Sprite transparency character (0 means none).
261  char getTransparency() const;
262 
263  /// Set index of current Sprite frame to be displayed.
264  void setSpriteIndex(int new_sprite_index);
265 
266  /// Return index of current Sprite frame to be displayed.
267  int getSpriteIndex() const;
268 
269  /// Slows down sprite animations.
270  /// Slowdown value in multiples of GameManager frame time.
271  void setSpriteSlowdown(int new_sprite_slowdown);
272  int getSpriteSlowdown() const;
273  void setSpriteSlowdownCount(int new_sprite_slowdown_count);
274  int getSpriteSlowdownCount() const;
275 
276  /// Serialize attributes to single string.
277  /// e.g., "id:110,is_active:true, ...
278  /// Only modified attributes are serialized (unless all is true).
279  /// Clear modified[] array.
280  virtual std::string serialize(bool all=false);
281 
282  /// Deserialize string to become attributes.
283  /// Return 0 if no errors, else -1.
284  virtual int deserialize(std::string s);
285 
286  /// Return true if attribute modified since last serialize.
287  virtual bool isModified(enum ObjectAttribute attribute) const;
288 
289  /// Return true if any attribute modified since last serialize.
290  virtual bool isModified() const;
291 
292  /// Set drawing shape.
293  void setShape(Shape new_shape);
294 
295  /// Get drawing shape.
296  Shape getShape() const;
297 
298  /// Set follow_path (note: is also set in pathTo() method).
299  /// If path defined, start moving to first node.
300  void setFollowPath(bool new_follow_path=true);
301 
302  /// Returns true if following a path (note: is set in pathTo() method).
303  bool getFollowPath() const;
304 
305  /// Set Path (note: is also set in pathTo() method).
306  void setPath(Path new_path);
307 
308  /// Get Path (note: is set in pathTo() method).
309  Path getPath() const;
310 
311  /// Get object doing pathfinding.
313 };
314 
315 } // end of namespace df
316 #endif // __OBJECT_H__
Definition: Path.h:16
Object()
Construct Object.
char getTransparency() const
Get Sprite transparency character (0 means none).
Solidness
Types of solidness of Object.
Definition: Object.h:26
void setBox(Box new_box)
Set bounding box of Object.
Path m_path
If following path, holds path information.
Definition: Object.h:86
void setSpriteIndex(int new_sprite_index)
Set index of current Sprite frame to be displayed.
Path getPath() const
Get Path (note: is set in pathTo() method).
Definition: Shape.h:24
Object causes collision, but doesn&#39;t impede.
Definition: Object.h:28
Vector predictPosition()
Predict Object position based on speed and direction.
Object doesn&#39;t cause collisions.
Definition: Object.h:29
Vector getAcceleration() const
Get acceleration of Object.
Sprite * getSprite() const
Return pointer to Sprite associated with this Object.
virtual void draw()
Draw single Sprite Frame.
Vector getDirection() const
Get direction of Object.
void setType(std::string new_type)
Set type identifier of Object.
bool m_follow_path
True if following path.
Definition: Object.h:87
virtual int eventHandler(const Event *p_event)
Handle event (default only handles pathfinding).
bool m_no_soft
True if won&#39;t move on SOFT Objects.
Definition: Object.h:80
int setActive(bool active=true)
Set activeness of Object.
bool isSolid() const
Return True if Object is HARD or SOFT, else false.
void pathTo(Vector position)
Navigate Object from current position to target position using pathfinding.
Vector m_direction
Direction vector.
Definition: Object.h:68
bool getNoSoft() const
Get "no soft" setting of Object (true - cannot move onto SOFT Objects).
bool m_is_visible
If true, Object gets drawn.
Definition: Object.h:65
PathFind * m_p_path_find
Object performing pathfinding.
Definition: Object.h:85
void setSpeed(float speed)
Set speed of Object.
void setSpriteSlowdown(int new_sprite_slowdown)
Slows down sprite animations.
void setPath(Path new_path)
Set Path (note: is also set in pathTo() method).
std::string getSpriteName() const
Get Sprite name (in ResourceManager) for Object.
Definition: Event.h:15
ObjectAttribute
Categories of attributes that indicate modification.
Definition: Object.h:36
A 2-d bounding box.
Definition: Box.h:10
void setShape(Shape new_shape)
Set drawing shape.
virtual void setPosition(Vector new_position)
Set position of Object.
const int MAX_OBJ_EVENTS
Max number of events Object can be interested in.
Definition: Object.h:23
bool m_avoid_collisions
True if should avoid collisions with HARD.
Definition: Object.h:88
PathFind * getPathFind()
Get object doing pathfinding.
void setId(int new_id)
Set Object id.
int unregisterInterest(std::string event_type)
Unregister for interest in event.
Definition: PathFind.h:29
Box m_box
Box for sprite boundary & collisions.
Definition: Object.h:66
std::string sprite_name
Sprite name in ResourceManager.
Definition: Object.h:71
int m_id
Unique game engine defined identifier.
Definition: Object.h:62
Object causes collisions and impedes.
Definition: Object.h:27
int m_event_count
Number of events of interest.
Definition: Object.h:81
Sprite * m_p_sprite
Sprite associated with Object.
Definition: Object.h:72
float m_speed
Object speed in direction.
Definition: Object.h:69
virtual bool isModified() const
Return true if any attribute modified since last serialize.
bool getFollowPath() const
Returns true if following a path (note: is set in pathTo() method).
Vector getPosition() const
Get position of Object.
int getAltitude() const
Return altitude of Object.
int setAltitude(int new_altitude)
Set altitude of Object, with checks for range [0, MAX_ALTITUDE].
std::string m_event_name[MAX_OBJ_EVENTS]
Names of events interested in.
Definition: Object.h:82
virtual ~Object()
Destroy Object.
void setAvoidCollisions(bool new_avoid_collisions=true)
Set "avoid collisions" (true - avoid collisions with HARD when moving).
void setDirection(Vector new_direction)
Set direction of Object.
Solidness m_solidness
Solidness state of Object.
Definition: Object.h:79
Definition: Object.h:59
void setVelocity(Vector velocity)
Set direction and speed of Object.
void doPathFollowing()
If following a path, update path progress.
int setSolidness(Solidness new_solid)
Set solidness of Object, with checks for consistency.
int m_sprite_slowdown_count
Slowdown counter.
Definition: Object.h:77
void setFollowPath(bool new_follow_path=true)
Set follow_path (note: is also set in pathTo() method).
virtual int deserialize(std::string s)
Deserialize string to become attributes.
float getSpeed() const
Get speed of Object.
bool isActive() const
Return activeness of Object.
void accelerate()
Add acceleration to Object velocity.
bool m_modified[ATTR_COUNT]
Mod. attrib. since last serialize().
Definition: Object.h:83
void setSprite(Sprite *p_new_sprite, bool set_box=true)
Set Object Sprite to new one.
Definition: Sprite.h:17
void moveTo(Vector position)
Set direction of Object towards position.
void setAcceleration(Vector new_acceleration)
Set acceleration of Object.
int m_sprite_index
Current index frame for sprite.
Definition: Object.h:75
void setCentered(bool centered=true)
Set Sprite to be centered at Object position.
Definition: Box.h:12
bool getAvoidCollisions() const
Get "avoid collisions" (true - avoid collisions with HARD when moving).
const int ATTR_COUNT
Count of number of modified attribute categories.
Definition: Object.h:33
Shape getShape() const
Get drawing shape.
int getSpriteIndex() const
Return index of current Sprite frame to be displayed.
bool m_is_active
If false, Object not acted upon.
Definition: Object.h:64
Vector m_position
Position in game world.
Definition: Object.h:67
bool m_sprite_center
True if sprite centered on position.
Definition: Object.h:73
void setTransparency(char transparent=' ')
Set Sprite transparency character (0 means none).
void setNoSoft(bool new_no_soft=true)
Set "no soft" setting of Object (true - cannot move onto SOFT Objects).
Shape m_shape
Basic geometric shape to draw.
Definition: Object.h:84
int setVisible(bool visible=true)
Set visibility of Object.
int m_altitude
0 to MAX_ALTITUDE (higher drawn on top).
Definition: Object.h:78
int m_sprite_slowdown
Slowdown rate (1 = no slowdown, 0 = stop).
Definition: Object.h:76
Vector m_acceleration
Acceleration vector.
Definition: Object.h:70
Box getBox() const
Get bounding box of Object.
int registerInterest(std::string event_type)
Register for interest in event.
bool isCentered() const
Indicates if Sprite is centered at Object position.
virtual std::string serialize(bool all=false)
Serialize attributes to single string.
std::string m_type
Game-programmer defined identification.
Definition: Object.h:63
int getId() const
Get Object id.
char m_sprite_transparency
Sprite transparent character (0 if none).
Definition: Object.h:74
Definition: Vector.h:10
Solidness getSolidness() const
Return solidness of Object.
Vector getVelocity() const
Get velocity of Object based on direction and speed.
void setSpriteName(std::string new_name)
Set Sprite name (in ResourceManager) for Object.
bool isVisible() const
Return visibility of Object. Objects not visible are not drawn.
std::string getType() const
Get type identifier of Object.