PageRenderTime 39ms CodeModel.GetById 7ms RepoModel.GetById 1ms app.codeStats 0ms

/src/GameLogics/PlayerShip.cpp

https://bitbucket.org/maartenvandenheuvel/tyrian
C++ | 116 lines | 90 code | 20 blank | 6 comment | 18 complexity | b5d7c80e0e493796df47982ba3bd2730 MD5 | raw file
  1. /*
  2. * PlayerShip.cpp
  3. *
  4. * Created on: Dec 28, 2011
  5. * Author: maarten
  6. */
  7. #include "PlayerShip.h"
  8. #include "World.h"
  9. #include "Factory.h"
  10. #include "PlayerBullet.h"
  11. #include "EnemyBullet.h"
  12. #include "EnemyShip.h"
  13. using namespace Tyrian;
  14. PlayerShip::PlayerShip( double x, double y, double s, Coord* loc, int h, double f, int d )
  15. : Ship( x, y, s, loc, h, f, d)
  16. {}
  17. void PlayerShip::AddKeyboard( Keyboard* keyb)
  18. {
  19. this->thisKeyboard = keyb;
  20. }
  21. bool PlayerShip::CollidesWith( Entity* src )
  22. {
  23. if ( dynamic_cast<PlayerBullet*> (src) != 0)
  24. {
  25. return false;
  26. }
  27. if( dynamic_cast<EnemyBullet*> (src) != 0)
  28. {
  29. EnemyBullet* ptr = dynamic_cast<EnemyBullet*> (src);
  30. int tempHealth = ptr->GetHealth();
  31. ptr->SetHealth( ptr->GetHealth() - this->GetHealth() );
  32. this->SetHealth( this->GetHealth() - tempHealth);
  33. if ( this->GetHealth() <= 0 )
  34. {
  35. return true;
  36. }
  37. }
  38. if( dynamic_cast<EnemyShip*> (src) != 0)
  39. {
  40. EnemyShip* ptr = dynamic_cast<EnemyShip*> (src);
  41. int tempHealth = ptr->GetHealth();
  42. ptr->SetHealth( ptr->GetHealth() - this->GetHealth() );
  43. this->SetHealth( this->GetHealth() - tempHealth);
  44. if ( this->GetHealth() <= 0 )
  45. {
  46. return true;
  47. }
  48. }
  49. return false;
  50. }
  51. void PlayerShip::Move( unsigned long frameDelay )
  52. {
  53. Coord* temp = new Coord();
  54. if ( this->thisKeyboard->KeyUpPressed() )
  55. {
  56. if ( (this->GetCoord()->GetY() <= YAXIS/2 + this->GetYSize()/2) )
  57. {
  58. temp->SetY( temp->GetY() + ( this->GetSpeed()*(frameDelay/TICKSPERSECOND) ));
  59. }
  60. }
  61. if ( this->thisKeyboard->KeyDownPressed() )
  62. {
  63. if ( (this->GetCoord()->GetY() >= -(YAXIS/2 )+ this->GetYSize()/2))
  64. {
  65. temp->SetY( temp->GetY() - (this->GetSpeed()*(frameDelay/TICKSPERSECOND)) );
  66. }
  67. }
  68. if ( this->thisKeyboard->KeyLeftPressed() )
  69. {
  70. if ( (this->GetCoord()->GetX() >= -(XAXIS/2) - this->GetXSize()/2))
  71. {
  72. temp->SetX( temp->GetX() - (this->GetSpeed()*(frameDelay/TICKSPERSECOND)) );
  73. }
  74. }
  75. if ( this->thisKeyboard->KeyRightPressed() )
  76. {
  77. if ( (this->GetCoord()->GetX() <= XAXIS/2 - this->GetXSize()/2))
  78. {
  79. temp->SetX( temp->GetX() + (this->GetSpeed()*(frameDelay/TICKSPERSECOND)) );
  80. }
  81. }
  82. if ( this->thisKeyboard->KeySpacePressed() )
  83. {
  84. if ( this->lastShot > this->fireDelay)
  85. {
  86. this->GetWorld()->AddToList(this->GetFactory()->CreatePlayerBullet( *this->GetCoord(), this->damage ));
  87. this->lastShot = 0;
  88. }
  89. }
  90. this->lastShot += frameDelay;
  91. *this->GetCoord() += *temp;
  92. }
  93. PlayerShip::~PlayerShip()
  94. {
  95. this->thisKeyboard = 0;
  96. }