1 | #include <string> |
---|
2 | #include <list> |
---|
3 | |
---|
4 | #include "script.h" |
---|
5 | #include "script_manager.h" |
---|
6 | #include "lunar.h" |
---|
7 | |
---|
8 | |
---|
9 | ScriptManager::ScriptManager() |
---|
10 | { |
---|
11 | this->init(); |
---|
12 | } |
---|
13 | |
---|
14 | ScriptManager::~ScriptManager() |
---|
15 | { |
---|
16 | |
---|
17 | |
---|
18 | } |
---|
19 | |
---|
20 | |
---|
21 | void ScriptManager::loadParams(const TiXmlElement* root) |
---|
22 | { |
---|
23 | //BaseObject::loadParams(root); |
---|
24 | |
---|
25 | const TiXmlElement* scripts = root->FirstChildElement("Scripts"); |
---|
26 | |
---|
27 | if( scripts == NULL) |
---|
28 | { |
---|
29 | PRINTF(1)("ScriptManager is missing 'Scripts'\n"); |
---|
30 | } |
---|
31 | else |
---|
32 | { |
---|
33 | createScriptList(scripts); |
---|
34 | } |
---|
35 | |
---|
36 | |
---|
37 | } |
---|
38 | |
---|
39 | void ScriptManager::tick(float timestep) |
---|
40 | { |
---|
41 | } |
---|
42 | |
---|
43 | |
---|
44 | void ScriptManager::initScripts() |
---|
45 | { |
---|
46 | for(std::list<LuaScript>::iterator it = scriptList.begin(); it != scriptList.end(); it++ ) |
---|
47 | { |
---|
48 | (*it).script.loadFile((*it).name); |
---|
49 | } |
---|
50 | } |
---|
51 | |
---|
52 | void ScriptManager::init() |
---|
53 | { |
---|
54 | currentWorld.assign(""); |
---|
55 | |
---|
56 | } |
---|
57 | |
---|
58 | |
---|
59 | void ScriptManager::createScriptList(const TiXmlElement* scripts) |
---|
60 | { |
---|
61 | const TiXmlElement* script = scripts->FirstChildElement(); |
---|
62 | |
---|
63 | PRINTF(4)("Creating scriptlist\n"); |
---|
64 | |
---|
65 | while( script != NULL) |
---|
66 | { |
---|
67 | { // scope to make the LoadParam work |
---|
68 | LoadParam(script, "file", this, ScriptManager, setCurrentScriptFile) |
---|
69 | .describe("the fileName of the script, that should be loaded into this world") |
---|
70 | .defaultValues(""); |
---|
71 | } |
---|
72 | |
---|
73 | if(!currentScript.name.empty()) // if LoadParam didn't fail... fill the object list with the needed objects |
---|
74 | { |
---|
75 | |
---|
76 | LOAD_PARAM_START_CYCLE(script, object); |
---|
77 | { |
---|
78 | // LoadParam_CYCLE(object, "object", this, ScriptManager, addObjectToScript) |
---|
79 | // .describe("The name of an object that is needed by a script"); |
---|
80 | } |
---|
81 | LOAD_PARAM_END_CYCLE(object); |
---|
82 | |
---|
83 | scriptList.push_back(currentScript); |
---|
84 | } |
---|
85 | |
---|
86 | script = script->NextSiblingElement("Script"); |
---|
87 | |
---|
88 | } |
---|
89 | |
---|
90 | |
---|
91 | } |
---|
92 | |
---|
93 | |
---|
94 | Script* ScriptManager::getScriptByFile(std::string& file) |
---|
95 | { |
---|
96 | |
---|
97 | for(std::list<LuaScript>::iterator it = scriptList.begin(); it != scriptList.end(); it++ ) |
---|
98 | { |
---|
99 | if( (*it).name.compare(file) == 0) |
---|
100 | { |
---|
101 | return &((*it).script); |
---|
102 | } |
---|
103 | } |
---|
104 | return NULL; |
---|
105 | |
---|
106 | } |
---|
107 | |
---|
108 | void ScriptManager::setCurrentScriptFile(const std::string& file) |
---|
109 | { |
---|
110 | if( !fileIsInScriptList(file) ) |
---|
111 | currentScript.name = file; |
---|
112 | else |
---|
113 | currentScript.name.assign(""); |
---|
114 | } |
---|
115 | |
---|
116 | void ScriptManager::addObjectToScript(const std::string object) |
---|
117 | { |
---|
118 | if(!objectIsInObjectList(object,currentScript)) |
---|
119 | currentScript.objectList.push_back(object); |
---|
120 | } |
---|
121 | |
---|
122 | |
---|
123 | bool ScriptManager::fileIsInScriptList(const std::string& file) |
---|
124 | { |
---|
125 | for(std::list<LuaScript>::const_iterator it = scriptList.begin(); it != scriptList.end(); it++ ) |
---|
126 | { |
---|
127 | if( (*it).name.compare(file) == 0) |
---|
128 | { |
---|
129 | return true; |
---|
130 | } |
---|
131 | } |
---|
132 | return false; |
---|
133 | } |
---|
134 | |
---|
135 | |
---|
136 | bool ScriptManager::objectIsInObjectList(const std::string& object, const LuaScript& script) |
---|
137 | { |
---|
138 | for(std::list<std::string>::const_iterator it = script.objectList.begin(); it != script.objectList.end(); it++ ) |
---|
139 | { |
---|
140 | if( (*it).compare(object) == 0) |
---|
141 | { |
---|
142 | return true; |
---|
143 | } |
---|
144 | } |
---|
145 | return false; |
---|
146 | } |
---|
147 | |
---|
148 | |
---|