Drop.cpp 2.0 KB

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