Asteroid.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include "Asteroid.h"
  2. #include <Random.h>
  3. Asteroid::Asteroid( int id, Vertex pos, Vertex speed, float rot, float rotS, Model2DData *m2d, Textur2D *textur )
  4. : Model2DObject()
  5. {
  6. this->id = id;
  7. setPosition( pos );
  8. setSpeed( speed );
  9. setDrehung( rot );
  10. setDrehungSpeed( rotS );
  11. setModel( m2d );
  12. setTextur( textur );
  13. alive = true;
  14. save();
  15. }
  16. bool Asteroid::tick( const WeltInfo &info, double zeit )
  17. {
  18. if( !alive )
  19. size -= (float)zeit;
  20. return __super::tick( info, zeit );
  21. }
  22. Asteroid *Asteroid::split( Vertex pos, Vertex speed, __int64 seed, int asteroidId )
  23. {
  24. Polygon2D a, b;
  25. Punkt pa, pb;
  26. Vertex hp;
  27. RandomGenerator randG;
  28. randG.setSeed( seed );
  29. if( calcHitPoint( pos - speed, speed, hp ) )
  30. {
  31. if( zModel()->split( getObjectPos( hp ), getObjectDir( speed ) * 0.1f, ( char* )"", a, b, pa, pb, [ &randG ]() -> double
  32. {
  33. return randG.rand();
  34. } ) )
  35. {
  36. impuls( pos - speed, speed * 0.05f );
  37. hp = ( hp * getSize() ).rotation( getDrehung() ) + getPosition();
  38. Vertex pav = ( (Vertex)pa ).rotation( getDrehung() ) + getPosition();
  39. Vertex pbv = ( (Vertex)pb ).rotation( getDrehung() ) + getPosition();
  40. Array< Polygon2D > *npaA = new Array< Polygon2D >();
  41. npaA->add( a );
  42. Model2DData *npdA = new Model2DData();
  43. npdA->erstelleModell( npaA );
  44. Array< Polygon2D > *npaB = new Array< Polygon2D >();
  45. npaB->add( b );
  46. Model2DData *npdB = new Model2DData();
  47. npdB->erstelleModell( npaB );
  48. Asteroid *astr = new Asteroid( asteroidId, pav, getSpeed() * (float)randG.rand(), getDrehung(), getDrehungSpeed() * (float)randG.rand(), npdA, getTextur() );
  49. setSpeed( getSpeed() * (float)randG.rand() );
  50. setDrehungSpeed( getDrehungSpeed() * (float)randG.rand() );
  51. setModel( npdB );
  52. setPosition( pbv );
  53. return astr;
  54. }
  55. }
  56. return 0;
  57. }
  58. void Asteroid::save()
  59. {
  60. backup.pos = getPosition();
  61. backup.speed = getSpeed();
  62. backup.rotation = getDrehung();
  63. backup.rSpeed = getDrehungSpeed();
  64. }
  65. void Asteroid::load()
  66. {
  67. setPosition( backup.pos );
  68. setSpeed( backup.speed );
  69. setDrehung( backup.rotation );
  70. setDrehungSpeed( backup.rSpeed );
  71. }
  72. void Asteroid::setDead()
  73. {
  74. alive = false;
  75. collision = false;
  76. }
  77. bool Asteroid::isDead() const
  78. {
  79. return size <= 0;
  80. }
  81. int Asteroid::getId() const
  82. {
  83. return id;
  84. }