📄 tplayersship.cpp
字号:
// Copyright 2002 Kenneth Guy,
//
// TPlayersShip.cpp
/** \file TPlayersShip.cpp
implementation of class TPlayersShip */
#include "TPlayersShip.h"
/** Construct a players ship
\param aX Initial X position.
\param aY Initial Y position.
\param aHealth Initial health.
\param aShipSprite Sprite to use for healthy ship.
\param aCrashingStartSprite First frame of explosion animation
\param aCrashingSpriteEnd Last frame of explosion animation (inclusive)
*/
TPlayersShip::TPlayersShip(TInt16 aX,TInt16 aY, TInt16 aHealth,
TInt16 aShipSprite, TInt16 aCrashingStartSprite,
TInt16 aCrashingEndSprite) :
iHealth(aHealth), iCrashFrame(0),
iShipSprite(aShipSprite),
iCrashingStartSprite(aCrashingStartSprite),
iCrashingEndSprite(aCrashingEndSprite) {
iX=aX;
iY=aY;
iLastX=aX;
iLastY=aY;
UpdateSprite();
}
/** Update sprite.
Either set the sprite to the healthy ship or the correct frame
of the explossion.
*/
void TPlayersShip::UpdateSprite() {
if(iCrashFrame!=0) {
TInt16 spriteNo=iCrashingStartSprite+((iCrashFrame/2)-1);
if(spriteNo <= iCrashingEndSprite)
SetSpriteNo(spriteNo);
} else {
SetSpriteNo(iShipSprite);
}
}
/** Change players health
Is iHealth <= 0 player is dead and explosion animation starts.
*/
void TPlayersShip::AddHealth(TInt16 aHealth) {
SetHealth(iHealth+aHealth);
}
/** Set players health
Is aHealth <= 0 player is dead and explosion animation starts.
*/
void TPlayersShip::SetHealth(TInt16 aHealth) {
if(iCrashFrame==0) {
iHealth=aHealth;
if(iHealth <= 0) {
iHealth=0;
iCrashFrame=2;
}
UpdateSprite();
}
}
/** Query players health.
\return Health, Health <=0 is dead or dieing
*/
TInt16 TPlayersShip::Health() {
return iHealth;
}
/**
Is the ship currently exploding.
This is used to stop the player finishing the level or shooting while
the ship is exploding.
\return ETrue if dieing or dead.
*/
TBool TPlayersShip::Dieing() {
return iCrashFrame == 0 ? EFalse : ETrue;
}
/**
Is the ship dead.
\return ETrue if the player has finished exploding.
*/
TBool TPlayersShip::Dead() {
TInt16 spriteNo=iCrashingStartSprite+((iCrashFrame/2)-1);
if(spriteNo <= iCrashingEndSprite) {
return EFalse;
} else {
return ETrue;
}
}
/**
Handles movement of players ship.
Called for for each frame of the game. The function is used to
both move the ship and to animate the explossion of the ship.
It could also be used to provide an animated ship, or different
ships when the player is moving in different directions, saves
last x and y positions so we could do bouncing of bad guys.
\param aX X movement of players ship.
\param aY Y movement of players ship.
\param aAllowOfScreen ETrue at the end of the level to allow the player to fly off the end of the level, and hence finish the level.
*/
void TPlayersShip::MoveBy(TInt16 aX,TInt16 aY, TBool aAllowOfScreen) {
iLastX=iX;
iLastY=iY;
iX+=aX;
iY+=aY;
if(iY<0) iY=0;
if(iY>176) iY=176;
if(iX<16) iX=16;
// allow player to fly off screen at end of level
if(aAllowOfScreen!=EFalse) {
if(iX>304) iX=304;
} else {
if(iX>288) iX=288;
}
// if crashing animate,
if(iCrashFrame!=0) {
iCrashFrame++;
}
UpdateSprite();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -