Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 995 for code


Ignore:
Timestamp:
Apr 5, 2008, 6:46:43 PM (17 years ago)
Author:
landauf
Message:

added 'delay time command' console command that executes 'command' after 'time' seconds. it uses a automatically destroying timer for every execution, so you can start several delayed commands at the same time without overwriting older entries.

and this is great:

append disco.txt delay 0.0 Ambient setAmbientLightTest 1,0,0,1
append disco.txt delay 0.2 Ambient setAmbientLightTest 0,1,0,1
append disco.txt delay 0.4 Ambient setAmbientLightTest 0,0,1,1
append disco.txt delay 0.6 exec disco.txt

exec disco.txt

and you'll have disco in space… forever! :D

Location:
code/branches/core2/src/orxonox
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • code/branches/core2/src/orxonox/objects/Explosion.cc

    r790 r995  
    3232#include <OgreSceneNode.h>
    3333
    34 #include "../core/CoreIncludes.h"
     34#include "core/CoreIncludes.h"
     35#include "core/Executor.h"
     36
    3537#include "util/Math.h"
    3638#include "../Orxonox.h"
     
    5254        if (owner)
    5355        {
    54             this->destroyTimer_.setTimer(this->lifetime_, false, this, &Explosion::destroyObject);
     56            this->destroyTimer_.setTimer(this->lifetime_, false, this, createExecutor(createFunctor(&Explosion::destroyObject)));
    5557
    5658            Vector3 position = owner->getNode()->getWorldPosition();
  • code/branches/core2/src/orxonox/objects/Projectile.cc

    r957 r995  
    2828#include "OrxonoxStableHeaders.h"
    2929
    30 #include "../core/CoreIncludes.h"
     30#include "core/CoreIncludes.h"
     31#include "core/Executor.h"
     32
    3133#include "SpaceShip.h"
    3234#include "Explosion.h"
     
    6062        }
    6163
    62         this->destroyTimer_.setTimer(this->lifetime_, false, this, &Projectile::destroyObject);
     64        this->destroyTimer_.setTimer(this->lifetime_, false, this, createExecutor(createFunctor(&Projectile::destroyObject)));
    6365    }
    6466
  • code/branches/core2/src/orxonox/objects/test2.cc

    r871 r995  
    3030#include "test3.h"
    3131#include "core/CoreIncludes.h"
     32#include "core/Executor.h"
    3233
    3334namespace orxonox
     
    4344        this->usefullClass3_ = Class(Test3);
    4445
    45         timer1.setTimer(1, true, this, &Test2::timerFunction1);
    46         timer2.setTimer(5, true, this, &Test2::timerFunction2);
    47         timer3.setTimer(10, false, this, &Test2::timerFunction3);
     46        timer1.setTimer(1, true, this, createExecutor(createFunctor(&Test2::timerFunction1)));
     47        timer2.setTimer(5, true, this, createExecutor(createFunctor(&Test2::timerFunction2)));
     48        timer3.setTimer(10, false, this, createExecutor(createFunctor(&Test2::timerFunction3)));
    4849    }
    4950
  • code/branches/core2/src/orxonox/tools/Timer.cc

    r871 r995  
     1/*
     2 *   ORXONOX - the hottest 3D action shooter ever to exist
     3 *
     4 *
     5 *   License notice:
     6 *
     7 *   This program is free software; you can redistribute it and/or
     8 *   modify it under the terms of the GNU General Public License
     9 *   as published by the Free Software Foundation; either version 2
     10 *   of the License, or (at your option) any later version.
     11 *
     12 *   This program is distributed in the hope that it will be useful,
     13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 *   GNU General Public License for more details.
     16 *
     17 *   You should have received a copy of the GNU General Public License
     18 *   along with this program; if not, write to the Free Software
     19 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
     20 *
     21 *   Author:
     22 *      Fabian 'x3n' Landau
     23 *   Co-authors:
     24 *      ...
     25 *
     26 */
     27
     28#include "core/Executor.h"
    129#include "core/CoreIncludes.h"
     30#include "core/ConsoleCommand.h"
     31#include "core/CommandExecutor.h"
    232#include "Timer.h"
    333
    434namespace orxonox
    535{
     36    ConsoleCommandShortcutExtern(delay, AccessLevel::None);
     37
     38    void delay(float delay, const std::string& command)
     39    {
     40        StaticTimer *delaytimer = new StaticTimer();
     41        ExecutorStatic* delayexecutor = createExecutor(createFunctor(&executeDelayedCommand));
     42        delayexecutor->setDefaultValues(delaytimer, command);
     43        delaytimer->setTimer(delay, false, delayexecutor);
     44    }
     45
     46    void executeDelayedCommand(StaticTimer* timer, const std::string& command)
     47    {
     48        CommandExecutor::execute(command);
     49        delete timer;
     50    }
     51
    652    /**
    753        @brief Constructor: Sets the default-values.
     
    1157        RegisterRootObject(TimerBase);
    1258
     59        this->executor_ = 0;
    1360        this->interval_ = 0;
    1461        this->bLoop_ = false;
     
    1764        this->time_ = 0;
    1865    }
     66
     67    TimerBase::~TimerBase()
     68    {
     69        delete this->executor_;
     70    }
     71
     72    void TimerBase::run() const
     73    {
     74        (*this->executor_)();
     75    }
    1976}
  • code/branches/core2/src/orxonox/tools/Timer.h

    r970 r995  
    4343
    4444    source.cc:
     45        include "core/Executor.h"
     46
    4547        ClassName::ClassName()
    4648        {
    47             myTimer.setTimer(interval_in_seconds, bLoop, this, &ClassName::functionName);
     49            myTimer.setTimer(interval_in_seconds, bLoop, this, createExecutor(createFunctor(&ClassName::functionName)));
    4850        }
    4951
     
    5961
    6062#include <OgreFrameListener.h>
    61 #include "../OrxonoxPrereqs.h"
     63#include "OrxonoxPrereqs.h"
     64#include "core/CorePrereqs.h"
    6265
    6366namespace orxonox
    6467{
     68    class StaticTimer;
     69    void delay(float delay, const std::string& command);
     70    void executeDelayedCommand(StaticTimer* timer, const std::string& command);
     71
    6572    //! TimerBase is the parent of the Timer class.
    6673    class _OrxonoxExport TimerBase : public OrxonoxClass
     
    6976
    7077        public:
     78            ~TimerBase();
     79
     80            void run() const;
     81
     82            /** @brief Starts the Timer: Function-call after 'interval' seconds. */
     83            inline void startTimer()
     84                { this->bActive_ = true; this->time_ = this->interval_; }
     85            /** @brief Stops the Timer. */
     86            inline void stopTimer()
     87                { this->bActive_ = false; this->time_ = this->interval_; }
     88            /** @brief Pauses the Timer - it will continue with the actual state if you unpause it. */
     89            inline void pauseTimer()
     90                { this->bActive_ = false; }
     91            /** @brief Unpauses the Timer - continues with the given state. */
     92            inline void unpauseTimer()
     93                { this->bActive_ = true; }
     94            /** @brief Returns true if the Timer is active (= not stoped, not paused). @return True = Time is active */
     95            inline bool isActive() const
     96                { return this->bActive_; }
     97            /** @brief Gives the Timer some extra time. @param time The amount of extra time in seconds */
     98            inline void addTime(float time)
     99                { this->time_ += time; }
     100            /** @brief Decreases the remaining time of the Timer. @param time The amount of time to remove */
     101            inline void removeTime(float time)
     102                { this->time_ -= time; }
     103            /** @brief Sets the interval of the Timer. @param interval The interval */
     104            inline void setInterval(float interval)
     105                { this->interval_ = interval; }
     106            /** @brief Sets bLoop to a given value. @param bLoop True = loop */
     107            inline void setLoop(bool bLoop)
     108                { this->bLoop_ = bLoop; }
     109
     110        protected:
    71111            TimerBase();
    72112
    73             virtual void run() const = 0;
    74 
    75             /** @brief Starts the Timer: Function-call after 'interval' seconds. */
    76             inline void startTimer() { this->bActive_ = true; this->time_ = this->interval_; }
    77             /** @brief Stops the Timer. */
    78             inline void stopTimer() { this->bActive_ = false; this->time_ = this->interval_; }
    79             /** @brief Pauses the Timer - it will continue with the actual state if you unpause it. */
    80             inline void pauseTimer() { this->bActive_ = false; }
    81             /** @brief Unpauses the Timer - continues with the given state. */
    82             inline void unpauseTimer() { this->bActive_ = true; }
    83             /** @brief Returns true if the Timer is active (= not stoped, not paused). @return True = Time is active */
    84             inline bool isActive() const { return this->bActive_; }
    85             /** @brief Gives the Timer some extra time. @param time The amount of extra time in seconds */
    86             inline void addTime(float time) { this->time_ += time; }
    87             /** @brief Decreases the remaining time of the Timer. @param time The amount of time to remove */
    88             inline void removeTime(float time) { this->time_ -= time; }
    89 
    90         protected:
    91             float interval_;    //!< The time-interval in seconds
    92             bool bLoop_;        //!< If true, the function gets called every 'interval' seconds
    93             bool bActive_;      //!< If true, the Timer ticks and calls the function if the time's up
    94 
    95             float time_;        //!< Internal variable, counting the time till the next function-call
     113            Executor* executor_; //!< The executor of the function that should be called when the time expires
     114
     115            float interval_;     //!< The time-interval in seconds
     116            bool bLoop_;         //!< If true, the function gets called every 'interval' seconds
     117            bool bActive_;       //!< If true, the Timer ticks and calls the function if the time's up
     118
     119            float time_;         //!< Internal variable, counting the time till the next function-call
    96120    };
    97121
     
    101125    {
    102126        public:
    103             /** @brief Constructor: Sets the default-values. */
    104             Timer()
    105             {
    106                 this->timerFunction_ = 0;
    107                 this->object_ = 0;
    108             }
     127            Timer() {}
    109128
    110129            /**
     
    113132                @param bLoop If true, the function gets called every 'interval' seconds
    114133                @param object The object owning the timer and the function
    115                 @param timerFunction A function pointer to the function to call
    116             */
    117             Timer(float interval, bool bLoop, T* object, void (T::*timerFunction)())
    118             {
    119                 this->setTimer(interval, bLoop, timerFunction, object);
     134                @param exeuctor A executor of the function to call
     135            */
     136            Timer(float interval, bool bLoop, T* object, ExecutorMember<T>* exeuctor)
     137            {
     138                this->setTimer(interval, bLoop, object, exeuctor);
    120139            }
    121140
     
    125144                @param bLoop If true, the function gets called every 'interval' seconds
    126145                @param object The object owning the timer and the function
    127                 @param timerFunction A function pointer to the function to call
    128             */
    129             void setTimer(float interval, bool bLoop, T* object, void (T::*timerFunction)())
     146                @param exeuctor A executor of the function to call
     147            */
     148            void setTimer(float interval, bool bLoop, T* object, ExecutorMember<T>* executor)
    130149            {
    131150                this->interval_ = interval;
    132151                this->bLoop_ = bLoop;
    133                 this->timerFunction_ = timerFunction;
    134                 this->object_ = object;
     152                executor->setObject(object);
     153                this->executor_ = (Executor*)executor;
    135154                this->bActive_ = true;
    136155
    137156                this->time_ = interval;
    138157            }
    139 
    140             /** @brief Calls the given function of the given object. */
    141             void run() const
    142             {
    143                 ((*this->object_).*timerFunction_)();
    144             }
    145 
    146         private:
    147             void (T::*timerFunction_)();
    148             T* object_;
     158    };
     159
     160    //! The StaticTimer is a callback-object, calling a static function after a given time-interval.
     161    class StaticTimer : public TimerBase
     162    {
     163        public:
     164            StaticTimer() {}
     165
     166            /**
     167                @brief Constructor: Initializes the Timer with given values.
     168                @param interval The timer-interval in seconds
     169                @param bLoop If true, the function gets called every 'interval' seconds
     170                @param exeuctor A executor of the function to call
     171            */
     172            StaticTimer(float interval, bool bLoop, ExecutorStatic* executor)
     173            {
     174                this->setTimer(interval, bLoop, executor);
     175            }
     176
     177            /**
     178                @brief Initializes the Timer with given values.
     179                @param interval The timer-interval in seconds
     180                @param bLoop If true, the function gets called every 'interval' seconds
     181                @param object The object owning the timer and the function
     182                @param executor A executor of the function to call
     183            */
     184            void setTimer(float interval, bool bLoop, ExecutorStatic* executor)
     185            {
     186                this->interval_ = interval;
     187                this->bLoop_ = bLoop;
     188                this->executor_ = (Executor*)executor;
     189                this->bActive_ = true;
     190
     191                this->time_ = interval;
     192            }
    149193    };
    150194
Note: See TracChangeset for help on using the changeset viewer.