Ship.cpp 3.5 KB

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