TapestryEngine

A 2D Platformer Game Engine
Log | Files | Refs

248e4b73ddead71c5f2f67dfe55a37a10f6d7250.svn-base (13210B)


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