Browse Source

Aktionen zum Interagieren mit Triggern und Timern implementiert

Kolja Strohm 5 years ago
parent
commit
6168e5d3ba

+ 374 - 0
StickmanWorldOnline/Aktionen.cpp

@@ -2360,3 +2360,377 @@ bool BaseSetTeam::runNext( Spiel *zSpiel, Ereignis *zEreignis, LocalMemory *zMem
     zPC->stepOut();
     return 0;
 }
+
+TriggerRunStart::TriggerRunStart( Aktion *trigger )
+    : Aktion( TRIGGER_RUN_START )
+{
+    this->trigger = trigger;
+}
+
+TriggerRunStart::~TriggerRunStart()
+{
+    trigger->release();
+}
+
+bool TriggerRunStart::runNext( Spiel *zSpiel, Ereignis *zEreignis, LocalMemory *zMemory, ProgramCounter *zPC, double &waitCount )
+{
+    if( trigger->runNext( zSpiel, zEreignis, zMemory, zPC, waitCount ) )
+    {
+        Variable *t = zMemory->zVariable( "__return__" );
+        if( !t || t->getVariableTyp() != TRIGGER )
+        {
+            zPC->stepOut();
+            return 1;
+        }
+        zSpiel->addTriggerRun( ( (Trigger *)t )->runTrigger( zEreignis->getThis(), zSpiel ) );
+        zPC->stepOut();
+        return 1;
+    }
+    return 0;
+}
+
+
+TriggerSetEnabled::TriggerSetEnabled( Aktion *enabled, Aktion *trigger )
+    : Aktion( TRIGGER_SET_ENABLED )
+{
+    this->enabled = enabled;
+    this->trigger = trigger;
+}
+
+TriggerSetEnabled::~TriggerSetEnabled()
+{
+    enabled->release();
+    trigger->release();
+}
+
+bool TriggerSetEnabled::runNext( Spiel *zSpiel, Ereignis *zEreignis, LocalMemory *zMemory, ProgramCounter *zPC, double &waitCount )
+{
+    zPC->stepIn();
+    if( zPC->currentPosition() == 0 )
+    {
+        if( enabled->runNext( zSpiel, zEreignis, zMemory, zPC, waitCount ) )
+        {
+            Variable *t = zMemory->zVariable( "__return__" );
+            if( !t || t->getVariableTyp() != BOOLEAN )
+            {
+                zPC->stepOut();
+                return 1;
+            }
+            zMemory->setVar( zPC->getUniqueString() + "R0__", t->getThis() );
+            zPC->count();
+        }
+    }
+    if( zPC->currentPosition() == 1 && waitCount <= 0 )
+    {
+        if( trigger->runNext( zSpiel, zEreignis, zMemory, zPC, waitCount ) )
+        {
+            Variable *t = zMemory->zVariable( "__return__" );
+            if( !t || t->getVariableTyp() != TRIGGER )
+            {
+                zPC->stepOut();
+                return 1;
+            }
+            ( (Trigger *)t )->setAktiv( ( (Boolean *)zMemory->getVariable( zPC->getUniqueString() + "R0__" ) )->getValue() );
+            zPC->stepOut();
+            return 1;
+        }
+    }
+    zPC->stepOut();
+    return 0;
+}
+
+
+TeamSetPunkte::TeamSetPunkte( Aktion *punkte, Aktion *team )
+    : Aktion( TEAM_SET_PUNKTE )
+{
+    this->punkte = punkte;
+    this->team = team;
+}
+
+TeamSetPunkte::~TeamSetPunkte()
+{
+    punkte->release();
+    team->release();
+}
+
+bool TeamSetPunkte::runNext( Spiel *zSpiel, Ereignis *zEreignis, LocalMemory *zMemory, ProgramCounter *zPC, double &waitCount )
+{
+    zPC->stepIn();
+    if( zPC->currentPosition() == 0 )
+    {
+        if( punkte->runNext( zSpiel, zEreignis, zMemory, zPC, waitCount ) )
+        {
+            Variable *t = zMemory->zVariable( "__return__" );
+            if( !t || t->getVariableTyp() != INTEGER )
+            {
+                zPC->stepOut();
+                return 1;
+            }
+            zMemory->setVar( zPC->getUniqueString() + "R0__", t->getThis() );
+            zPC->count();
+        }
+    }
+    if( zPC->currentPosition() == 1 && waitCount <= 0 )
+    {
+        if( team->runNext( zSpiel, zEreignis, zMemory, zPC, waitCount ) )
+        {
+            Variable *t = zMemory->zVariable( "__return__" );
+            if( !t || t->getVariableTyp() != TEAM )
+            {
+                zPC->stepOut();
+                return 1;
+            }
+            ( (Team *)t )->setPunkte( ( (Integer *)zMemory->getVariable( zPC->getUniqueString() + "R0__" ) )->getValue() );
+            zPC->stepOut();
+            return 1;
+        }
+    }
+    zPC->stepOut();
+    return 0;
+}
+
+
+TimerSetPosition::TimerSetPosition( Aktion *x, Aktion *y, Aktion *timer )
+    : Aktion( TIMER_SET_POSITION )
+{
+    this->x = x;
+    this->y = y;
+    this->timer = timer;
+}
+
+TimerSetPosition::~TimerSetPosition()
+{
+    x->release();
+    y->release();
+    timer->release();
+}
+
+bool TimerSetPosition::runNext( Spiel *zSpiel, Ereignis *zEreignis, LocalMemory *zMemory, ProgramCounter *zPC, double &waitCount )
+{
+    zPC->stepIn();
+    if( zPC->currentPosition() == 0 )
+    {
+        if( x->runNext( zSpiel, zEreignis, zMemory, zPC, waitCount ) )
+        {
+            Variable *t = zMemory->zVariable( "__return__" );
+            if( !t || t->getVariableTyp() != FLOAT )
+            {
+                zPC->stepOut();
+                return 1;
+            }
+            zMemory->setVar( zPC->getUniqueString() + "R0__", t->getThis() );
+            zPC->count();
+        }
+    }
+    if( zPC->currentPosition() == 1 && waitCount <= 0 )
+    {
+        if( y->runNext( zSpiel, zEreignis, zMemory, zPC, waitCount ) )
+        {
+            Variable *t = zMemory->zVariable( "__return__" );
+            if( !t || t->getVariableTyp() != FLOAT )
+            {
+                zPC->stepOut();
+                return 1;
+            }
+            zMemory->setVar( zPC->getUniqueString() + "R1__", t->getThis() );
+            zPC->count();
+        }
+    }
+    if( zPC->currentPosition() == 2 && waitCount <= 0 )
+    {
+        if( timer->runNext( zSpiel, zEreignis, zMemory, zPC, waitCount ) )
+        {
+            Variable *t = zMemory->zVariable( "__return__" );
+            if( !t || t->getVariableTyp() != TIMER )
+            {
+                zPC->stepOut();
+                return 1;
+            }
+            ( (Timer *)t )->setX( ( (Float *)zMemory->zVariable( zPC->getUniqueString() + "R0__" ) )->getValue() );
+            ( (Timer *)t )->setY( ( (Float *)zMemory->zVariable( zPC->getUniqueString() + "R1__" ) )->getValue() );
+            zPC->stepOut();
+            return 1;
+        }
+    }
+    zPC->stepOut();
+    return 0;
+}
+
+
+TimerSetPause::TimerSetPause( Aktion *pause, Aktion *timer )
+    : Aktion( TIMER_SET_PAUSE )
+{
+    this->pause = pause;
+    this->timer = timer;
+}
+
+TimerSetPause::~TimerSetPause()
+{
+    pause->release();
+    timer->release();
+}
+
+bool TimerSetPause::runNext( Spiel *zSpiel, Ereignis *zEreignis, LocalMemory *zMemory, ProgramCounter *zPC, double &waitCount )
+{
+    zPC->stepIn();
+    if( zPC->currentPosition() == 0 )
+    {
+        if( pause->runNext( zSpiel, zEreignis, zMemory, zPC, waitCount ) )
+        {
+            Variable *t = zMemory->zVariable( "__return__" );
+            if( !t || t->getVariableTyp() != BOOLEAN )
+            {
+                zPC->stepOut();
+                return 1;
+            }
+            zMemory->setVar( zPC->getUniqueString() + "R0__", t->getThis() );
+            zPC->count();
+        }
+    }
+    if( zPC->currentPosition() == 1 && waitCount <= 0 )
+    {
+        if( timer->runNext( zSpiel, zEreignis, zMemory, zPC, waitCount ) )
+        {
+            Variable *t = zMemory->zVariable( "__return__" );
+            if( !t || t->getVariableTyp() != TIMER )
+            {
+                zPC->stepOut();
+                return 1;
+            }
+            ( (Timer *)t )->setPause( ( (Boolean *)zMemory->getVariable( zPC->getUniqueString() + "R0__" ) )->getValue() );
+            zPC->stepOut();
+            return 1;
+        }
+    }
+    zPC->stepOut();
+    return 0;
+}
+
+
+TimerStart::TimerStart( Aktion *timer )
+    : Aktion( TIMER_START )
+{
+    this->timer = timer;
+}
+
+TimerStart::~TimerStart()
+{
+    timer->release();
+}
+
+bool TimerStart::runNext( Spiel *zSpiel, Ereignis *zEreignis, LocalMemory *zMemory, ProgramCounter *zPC, double &waitCount )
+{
+    if( timer->runNext( zSpiel, zEreignis, zMemory, zPC, waitCount ) )
+    {
+        Variable *t = zMemory->zVariable( "__return__" );
+        if( !t || t->getVariableTyp() != TIMER )
+        {
+            zPC->stepOut();
+            return 1;
+        }
+        ( (Timer *)t )->start( zSpiel );
+        zPC->stepOut();
+        return 1;
+    }
+    return 0;
+}
+
+
+TimerSetZeit::TimerSetZeit( Aktion *zeit, Aktion *timer )
+    : Aktion( TIMER_SET_ZEIT )
+{
+    this->zeit = zeit;
+    this->timer = timer;
+}
+
+TimerSetZeit::~TimerSetZeit()
+{
+    zeit->release();
+    timer->release();
+}
+
+bool TimerSetZeit::runNext( Spiel *zSpiel, Ereignis *zEreignis, LocalMemory *zMemory, ProgramCounter *zPC, double &waitCount )
+{
+    zPC->stepIn();
+    if( zPC->currentPosition() == 0 )
+    {
+        if( zeit->runNext( zSpiel, zEreignis, zMemory, zPC, waitCount ) )
+        {
+            Variable *t = zMemory->zVariable( "__return__" );
+            if( !t || t->getVariableTyp() != FLOAT )
+            {
+                zPC->stepOut();
+                return 1;
+            }
+            zMemory->setVar( zPC->getUniqueString() + "R0__", t->getThis() );
+            zPC->count();
+        }
+    }
+    if( zPC->currentPosition() == 1 && waitCount <= 0 )
+    {
+        if( timer->runNext( zSpiel, zEreignis, zMemory, zPC, waitCount ) )
+        {
+            Variable *t = zMemory->zVariable( "__return__" );
+            if( !t || t->getVariableTyp() != TIMER )
+            {
+                zPC->stepOut();
+                return 1;
+            }
+            ( (Timer *)t )->setZeit( ( (Float *)zMemory->getVariable( zPC->getUniqueString() + "R0__" ) )->getValue() );
+            zPC->stepOut();
+            return 1;
+        }
+    }
+    zPC->stepOut();
+    return 0;
+}
+
+
+TimerSetSichtbar::TimerSetSichtbar( Aktion *sichtbar, Aktion *timer )
+    : Aktion( TIMER_SET_SICHTBAR )
+{
+    this->sichtbar = sichtbar;
+    this->timer = timer;
+}
+
+TimerSetSichtbar::~TimerSetSichtbar()
+{
+    sichtbar->release();
+    timer->release();
+}
+
+bool TimerSetSichtbar::runNext( Spiel *zSpiel, Ereignis *zEreignis, LocalMemory *zMemory, ProgramCounter *zPC, double &waitCount )
+{
+    zPC->stepIn();
+    if( zPC->currentPosition() == 0 )
+    {
+        if( sichtbar->runNext( zSpiel, zEreignis, zMemory, zPC, waitCount ) )
+        {
+            Variable *t = zMemory->zVariable( "__return__" );
+            if( !t || t->getVariableTyp() != BOOLEAN )
+            {
+                zPC->stepOut();
+                return 1;
+            }
+            zMemory->setVar( zPC->getUniqueString() + "R0__", t->getThis() );
+            zPC->count();
+        }
+    }
+    if( zPC->currentPosition() == 1 && waitCount <= 0 )
+    {
+        if( timer->runNext( zSpiel, zEreignis, zMemory, zPC, waitCount ) )
+        {
+            Variable *t = zMemory->zVariable( "__return__" );
+            if( !t || t->getVariableTyp() != TIMER )
+            {
+                zPC->stepOut();
+                return 1;
+            }
+            ( (Timer *)t )->setSichtbar( ( (Boolean *)zMemory->getVariable( zPC->getUniqueString() + "R0__" ) )->getValue() );
+            zPC->stepOut();
+            return 1;
+        }
+    }
+    zPC->stepOut();
+    return 0;
+}

