Drop.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include "Drop.h"
  2. #include "Spiel.h"
  3. #include "Ereignis.h"
  4. Drop::Drop( int id, int minX, int maxX, int minY, int maxY, int maxTime, int numDrops, const char *name, float wkeit[ ITEMANZAHL ] )
  5. : Variable( DROP )
  6. {
  7. this->id = id;
  8. this->name = name;
  9. this->minX = minX;
  10. this->maxX = maxX;
  11. this->minY = minY;
  12. this->maxY = maxY;
  13. this->maxTime = maxTime;
  14. this->nextDrop = (float)maxTime;
  15. this->numDrops = numDrops;
  16. memcpy( wahrscheinlichkeit, wkeit, sizeof( float ) * ITEMANZAHL );
  17. }
  18. Drop::~Drop()
  19. {}
  20. void Drop::setMinX( int x )
  21. {
  22. minX = x;
  23. }
  24. void Drop::setMaxX( int x )
  25. {
  26. maxX = x;
  27. }
  28. void Drop::setMinY( int y )
  29. {
  30. minY = y;
  31. }
  32. void Drop::setMaxY( int y )
  33. {
  34. maxY = y;
  35. }
  36. void Drop::setMaxTime( int seconds )
  37. {
  38. maxTime = seconds;
  39. }
  40. void Drop::setTime( float time )
  41. {
  42. this->nextDrop = time;
  43. }
  44. void Drop::doDrop( Spiel *zSpiel )
  45. {
  46. nextDrop = (float)maxTime;
  47. Ereignis *e = new Ereignis( DROP_AKTION );
  48. e->addParameter( "Betroffener Drop", getThis() );
  49. zSpiel->throwEvent( e );
  50. for( int i = 0; i < numDrops; i++ )
  51. {
  52. double p = zSpiel->getRand();
  53. int typ = 0;
  54. for( ; typ < ITEMANZAHL; typ++ )
  55. {
  56. if( p <= wahrscheinlichkeit[ typ ] )
  57. break;
  58. p -= wahrscheinlichkeit[ typ ];
  59. }
  60. int x = (int)( zSpiel->getRand() * ( maxX - minX - 100 ) + minX + 50 );
  61. int y = (int)( zSpiel->getRand() * ( maxY - minY - 100 ) + minY + 50 );
  62. if( x >= minX + 50 && x < maxX - 50 && y >= minY + 50 && y < maxY - 50 )
  63. zSpiel->addGegenstand( new Gegenstand( zSpiel->getNextId(), (GegenstandTyp)typ, x, y ) );
  64. }
  65. }
  66. void Drop::tick( double time, Spiel *zSpiel )
  67. {
  68. nextDrop -= (float)time;
  69. if( nextDrop <= 0 )
  70. doDrop( zSpiel );
  71. }
  72. int Drop::getNumDrops() const
  73. {
  74. return numDrops;
  75. }
  76. int Drop::getMinX() const
  77. {
  78. return minX;
  79. }
  80. int Drop::getMaxX() const
  81. {
  82. return maxX;
  83. }
  84. int Drop::getMinY() const
  85. {
  86. return minY;
  87. }
  88. int Drop::getMaxY() const
  89. {
  90. return maxY;
  91. }
  92. int Drop::getMaxTime() const
  93. {
  94. return maxTime;
  95. }
  96. int Drop::getId() const
  97. {
  98. return id;
  99. }