TapestryEngine

A 2D Platformer Game Engine
Log | Files | Refs

68ef1350d2e4f56f3671234f31f9f4a8e7502c58.svn-base (2706B)


      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 		return ( ((Character*)(mAHM->GetActorAddress(id)))->GetSpatCell() ); //can't cast as collider tramples the object
     95 		
     96 		//return mActorCells[id];
     97 	}
     98 
     99 	//int* GetAdjacentCells(int cellnum, int cells[9]); //populates and returns cells[9] with the cell numbers of cellnum and the 8 surrounding cells
    100 
    101 	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
    102 	
    103 	bool HandleRectSensors(SDL_Rect* sensor, int CallerID, SensorType SeType);
    104 
    105 	bool TestBlockers(SDL_Rect* sensor, int CallerID);
    106 
    107 
    108 	//BinaryTree mActors;
    109 	ActorHandleManager* mAHM;
    110 	//int mActorCells[MAX_ACTORS];
    111 	Cell mCells[CELL_COUNT];
    112 	//int mActorCount;
    113 	//int mAdjacentActorIDs[MAX_ACTORS];
    114 	//int mAdjacentActorCount;
    115 };
    116 
    117 #endif
    118