Dragonfly 4.20
A text-based game engine
Grid.h
1//
2// Grid.h - A grid class for game objects.
3//
4
5#ifndef __GRID_H__
6#define __GRID_H__
7
8// System includes.
9#include <tuple>
10#include <set>
11
12// Engine includes.
13#include "ObjectList.h"
14
15namespace df {
16
17const int GRID_WIDTH_DEFAULT = 20;
18const int GRID_HEIGHT_DEFAULT = 8;
19
20typedef std::tuple<int, int> Cell;
21
22class Grid {
23
24 private:
25 ObjectList **m_p_grid; ///< The grid.
26 ObjectList m_out_of_bounds; ///< All out of bounds objects.
27 int m_num_cols; ///< Number of columns in grid.
28 int m_num_rows; ///< Number of rows in grid.
29 int m_width; ///< Grid cell width.
30 int m_height; ///< Grid cell height.
31
32 public:
33
34 /// Constructor.
36
37 /// Destructor.
39
40 /// Set dimensions. Deletes and re-creates grid.
41 void setDimensions(int new_width, int new_height);
42
43 /// Get height.
44 int getHeight() const;
45
46 /// Get width.
47 int getWidth() const;
48
49 /// Insert Object into grid.
50 /// Return 0 if ok, else -1.
52
53 /// Remove Object from grid.
54 /// Return 0 if ok, else -1.
56
57 /// Return Cell (x,y) for given location.
58 Cell positionToCell(Vector where) const;
59
60 /// Return list of Cells for given Object at given location.
61 /// Out of bounds cell is (-1, -1).
62 std::set<Cell> positionToCells(Object *p_o, Vector where) const;
63
64 /// Return all Objects that collide at given location.
65 /// List is empty if none.
67
68 /// Move Object from old position, add to new position.
69 /// Return 0 if ok, else -1.
70 int updatePosition(Object *p_o, Vector where);
71
72 /// Get Objects in a given Cell.
73 ObjectList getObjects(Cell cell) const;
74
75 /// Return grid as string.
76 std::string toString() const;
77
78 /// Startup (create grid).
79 /// Return 0 if ok, else -1.
80 int startUp();
81
82 /// Shutdown (delete grid).
83 void shutDown();
84
85#ifdef DEBUG_GRID
86 /// Draw self.
87 void draw();
88
89#endif
90
91 /// Print grid cell count to logfile.
92 void printLog() const;
93};
94
95} // end of namespace df
96
97#endif // __GRID_H__
Definition: Grid.h:22
int removeObject(Object *p_o)
Remove Object from grid.
int m_num_rows
Number of rows in grid.
Definition: Grid.h:28
std::set< Cell > positionToCells(Object *p_o, Vector where) const
Return list of Cells for given Object at given location.
void shutDown()
Shutdown (delete grid).
void printLog() const
Print grid cell count to logfile.
int m_width
Grid cell width.
Definition: Grid.h:29
int m_height
Grid cell height.
Definition: Grid.h:30
int insertObject(Object *p_o)
Insert Object into grid.
ObjectList m_out_of_bounds
All out of bounds objects.
Definition: Grid.h:26
Grid()
Constructor.
Cell positionToCell(Vector where) const
Return Cell (x,y) for given location.
int startUp()
Startup (create grid).
int updatePosition(Object *p_o, Vector where)
Move Object from old position, add to new position.
ObjectList getCollisions(Object *p_o, Vector where) const
Return all Objects that collide at given location.
void setDimensions(int new_width, int new_height)
Set dimensions. Deletes and re-creates grid.
~Grid()
Destructor.
std::string toString() const
Return grid as string.
ObjectList ** m_p_grid
The grid.
Definition: Grid.h:25
ObjectList getObjects(Cell cell) const
Get Objects in a given Cell.
int getHeight() const
Get height.
int m_num_cols
Number of columns in grid.
Definition: Grid.h:27
int getWidth() const
Get width.
Definition: ObjectList.h:15
Definition: Object.h:57
Definition: Vector.h:12
An animation for a sprite.
Definition: Animation.h:15