📄 player.cpp
字号:
/**
* File : Player.cpp
* Author : Kevin Lynx
* Date : 2007/7/30
*/
#include "stdafx.h"
#include "Player.h"
#include "MastEventReceiver.h"
#include "World.h"
#include "AnimManager.h"
#include "MonsterManager.h"
#include "Bullet.h"
#include "App.h"
#include "ResourceMgr.h"
#include "Effect.h"
#include "SoundManager.h"
#include "Bonus.h"
Player::Player() :
Sprite( Sprite::ST_PLAYER, DEFAULT_PLAYER_SPEED )
{
mLife = INIT_LIFE;
mMaxFire = INIT_MAX_FIRE;
}
Player::~Player()
{
}
bool Player::init( ISceneManager *smgr, IAnimatedMesh *mesh, ITexture *texture )
{
if( !Sprite::init( smgr, mesh, texture ) )
{
return false;
}
/// init the animation
mAnimMgr->addState( AS_NORMAL, 0, 71, 50, true );
mAnimMgr->addState( AS_RUN, 72, 195, 100, true );
mAnimMgr->setCurrentState( AS_NORMAL );
reset();
return true;
}
void Player::reset( bool timeOut )
{
mDir.set( 0, 0, -1 );
/// init the status
mStatus = SpriteStatus::NORMAL;
if( !timeOut )
{
mLife = INIT_LIFE;
mScore = 0;
mMaxFire = INIT_MAX_FIRE;
m4SidesFire = false;
}
float px = World::GetSingletonPtr()->mPlayerWorldPos.X;
float pz = World::GetSingletonPtr()->mPlayerWorldPos.Y;
setPosition( vector3df( px, 30, pz ) );
mNode->setVisible( true );
rotate( mDir );
///
mbGOD = false;
}
void Player::update( float dt )
{
/// 4 sides bonus time stuff
if( m4SidesFire )
{
m4SidesTime += dt;
if( m4SidesTime >= B_4SIDES_FIRE_TIME )
{
m4SidesFire = false;
}
}
/// deal with the status
if( mStatus != SpriteStatus::NORMAL )
{
if( SpriteStatus::update( Sprite::ST_PLAYER ) )
{
mNode->setVisible( true );//normal
}
}
if( mStatus == SpriteStatus::DEAD )
{
return ;
}
if( mStatus == SpriteStatus::BORN_HIDE &&
mNode->isVisible() )
{
mNode->setVisible( false );
}
if( mStatus == SpriteStatus::BORN_SHOW &&
!mNode->isVisible() )
{
mNode->setVisible( true );
}
mSpeed = 0.0f;
vector3df lastDir = mDir;
MastEventReceiver *input = MastEventReceiver::GetSingletonPtr();
/// handle input
if( input->keyDown( irr::KEY_LEFT ) )
{
mDir.set( -1, 0, 0 );
mSpeed = DEFAULT_PLAYER_SPEED;
}
else if( input->keyDown( irr::KEY_RIGHT ) )
{
mDir.set( 1, 0, 0 );
mSpeed = DEFAULT_PLAYER_SPEED;
}
else if( input->keyDown( irr::KEY_DOWN ) )
{
mDir.set( 0, 0, -1 );
mSpeed = DEFAULT_PLAYER_SPEED;
}
else if( input->keyDown( irr::KEY_UP ) )
{
mDir.set( 0, 0, 1 );
mSpeed = DEFAULT_PLAYER_SPEED;
}
else if( input->keyDown( irr::KEY_SHIFT ) &&
input->keyDown( irr::KEY_KEY_G ) &&
input->keyDown( irr::KEY_CONTROL ) )
{
mbGOD = !mbGOD ;///our hero will become the GOD, like me in my codes.
}
/// 4 sides bonus
int maxFire = m4SidesFire ? 4 * mMaxFire : mMaxFire;
if( input->keyDown( irr::KEY_SPACE ) &&
App::GetSingletonPtr()->getRealTime() - mLastFireTime > mFireDelay &&
BulletManager::GetSingletonPtr()->getPlayerBulletCount() < maxFire )
{
if( !m4SidesFire )
{
BulletManager::GetSingletonPtr()->createPlayerBullet(
mNode->getPosition(), mDir );
}
else
{
BulletManager::GetSingletonPtr()->createPlayerBullet(
mNode->getPosition(), vector3df( -1, 0, 0 ) );
BulletManager::GetSingletonPtr()->createPlayerBullet(
mNode->getPosition(), vector3df( 1, 0, 0 ) );
BulletManager::GetSingletonPtr()->createPlayerBullet(
mNode->getPosition(), vector3df( 0, 0, 1 ) );
BulletManager::GetSingletonPtr()->createPlayerBullet(
mNode->getPosition(), vector3df( 0, 0, -1 ) );
}
mLastFireTime = App::GetSingletonPtr()->getRealTime();
}
/// rotate if the direction is changed
if( lastDir != mDir )
{
rotate( mDir );
}
/// move
move( dt );
/// check out the collision check result
if( mCollResult == World::STONE_COUNT + World::B_BEAN || true )
{
/// go on checking whether eat the bean
aabbox3df box = mNode->getTransformedBoundingBox();
/// decress the box to make collision check more sense
box.MinEdge.X += 2.0f;
box.MinEdge.Z += 2.0f;
box.MaxEdge.X -= 2.0f;
box.MaxEdge.Z -= 2.0f;
if( World::GetSingletonPtr()->isGotBean( box ) )
{
/// cool!you got a bean
SoundManager::GetSingletonPtr()->playSound( SoundManager::SND_EAT,
this->getPosition() );
//cout << "Cool` ate the bean successfully~" << endl;
mScore += 10;
/// check wheter the level is clear
if( World::GetSingletonPtr()->isLevelClear() )
{
/// level clear
App::GetSingletonPtr()->setGameState( App::GAME_STATE_CLEAR );
/// kill all monsters
MonsterManager::GetSingletonPtr()->killAll();
}
}
}
/// check with the monsters
if( mStatus == SpriteStatus::NORMAL &&
MonsterManager::GetSingletonPtr()->checkPlayer( this ) && !mbGOD )
{
/// sorry about the pacman's death
//cout << "~Shit....the Pacman is dead" << endl;
if( kill() )
{
/// game over
App::GetSingletonPtr()->setGameState( App::GAME_STATE_GAMEOVER );
}
}
/// check with the monster bullets
if( mStatus == SpriteStatus::NORMAL &&
BulletManager::GetSingletonPtr()->checkPlayer( this ) && !mbGOD )
{
//cout << "Sorry~ PacMan was fucked by the Monster's Bullets" << endl;
if( kill() )
{
/// game over
App::GetSingletonPtr()->setGameState( App::GAME_STATE_GAMEOVER );
}
}
/// check with all the bonuses
if( //mStatus == SpriteStatus::NORMAL &&
BonusManager::GetSingletonPtr()->checkPlayer( this ) )
{
/// check out : sendBonus
/// take bonus sound
SoundManager::GetSingletonPtr()->playSound( SoundManager::SND_TAKE_BONUS,
getPosition() );
}
/// update the animation
if( equals( mSpeed, 0.0f ) && mAnimMgr->getCurrentState() == AS_RUN )
{
mAnimMgr->setCurrentState( AS_NORMAL );
}
else if( !equals( mSpeed, 0.0f ) && mAnimMgr->getCurrentState() == AS_NORMAL )
{
mAnimMgr->setCurrentState( AS_RUN );
}
}
aabbox3df Player::getWorldAABB()
{
aabbox3df box = mNode->getTransformedBoundingBox();
box.MinEdge.X += 2.0f;
box.MinEdge.Z += 2.0f;
box.MaxEdge.X -= 2.0f;
box.MaxEdge.Z -= 2.0f;
return box;
}
void Player::setPosition( const vector3df &pos )
{
Sprite::setPosition( pos );
/// update the camera
const vector3df lookat = getPosition();
vector3df camPos( lookat.X, lookat.Y + 600, lookat.Z - 200 );
App::GetSingletonPtr()->updateCamera( camPos, lookat );
}
void Player::getArrayPos( int &ax, int &az )
{
if( mStatus == SpriteStatus::NORMAL )
{
Sprite::getArrayPos( ax, az );
}
else
{
/// return a random array position , so the monster
/// cannot find the player
float x, z;
World::GetSingletonPtr()->getValidPos( x, z );
World::GetSingletonPtr()->worldToMap( x, z, ax, az );
}
}
bool Player::kill()
{
/// sound
SoundManager::GetSingletonPtr()->playSound( SoundManager::SND_DEATH_1,
getPosition() );
/// hurt effect first
EffectManager::GetSingletonPtr()->createExp( ResourceMgr::EXP_HURT_SPRITE,
getExpPosition() );
mNode->setVisible( false );
changeStatus( SpriteStatus::NORMAL, Sprite::ST_PLAYER );
/// no bonus
m4SidesFire = false;
mLife --;
if( mLife <= 0 )
{
return true;
}
else
{
return false;
}
}
void Player::addScore( int score )
{
mScore += score;
}
void Player::sendBonus( int type )
{
if( type == World::B_4_SIDES )
{
m4SidesFire = true;
m4SidesTime = 0.0f;
}
if( type == World::B_MONEY )
{
addScore( 200 );
}
if( type == World::B_SUPER_FIRE )
{
mMaxFire += 2;
}
if( type == World::B_TIME )
{
App::GetSingletonPtr()->addLevelTime( 30000 );
}
if( type == World::B_LIFE )
{
mLife ++;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -