Asteroid.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #include "Asteroid.h"
  2. #include <Text.h>
  3. #include <Globals.h>
  4. #include <TastaturEreignis.h>
  5. #include "Schuss.h"
  6. #include <iostream>
  7. #include <Textur2D.h>
  8. #include "Schuss.h"
  9. // Inhalt der Asteroid Klasse aus Asteroid.h
  10. // Konstruktor
  11. Asteroid::Asteroid( Model2DData *data, Bild *textur, Vec2< float > p, Vec2< float > s, float rS, float r, float gr, char num )
  12. {
  13. txt = 0;
  14. asteroid = new Model2D();
  15. asteroid->setStyle( Model2D::Style::Sichtbar | Model2D::Style::Textur );
  16. asteroid->setModel( data );
  17. asteroid->setDrehung( r );
  18. asteroid->setSize( gr );
  19. if( textur )
  20. setTextur( textur );
  21. pos = p;
  22. speed = s;
  23. rSpeed = rS;
  24. id = num;
  25. ref = 1;
  26. }
  27. // Destruktor
  28. Asteroid::~Asteroid()
  29. {
  30. if( txt )
  31. txt->release();
  32. asteroid->release();
  33. }
  34. // nicht constant
  35. void Asteroid::setTextur( Bild *textur )
  36. {
  37. if( txt )
  38. txt->release();
  39. txt = textur->getThis();
  40. Textur2D *txt = new Textur2D();
  41. txt->setTexturZ( textur );
  42. asteroid->setTextur( txt->getThis() );
  43. }
  44. bool Asteroid::tick( double zeit, int breite, int höhe )
  45. {
  46. Vertex minP = (Vertex)asteroid->zModel()->minP * asteroid->getSize() + asteroid->getPosition();
  47. Vertex maxP = (Vertex)asteroid->zModel()->maxP * asteroid->getSize() + asteroid->getPosition();
  48. if( maxP.x < 0 && speed.x < 0 )
  49. pos.x += breite + asteroid->zModel()->maxP.x * asteroid->getSize();
  50. if( maxP.y < 0 && speed.y < 0 )
  51. pos.y += höhe + asteroid->zModel()->maxP.y * asteroid->getSize();
  52. if( minP.x > breite && speed.x > 0 )
  53. pos.x -= breite + asteroid->zModel()->maxP.x * asteroid->getSize();
  54. if( minP.y > höhe && speed.y > 0 )
  55. pos.y -= höhe + asteroid->zModel()->maxP.y * asteroid->getSize();
  56. asteroid->setPosition( pos );
  57. if( rSpeed )
  58. asteroid->addDrehung( rSpeed * (float)zeit );
  59. if( ( asteroid->zModel()->maxP - asteroid->zModel()->minP ).x < 50 || ( asteroid->zModel()->maxP - asteroid->zModel()->minP ).y < 50 )
  60. {
  61. asteroid->setSize( asteroid->getSize() - (float)zeit * 3 );
  62. if( asteroid->getSize() < 0 )
  63. asteroid->setSize( 0 );
  64. }
  65. if( speed.x || speed.y )
  66. {
  67. pos += speed * (float)zeit;
  68. asteroid->setPosition( pos );
  69. }
  70. return asteroid->tick( zeit );
  71. }
  72. void Asteroid::render( Bild &zRObj )
  73. {
  74. asteroid->render( zRObj );
  75. }
  76. bool Asteroid::istGetroffen( Schuss *zSchuss, Polygon2D &a, Polygon2D &b, Punkt &pa, Punkt &pb, RandomGenerator *zRand )
  77. {
  78. if( ( asteroid->zModel()->maxP - asteroid->zModel()->minP ).x < 50 || ( asteroid->zModel()->maxP - asteroid->zModel()->minP ).y < 50 )
  79. return 0;
  80. Vertex sp( 0, 0);
  81. float r = 0;
  82. Vertex hp;
  83. if( zSchuss->istInM2( *asteroid, sp, r, hp ) )
  84. {
  85. asteroid->zModel()->split( hp, sp.rotation( -asteroid->getDrehung() ) * 0.1f, "", a, b, pa, pb, [ zRand ]()
  86. {
  87. return zRand->rand();
  88. } );
  89. hp = ( hp * asteroid->getSize() ).rotation( asteroid->getDrehung() ) + asteroid->getPosition();
  90. speed += sp * 0.1f;
  91. rSpeed += r * 0.1f;
  92. zSchuss->getPos();
  93. return 1;
  94. }
  95. return 0;
  96. }
  97. void Asteroid::setDead()
  98. {
  99. asteroid->setSize( 0 );
  100. }
  101. // constant
  102. void Asteroid::save( Datei *zD ) const
  103. {
  104. zD->schreibe( (char*)&id, 1 );
  105. zD->schreibe( (char*)&pos.x, 4 );
  106. zD->schreibe( (char*)&pos.y, 4 );
  107. zD->schreibe( (char*)&speed.x, 4 );
  108. zD->schreibe( (char*)&speed.y, 4 );
  109. zD->schreibe( (char*)&rSpeed, 4 );
  110. float r = asteroid->getDrehung();
  111. zD->schreibe( (char*)&r, 4 );
  112. float gr = asteroid->getSize();
  113. zD->schreibe( (char*)&gr, 4 );
  114. }
  115. bool Asteroid::amLeben() const
  116. {
  117. return asteroid->getSize() != 0;
  118. }
  119. Model2D *Asteroid::zModel() const
  120. {
  121. return asteroid;
  122. }
  123. Punkt Asteroid::getPos() const
  124. {
  125. return (Punkt)pos;
  126. }
  127. Bild *Asteroid::getTextur() const
  128. {
  129. return txt ? txt->getThis() : 0;
  130. }
  131. Vec2< float > Asteroid::getSpeed() const
  132. {
  133. return speed;
  134. }
  135. float Asteroid::getRSpeed() const
  136. {
  137. return rSpeed;
  138. }
  139. char Asteroid::getId() const
  140. {
  141. return id;
  142. }
  143. int Asteroid::getScore() const
  144. {
  145. if( ( asteroid->zModel()->maxP - asteroid->zModel()->minP ).x < 50 || ( asteroid->zModel()->maxP - asteroid->zModel()->minP ).y < 50 )
  146. return 1;
  147. return 0;
  148. }
  149. // Refernece Counting
  150. Asteroid *Asteroid::getThis()
  151. {
  152. ref++;
  153. return this;
  154. }
  155. Asteroid *Asteroid::release()
  156. {
  157. ref--;
  158. if( !ref )
  159. delete this;
  160. return 0;
  161. }