incorporated another patch from Ondra Hosek which fixes tux not getting killed on...
[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   struct ObjectWrapper
29   {
30     MovingObject* object;
31     Rectangle dest;
32     /** (pseudo) timestamp. When reading from the grid the timestamp is
33      * changed so that you can easily avoid reading an object multiple times
34      * when it is in several cells that you check.
35      */
36     int timestamp;
37     /// index in the objects vector
38     int id;
39   };
40  
41   /** Element for the single linked list in each grid cell */
42   struct GridEntry
43   {
44     GridEntry* next;
45     ObjectWrapper* object_wrapper;
46   };
47
48   void remove_object_from_gridcell(int gridcell, MovingObject* object);
49   void collide_object(ObjectWrapper* wrapper);
50   void collide_object_object(ObjectWrapper* wrapper, ObjectWrapper* wrapper2);
51   
52   typedef std::vector<GridEntry*> GridEntries;
53   GridEntries grid;
54   typedef std::vector<ObjectWrapper*> Objects;
55   Objects objects;
56   size_t cells_x, cells_y;
57   float width;
58   float height;
59   float cell_width;
60   float cell_height;
61 };
62
63 extern CollisionGrid* bla;
64
65 #endif
66