[890] | 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 "OrxonoxStableHeaders.h" |
---|
[1052] | 29 | #include "core/Executor.h" |
---|
| 30 | #include "core/CoreIncludes.h" |
---|
| 31 | #include "core/ConsoleCommand.h" |
---|
| 32 | #include "core/CommandExecutor.h" |
---|
[1039] | 33 | #include "Timer.h" |
---|
[890] | 34 | |
---|
[852] | 35 | namespace orxonox |
---|
| 36 | { |
---|
[1052] | 37 | ConsoleCommandShortcutExtern(delay, AccessLevel::None); |
---|
| 38 | |
---|
[852] | 39 | /** |
---|
[1052] | 40 | @brief Calls a console command after 'delay' seconds. |
---|
| 41 | @param delay The delay in seconds |
---|
| 42 | @param command The console command |
---|
| 43 | */ |
---|
| 44 | void delay(float delay, const std::string& command) |
---|
| 45 | { |
---|
| 46 | StaticTimer *delaytimer = new StaticTimer(); |
---|
| 47 | ExecutorStatic* delayexecutor = createExecutor(createFunctor(&executeDelayedCommand)); |
---|
| 48 | delayexecutor->setDefaultValues(delaytimer, command); |
---|
| 49 | delaytimer->setTimer(delay, false, delayexecutor); |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | /** |
---|
| 53 | @brief Executes the command. |
---|
| 54 | @param timer The timer to destroy after the command-execution |
---|
| 55 | @param command The command to execute |
---|
| 56 | */ |
---|
| 57 | void executeDelayedCommand(StaticTimer* timer, const std::string& command) |
---|
| 58 | { |
---|
| 59 | CommandExecutor::execute(command); |
---|
| 60 | delete timer; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | /** |
---|
[852] | 64 | @brief Constructor: Sets the default-values. |
---|
| 65 | */ |
---|
| 66 | TimerBase::TimerBase() |
---|
| 67 | { |
---|
| 68 | RegisterRootObject(TimerBase); |
---|
| 69 | |
---|
[1052] | 70 | this->executor_ = 0; |
---|
[852] | 71 | this->interval_ = 0; |
---|
| 72 | this->bLoop_ = false; |
---|
| 73 | this->bActive_ = false; |
---|
| 74 | |
---|
| 75 | this->time_ = 0; |
---|
| 76 | } |
---|
[1021] | 77 | |
---|
| 78 | /** |
---|
[1052] | 79 | @brief Deletes the executor. |
---|
| 80 | */ |
---|
| 81 | TimerBase::~TimerBase() |
---|
| 82 | { |
---|
| 83 | delete this->executor_; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | /** |
---|
| 87 | @brief Executes the executor. |
---|
| 88 | */ |
---|
| 89 | void TimerBase::run() const |
---|
| 90 | { |
---|
| 91 | (*this->executor_)(); |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | /** |
---|
[1021] | 95 | @brief Updates the timer before the frames are rendered. |
---|
| 96 | */ |
---|
| 97 | void TimerBase::tick(float dt) |
---|
| 98 | { |
---|
| 99 | if (this->bActive_) |
---|
| 100 | { |
---|
| 101 | // If active: Decrease the timer by the duration of the last frame |
---|
| 102 | this->time_ -= dt; |
---|
| 103 | |
---|
| 104 | if (this->time_ <= 0) |
---|
| 105 | { |
---|
| 106 | // It's time to call the function |
---|
| 107 | if (this->bLoop_) |
---|
| 108 | // Q: Why '+=' and not '='? A: Think about it. It's more accurate like that. Seriously. |
---|
| 109 | this->time_ += this->interval_; |
---|
| 110 | else |
---|
| 111 | this->stopTimer(); // Stop the timer if we don't want to loop |
---|
| 112 | |
---|
| 113 | this->run(); |
---|
| 114 | } |
---|
| 115 | } |
---|
| 116 | } |
---|
| 117 | |
---|
[852] | 118 | } |
---|