TapestryEngine

A 2D Platformer Game Engine
Log | Files | Refs

8377874e5b4e287f10d941bcca7e724c8081f352.svn-base (12646B)


      1 #ifndef ACTOR_H
      2 #define ACTOR_H
      3 
      4 #include "Utils.h"
      5 #include "Console.h"
      6 #include "Animation.h"
      7 #include "AnimationGraph.h"
      8 #include "Physics.h"
      9 #include "Terrain.h"
     10 //#include "ActorHandle.h"
     11 
     12 //Actor State Defines
     13 #define IDLE       10
     14 #define RUN        20
     15 #define IDLE_BEGIN 30
     16 #define JUMP       40
     17 #define WANDER     50
     18 #define PURSUE     60
     19 
     20 #define ALIVE      70
     21 #define DEAD	   80
     22 
     23 #define SPLASH	   90
     24 
     25 #define RUN_LEFT  0x1
     26 #define RUN_RIGHT 0x02
     27 #define RUN_DOWN  0x003
     28 #define RUN_UP    0x0004
     29 //
     30 
     31 struct Trajectory
     32 {
     33 	float slope;
     34 	int dir;
     35 };
     36 
     37 class ActorHandleManager;
     38 class SpatialMonitor;
     39 
     40 class Actor : public EventReceiver
     41 {
     42 public:
     43 
     44 	Actor() : mOpacity(255) {}
     45 
     46 	Actor(AnimGraph* AnimData, SDL_Rect Pos, SDL_Rect DrawPos, ActorHandleManager* AHM, SpatialMonitor* spat, EventReceiver* prtLib, EventReceiver* SoundLib);
     47 
     48 	virtual bool UpdateAnimation() = 0;
     49 
     50 	virtual bool ActorUpdate() = 0;
     51 
     52 	int GetX()
     53 	{
     54 		return mPosData.x;
     55 	}
     56 
     57 	int GetY()
     58 	{
     59 		return mPosData.y;
     60 	}
     61 
     62 	int GetHeight()
     63 	{
     64 		return mPosData.h;
     65 	}
     66 
     67 	int GetWidth()
     68 	{
     69 		return mPosData.w;
     70 	}
     71 
     72 	int GetDrawX()
     73 	{
     74 		return mDrawPos.x;
     75 	}
     76 
     77 	int GetDrawY()
     78 	{
     79 		return mDrawPos.y;
     80 	}
     81 
     82 	int GetDrawHeight()
     83 	{
     84 		return mDrawPos.h;
     85 	}
     86 
     87 	int GetDrawWidth()
     88 	{
     89 		return mDrawPos.w;
     90 	}
     91 
     92 	SDL_Rect* GetPosition()
     93 	{
     94 		return &mPosData;
     95 	}
     96 
     97 	SDL_Rect* GetDrawPosition()
     98 	{
     99 		return &mDrawPos;
    100 	}
    101 
    102 	SDL_Rect CalcDrawRect()
    103 	{
    104 		if (mDir == RIGHT)
    105 		{
    106 			mDrawPos.x = mOff_x;
    107 		}
    108 		else //mDir = LEFT
    109 		{
    110 			mDrawPos.x = -(mDrawPos.w + (mOff_x - mPosData.w) );
    111 		}
    112 		return mDrawPos;
    113 	}
    114 
    115 	int GetOpacity()
    116 	{
    117 		return mOpacity;
    118 	}
    119 
    120 	int SetOpacity(int i)
    121 	{
    122 		return mOpacity = i;
    123 	}
    124 
    125 	Frame* GetFrame()
    126 	{
    127 		return mFrame;
    128 	}
    129 
    130 	int GetState()
    131 	{
    132 		return mState;
    133 	}
    134 
    135 	int GetDir()
    136 	{
    137 		return mDir;
    138 	}
    139 
    140 	std::vector<Actor*> GetHats()
    141 	{
    142 		return mHats;
    143 	}
    144 
    145 	int AttachHat(Actor* hat)
    146 	{
    147 		mHats.push_back(hat);
    148 		return ((int)mHats.size() - 1);
    149 	}
    150 
    151 	int SetHandle(int i)
    152 	{
    153 		return mHandle = i;
    154 	}
    155 
    156 	int GetHandle()
    157 	{
    158 		return mHandle;
    159 	}
    160 
    161 	int SetSpatCell(int i)
    162 	{
    163 		return mSpatCell = i;
    164 	}
    165 
    166 	int GetSpatCell ()
    167 	{
    168 		return mSpatCell;
    169 	}
    170 
    171 	AnimGraph* GetAnimGraph()
    172 	{
    173 		return mAnimGraph;
    174 	}
    175 
    176 	void PassAnimGraphState()
    177 	{
    178 		mAnimGraph->GiveActorState(&mAnimID);
    179 	}
    180 
    181 	int GetAnimID()
    182 	{
    183 		return mAnimID;
    184 	}
    185 
    186 	int GetChannel()
    187 	{
    188 		return mSoundChannel;
    189 	}
    190 
    191 	bool IsBlocking()
    192 	{
    193 		return mBlocking;
    194 	}
    195 
    196 	EventFeed mFeed;
    197 
    198 protected:
    199 
    200 	AnimGraph* mAnimGraph;
    201 	Frame* mFrame;
    202 	int mOpacity;
    203 
    204 	int mState;
    205 	int mDir;
    206 	int mAnimID;
    207 
    208 	SDL_Rect mPosData; //Where the actors collision box is
    209 	SDL_Rect mDrawPos; //Where the actor is drawn relative to its physical position (mPosData)
    210 	
    211 	int mOff_x;
    212 	int mOff_y;
    213 
    214 	std::vector<Actor*> mHats;
    215 
    216 	ActorHandleManager* mAHM;
    217 	int mHandle;
    218 
    219 	SpatialMonitor* mSpat; //SpatialMonitor deals with actor collision and other region checking (like LoS)
    220 	int mSpatCell;
    221 
    222 	EventReceiver* mParticleLib; //Deals with the spawning of particles
    223 
    224 	EventReceiver* mSoundLib;	//Deals with sounds and the spawning of point sounds
    225 	int mSoundChannel; //used to play sounds directly from the actor
    226 
    227 	bool mBlocking; //determines if an actor blocks other actors from passing through it
    228 };
    229 
    230 class Winch : public Actor
    231 {
    232 public:
    233 	Winch(AnimGraph* AnimData, SDL_Rect Pos, SDL_Rect DrawPos, ActorHandleManager* AHM, SpatialMonitor* spat, EventReceiver* prtLib, EventReceiver* SoundLib, Actor* gate, int dir) : Actor(AnimData, Pos, DrawPos, AHM, spat, prtLib, SoundLib), mGate(gate)
    234 	{
    235 		mState = IDLE;
    236 		mDir = dir;
    237 	}
    238 
    239 	bool EventProcess(Event eve);
    240 
    241 	bool UpdateAnimation();
    242 
    243 	bool ActorUpdate();
    244 
    245 protected:
    246 
    247 	Actor* mGate;
    248 };
    249 
    250 class Blocker : public Actor
    251 {
    252 public:
    253 	Blocker(AnimGraph* AnimData, SDL_Rect Pos, SDL_Rect DrawPos, ActorHandleManager* AHM, SpatialMonitor* spat, EventReceiver* prtLib, EventReceiver* SoundLib);
    254 
    255 protected:
    256 };
    257 
    258 class Test_Blocker : public Blocker
    259 {
    260 public:
    261 	Test_Blocker(AnimGraph* AnimData, SDL_Rect Pos, SDL_Rect DrawPos, ActorHandleManager* AHM, SpatialMonitor* spat, EventReceiver* prtLib, EventReceiver* SoundLib);
    262 
    263 	bool EventProcess(Event eve);
    264 
    265 	bool UpdateAnimation();
    266 
    267 	bool ActorUpdate();
    268 protected:
    269 };
    270 
    271 class Gate : public Blocker
    272 {
    273 public:
    274 	Gate(AnimGraph* AnimData, SDL_Rect Pos, SDL_Rect DrawPos, ActorHandleManager* AHM, SpatialMonitor* spat, EventReceiver* prtLib, EventReceiver* SoundLib, int dir = RIGHT);
    275 
    276 	bool EventProcess(Event eve);
    277 
    278 	bool UpdateAnimation();
    279 
    280 	bool ActorUpdate();
    281 protected:
    282 };
    283 
    284 class Interactable : public Actor
    285 {
    286 public:
    287 
    288 	Interactable(AnimGraph* AnimData, SDL_Rect Pos, SDL_Rect DrawPos, ActorHandleManager* AHM, SpatialMonitor* spat, EventReceiver* prtLib, EventReceiver* SoundLib);
    289 
    290 protected:
    291 };
    292 
    293 class Talker : public Interactable
    294 {
    295 public:
    296 	Talker(AnimGraph* AnimData, SDL_Rect Pos, SDL_Rect DrawPos, ActorHandleManager* AHM, SpatialMonitor* spat, EventReceiver* prtLib, EventReceiver* SoundLib, char* str) : Interactable(AnimData, Pos, DrawPos, AHM, spat, prtLib, SoundLib), mStr(str)
    297 	{}
    298 	
    299 	bool EventProcess(Event eve);
    300 
    301 	bool UpdateAnimation();
    302 
    303 	bool ActorUpdate();
    304 
    305 protected:
    306 
    307 	char* mStr;
    308 };
    309 
    310 class Gateman : public Talker
    311 {
    312 public:
    313 
    314 	//Gateman(AnimGraph* AnimData, SDL_Rect Pos, SDL_Rect DrawPos, ActorHandleManager* AHM, SpatialMonitor* spat, EventReceiver* prtLib, EventReceiver* SoundLib, char* str, EventReceiver* gate) : Talker(AnimData, Pos, DrawPos, AHM, spat, prtLib, SoundLib, str), mGate(gate)
    315 	Gateman(AnimGraph* AnimData, SDL_Rect Pos, SDL_Rect DrawPos, ActorHandleManager* AHM, SpatialMonitor* spat, EventReceiver* prtLib, EventReceiver* SoundLib, char* str, int gate) : Talker(AnimData, Pos, DrawPos, AHM, spat, prtLib, SoundLib, str), mGateH(gate)
    316 	{
    317 		mLOS = Pos;
    318 		mLOS.w = 250;
    319 		mLOS.x = mLOS.x - 125 - (int)((float)mPosData.w / 2);//(mLOS.x - (30 - (mPosData.w) / 2));
    320 		mLOS.h = 70;
    321 
    322 		mTalked = false;
    323 		mState = GATE_DOWN;
    324 	}
    325 
    326 	bool EventProcess(Event eve);
    327 
    328 	bool UpdateAnimation();
    329 
    330 	bool ActorUpdate();
    331 
    332 protected:
    333 
    334 	SDL_Rect mLOS;
    335 
    336 	int mGateH; //handle for paired gate
    337 
    338 	bool mTalked;
    339 };
    340 
    341 class Door : public Interactable
    342 {
    343 public:
    344 
    345 	Door(AnimGraph* AnimData, SDL_Rect Pos, SDL_Rect DrawPos, ActorHandleManager* AHM, SpatialMonitor* spat, EventReceiver* prtLib, EventReceiver* SoundLib, int x_off, int y_off) : Interactable(AnimData, Pos, DrawPos, AHM, spat, prtLib, SoundLib)
    346 	{
    347 		mTele_Pos = mPosData;
    348 		mTele_Pos.x += x_off;
    349 		mTele_Pos.y += y_off;
    350 	}
    351 
    352 	SDL_Rect* GetTelePos()
    353 	{
    354 		return &mTele_Pos;
    355 	}
    356 
    357 	void SetTeleDest(SDL_Rect* dest)
    358 	{
    359 		mTele_Dest = dest;
    360 	}
    361 
    362 	bool EventProcess(Event eve);
    363 
    364 	bool UpdateAnimation();
    365 
    366 	bool ActorUpdate();
    367 
    368 protected:
    369 
    370 	SDL_Rect mTele_Pos;
    371 	SDL_Rect* mTele_Dest;
    372 };
    373 
    374 class Mobile : public Actor
    375 {
    376 public:
    377 
    378 	Mobile() : mXspd(0), mYspd(0) {}
    379 
    380 	Mobile(AnimGraph* AnimData, SDL_Rect Pos, SDL_Rect DrawPos, ActorHandleManager* AHM, SpatialMonitor* spat, EventReceiver* prtLib, EventReceiver* SoundLib);
    381 	
    382 	void SetPosition(SDL_Rect* Dest);
    383 
    384 	bool ModifyActorPosition(int x, int y);
    385 
    386 	SDL_Rect MoveActor(int xspd, int yspd);
    387 
    388 	int MoveActorDirect(float xspd, float yspd);
    389 
    390 protected:
    391 	float mXspd;
    392 	float mYspd;
    393 	int mXmove;
    394 	int mYmove;
    395 };
    396 
    397 class Hat : public Mobile
    398 {
    399 public:
    400 	Hat(AnimGraph* AnimData, Actor* tar, ActorHandleManager* AHM, SpatialMonitor* spat, EventReceiver* prtLib, EventReceiver* SoundLib) : mTarget(tar), Mobile(AnimData, *tar->GetPosition(), *tar->GetDrawPosition(), AHM, spat, prtLib, SoundLib)
    401 	{
    402 		mEnabled = true;
    403 	}
    404 
    405 	bool IsEnabled()
    406 	{
    407 		return mEnabled;
    408 	}
    409 
    410 	void ToggleHat(bool state)
    411 	{
    412 		mEnabled = state;
    413 	}
    414 
    415 	bool EventProcess(Event eve)
    416 	{
    417 		return true;
    418 	}
    419 
    420 	bool ActorUpdate();
    421 
    422 protected:
    423 
    424 	Actor* mTarget;
    425 	bool mEnabled;
    426 };
    427 
    428 class bad_hat : public Hat
    429 {
    430 public:
    431 	
    432 	bad_hat(AnimGraph* AnimData, Actor* tar, ActorHandleManager* AHM, SpatialMonitor* spat, EventReceiver* prtLib, EventReceiver* SoundLib) : Hat(AnimData, tar, AHM, spat, prtLib, SoundLib)
    433 	{
    434 	}
    435 
    436 	bool UpdateAnimation()
    437 	{
    438 		switch (mState)
    439 		{
    440 		case RUN:
    441 		default:
    442 			mAnimID = IDLE;
    443 			break;
    444 		}
    445 		mFrame = mAnimGraph->UpdateAnimation();
    446 		return false;
    447 	}
    448 protected:
    449 };
    450 
    451 class sword : public Hat
    452 {
    453 public:
    454 
    455 	sword(AnimGraph* AnimData, Actor* tar, ActorHandleManager* AHM, SpatialMonitor* spat, EventReceiver* prtLib, EventReceiver* SoundLib) : Hat(AnimData, tar, AHM, spat, prtLib, SoundLib)
    456 	{
    457 		mEnabled = false;
    458 	}
    459 
    460 	bool UpdateAnimation()
    461 	{
    462 		mAnimID = mTarget->GetAnimID();
    463 		mFrame = mAnimGraph->UpdateAnimation();
    464 		return false;
    465 	}
    466 protected:
    467 };
    468 
    469 class Character : public Mobile
    470 {
    471 public:
    472 
    473 	Character() {}
    474 
    475 	Character(SDL_Surface* ter, AnimGraph* AnimData, SDL_Rect Pos, SDL_Rect DrawPos, ActorHandleManager* AHM, SpatialMonitor* spat, EventReceiver* prtLib, EventReceiver* SoundLib);
    476 
    477 	bool DetectFalling();
    478 
    479 	bool UpdatePhysics();
    480 
    481 	bool UpdateFireables();
    482 
    483 	Trajectory GetTrajectory(SDL_Rect Dest, SDL_Rect Init);
    484 
    485 	SDL_Rect RefinePosition(SDL_Rect* Dest, Trajectory trj, float destx, float desty, int RecursionCount = 0);
    486 
    487 	bool HandleDirectionalCollisions(SDL_Rect& Destination);
    488 
    489 	bool UpdatePosition();
    490 
    491 	TerrainCollisionManager* getColMan()
    492 	{
    493 		return &mColMan;
    494 	}
    495 
    496 protected:
    497 
    498 	SDL_Rect mDestData;
    499 	colman_Character mColMan;
    500 	PhysicsManager mPhysMan;
    501 };
    502 
    503 class Player : public Character
    504 {
    505 public:
    506 	Player() {}
    507 
    508 	Player(SDL_Surface* ter, AnimGraph* AnimData, SDL_Rect Pos, SDL_Rect DrawPos, ActorHandleManager* AHM, SpatialMonitor* spat, EventReceiver* prtLib, EventReceiver* SoundLib) : mWielded(false), mTalkerID(-1), mHP(100), mHPmax(100), mRecover(0), mTouch(Pos), Character(ter, AnimData, Pos, DrawPos, AHM, spat, prtLib, SoundLib)
    509 	{
    510 		mTouch.h = Pos.h;
    511 		mTouch.w = 18;
    512 	}
    513 
    514 	bool EventProcess(Event eve);
    515 
    516 	bool UpdateAnimation();
    517 
    518 	bool ActorUpdate();
    519 
    520 	int* GetHP()
    521 	{
    522 		return &mHP;
    523 	}
    524 
    525 	int* GetMaxHP()
    526 	{
    527 		return &mHPmax;
    528 	}
    529 
    530 	void SetSword(int index)
    531 	{
    532 		mHatIndex_Sword = index;
    533 	}
    534 
    535 protected:
    536 
    537 //	bool mTalking;
    538 	int mTalkerID;
    539 
    540 	bool mWielded;
    541 	int mHatIndex_Sword;
    542 
    543 	int mHP;
    544 	int mHPmax;
    545 
    546 	int mRecover; //Timer to force recovery pause between jumps
    547 
    548 	SDL_Rect mTouch; //Rect that deterimes what can be interacted with
    549 };
    550 
    551 class Loyal_Bug : public Character
    552 {
    553 public:
    554 	Loyal_Bug(SDL_Surface* ter, AnimGraph* AnimData, SDL_Rect Pos, SDL_Rect DrawPos, ActorHandleManager* AHM, SpatialMonitor* spat, EventReceiver* prtLib, EventReceiver* SoundLib) : mMounted(false), Character(ter, AnimData, Pos, DrawPos, AHM, spat, prtLib, SoundLib)
    555 	{
    556 	}
    557 
    558 	bool EventProcess(Event eve);
    559 
    560 	bool UpdateAnimation();
    561 
    562 	bool ActorUpdate();
    563 
    564 protected:
    565 
    566 	bool mMounted;
    567 };
    568 
    569 class Bug : public Character
    570 {
    571 public:
    572 	Bug(SDL_Surface* ter, AnimGraph* AnimData, SDL_Rect Pos, SDL_Rect DrawPos, ActorHandleManager* AHM, SpatialMonitor* spat, EventReceiver* prtLib, EventReceiver* SoundLib) : Character(ter, AnimData, Pos, DrawPos, AHM, spat, prtLib, SoundLib)
    573 	{
    574 	}
    575 
    576 protected:
    577 
    578 	int mTimer;
    579 
    580 };
    581 
    582 class Wanderer : public Character
    583 {
    584 public:
    585 
    586 	Wanderer() : mWaitLimit(0), mWaitTimer(0), mWanderLimit(0), mWanderTimer(0) {}
    587 
    588 	Wanderer(SDL_Surface* ter, AnimGraph* AnimData, SDL_Rect Pos, SDL_Rect DrawPos, ActorHandleManager* AHM, SpatialMonitor* spat, EventReceiver* prtLib, EventReceiver* SoundLib) : Character(ter, AnimData, Pos, DrawPos, AHM, spat, prtLib, SoundLib), mWanderTimer(0), mWanderLimit(0), mWaitTimer(0), mWaitLimit(0)
    589 	{
    590 	}
    591 
    592 	bool Wander();
    593 
    594 	bool WaitRandom();
    595 
    596 	bool EventProcess(Event eve);
    597 
    598 	virtual bool UpdateAnimation();
    599 
    600 	bool ActorUpdate();
    601 
    602 protected:
    603 
    604 	int mWanderTimer;
    605 	int mWanderLimit;
    606 	int mWaitLimit;
    607 	int mWaitTimer;
    608 };
    609 
    610 class Pursuer : public Character
    611 {
    612 public:
    613 	Pursuer() {}
    614 
    615 	Pursuer(SDL_Surface* ter, AnimGraph* AnimData, SDL_Rect Pos, SDL_Rect DrawPos, ActorHandleManager* AHM, SpatialMonitor* spat, EventReceiver* prtLib, EventReceiver* SoundLib) : Character(ter, AnimData, Pos, DrawPos, AHM, spat, prtLib, SoundLib), mRestTimer(0)
    616 	{
    617 		mState = IDLE;
    618 		mLOS = mPosData;
    619 		mLOS.x -= (170 - mLOS.w) / 2;
    620 		mLOS.w = 170;
    621 	}
    622 
    623 	bool Pursue();
    624 
    625 	bool rest(int t);
    626 
    627 	bool EventProcess(Event eve);
    628 
    629 	virtual bool UpdateAnimation();
    630 
    631 	bool ActorUpdate();
    632 
    633 protected:
    634 
    635 	Actor* mTarget;
    636 	int mRestTimer;
    637 	SDL_Rect mLOS;
    638 };
    639 
    640 class Rabbit : public Character
    641 {
    642 public:
    643 	Rabbit() {}
    644 
    645 	Rabbit(SDL_Surface* ter, AnimGraph* AnimData, SDL_Rect Pos, SDL_Rect DrawPos, ActorHandleManager* AHM, SpatialMonitor* spat, EventReceiver* prtLib, EventReceiver* SoundLib) : Character(ter, AnimData, Pos, DrawPos, AHM, spat, prtLib, SoundLib), mWaitLimit(0), mWaitTimer(0), mWanderLimit(0), mWanderTimer(0)
    646 	{
    647 	}
    648 
    649 	bool Hop();
    650 
    651 	bool Wander();
    652 
    653 	bool Behave();
    654 
    655 	bool WaitRandom();
    656 
    657 	bool EventProcess(Event eve);
    658 
    659 	virtual bool UpdateAnimation();
    660 
    661 	bool ActorUpdate();
    662 
    663 protected:
    664 
    665 	int mWanderTimer;
    666 	int mWanderLimit;
    667 	int mWaitLimit;
    668 	int mWaitTimer;
    669 };
    670 
    671 #endif