CDopeFish.cpp 3.33 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * CDopeFish.cpp
 *
 *  Created on: 26.07.2011
 *      Author: gerstrong
 */

#include "CDopeFish.h"
9 10
#include "CPlayerDive.h"
#include "CSchoolFish.h"
11
#include "CBubbles.h"
12

13 14
namespace galaxy
{
15

Gerhard Stein's avatar
Gerhard Stein committed
16 17 18 19 20
const int A_DOPEFISH_SWIM = 0;
const int A_DOPEFISH_EAT = 2;
const int A_DOPEFISH_START_BURP = 3;
const int A_DOPEFISH_BURPING = 5;
const int A_DOPEFISH_BURP_FINISHED = 6;
Gerstrong's avatar
Gerstrong committed
21

22 23
const int DOPE_SPEED = 30;
const int DOPE_BITE_SPEED = 60;
Gerhard Stein's avatar
Gerhard Stein committed
24
const int CSF_MIN_DISTANCE_TO_CHARGE = 1<<CSF;
25
const int CSF_DISTANCE_TO_FOLLOW_TOLERANCE = 2<<CSF;
Gerhard Stein's avatar
Gerhard Stein committed
26
const int CSF_DISTANCE_TO_FOLLOW = 20<<CSF;
Gerstrong's avatar
Gerstrong committed
27 28
const int DOPE_EAT_TIMER = 50;

29

Gerhard Stein's avatar
Gerhard Stein committed
30 31
CDopeFish::CDopeFish(CMap *pmap, const Uint16 foeID, Uint32 x, Uint32 y, const int sprVar) :
CGalaxySpriteObject(pmap, foeID, x, y, sprVar),
Gerstrong's avatar
Gerstrong committed
32 33
m_eatTimer(0),
m_burped(false)
34 35 36
{
	setupGalaxyObjectOnMap(0x35C0, A_DOPEFISH_SWIM);
	mp_processState = &CDopeFish::processSwim;
Gerhard Stein's avatar
Gerhard Stein committed
37 38
	xDirection = RIGHT;
	yDirection = UP;
Gerstrong's avatar
Gerstrong committed
39 40
}

41
bool CDopeFish::isNearby(CSpriteObject &theObject)
Gerstrong's avatar
Gerstrong committed
42
{
Gerhard Stein's avatar
Gerhard Stein committed
43 44
	if( dynamic_cast<CPlayerBase*>(&theObject) ||
		dynamic_cast<CSchoolFish*>(&theObject) )
Gerstrong's avatar
Gerstrong committed
45
	{
Gerhard Stein's avatar
Gerhard Stein committed
46 47 48 49 50 51
		const int objX = theObject.getXMidPos();
		const int objY = theObject.getYMidPos();
		const int fishX = getXMidPos();
		const int fishY = getYMidPos();
		const int dx = objX - fishX;
		const int dy = objY - fishY;
52

Gerhard Stein's avatar
Gerhard Stein committed
53 54 55 56 57 58 59 60 61 62

		if( objX < fishX - CSF_DISTANCE_TO_FOLLOW ||
			objX > fishX + CSF_DISTANCE_TO_FOLLOW )
			return false;

		if( objY < fishY - CSF_DISTANCE_TO_FOLLOW ||
			objY > fishY + CSF_DISTANCE_TO_FOLLOW )
			return false;


63
		if( dx<-CSF_DISTANCE_TO_FOLLOW_TOLERANCE )
Gerhard Stein's avatar
Gerhard Stein committed
64
			xDirection = LEFT;
65
		else if( dx>+CSF_DISTANCE_TO_FOLLOW_TOLERANCE )
Gerhard Stein's avatar
Gerhard Stein committed
66
			xDirection = RIGHT;
Gerstrong's avatar
Gerstrong committed
67

68
		if( dy<-CSF_DISTANCE_TO_FOLLOW_TOLERANCE )
Gerhard Stein's avatar
Gerhard Stein committed
69
			yDirection = (rand()%5) ? UP : DOWN;
70
		else if( dy>+CSF_DISTANCE_TO_FOLLOW_TOLERANCE )
Gerhard Stein's avatar
Gerhard Stein committed
71
			yDirection = (rand()%5) ? DOWN : UP;
Gerstrong's avatar
Gerstrong committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

		if(getActionNumber(A_DOPEFISH_SWIM))
		{
			int absdx = (dx<0) ? -dx : dx;
			int absdy = (dy<0) ? -dy : dy;

			if( absdx < CSF_MIN_DISTANCE_TO_CHARGE &&
			    absdy < CSF_MIN_DISTANCE_TO_CHARGE )
			{
				setAction(A_DOPEFISH_EAT);
				mp_processState = &CDopeFish::processEat;
				m_eatTimer = DOPE_EAT_TIMER;
			}
		}

	}

	return true;
90 91
}

92
void CDopeFish::getTouchedBy(CSpriteObject &theObject)
93 94 95 96 97 98
{
	if(theObject.dead)
		return;

	if(CPlayerBase *Player = dynamic_cast<CPlayerBase*>(&theObject))
	{
Gerhard Stein's avatar
Gerhard Stein committed
99 100
		if( getActionNumber(A_DOPEFISH_EAT) )
			Player->getEaten();
101 102 103
	}
}

Gerstrong's avatar
Gerstrong committed
104
void CDopeFish::moveDope(const int speed)
105
{
Gerhard Stein's avatar
Gerhard Stein committed
106
	if(xDirection == LEFT)
Gerstrong's avatar
Gerstrong committed
107 108 109 110
		moveLeft(speed);
	else
		moveRight(speed);

Gerhard Stein's avatar
Gerhard Stein committed
111
	if(yDirection == UP)
Gerstrong's avatar
Gerstrong committed
112 113 114
		moveUp(speed);
	else
		moveDown(speed);
115 116 117

}

Gerstrong's avatar
Gerstrong committed
118 119 120 121 122
void CDopeFish::processSwim()
{
	moveDope(DOPE_SPEED);
}

123 124
void CDopeFish::processEat()
{
Gerstrong's avatar
Gerstrong committed
125 126 127 128 129 130 131 132 133 134
	moveDope(DOPE_BITE_SPEED);

	if(m_eatTimer>0)
	{
		m_eatTimer--;
	}
	else
	{
		m_eatTimer = 0;
		setAction(A_DOPEFISH_START_BURP);
135

Gerstrong's avatar
Gerstrong committed
136 137 138
		m_burped = false;
		mp_processState = &CDopeFish::processBurp;
	}
139 140 141 142
}

void CDopeFish::processBurp()
{
Gerstrong's avatar
Gerstrong committed
143 144 145
	if(!m_burped && getActionStatus(A_DOPEFISH_BURPING))
	{
		g_pSound->playSound(SOUND_DOPEFISH_BURP);
146
		CBubbles *Bubble = new CBubbles(mp_Map, 0, getXMidPos()+(1<<CSF), getYMidPos()+(1<<CSF), true);
147
		gEventManager.add( new EventSpawnObject( Bubble ) );
Gerstrong's avatar
Gerstrong committed
148 149
		m_burped = true;
	}
150

Gerstrong's avatar
Gerstrong committed
151 152 153 154 155
	if(getActionStatus(A_DOPEFISH_BURP_FINISHED))
	{
		setAction(A_DOPEFISH_SWIM);
		mp_processState = &CDopeFish::processSwim;
	}
156 157 158 159 160
}

void CDopeFish::process()
{
	(this->*mp_processState)();
Gerstrong's avatar
Gerstrong committed
161 162

	processActionRoutine();
163 164 165
}

} /* namespace galaxy */