a31fee0bf9e64054705adea05c59302c07999788.svn-base (2644B)
1 #ifndef ACTORCOLLISION_H 2 #define ACTORCOLLISION_H 3 4 #include "Utils.h" 5 #include "Console.h" 6 #include "DiagnosticDraw.h" 7 #include "Event.h" 8 #include "Actor.h" 9 #include "ActorHandle.h" 10 11 #define MAX_ACTORS 1024 12 13 #define CELLS_X 10 14 #define CELLS_Y 2 15 #define CELL_COUNT (CELLS_X*CELLS_Y) 16 17 class Cell 18 { 19 public: 20 Cell() : mCell_Rect() 21 { 22 mActors_In_Cell = BinaryTree(); 23 } 24 25 Cell(SDL_Rect rect) : mCell_Rect(rect) 26 { 27 mActors_In_Cell = BinaryTree(); 28 } 29 30 SDL_Rect* GetCellRect() 31 { 32 return &mCell_Rect; 33 } 34 35 BinaryTree* GetActorTree() 36 { 37 return &mActors_In_Cell; 38 } 39 protected: 40 SDL_Rect mCell_Rect; 41 BinaryTree mActors_In_Cell; 42 }; 43 44 class SpatialMonitor : public EventReceiver 45 { 46 public: 47 48 SpatialMonitor() 49 { 50 } 51 52 SpatialMonitor(int worldH, int worldW, ActorHandleManager* AHM) : mAHM(AHM) 53 { 54 PartitionCells(worldH, worldW); 55 } 56 57 int GetAdjacentCells(int cellnum, Cell* cells[9]); 58 59 bool LogActor(int id); //Logs an actor with the spatial monitor. called on program startup 60 61 bool EventProcess(Event eve); 62 63 Cell* GetCell(int i) //Gets the cell from a cell number. 64 { 65 if ((0 <= i) && (i < CELL_COUNT)) 66 { 67 return &mCells[i]; 68 } 69 else 70 { 71 //gCons->ConsPrintf("Cell %i does not exist\n", i) 72 return NULL; 73 } 74 } 75 76 protected: 77 78 bool PartitionCells(int worldH, int worldW); //divides the level into cells based on macro'd parameters 79 80 //bool GetActorsInCell(int cellnum); 81 82 //bool GetAdjacentActors(int cellnum); 83 84 bool DetectActorCollision(int id1, int id2); //checks two logged actors for intersection 85 86 bool DetectRectSensors(SDL_Rect* sensor, int detectorID, int foreignID); //Called in HandleRectSensors, checks given rectangle for intersects 87 88 bool ClearActorCell(int id); 89 90 int UpdateActorCell(int id); //Updates the the cell number that contains a logged actor, Called when an MOVED_THIS_FRAME event is processed 91 92 int GetActorCell(int id) //Gets the cell number of an actor 93 { 94 95 return (mAHM->GetActorAddress(id)->GetSpatCell()); 96 97 //return mActorCells[id]; 98 } 99 100 //int* GetAdjacentCells(int cellnum, int cells[9]); //populates and returns cells[9] with the cell numbers of cellnum and the 8 surrounding cells 101 102 bool HandleCollisions(int CallerID); //Called in event process, finds the actors in the cells adjacent to the cell of CallerID and checks them for collision with CallerID 103 104 bool HandleRectSensors(SDL_Rect* sensor, int CallerID, SensorType SeType); 105 106 bool TestBlockers(SDL_Rect* sensor, int CallerID); 107 108 109 //BinaryTree mActors; 110 ActorHandleManager* mAHM; 111 //int mActorCells[MAX_ACTORS]; 112 Cell mCells[CELL_COUNT]; 113 //int mActorCount; 114 //int mAdjacentActorIDs[MAX_ACTORS]; 115 //int mAdjacentActorCount; 116 }; 117 118 #endif 119