Ship.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include "Ship.h"
  2. #include <Text.h>
  3. #include <Globals.h>
  4. #include <TastaturEreignis.h>
  5. #include <FrameworkMath.h>
  6. #include "Schuss.h"
  7. #include "Asteroid.h"
  8. // Inhalt der Ship Klasse aus Ship.h
  9. // Konstruktor
  10. Ship::Ship( Model2DData *data, Bild *textur, Vec2< float > p, Vec2< float > s, float r )
  11. {
  12. ship = new Model2D();
  13. ship->setStyle( Model2D::Style::Sichtbar | Model2D::Style::Textur );
  14. ship->setModel( data );
  15. ship->setTextur( textur );
  16. ship->setDrehung( r );
  17. pos = p;
  18. speed = s;
  19. ref = 1;
  20. }
  21. // Destruktor
  22. Ship::~Ship()
  23. {
  24. ship->release();
  25. }
  26. // nicht constant
  27. bool Ship::tick( double zeit, int breite, int höhe )
  28. {
  29. Vertex minP = (Vertex)ship->zModel()->minP * ship->getSize() + ship->getPosition();
  30. Vertex maxP = (Vertex)ship->zModel()->maxP * ship->getSize() + ship->getPosition();
  31. if( maxP.x < 0 && speed.x < 0 )
  32. pos.x = breite + ship->zModel()->maxP.x * ship->getSize();
  33. if( maxP.y < 0 && speed.y < 0 )
  34. pos.y = höhe + ship->zModel()->maxP.y * ship->getSize();
  35. if( minP.x > breite && speed.x > 0 )
  36. pos.x = 0 - ship->zModel()->maxP.x * ship->getSize();
  37. if( minP.y > höhe && speed.y > 0 )
  38. pos.y = 0 - ship->zModel()->maxP.y * ship->getSize();
  39. ship->setPosition( pos );
  40. if( getTastenStand( T_Oben ) || getTastenStand( 'w' ) || getTastenStand( 'W' ) )
  41. {
  42. speed.x += (float)( 4.2 * cos( ship->getDrehung() ) );
  43. speed.y += (float)( 4.2 * sin( ship->getDrehung() ) );
  44. }
  45. else
  46. {
  47. float movementAngle = atan2( speed.y, speed.x );
  48. if( speed.x != 0 )
  49. speed.x += 1.2f * cos( movementAngle + (float)PI );
  50. if( speed.y != 0 )
  51. speed.y += 1.2f * sin( movementAngle + (float)PI );
  52. if( speed.x < 6.f && speed.x > -6.f )
  53. speed.x = 0;
  54. if( speed.y < 6.f && speed.y > -6.f )
  55. speed.y = 0;
  56. }
  57. if( !getTastenStand( T_Space ) )
  58. {
  59. if( getTastenStand( T_Rechts ) || getTastenStand( 'd' ) || getTastenStand( 'D' ) )
  60. ship->addDrehung( (float)zeit * 2 );
  61. if( getTastenStand( T_Links ) || getTastenStand( 'a' ) || getTastenStand( 'A' ) )
  62. ship->addDrehung( (float)-zeit * 2 );
  63. }
  64. if( speed.x || speed.y )
  65. {
  66. pos += speed * (float)zeit;
  67. ship->setPosition( pos );
  68. }
  69. return ship->tick( zeit );
  70. }
  71. void Ship::render( Bild &zRObj )
  72. {
  73. ship->render( zRObj );
  74. }
  75. // constant
  76. void Ship::save( Datei *zD ) const
  77. {
  78. zD->schreibe( (char*)&pos.x, 4 );
  79. zD->schreibe( (char*)&pos.y, 4 );
  80. zD->schreibe( (char*)&speed.x, 4 );
  81. zD->schreibe( (char*)&speed.y, 4 );
  82. float r = ship->getDrehung();
  83. zD->schreibe( (char*)&r, 4 );
  84. }
  85. Schuss *Ship::getSchuss() const
  86. {
  87. return new Schuss( pos, Vec2<float>( cos( ship->getDrehung() ), sin( ship->getDrehung() ) ) * 200 );
  88. }
  89. bool Ship::istTod( Asteroid *zA ) const
  90. {
  91. return ship->istModelInnen( zA->zModel() );
  92. }
  93. Punkt Ship::getKamPos( int breite, int höhe ) const
  94. {
  95. Punkt ret;
  96. ret.x = (int)pos.x - 400;
  97. ret.y = (int)pos.y - 250;
  98. if( ret.x < 0 )
  99. ret.x = 0;
  100. if( ret.y < 0 )
  101. ret.y = 0;
  102. if( ret.x > breite - 800 )
  103. ret.x = breite - 800;
  104. if( ret.y > höhe - 500 )
  105. ret.y = höhe - 500;
  106. return ret;
  107. }
  108. Punkt Ship::getPos() const
  109. {
  110. return (Punkt)pos;
  111. }
  112. // Reference Counting
  113. Ship *Ship::getThis()
  114. {
  115. ref++;
  116. return this;
  117. }
  118. Ship *Ship::release()
  119. {
  120. ref--;
  121. if( !ref )
  122. delete this;
  123. return 0;
  124. }