#include "Drop.h" #include "Spiel.h" #include "Ereignis.h" Drop::Drop( int id, int minX, int maxX, int minY, int maxY, int maxTime, int numDrops, const char *name, float wkeit[ ITEMANZAHL ] ) : Variable( DROP ) { this->id = id; this->name = name; this->minX = minX; this->maxX = maxX; this->minY = minY; this->maxY = maxY; this->maxTime = maxTime; this->nextDrop = (float)maxTime; this->numDrops = numDrops; memcpy( wahrscheinlichkeit, wkeit, sizeof( float ) * ITEMANZAHL ); } Drop::~Drop() {} void Drop::setMinX( int x ) { minX = x; } void Drop::setMaxX( int x ) { maxX = x; } void Drop::setMinY( int y ) { minY = y; } void Drop::setMaxY( int y ) { maxY = y; } void Drop::setMaxTime( int seconds ) { maxTime = seconds; } void Drop::setTime( float time ) { this->nextDrop = time; } void Drop::doDrop( Spiel *zSpiel ) { zSpiel->setLastDrop( dynamic_cast( getThis() ) ); nextDrop = (float)maxTime; for( int i = 0; i < numDrops; i++ ) { double p = zSpiel->getRand(); int typ = 0; for( ; typ < ITEMANZAHL; typ++ ) { if( p <= wahrscheinlichkeit[ typ ] ) break; p -= wahrscheinlichkeit[ typ ]; } int x = (int)( zSpiel->getRand() * ( maxX - minX - 100 ) + minX + 50 ); int y = (int)( zSpiel->getRand() * ( maxY - minY - 100 ) + minY + 50 ); if( x >= minX + 50 && x < maxX - 50 && y >= minY + 50 && y < maxY - 50 ) zSpiel->addGegenstand( new Gegenstand( zSpiel->getNextId(), (GegenstandTyp)typ, x, y ) ); } Ereignis *e = new Ereignis( DROP_AKTION ); e->addParameter( "Betroffener Drop", dynamic_cast( getThis() ) ); zSpiel->throwEvent( e ); } void Drop::tick( double time, Spiel *zSpiel ) { nextDrop -= (float)time; if( nextDrop <= 0 ) doDrop( zSpiel ); } int Drop::getNumDrops() const { return numDrops; } int Drop::getMinX() const { return minX; } int Drop::getMaxX() const { return maxX; } int Drop::getMinY() const { return minY; } int Drop::getMaxY() const { return maxY; } float Drop::getZeit() const { return nextDrop; } int Drop::getMaxTime() const { return maxTime; } int Drop::getId() const { return id; }