+ 1 - 1
StickmanWorldOnline/Aktionen.h

@@ -825,7 +825,7 @@ private:
     Aktion *trigger;
 
 public:
-    TriggerRunStart( Aktion *base );
+    TriggerRunStart( Aktion *trigger );
     ~TriggerRunStart();
     bool runNext( Spiel *zSpiel, Ereignis *zEreignis, LocalMemory *zMemory, ProgramCounter *zPC, double &waitCount ) override;
 };

+ 7 - 2
StickmanWorldOnline/Spiel.cpp

@@ -702,7 +702,8 @@ void Spiel::addTrigger( Trigger * trigger )
 
 void Spiel::addTriggerRun( TriggerRun * tRun )
 {
-    triggerRuns.add( tRun );
+    if( tRun )
+        triggerRuns.add( tRun );
 }
 
 Team *Spiel::getTeam( int id ) const
@@ -869,7 +870,11 @@ void Spiel::throwEvent( Ereignis *e )
     for( auto t = trigger.getIterator(); t; t++ )
     {
         if( t->hatEreignis( e->getTyp() ) )
-            triggerRuns.add( t->runTrigger( e->getThis(), this ) );
+        {
+            TriggerRun *tr = t->runTrigger( e->getThis(), this );
+            if( tr )
+                triggerRuns.add( tr );
+        }
     }
     e->release();
 }

