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 "script_trigger.h" |
---|
18 | #include "script.h" |
---|
19 | |
---|
20 | #include "state.h" |
---|
21 | #include "debug.h" |
---|
22 | ObjectListDefinition(ScriptTrigger); |
---|
23 | |
---|
24 | |
---|
25 | /** |
---|
26 | * Constructs a new ScriptTrigger. |
---|
27 | * @param root the xml element to load the parameters from. |
---|
28 | * |
---|
29 | */ |
---|
30 | ScriptTrigger::ScriptTrigger(const TiXmlElement* root) |
---|
31 | { |
---|
32 | this->registerObject(this, ScriptTrigger::_objectList); |
---|
33 | this->toList(OM_COMMON); |
---|
34 | |
---|
35 | returnCount = 1; |
---|
36 | scriptFinished = false; |
---|
37 | doDebugDraw = false; |
---|
38 | |
---|
39 | scriptCalled = false; |
---|
40 | scriptIsOk = false; |
---|
41 | executionStopped = false; // true when something goes wrong and the trigger has to be stopped |
---|
42 | addToScript = false; |
---|
43 | |
---|
44 | } |
---|
45 | |
---|
46 | /** |
---|
47 | * Deletes the ScriptTrigger. |
---|
48 | * |
---|
49 | */ |
---|
50 | ScriptTrigger::~ScriptTrigger() |
---|
51 | { |
---|
52 | |
---|
53 | } |
---|
54 | |
---|
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 | */ |
---|
60 | void ScriptTrigger::loadParams(const TiXmlElement* root) |
---|
61 | { |
---|
62 | |
---|
63 | WorldEntity ::loadParams(root); |
---|
64 | |
---|
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("") |
---|
79 | .defaultValues(false); |
---|
80 | LoadParam(root, "addtoscript", this, ScriptTrigger, setAddToScript) |
---|
81 | .describe("True if this scripttrigger should be aviable in the script") |
---|
82 | .defaultValues(false); |
---|
83 | } |
---|
84 | |
---|
85 | |
---|
86 | /** |
---|
87 | * Sets the parent of the trigger. |
---|
88 | * @param parent The parent. |
---|
89 | */ |
---|
90 | void ScriptTrigger::setTriggerParent(const std::string& parent) |
---|
91 | { |
---|
92 | WorldEntity* parentEntity = WorldEntity::objectList().getObject(parent); |
---|
93 | |
---|
94 | if (parentEntity != NULL) |
---|
95 | { |
---|
96 | this->setParent(parentEntity); |
---|
97 | this->setParentMode(PNODE_MOVEMENT); |
---|
98 | } |
---|
99 | else |
---|
100 | { |
---|
101 | PRINTF(2)("ERROR SCRTIPTTRIGGER : Parent %s for %s::%s does not Exist\n", parent.c_str(), this->getClassCName(), this->getCName()); |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | |
---|
106 | void ScriptTrigger::executeScriptFunction(float timestep) |
---|
107 | { |
---|
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 | |
---|
114 | if(scriptIsOk) |
---|
115 | { |
---|
116 | if(!(script->selectFunction(this->functionName,returnCount)) ) |
---|
117 | { |
---|
118 | PRINT(1)("ERROR SCRTIPTTRIGGER : Selection of %s in %s failed.\n",functionName.c_str(), script->getFileName().c_str()); |
---|
119 | executionStopped = true; //Since the triggersettings won't change on runtime, it makes no sense to call the function again. |
---|
120 | return; |
---|
121 | } |
---|
122 | |
---|
123 | if (! (script->pushParam( timestep, this->functionName)) ) |
---|
124 | { |
---|
125 | executionStopped = true; //Since the triggersettings won't change on runtime, it makes no sense to call the function again. |
---|
126 | return; |
---|
127 | } |
---|
128 | if( !(script->executeFunction()) ) |
---|
129 | { |
---|
130 | PRINT(1)("ERROR SCRTIPTTRIGGER : Execution of %s in %s failed.\n",functionName.c_str(), script->getFileName().c_str()); |
---|
131 | executionStopped = true; //Since the triggersettings won't change on runtime, it makes no sense to call the function again. |
---|
132 | return; |
---|
133 | } |
---|
134 | scriptFinished = script->getReturnedBool(); |
---|
135 | } |
---|
136 | else |
---|
137 | printf("ERROR SCRTIPTTRIGGER : Script could not be executed !\n"); |
---|
138 | |
---|
139 | |
---|
140 | } |
---|
141 | |
---|
142 | |
---|
143 | void ScriptTrigger::setScript(const std::string& filename) |
---|
144 | { |
---|
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 | |
---|
154 | ScriptManager* scriptManager = State::getScriptManager(); |
---|
155 | if (scriptManager != NULL) |
---|
156 | { |
---|
157 | |
---|
158 | script = scriptManager->getScriptByFile(file); |
---|
159 | if(script != NULL) |
---|
160 | { |
---|
161 | scriptIsOk = true; |
---|
162 | } |
---|
163 | else |
---|
164 | printf("ERROR SCRTIPTTRIGGER : Could not find the wrapperobject of %s , the script won't be executed ! \n", file.c_str()); |
---|
165 | } |
---|
166 | } |
---|