Ship.cpp 3.1 KB

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