+ 1 - 0
StickmanWorldOnline/Team.h

@@ -23,6 +23,7 @@ public:
     void addTod();
     void addKill();
     void addPunkte( int punkte );
+    void setPunkte( int punkte );
     Text getName() const;
     int getMaxWiederbelebungsZeit() const;
     int getTode() const;

+ 1 - 0
StickmanWorldOnline/Timer.h

@@ -26,6 +26,7 @@ public:
     void setSichtbar( bool visible );
     void setFarbe( int farbe );
     void tick( double time, Spiel *zSpiel );
+    void setZeit( float zeit );
     float getTimeLeft() const;
     bool istSichtbar() const;
     int getFarbe() const;

+ 19 - 0
StickmanWorldOnline/Trigger.cpp

@@ -221,6 +221,7 @@ Trigger::Trigger( int id, const char *name, int ereignisAnzahl, EreignisTyp *ere
     this->ereignisse = ereignisse;
     this->bedingungen = bedingungen;
     this->aktionen = aktionen;
+    aktiv = 1;
 }
 
 Trigger::~Trigger()
@@ -230,6 +231,11 @@ Trigger::~Trigger()
     aktionen->release();
 }
 
+void Trigger::setAktiv( bool aktiv )
+{
+    this->aktiv = aktiv;
+}
+
 bool Trigger::hatEreignis( EreignisTyp typ ) const
 {
     for( int i = 0; i < ereignisAnzahl; i++ )
@@ -258,6 +264,19 @@ Aktion *Trigger::getAktion( int index ) const
 // return: 0, falls die bedingungen nicht erfüllt sind
 TriggerRun *Trigger::runTrigger( Ereignis *e, Spiel *zSpiel )
 {
+    if( !aktiv )
+    {
+        e->release();
+        return 0;
+    }
+    for( auto b = bedingungen->getIterator(); b; b++ )
+    {
+        if( !b->check( zSpiel, e ) )
+        {
+            e->release();
+            return 0;
+        }
+    }
     return new TriggerRun( (Trigger *)getThis(), e, zSpiel );
 }
 

+ 2 - 0
StickmanWorldOnline/Trigger.h

@@ -87,10 +87,12 @@ private:
     EreignisTyp *ereignisse;
     RCArray< Bedingung > *bedingungen;
     RCArray< Aktion > *aktionen;
+    bool aktiv;
 
 public:
     Trigger( int id, const char *name, int ereignisAnzahl, EreignisTyp *ereignisse, RCArray< Bedingung > *bedingungen, RCArray< Aktion > *aktionen );
     ~Trigger();
+    void setAktiv( bool aktiv );
     bool hatEreignis( EreignisTyp typ ) const;
     int getAktionAnzahl() const;
     Aktion *zAktion( int index ) const;