| 1 | // Copyright 2005 by Anthony Liekens [email protected] |
| 2 | |
| 3 | #include "actions.h" |
| 4 | |
| 5 | #include "settings.h" |
| 6 | #include <iostream> |
| 7 | |
| 8 | // ##### ACTION ##### |
| 9 | using namespace std; |
| 10 | |
| 11 | Action::Action() { |
| 12 | actionID = 0; |
| 13 | } |
| 14 | |
| 15 | void |
| 16 | Action::setActionQueue( ActionQueue* actionQueue ) { |
| 17 | this->actionQueue = actionQueue; |
| 18 | } |
| 19 | |
| 20 | ActionQueue* |
| 21 | Action::getActionQueue() const { |
| 22 | return actionQueue; |
| 23 | } |
| 24 | |
| 25 | // ##### ACTIONQUEUE ##### |
| 26 | |
| 27 | ActionQueue::ActionQueue() { |
| 28 | clear(); |
| 29 | time = 0; |
| 30 | } |
| 31 | |
| 32 | void |
| 33 | ActionQueue::scheduleAction( Uint32 time, Action* action ) { |
| 34 | if( this->time > time ) { |
| 35 | std::cerr << "Action was inserted before action that has already been executed" << endl; |
| 36 | } |
| 37 | action->setActionQueue( this ); |
| 38 | insert( pair< Uint32, Action* >( time, action ) ); |
| 39 | } |
| 40 | |
| 41 | Uint32 |
| 42 | ActionQueue::timeOfNextEvent() { |
| 43 | return this->begin()->first; |
| 44 | } |
| 45 | |
| 46 | void |
| 47 | ActionQueue::executeNextEvent() { |
| 48 | time = this->begin()->first; |
| 49 | Action* a = this->begin()->second; |
| 50 | // if( time > timer.getTime() ) { |
| 51 | // SDL_Delay( time - timer.getTime() ); |
| 52 | // } |
| 53 | a->execute( time ); |
| 54 | erase( this->begin() ); |
| 55 | delete a; |
| 56 | } |
| 57 | |
| 58 | void |
| 59 | ActionQueue::executeAllEvents() { |
| 60 | while( size() > 0 ) { |
| 61 | executeNextEvent(); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | void ActionQueue::executeEventsBefore( Uint32 time ) { |
| 66 | while( ( size() > 0 ) && ( timeOfNextEvent() < time ) ) { |
| 67 | executeNextEvent(); |
| 68 | } |
| 69 | } |
| 70 |