badguys can now have multiple hitpoints (default is 1)
[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   void move_object(MovingObject* object);
24
25   void check_collisions();
26
27 private:
28   friend class CollisionGridIterator;
29   
30   struct ObjectWrapper
31   {
32     MovingObject* object;
33     Rectangle dest;
34     /** (pseudo) timestamp. When reading from the grid the timestamp is
35      * changed so that you can easily avoid reading an object multiple times
36      * when it is in several cells that you check.
37      */
38     int timestamp;
39     /// index in the objects vector
40     int id;
41   };
42  
43   /** Element for the single linked list in each grid cell */
44   struct GridEntry
45   {
46     GridEntry* next;
47     ObjectWrapper* object_wrapper;
48   };
49
50   void remove_object_from_gridcell(int gridcell, MovingObject* object);
51   void collide_object(ObjectWrapper* wrapper);
52   void collide_object_object(ObjectWrapper* wrapper, ObjectWrapper* wrapper2);
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