[8711] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
[8408] | 3 | |
---|
[8711] | 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 | |
---|
[9006] | 16 | |
---|
[8171] | 17 | #include "script_trigger.h" |
---|
[8202] | 18 | #include "script.h" |
---|
[8171] | 19 | |
---|
[8239] | 20 | #include "state.h" |
---|
[9869] | 21 | #include "debug.h" |
---|
| 22 | ObjectListDefinition(ScriptTrigger); |
---|
[8408] | 23 | |
---|
| 24 | |
---|
| 25 | /** |
---|
| 26 | * Constructs a new ScriptTrigger. |
---|
| 27 | * @param root the xml element to load the parameters from. |
---|
| 28 | * |
---|
| 29 | */ |
---|
[8202] | 30 | ScriptTrigger::ScriptTrigger(const TiXmlElement* root) |
---|
[9869] | 31 | { |
---|
| 32 | this->registerObject(this, ScriptTrigger::_objectList); |
---|
[8408] | 33 | this->toList(OM_COMMON); |
---|
| 34 | |
---|
[8711] | 35 | returnCount = 1; |
---|
[8894] | 36 | scriptFinished = false; |
---|
[8408] | 37 | doDebugDraw = false; |
---|
[10607] | 38 | |
---|
[8202] | 39 | scriptCalled = false; |
---|
[8211] | 40 | scriptIsOk = false; |
---|
[10565] | 41 | executionStopped = false; // true when something goes wrong and the trigger has to be stopped |
---|
[8783] | 42 | addToScript = false; |
---|
[9406] | 43 | |
---|
[8171] | 44 | } |
---|
| 45 | |
---|
[8408] | 46 | /** |
---|
| 47 | * Deletes the ScriptTrigger. |
---|
[9406] | 48 | * |
---|
[8408] | 49 | */ |
---|
[8171] | 50 | ScriptTrigger::~ScriptTrigger() |
---|
| 51 | { |
---|
| 52 | |
---|
| 53 | } |
---|
| 54 | |
---|
[8408] | 55 | /** |
---|
| 56 | * Reads the values from the tml element and sets them. |
---|
| 57 | * @param root the xml element to load the parameters from. |
---|
| 58 | * |
---|
| 59 | */ |
---|
[8171] | 60 | void ScriptTrigger::loadParams(const TiXmlElement* root) |
---|
| 61 | { |
---|
[8783] | 62 | |
---|
[9006] | 63 | WorldEntity ::loadParams(root); |
---|
[8211] | 64 | |
---|
[9006] | 65 | LoadParam(root, "file", this, ScriptTrigger, setScript) |
---|
| 66 | .describe("the fileName of the script, that should be triggered by this script trigger") |
---|
| 67 | .defaultValues(""); |
---|
| 68 | LoadParam(root, "function", this, ScriptTrigger, setFunction) |
---|
| 69 | .describe("the function of the script, that should be triggered by this script trigger") |
---|
| 70 | .defaultValues(""); |
---|
| 71 | LoadParam(root, "abs-coor", this, ScriptTrigger, setAbsCoor) |
---|
| 72 | .describe("where this script trigger should be located") |
---|
| 73 | .defaultValues(""); |
---|
| 74 | LoadParam(root, "triggerparent", this, ScriptTrigger, setTriggerParent) |
---|
| 75 | .describe("The name of the parent as it is in the *.oxw file") |
---|
| 76 | .defaultValues(""); |
---|
| 77 | LoadParam(root, "debugdraw", this, ScriptTrigger, setDebugDraw) |
---|
| 78 | .describe("") |
---|
[9110] | 79 | .defaultValues(false); |
---|
[9006] | 80 | LoadParam(root, "addtoscript", this, ScriptTrigger, setAddToScript) |
---|
| 81 | .describe("True if this scripttrigger should be aviable in the script") |
---|
[9110] | 82 | .defaultValues(false); |
---|
[8171] | 83 | } |
---|
| 84 | |
---|
| 85 | |
---|
[8408] | 86 | /** |
---|
| 87 | * Sets the parent of the trigger. |
---|
[10607] | 88 | * @param parent The parent. |
---|
[8408] | 89 | */ |
---|
[8205] | 90 | void ScriptTrigger::setTriggerParent(const std::string& parent) |
---|
| 91 | { |
---|
[9869] | 92 | WorldEntity* parentEntity = WorldEntity::objectList().getObject(parent); |
---|
[8211] | 93 | |
---|
[8205] | 94 | if (parentEntity != NULL) |
---|
| 95 | { |
---|
[9869] | 96 | this->setParent(parentEntity); |
---|
[8207] | 97 | this->setParentMode(PNODE_MOVEMENT); |
---|
[8205] | 98 | } |
---|
| 99 | else |
---|
| 100 | { |
---|
[10381] | 101 | PRINTF(2)("ERROR SCRTIPTTRIGGER : Parent %s for %s::%s does not Exist\n", parent.c_str(), this->getClassCName(), this->getCName()); |
---|
[8205] | 102 | } |
---|
| 103 | } |
---|
| 104 | |
---|
[8408] | 105 | |
---|
[10638] | 106 | void ScriptTrigger::executeScriptFunction(float timestep) |
---|
[8171] | 107 | { |
---|
[10565] | 108 | if(executionStopped && scriptIsOk) // If the script has been loaded correctly but something is wrong with the settings of the trigger |
---|
| 109 | { |
---|
| 110 | PRINT(1)("ERROR SCRTIPTTRIGGER: Something went wrong while executing %s in %s . Execution stopped! \n", functionName.c_str(), script->getFileName().c_str()); |
---|
| 111 | return; |
---|
| 112 | } |
---|
| 113 | |
---|
[9006] | 114 | if(scriptIsOk) |
---|
| 115 | { |
---|
| 116 | if(!(script->selectFunction(this->functionName,returnCount)) ) |
---|
[10564] | 117 | { |
---|
[10381] | 118 | PRINT(1)("ERROR SCRTIPTTRIGGER : Selection of %s in %s failed.\n",functionName.c_str(), script->getFileName().c_str()); |
---|
[10565] | 119 | executionStopped = true; //Since the triggersettings won't change on runtime, it makes no sense to call the function again. |
---|
[10564] | 120 | return; |
---|
| 121 | } |
---|
[9406] | 122 | |
---|
[10564] | 123 | if (! (script->pushParam( timestep, this->functionName)) ) |
---|
[10565] | 124 | { |
---|
| 125 | executionStopped = true; //Since the triggersettings won't change on runtime, it makes no sense to call the function again. |
---|
[10564] | 126 | return; |
---|
[10565] | 127 | } |
---|
[9006] | 128 | if( !(script->executeFunction()) ) |
---|
[10564] | 129 | { |
---|
[10381] | 130 | PRINT(1)("ERROR SCRTIPTTRIGGER : Execution of %s in %s failed.\n",functionName.c_str(), script->getFileName().c_str()); |
---|
[10565] | 131 | executionStopped = true; //Since the triggersettings won't change on runtime, it makes no sense to call the function again. |
---|
[10564] | 132 | return; |
---|
| 133 | } |
---|
[9006] | 134 | scriptFinished = script->getReturnedBool(); |
---|
| 135 | } |
---|
[10381] | 136 | else |
---|
| 137 | printf("ERROR SCRTIPTTRIGGER : Script could not be executed !\n"); |
---|
[9406] | 138 | |
---|
| 139 | |
---|
[8171] | 140 | } |
---|
[8211] | 141 | |
---|
| 142 | |
---|
[10619] | 143 | void ScriptTrigger::setScript(const std::string& filename) |
---|
[8211] | 144 | { |
---|
[10619] | 145 | |
---|
| 146 | std::string file = filename; |
---|
| 147 | |
---|
| 148 | unsigned int seperation = filename.find_last_of('/'); |
---|
| 149 | if (seperation != std::string::npos) |
---|
| 150 | { |
---|
| 151 | file = filename.substr( seperation+1 ); |
---|
| 152 | } |
---|
| 153 | |
---|
[8239] | 154 | ScriptManager* scriptManager = State::getScriptManager(); |
---|
| 155 | if (scriptManager != NULL) |
---|
| 156 | { |
---|
[8408] | 157 | |
---|
[8239] | 158 | script = scriptManager->getScriptByFile(file); |
---|
| 159 | if(script != NULL) |
---|
[8408] | 160 | { |
---|
[8239] | 161 | scriptIsOk = true; |
---|
[8408] | 162 | } |
---|
[10381] | 163 | else |
---|
| 164 | printf("ERROR SCRTIPTTRIGGER : Could not find the wrapperobject of %s , the script won't be executed ! \n", file.c_str()); |
---|
[8239] | 165 | } |
---|
[8214] | 166 | } |
---|