Dragonfly  4.3
A text-based game engine
WorldManager.h
1 ///
2 /// The game world manager
3 ///
4 
5 #ifndef __WORLD_MANAGER_H__
6 #define __WORLD_MANAGER_H__
7 
8 #include "Box.h"
9 #include "Circle.h"
10 #include "Line.h"
11 #include "Manager.h"
12 #include "ObjectList.h"
13 #include "Vector.h"
14 #include "SceneGraph.h"
15 
16 // Two-letter acronym for easier access to manager.
17 #define WM df::WorldManager::getInstance()
18 
19 namespace df {
20 
21 class WorldManager : public Manager {
22 
23  private:
24  WorldManager(); ///< Private since a singleton.
25  WorldManager (WorldManager const&); ///< Don't allow copy.
26  void operator=(WorldManager const&); ///< Don't allow assignment.
27  SceneGraph m_scene_graph; ///< Storage for all Objects, game and view.
28  ObjectList m_deletions; ///< List of all Objects to delete.
29  Object *m_p_view_following; ///< Object view is following.
30  Box m_boundary; ///< World boundary.
31  Box m_view; ///< Player view of game world.
32  Vector m_view_slack; ///< "Slack" for view following.
33 
34  public:
35  /// Get the one and only instance of the WorldManager.
36  static WorldManager &getInstance();
37 
38  /// Startup game world (initialize everything to empty).
39  /// Return 0.
40  int startUp();
41 
42  /// Shutdown game world (delete all game world Objects).
43  void shutDown();
44 
45  /// Accept all user-defined events (returns true).
46  /// Return false for other engine events.
47  bool isValid(std::string event_type) const;
48 
49  /// Add Object to world.
50  /// Return 0 if ok, else -1.
51  int insertObject(Object *p_o);
52 
53  /// Remove Object from world.
54  /// Return 0 if ok, else -1.
55  int removeObject(Object *p_o);
56 
57  /// Return list of all Objects in world.
58  /// If inactive is true, include inactive Objects.
59  ObjectList getAllObjects(bool inactive=false) const;
60 
61  /// Indicate Object is to be deleted at end of current game loop.
62  /// Return 0 if ok, else -1.
63  int markForDelete(Object *p_o);
64 
65  /// Indicate Objects in list are to be deleted at end of current game loop.
66  /// Return 0 if ok, else -1.
67  int markForDelete(ObjectList obj_list);
68 
69  /// Update world.
70  /// Update positions of Objects based on their velocities.
71  /// Lastly, delete Objects marked for deletion.
72  void update();
73 
74  /// Draw all Objects in view.
75  /// Draw bottom up, from 0 to MAX_ALTITUDE.
76  void draw();
77 
78  /// Move Object.
79  /// If collision with solid, send collision events.
80  /// If no collision with solid, move ok else don't move Object.
81  /// If Object is Spectral, move ok.
82  /// If move ok, adjust screen if following this Object.
83  /// If moved from inside world boundary to outside, generate EventOut.
84  /// Return 0 if move ok, else -1 if collision with solid.
85  int moveObject(Object *p_o, Vector where);
86 
87  /// Return list of Objects collided with at position 'where'.
88  /// Collisions only with solid Objects.
89  /// Does not consider if p_o is solid or not.
90  ObjectList isCollision(const Object *p_o, Vector where) const;
91 
92  /// Return Object with indicated id.
93  /// NULL if Object is not found.
94  Object *objectWithId(int id) const;
95 
96  /// Return list of Objects matching type.
97  /// List is empty if none found.
98  ObjectList objectsOfType(std::string type) const;
99 
100  /// Return list of all Objects at position 'where'.
101  /// Does include bounding boxes. Return empty list if none found.
102  ObjectList objectsAtPosition(Vector where) const;
103 
104  /// Return list of all Objects in Box.
105  /// Does include bounding boxes. Return empty list if none found.
106  ObjectList objectsInBox(Box box) const;
107 
108  /// Return a list of all Objects on line from point1 to point2.
109  /// Does include bounding boxes. Return empty list if none found.
110  ObjectList objectsOnLine(Line line) const;
111 
112  /// Return a list of all Objects in circle.
113  /// Does include bounding boxes. Return empty list if none found.
114  ObjectList objectsInCircle(Circle circle) const;
115 
116  /// Get game world boundary.
117  Box getBoundary() const;
118 
119  /// Set game world boundary.
120  void setBoundary(Box new_boundary);
121 
122  /// Get player view of game world.
123  Box getView() const;
124 
125  /// Set player view of game world.
126  void setView(Box new_view);
127 
128  /// Get player "slack" for view following.
129  Vector getViewSlack() const;
130 
131  /// Set player "slack" for view following.
132  void setViewSlack(Vector new_slack);
133 
134  /// Set view to center screen on Object.
135  /// Set to NULL to stop following.
136  /// If Object not legit, return -1 else return 0.
137  int setViewFollowing(Object *p_new_view_following);
138 
139  /// Get Object view is following (NULL if none).
140  Object *getViewFollowing() const;
141 
142  /// Set view to center screen on position view_pos.
143  /// View edge will not go beyond world boundary.
144  void setViewPosition(Vector view_pos);
145 
146  /// Return reference to SceneGraph.
147  SceneGraph &getSceneGraph() const;
148 };
149 
150 } // end of namespace df
151 #endif // __WORLD_MANAGER_H__
ObjectList getAllObjects(bool inactive=false) const
Return list of all Objects in world.
Definition: Line.h:12
Definition: Circle.h:12
Definition: SceneGraph.h:15
void setViewSlack(Vector new_slack)
Set player "slack" for view following.
Definition: ObjectList.h:15
void operator=(WorldManager const &)
Don&#39;t allow assignment.
Box m_boundary
World boundary.
Definition: WorldManager.h:30
int setViewFollowing(Object *p_new_view_following)
Set view to center screen on Object.
int removeObject(Object *p_o)
Remove Object from world.
WorldManager()
Private since a singleton.
Vector m_view_slack
"Slack" for view following.
Definition: WorldManager.h:32
A 2-d bounding box.
Definition: Box.h:10
int startUp()
Startup game world (initialize everything to empty).
void draw()
Draw all Objects in view.
bool isValid(std::string event_type) const
Accept all user-defined events (returns true).
void update()
Update world.
void setBoundary(Box new_boundary)
Set game world boundary.
Box getBoundary() const
Get game world boundary.
Object * m_p_view_following
Object view is following.
Definition: WorldManager.h:29
Object * objectWithId(int id) const
Return Object with indicated id.
Box m_view
Player view of game world.
Definition: WorldManager.h:31
int moveObject(Object *p_o, Vector where)
Move Object.
SceneGraph m_scene_graph
Storage for all Objects, game and view.
Definition: WorldManager.h:27
Definition: WorldManager.h:21
SceneGraph & getSceneGraph() const
Return reference to SceneGraph.
Definition: Object.h:59
ObjectList objectsInCircle(Circle circle) const
Return a list of all Objects in circle.
ObjectList objectsOfType(std::string type) const
Return list of Objects matching type.
int markForDelete(Object *p_o)
Indicate Object is to be deleted at end of current game loop.
ObjectList isCollision(const Object *p_o, Vector where) const
Return list of Objects collided with at position &#39;where&#39;.
Box getView() const
Get player view of game world.
ObjectList m_deletions
List of all Objects to delete.
Definition: WorldManager.h:28
Vector getViewSlack() const
Get player "slack" for view following.
Definition: Box.h:12
int insertObject(Object *p_o)
Add Object to world.
void shutDown()
Shutdown game world (delete all game world Objects).
void setView(Box new_view)
Set player view of game world.
static WorldManager & getInstance()
Get the one and only instance of the WorldManager.
Object * getViewFollowing() const
Get Object view is following (NULL if none).
ObjectList objectsAtPosition(Vector where) const
Return list of all Objects at position &#39;where&#39;.
Definition: Manager.h:20
ObjectList objectsInBox(Box box) const
Return list of all Objects in Box.
ObjectList objectsOnLine(Line line) const
Return a list of all Objects on line from point1 to point2.
void setViewPosition(Vector view_pos)
Set view to center screen on position view_pos.
Definition: Vector.h:10