Asteroid.cpp 3.8 KB

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