📄 ghost.cpp
字号:
#include "Ghost.h"
CGhost::CGhost(CApplicationBase& aApp, CLevel& aLevel)
: CRotatingSprite(aApp),
iLevel(aLevel)
{
}
CGhost::~CGhost()
{
}
TInt CGhost::Init( IBitmap* aBitmap,
IBitmap* aMask)
{
TInt error;
error = CRotatingSprite::Init(aBitmap, aMask);
if (error != KErrNone)
{
return error;
}
return KErrNone;
}
void CGhost::Update(const TReal64 aFrametime)
{
// run ghost AI
switch (Id())
{
case KBitmapGhostRed:
UpdateAIRed(aFrametime);
break;
case KBitmapGhostGreen:
UpdateAIGreen(aFrametime);
break;
case KBitmapGhostBlue:
UpdateAIBlue(aFrametime);
break;
}
// limit ghost speed
if (Direction().LengthSq() > (iSpeed * iSpeed))
{
Direction().SetLength(iSpeed);
}
TSize levelsize = iLevel.SizeInPixels();
// check level boundaries
if (iPos.iX < 0.0)
{
iPos.iX = 0.0;
iDir.iX = -iDir.iX * 0.5;
}
if (iPos.iX > levelsize.iWidth)
{
iPos.iX = levelsize.iWidth;
iDir.iX = -iDir.iX * 0.5;
}
if (iPos.iY < 0.0)
{
iPos.iY = 0.0;
iDir.iY = -iDir.iY * 0.5;
}
if (iPos.iY > levelsize.iHeight)
{
iPos.iY = levelsize.iHeight;
iDir.iY = -iDir.iY * 0.5;
}
}
void CGhost::UpdateAIRed(const TReal64 aFrametime)
{
CRotatingSprite::Update(aFrametime);
TVector2 dir;
dir.iX = iPlayer->Position().iX - iPos.iX;
dir.iY = iPlayer->Position().iY - iPos.iY;
// accelerate towards player
SetAcceleration(dir);
}
void CGhost::UpdateAIGreen(const TReal64 aFrametime)
{
CRotatingSprite::Update(aFrametime);
// get the point where ghost is moving to
TVector2 goingto(iStartPos);
if (iMovingToEndPos)
{
goingto = iEndPos;
}
// accelerate towards destination point
TVector2 dir;
dir.iX = goingto.iX - iPos.iX;
dir.iY = goingto.iY - iPos.iY;
SetAcceleration(dir);
// check if ghost has reached the destination
if (dir.LengthSq() < (32.0 * 32.0))
{
// switch target position
iMovingToEndPos = !iMovingToEndPos;
}
}
void CGhost::UpdateAIBlue(const TReal64 aFrametime)
{
CSpriteBase::Update(aFrametime);
// turn to face the player
TVector2 dir;
dir.iX = iPlayer->Position().iX - iPos.iX;
dir.iY = iPlayer->Position().iY - iPos.iY;
ComputeRotationAngle(dir);
// pull player with pull force
TVector2 pos = iPlayer->Position();
const TReal32 radiussq = iPullForce * iPullForce;
TReal32 ftemp, lensq;
TReal64 length;
lensq = dir.LengthSq();
if (lensq > 0.0001 && lensq < radiussq)
{
Math::Sqrt(length, lensq);
ftemp = 1.0f - (length / iPullForce);
ftemp *= -iPullForce;
ftemp *= aFrametime;
dir.iX = dir.iX / length * ftemp;
dir.iY = dir.iY / length * ftemp;
pos.iX += dir.iX;
pos.iY += dir.iY;
iPlayer->SetPosition(pos);
}
}
void CGhost::Draw(IGraphicsContext& aContext, const TVector2& aCamera)
{
CRotatingSprite::Draw(aContext, aCamera);
}
void CGhost::SetAIParams( CSpriteBase* aPlayer,
const TPoint& aStartPos,
const TPoint& aEndPos,
const TInt aExtraParam,
const TInt aType)
{
iPlayer = aPlayer;
iStartPos = aStartPos;
iEndPos = aEndPos;
// set the id to the ghost to run separate AI for each
SetId(aType);
// set the starting position
SetPosition(TVector2(iStartPos));
// set the extra param
switch (aType)
{
case KBitmapGhostRed:
iSpeed = (TReal64)aExtraParam;
break;
case KBitmapGhostGreen:
iSpeed = (TReal64)aExtraParam;
iMovingToEndPos = ETrue;
break;
case KBitmapGhostBlue:
iSpeed = 0.0;
iPullForce = (TReal64)aExtraParam;
break;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -