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