TapestryEngine

A 2D Platformer Game Engine
Log | Files | Refs

138686a7943b7d768ec6f6540fc37d5d7a3a36ed.svn-base (13272B)


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