| 1 | /* | 
|---|
| 2 |    orxonox - the future of 3D-vertical-scrollers | 
|---|
| 3 |  | 
|---|
| 4 |    Copyright (C) 2004 orx | 
|---|
| 5 |  | 
|---|
| 6 |    This program is free software; you can redistribute it and/or modify | 
|---|
| 7 |    it under the terms of the GNU General Public License as published by | 
|---|
| 8 |    the Free Software Foundation; either version 2, or (at your option) | 
|---|
| 9 |    any later version. | 
|---|
| 10 |  | 
|---|
| 11 | ### File Specific: | 
|---|
| 12 |    main-programmer: Silvan Nellen | 
|---|
| 13 |    co-programmer: ... | 
|---|
| 14 | */ | 
|---|
| 15 |  | 
|---|
| 16 | #include "util/loading/factory.h" | 
|---|
| 17 | #include "time_trigger.h" | 
|---|
| 18 | #include "debug.h" | 
|---|
| 19 | #include "script_class.h" | 
|---|
| 20 |  | 
|---|
| 21 | ObjectListDefinition(TimeTrigger); | 
|---|
| 22 | CREATE_FACTORY(TimeTrigger); | 
|---|
| 23 |  | 
|---|
| 24 | CREATE_SCRIPTABLE_CLASS(TimeTrigger, | 
|---|
| 25 |              // Coordinates | 
|---|
| 26 |                         addMethod("setAbsCoor", Executor3<PNode, lua_State*,float,float,float>(&PNode::setAbsCoor)) | 
|---|
| 27 |                             ->addMethod("getAbsCoorX", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorX)) | 
|---|
| 28 |                             ->addMethod("getAbsCoorY", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorY)) | 
|---|
| 29 |                             ->addMethod("getAbsCoorZ", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorZ)) | 
|---|
| 30 |              //Properties | 
|---|
| 31 |                             ->addMethod("setName", Executor1<BaseObject, lua_State*, const std::string&>(&BaseObject::setName)) | 
|---|
| 32 |                             ->addMethod("setScript", Executor1<ScriptTrigger, lua_State*, const std::string&>(&ScriptTrigger::setScript)) | 
|---|
| 33 |                             ->addMethod("setFunction", Executor1<ScriptTrigger, lua_State*, const std::string&>(&ScriptTrigger::setFunction)) | 
|---|
| 34 |                             ->addMethod("setDebugDraw", Executor1<ScriptTrigger, lua_State*, bool>(&ScriptTrigger::setDebugDraw)) | 
|---|
| 35 |                             ->addMethod("start", Executor0<TimeTrigger, lua_State*>(&TimeTrigger::start)) | 
|---|
| 36 |                             ->addMethod("stop", Executor0<TimeTrigger, lua_State*>(&TimeTrigger::stop)) | 
|---|
| 37 |                             ->addMethod("reset", Executor0<TimeTrigger, lua_State*>(&TimeTrigger::reset)) | 
|---|
| 38 |                             ->addMethod("setDelay", Executor1<TimeTrigger, lua_State*, float>(&TimeTrigger::setDelay)) | 
|---|
| 39 |                             | 
|---|
| 40 |                        ); | 
|---|
| 41 |  | 
|---|
| 42 |  | 
|---|
| 43 | /** | 
|---|
| 44 |  * Constructs a new TimeTrigger. | 
|---|
| 45 |  * @param root the xml element to load the parameters from. | 
|---|
| 46 |  * | 
|---|
| 47 |  */ | 
|---|
| 48 | TimeTrigger::TimeTrigger(const TiXmlElement* root) | 
|---|
| 49 | { | 
|---|
| 50 |   this->registerObject(this, TimeTrigger::_objectList); | 
|---|
| 51 |    | 
|---|
| 52 |   this->delay = 1; | 
|---|
| 53 |   this->currentTime = delay; | 
|---|
| 54 |   this->isStopped = true; | 
|---|
| 55 |  | 
|---|
| 56 |   if(root != NULL) | 
|---|
| 57 |   { | 
|---|
| 58 |     loadParams(root); | 
|---|
| 59 |  | 
|---|
| 60 |     if(addToScript && scriptIsOk) | 
|---|
| 61 |     { | 
|---|
| 62 |       script->addObject( "TimeTrigger", this->getName()); | 
|---|
| 63 |     } | 
|---|
| 64 |  | 
|---|
| 65 |   } | 
|---|
| 66 |  | 
|---|
| 67 | } | 
|---|
| 68 |  | 
|---|
| 69 | /** | 
|---|
| 70 |  * Deletes the TimeTrigger. | 
|---|
| 71 |  * | 
|---|
| 72 |  */ | 
|---|
| 73 | TimeTrigger::~TimeTrigger() | 
|---|
| 74 | { | 
|---|
| 75 |  | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | /** | 
|---|
| 79 |  * Reads the values from the tml element and sets them. | 
|---|
| 80 |  * @param root the xml element to load the parameters from. | 
|---|
| 81 |  * | 
|---|
| 82 |  */ | 
|---|
| 83 | void TimeTrigger::loadParams(const TiXmlElement* root) | 
|---|
| 84 | { | 
|---|
| 85 |  | 
|---|
| 86 |   ScriptTrigger ::loadParams(root); | 
|---|
| 87 |  | 
|---|
| 88 |   LoadParam(root, "delay", this, TimeTrigger, setDelay) | 
|---|
| 89 |       .describe("the time after wich the timer is activated") | 
|---|
| 90 |       .defaultValues(0); | 
|---|
| 91 |   | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|
| 94 | void TimeTrigger::tick(float timestep) | 
|---|
| 95 | { | 
|---|
| 96 |   if( scriptFinished ) return; | 
|---|
| 97 |    | 
|---|
| 98 |   if( !isStopped ) | 
|---|
| 99 |   { | 
|---|
| 100 |     currentTime -= timestep; | 
|---|
| 101 |      | 
|---|
| 102 |     if(currentTime < 0) | 
|---|
| 103 |     { | 
|---|
| 104 |       this->executeScriptFunction(timestep); | 
|---|
| 105 |       this->stop(); | 
|---|
| 106 |     } | 
|---|
| 107 |    | 
|---|
| 108 |   } | 
|---|
| 109 |  | 
|---|
| 110 |        | 
|---|
| 111 | } | 
|---|