TODO is now in the wiki
[supertux.git] / src / collision_grid.h
1 #ifndef __COLLISION_GRID_H__
2 #define __COLLISION_GRID_H__
3
4 #include <vector>
5 #include "special/moving_object.h"
6
7 using namespace SuperTux;
8
9 class CollisionGridIterator;
10
11 /**
12  * A rectangular grid to keep track of all moving game objects. It allows fast
13  * queries for all objects in a rectangular area.
14  */
15 class CollisionGrid
16 {
17 public:
18   CollisionGrid(float width, float height);
19   ~CollisionGrid();
20
21   void add_object(MovingObject* object);
22   void remove_object(MovingObject* object);
23
24   void check_collisions();
25
26 private:
27   friend class CollisionGridIterator;
28   
29   struct ObjectWrapper
30   {
31     MovingObject* object;
32     Rectangle dest;
33     /** (pseudo) timestamp. When reading from the grid the timestamp is
34      * changed so that you can easily avoid reading an object multiple times
35      * when it is in several cells that you check.
36      */
37     int timestamp;
38     /// index in the objects vector
39     int id;
40   };
41  
42   /** Element for the single linked list in each grid cell */
43   struct GridEntry
44   {
45     GridEntry* next;
46     ObjectWrapper* object_wrapper;
47   };
48
49   void remove_object_from_gridcell(int gridcell, ObjectWrapper* wrapper);
50   void collide_object(ObjectWrapper* wrapper);
51   void collide_object_object(ObjectWrapper* wrapper, ObjectWrapper* wrapper2);
52   void move_object(ObjectWrapper* wrapper);
53   
54   typedef std::vector<GridEntry*> GridEntries;
55   GridEntries grid;
56   typedef std::vector<ObjectWrapper*> Objects;
57   Objects objects;
58   size_t cells_x, cells_y;
59   float width;
60   float height;
61   float cell_width;
62   float cell_height;
63   int iterator_timestamp;
64 };
65
66 extern CollisionGrid* bla;
67
68 #endif
69