| 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 "space_trigger.h" | 
|---|
| 18 | #include "debug.h" | 
|---|
| 19 |  | 
|---|
| 20 | ObjectListDefinition(SpaceTrigger); | 
|---|
| 21 | CREATE_FACTORY(SpaceTrigger); | 
|---|
| 22 |  | 
|---|
| 23 | CREATE_SCRIPTABLE_CLASS(SpaceTrigger, | 
|---|
| 24 |             // Coordinates | 
|---|
| 25 |                         addMethod("setAbsCoor", Executor3<PNode, lua_State*,float,float,float>(&PNode::setAbsCoor)) | 
|---|
| 26 |                             ->addMethod("getAbsCoorX", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorX)) | 
|---|
| 27 |                             ->addMethod("getAbsCoorY", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorY)) | 
|---|
| 28 |                             ->addMethod("getAbsCoorZ", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorZ)) | 
|---|
| 29 |             //Properties | 
|---|
| 30 |                             ->addMethod("setName", Executor1<BaseObject, lua_State*, const std::string&>(&BaseObject::setName)) | 
|---|
| 31 |                             ->addMethod("setTarget", Executor1<SpaceTrigger, lua_State*, const std::string&>(&SpaceTrigger::setTarget)) | 
|---|
| 32 |                             ->addMethod("setTriggerParent", Executor1<ScriptTrigger, lua_State*, const std::string&>(&ScriptTrigger::setTriggerParent)) | 
|---|
| 33 |                             ->addMethod("setTriggerRemains", Executor1<SpaceTrigger, lua_State*, bool>(&SpaceTrigger::setTriggerRemains)) | 
|---|
| 34 |                             ->addMethod("setInvert", Executor1<SpaceTrigger, lua_State*, bool>(&SpaceTrigger::setInvert)) | 
|---|
| 35 |                             ->addMethod("setRadius", Executor1<SpaceTrigger, lua_State*, float>(&SpaceTrigger::setRadius)) | 
|---|
| 36 |                             ->addMethod("setScript", Executor1<ScriptTrigger, lua_State*, const std::string&>(&ScriptTrigger::setScript)) | 
|---|
| 37 |                             ->addMethod("setFunction", Executor1<ScriptTrigger, lua_State*, const std::string&>(&ScriptTrigger::setFunction)) | 
|---|
| 38 |                             ->addMethod("setDebugDraw", Executor1<ScriptTrigger, lua_State*, bool>(&ScriptTrigger::setDebugDraw)) | 
|---|
| 39 |                             ->addMethod("setAddToScript", Executor1<ScriptTrigger, lua_State*, bool>(&ScriptTrigger::setAddToScript)) | 
|---|
| 40 |                        ); | 
|---|
| 41 |  | 
|---|
| 42 |  | 
|---|
| 43 | /** | 
|---|
| 44 |  * Constructs a new SpaceTrigger. | 
|---|
| 45 |  * @param root the xml element to load the parameters from. | 
|---|
| 46 |  * | 
|---|
| 47 |  */ | 
|---|
| 48 | SpaceTrigger::SpaceTrigger(const TiXmlElement* root) | 
|---|
| 49 | { | 
|---|
| 50 |   this->registerObject(this, SpaceTrigger::_objectList); | 
|---|
| 51 |  | 
|---|
| 52 |   radius = 10; | 
|---|
| 53 |   invert = false; | 
|---|
| 54 |   triggerRemains = true; | 
|---|
| 55 |   | 
|---|
| 56 |    if(root != NULL) | 
|---|
| 57 |   { | 
|---|
| 58 |     loadParams(root); | 
|---|
| 59 |  | 
|---|
| 60 |     if(addToScript && scriptIsOk) | 
|---|
| 61 |     { | 
|---|
| 62 |       script->addObject( "SpaceTrigger", this->getName()); | 
|---|
| 63 |     } | 
|---|
| 64 |  | 
|---|
| 65 |   } | 
|---|
| 66 |  | 
|---|
| 67 | } | 
|---|
| 68 |  | 
|---|
| 69 | /** | 
|---|
| 70 |  * Deletes the SpaceTrigger. | 
|---|
| 71 |  * | 
|---|
| 72 |  */ | 
|---|
| 73 | SpaceTrigger::~SpaceTrigger() | 
|---|
| 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 SpaceTrigger::loadParams(const TiXmlElement* root) | 
|---|
| 84 | { | 
|---|
| 85 |  | 
|---|
| 86 |   ScriptTrigger ::loadParams(root); | 
|---|
| 87 |  | 
|---|
| 88 |   | 
|---|
| 89 |   LoadParam(root, "radius", this, SpaceTrigger, setRadius) | 
|---|
| 90 |       .describe("the fileName of the script, that should be triggered by this script trigger") | 
|---|
| 91 |       .defaultValues(0); | 
|---|
| 92 |   LoadParam(root, "worldentity", this, SpaceTrigger, setTarget) | 
|---|
| 93 |       .describe("The name of the target as it is in the *.oxw file") | 
|---|
| 94 |       .defaultValues(""); | 
|---|
| 95 |   LoadParam(root, "invert", this, SpaceTrigger, setInvert) | 
|---|
| 96 |       .describe("") | 
|---|
| 97 |       .defaultValues(false); | 
|---|
| 98 |   LoadParam(root, "triggerRemains", this, SpaceTrigger, setTriggerRemains) | 
|---|
| 99 |       .describe("") | 
|---|
| 100 |       .defaultValues(true); | 
|---|
| 101 |   | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 |  | 
|---|
| 105 | /** | 
|---|
| 106 |  * Sets the target(a world entity) of the SpaceTrigger. If the distance between the target and this trigger is smaller than the radius, the script gets triggered. | 
|---|
| 107 |  * @param target The worldentity that the script supervises. | 
|---|
| 108 |  */ | 
|---|
| 109 | void SpaceTrigger::setTarget(const std::string& target) | 
|---|
| 110 | { | 
|---|
| 111 |  | 
|---|
| 112 |   WorldEntity* targetEntity = WorldEntity::objectList().getObject(target); | 
|---|
| 113 |   if (targetEntity != NULL) | 
|---|
| 114 |   { | 
|---|
| 115 |     this->setTarget(targetEntity); | 
|---|
| 116 |   } | 
|---|
| 117 |   else | 
|---|
| 118 |   { | 
|---|
| 119 |     PRINTF(2)("ERROR SCRTIPTTRIGGER : Target %s for %s::%s does not Exist\n", target.c_str(), this->getClassCName(), this->getCName()); | 
|---|
| 120 |   } | 
|---|
| 121 | } | 
|---|
| 122 |  | 
|---|
| 123 |  | 
|---|
| 124 | void SpaceTrigger::tick(float timestep) | 
|---|
| 125 | { | 
|---|
| 126 |   if( scriptFinished ) return; | 
|---|
| 127 |  | 
|---|
| 128 |   if(triggerRemains && scriptCalled ) | 
|---|
| 129 |   { | 
|---|
| 130 |     executeScriptFunction(timestep); | 
|---|
| 131 |     return; | 
|---|
| 132 |   } | 
|---|
| 133 |  | 
|---|
| 134 |   if( this->target != NULL) | 
|---|
| 135 |   { | 
|---|
| 136 |     if( !invert && this->distance(target) < radius) | 
|---|
| 137 |     { | 
|---|
| 138 |       executeScriptFunction(timestep); | 
|---|
| 139 |       scriptCalled = true; | 
|---|
| 140 |       return; | 
|---|
| 141 |  | 
|---|
| 142 |     } | 
|---|
| 143 |     else if( invert && this->distance(target) > radius) | 
|---|
| 144 |     { | 
|---|
| 145 |       executeScriptFunction(timestep); | 
|---|
| 146 |       scriptCalled = true; | 
|---|
| 147 |       return; | 
|---|
| 148 |     } | 
|---|
| 149 |   } | 
|---|
| 150 | } | 
|---|
| 151 |  | 
|---|
| 152 |  | 
|---|
| 153 |  | 
|---|