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