/src/GameLogics/PlayerShip.cpp
C++ | 116 lines | 90 code | 20 blank | 6 comment | 18 complexity | b5d7c80e0e493796df47982ba3bd2730 MD5 | raw file
- /*
- * PlayerShip.cpp
- *
- * Created on: Dec 28, 2011
- * Author: maarten
- */
- #include "PlayerShip.h"
- #include "World.h"
- #include "Factory.h"
- #include "PlayerBullet.h"
- #include "EnemyBullet.h"
- #include "EnemyShip.h"
- using namespace Tyrian;
- PlayerShip::PlayerShip( double x, double y, double s, Coord* loc, int h, double f, int d )
- : Ship( x, y, s, loc, h, f, d)
- {}
- void PlayerShip::AddKeyboard( Keyboard* keyb)
- {
- this->thisKeyboard = keyb;
- }
- bool PlayerShip::CollidesWith( Entity* src )
- {
- if ( dynamic_cast<PlayerBullet*> (src) != 0)
- {
- return false;
- }
- if( dynamic_cast<EnemyBullet*> (src) != 0)
- {
- EnemyBullet* ptr = dynamic_cast<EnemyBullet*> (src);
- int tempHealth = ptr->GetHealth();
- ptr->SetHealth( ptr->GetHealth() - this->GetHealth() );
- this->SetHealth( this->GetHealth() - tempHealth);
- if ( this->GetHealth() <= 0 )
- {
- return true;
- }
- }
- if( dynamic_cast<EnemyShip*> (src) != 0)
- {
- EnemyShip* ptr = dynamic_cast<EnemyShip*> (src);
- int tempHealth = ptr->GetHealth();
- ptr->SetHealth( ptr->GetHealth() - this->GetHealth() );
- this->SetHealth( this->GetHealth() - tempHealth);
- if ( this->GetHealth() <= 0 )
- {
- return true;
- }
- }
- return false;
- }
- void PlayerShip::Move( unsigned long frameDelay )
- {
- Coord* temp = new Coord();
- if ( this->thisKeyboard->KeyUpPressed() )
- {
- if ( (this->GetCoord()->GetY() <= YAXIS/2 + this->GetYSize()/2) )
- {
- temp->SetY( temp->GetY() + ( this->GetSpeed()*(frameDelay/TICKSPERSECOND) ));
- }
- }
- if ( this->thisKeyboard->KeyDownPressed() )
- {
- if ( (this->GetCoord()->GetY() >= -(YAXIS/2 )+ this->GetYSize()/2))
- {
- temp->SetY( temp->GetY() - (this->GetSpeed()*(frameDelay/TICKSPERSECOND)) );
- }
- }
- if ( this->thisKeyboard->KeyLeftPressed() )
- {
- if ( (this->GetCoord()->GetX() >= -(XAXIS/2) - this->GetXSize()/2))
- {
- temp->SetX( temp->GetX() - (this->GetSpeed()*(frameDelay/TICKSPERSECOND)) );
- }
- }
- if ( this->thisKeyboard->KeyRightPressed() )
- {
- if ( (this->GetCoord()->GetX() <= XAXIS/2 - this->GetXSize()/2))
- {
- temp->SetX( temp->GetX() + (this->GetSpeed()*(frameDelay/TICKSPERSECOND)) );
- }
- }
- if ( this->thisKeyboard->KeySpacePressed() )
- {
- if ( this->lastShot > this->fireDelay)
- {
- this->GetWorld()->AddToList(this->GetFactory()->CreatePlayerBullet( *this->GetCoord(), this->damage ));
- this->lastShot = 0;
- }
- }
- this->lastShot += frameDelay;
- *this->GetCoord() += *temp;
- }
- PlayerShip::~PlayerShip()
- {
- this->thisKeyboard = 0;
- }