Ship.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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, Vec2< float > p, Vec2< float > s, float r )
  13. {
  14. for( auto i = data->polygons->getArray(); i.set; i++ )
  15. {
  16. if( i.var.name->istGleich( "engine_l" ) )
  17. {
  18. stL = *i.var.schwerpunkt;
  19. Vertex l, r;
  20. for( int j = 0; j < 4; j++ )
  21. {
  22. if( i.var.tKordinaten->get( j ).y == 1.f )
  23. {
  24. if( i.var.tKordinaten->get( j ).x == 0.f )
  25. l = i.var.vertex->get( j );
  26. if( i.var.tKordinaten->get( j ).x == 1.f )
  27. r = i.var.vertex->get( j );
  28. }
  29. }
  30. kL = ( ( l + ( r - l ) * 0.5 ) - stL );
  31. }
  32. if( i.var.name->istGleich( "engine_r" ) )
  33. {
  34. stR = *i.var.schwerpunkt;
  35. Vertex l, r;
  36. for( int j = 0; j < 4; j++ )
  37. {
  38. if( i.var.tKordinaten->get( j ).y == 1.f )
  39. {
  40. if( i.var.tKordinaten->get( j ).x == 0.f )
  41. l = i.var.vertex->get( j );
  42. if( i.var.tKordinaten->get( j ).x == 1.f )
  43. r = i.var.vertex->get( j );
  44. }
  45. }
  46. kR = ( ( l + ( r - l ) * 0.5 ) - stR );
  47. }
  48. if( i.var.name->istGleich( "engine_m" ) )
  49. {
  50. stM = *i.var.schwerpunkt;
  51. Vertex l, r;
  52. for( int j = 0; j < 4; j++ )
  53. {
  54. if( i.var.tKordinaten->get( j ).y == 1.f )
  55. {
  56. if( i.var.tKordinaten->get( j ).x == 0.f )
  57. l = i.var.vertex->get( j );
  58. if( i.var.tKordinaten->get( j ).x == 1.f )
  59. r = i.var.vertex->get( j );
  60. }
  61. }
  62. kM = ( ( l + ( r - l ) * 0.5 ) - stM );
  63. }
  64. }
  65. rSpeed = 0;
  66. ship = new Model2D();
  67. ship->setStyle( Model2D::Style::Sichtbar | Model2D::Style::Textur | Model2D::Style::Alpha );
  68. ship->setModel( data );
  69. ship->setFarbe( 0x00000000 );
  70. ship->setDrehung( r );
  71. pos = p;
  72. speed = s;
  73. ref = 1;
  74. }
  75. // Destruktor
  76. Ship::~Ship()
  77. {
  78. ship->release();
  79. }
  80. #define sgn( x ) x < 0 ? -1 : 1
  81. // nicht constant
  82. void Ship::setTexture( Textur2D *zFlammenM, Textur2D *zFlammenL, Textur2D *zFlammenR, Bild *textur )
  83. {
  84. Textur2D *txt = new Textur2D();
  85. txt->setTexturZ( textur );
  86. ship->setTextur( txt, "ship" );
  87. ship->setTextur( zFlammenL->getThis(), "engine_l" );
  88. ship->setTextur( zFlammenR->getThis(), "engine_r" );
  89. ship->setTextur( zFlammenM->getThis(), "engine_m" );
  90. }
  91. void Ship::setPRS( Vec2< float > p, Vec2< float > s, float r, float rSpeed )
  92. {
  93. this->rSpeed = rSpeed;
  94. ship->setDrehung( r );
  95. pos = p;
  96. speed = s;
  97. }
  98. bool Ship::tick( double zeit, int breite, int höhe, char tastenStände )
  99. {
  100. Vertex minP = (Vertex)ship->zModel()->minP * ship->getSize() + ship->getPosition();
  101. Vertex maxP = (Vertex)ship->zModel()->maxP * ship->getSize() + ship->getPosition();
  102. if( maxP.x < 0 && speed.x < 0 )
  103. pos.x += breite + ship->zModel()->maxP.x * ship->getSize();
  104. if( maxP.y < 0 && speed.y < 0 )
  105. pos.y += höhe + ship->zModel()->maxP.y * ship->getSize();
  106. if( minP.x > breite && speed.x > 0 )
  107. pos.x -= breite + ship->zModel()->maxP.x * ship->getSize();
  108. if( minP.y > höhe && speed.y > 0 )
  109. pos.y -= höhe + ship->zModel()->maxP.y * ship->getSize();
  110. ship->setPosition( pos );
  111. if( ( tastenStände | 1 ) == tastenStände )
  112. {
  113. Vertex hp;
  114. Vertex mv;
  115. float r;
  116. if( ship->zModel()->calcHitPoint( stM, kM, "ship", hp, mv, r ) )
  117. {
  118. rSpeed += r * (float)zeit;
  119. speed += mv.rotation( ship->getDrehung() ) * (float)zeit;
  120. }
  121. }
  122. else
  123. {
  124. float movementAngle = atan2( speed.y, speed.x );
  125. int ix = sgn( speed.x ), iy = sgn( speed.y );
  126. if( speed.x != 0 )
  127. speed.x += 1.2f * cos( movementAngle + (float)PI ) * (float)zeit * 10;
  128. if( speed.y != 0 )
  129. speed.y += 1.2f * sin( movementAngle + (float)PI ) * (float)zeit * 10;
  130. if( ix * sgn( speed.x ) < 0 )
  131. speed.x = 0;
  132. if( iy * sgn( speed.y ) < 0 )
  133. speed.y = 0;
  134. if( rSpeed > 0 )
  135. {
  136. rSpeed -= (float)zeit;
  137. if( rSpeed < 0 )
  138. rSpeed = 0;
  139. }
  140. if( rSpeed < 0 )
  141. {
  142. rSpeed += (float)zeit;
  143. if( rSpeed > 0 )
  144. rSpeed = 0;
  145. }
  146. }
  147. ship->addDrehung( (float)zeit * rSpeed );
  148. if( ( tastenStände | 2 ) == tastenStände )
  149. {
  150. Vertex hp;
  151. Vertex mv;
  152. float r;
  153. if( ship->zModel()->calcHitPoint( stL, kL, "ship", hp, mv, r ) )
  154. {
  155. rSpeed += r * (float)zeit;
  156. speed += mv.rotation( ship->getDrehung() ) * (float)zeit;
  157. }
  158. //ship->addDrehung( (float)zeit * 2 );
  159. }
  160. if( ( tastenStände | 4 ) == tastenStände )
  161. {
  162. Vertex hp;
  163. Vertex mv;
  164. float r;
  165. if( ship->zModel()->calcHitPoint( stR, kR, "ship", hp, mv, r ) )
  166. {
  167. rSpeed += r * (float)zeit;
  168. speed += mv.rotation( ship->getDrehung() ) * (float)zeit;
  169. }
  170. //ship->addDrehung( (float)-zeit * 2 );
  171. }
  172. if( speed.x || speed.y )
  173. {
  174. pos += speed * (float)zeit;
  175. ship->setPosition( pos );
  176. }
  177. return ship->tick( zeit );
  178. }
  179. void Ship::render( Bild &zRObj )
  180. {
  181. ship->render( zRObj );
  182. }
  183. // constant
  184. void Ship::save( Datei *zD ) const
  185. {
  186. zD->schreibe( (char*)&pos.x, 4 );
  187. zD->schreibe( (char*)&pos.y, 4 );
  188. zD->schreibe( (char*)&speed.x, 4 );
  189. zD->schreibe( (char*)&speed.y, 4 );
  190. float r = ship->getDrehung();
  191. zD->schreibe( (char*)&r, 4 );
  192. zD->schreibe( (char*)&rSpeed, 4 );
  193. }
  194. Schuss *Ship::getSchuss() const
  195. {
  196. return new Schuss( pos, Vec2<float>( cos( ship->getDrehung() ), sin( ship->getDrehung() ) ) * 200 );
  197. }
  198. bool Ship::istTod( Asteroid *zA ) const
  199. {
  200. return ship->istModelInnen( zA->zModel() );
  201. }
  202. Punkt Ship::getKamPos( int breite, int höhe ) const
  203. {
  204. Punkt ret;
  205. ret.x = (int)pos.x - 400;
  206. ret.y = (int)pos.y - 250;
  207. if( ret.x < 0 )
  208. ret.x = 0;
  209. if( ret.y < 0 )
  210. ret.y = 0;
  211. if( ret.x > breite - 800 )
  212. ret.x = breite - 800;
  213. if( ret.y > höhe - 500 )
  214. ret.y = höhe - 500;
  215. return ret;
  216. }
  217. Punkt Ship::getPos() const
  218. {
  219. return (Punkt)pos;
  220. }
  221. // Reference Counting
  222. Ship *Ship::getThis()
  223. {
  224. ref++;
  225. return this;
  226. }
  227. Ship *Ship::release()
  228. {
  229. ref--;
  230. if( !ref )
  231. delete this;
  232. return 0;
  233. }