Ship.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. 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. Textur2D *txt = new Textur2D();
  71. txt->setTexturZ( textur );
  72. ship->setTextur( txt, "ship" );
  73. ship->setTextur( zFlammenL->getThis(), "engine_l" );
  74. ship->setTextur( zFlammenR->getThis(), "engine_r" );
  75. ship->setTextur( zFlammenM->getThis(), "engine_m" );
  76. ship->setDrehung( r );
  77. pos = p;
  78. speed = s;
  79. ref = 1;
  80. }
  81. // Destruktor
  82. Ship::~Ship()
  83. {
  84. ship->release();
  85. }
  86. #define sgn( x ) x < 0 ? -1 : 1
  87. // nicht constant
  88. bool Ship::tick( double zeit, int breite, int höhe, char tastenStände )
  89. {
  90. Vertex minP = (Vertex)ship->zModel()->minP * ship->getSize() + ship->getPosition();
  91. Vertex maxP = (Vertex)ship->zModel()->maxP * ship->getSize() + ship->getPosition();
  92. if( maxP.x < 0 && speed.x < 0 )
  93. pos.x += breite + ship->zModel()->maxP.x * ship->getSize();
  94. if( maxP.y < 0 && speed.y < 0 )
  95. pos.y += höhe + ship->zModel()->maxP.y * ship->getSize();
  96. if( minP.x > breite && speed.x > 0 )
  97. pos.x -= breite + ship->zModel()->maxP.x * ship->getSize();
  98. if( minP.y > höhe && speed.y > 0 )
  99. pos.y -= höhe + ship->zModel()->maxP.y * ship->getSize();
  100. ship->setPosition( pos );
  101. if( ( tastenStände | 1 ) == tastenStände )
  102. {
  103. Vertex hp;
  104. Vertex mv;
  105. float r;
  106. if( ship->zModel()->calcHitPoint( stM, kM, "ship", hp, mv, r ) )
  107. {
  108. rSpeed += r * (float)zeit;
  109. speed += mv.rotation( ship->getDrehung() ) * (float)zeit;
  110. }
  111. }
  112. else
  113. {
  114. float movementAngle = atan2( speed.y, speed.x );
  115. int ix = sgn( speed.x ), iy = sgn( speed.y );
  116. if( speed.x != 0 )
  117. speed.x += 1.2f * cos( movementAngle + (float)PI ) * (float)zeit * 10;
  118. if( speed.y != 0 )
  119. speed.y += 1.2f * sin( movementAngle + (float)PI ) * (float)zeit * 10;
  120. if( ix * sgn( speed.x ) < 0 )
  121. speed.x = 0;
  122. if( iy * sgn( speed.y ) < 0 )
  123. speed.y = 0;
  124. if( rSpeed > 0 )
  125. {
  126. rSpeed -= (float)zeit;
  127. if( rSpeed < 0 )
  128. rSpeed = 0;
  129. }
  130. if( rSpeed < 0 )
  131. {
  132. rSpeed += (float)zeit;
  133. if( rSpeed > 0 )
  134. rSpeed = 0;
  135. }
  136. }
  137. ship->addDrehung( (float)zeit * rSpeed );
  138. if( ( tastenStände | 2 ) == tastenStände )
  139. {
  140. Vertex hp;
  141. Vertex mv;
  142. float r;
  143. if( ship->zModel()->calcHitPoint( stL, kL, "ship", hp, mv, r ) )
  144. {
  145. rSpeed += r * (float)zeit;
  146. speed += mv.rotation( ship->getDrehung() ) * (float)zeit;
  147. }
  148. //ship->addDrehung( (float)zeit * 2 );
  149. }
  150. if( ( tastenStände | 4 ) == tastenStände )
  151. {
  152. Vertex hp;
  153. Vertex mv;
  154. float r;
  155. if( ship->zModel()->calcHitPoint( stR, kR, "ship", hp, mv, r ) )
  156. {
  157. rSpeed += r * (float)zeit;
  158. speed += mv.rotation( ship->getDrehung() ) * (float)zeit;
  159. }
  160. //ship->addDrehung( (float)-zeit * 2 );
  161. }
  162. if( speed.x || speed.y )
  163. {
  164. pos += speed * (float)zeit;
  165. ship->setPosition( pos );
  166. }
  167. return ship->tick( zeit );
  168. }
  169. void Ship::render( Bild &zRObj )
  170. {
  171. ship->render( zRObj );
  172. }
  173. // constant
  174. void Ship::save( Datei *zD ) const
  175. {
  176. zD->schreibe( (char*)&pos.x, 4 );
  177. zD->schreibe( (char*)&pos.y, 4 );
  178. zD->schreibe( (char*)&speed.x, 4 );
  179. zD->schreibe( (char*)&speed.y, 4 );
  180. float r = ship->getDrehung();
  181. zD->schreibe( (char*)&r, 4 );
  182. }
  183. Schuss *Ship::getSchuss() const
  184. {
  185. return new Schuss( pos, Vec2<float>( cos( ship->getDrehung() ), sin( ship->getDrehung() ) ) * 200 );
  186. }
  187. bool Ship::istTod( Asteroid *zA ) const
  188. {
  189. return ship->istModelInnen( zA->zModel() );
  190. }
  191. Punkt Ship::getKamPos( int breite, int höhe ) const
  192. {
  193. Punkt ret;
  194. ret.x = (int)pos.x - 400;
  195. ret.y = (int)pos.y - 250;
  196. if( ret.x < 0 )
  197. ret.x = 0;
  198. if( ret.y < 0 )
  199. ret.y = 0;
  200. if( ret.x > breite - 800 )
  201. ret.x = breite - 800;
  202. if( ret.y > höhe - 500 )
  203. ret.y = höhe - 500;
  204. return ret;
  205. }
  206. Punkt Ship::getPos() const
  207. {
  208. return (Punkt)pos;
  209. }
  210. // Reference Counting
  211. Ship *Ship::getThis()
  212. {
  213. ref++;
  214. return this;
  215. }
  216. Ship *Ship::release()
  217. {
  218. ref--;
  219. if( !ref )
  220. delete this;
  221. return 0;
  222. }