[8093] | 1 | #include <string> |
---|
| 2 | #include <list> |
---|
[8085] | 3 | |
---|
| 4 | #include "script.h" |
---|
| 5 | #include "script_manager.h" |
---|
[8155] | 6 | #include "lunar.h" |
---|
[8093] | 7 | |
---|
| 8 | |
---|
[8155] | 9 | ScriptManager::ScriptManager() |
---|
[8093] | 10 | { |
---|
[8155] | 11 | this->init(); |
---|
[8093] | 12 | } |
---|
| 13 | |
---|
| 14 | ScriptManager::~ScriptManager() |
---|
| 15 | { |
---|
| 16 | |
---|
| 17 | |
---|
| 18 | } |
---|
| 19 | |
---|
[8138] | 20 | ScriptManager* ScriptManager::getInstance() |
---|
| 21 | { |
---|
| 22 | |
---|
| 23 | if (!ScriptManager::singletonRef) |
---|
| 24 | ScriptManager::singletonRef = new ScriptManager(); |
---|
| 25 | return ScriptManager::singletonRef; |
---|
| 26 | |
---|
| 27 | } |
---|
| 28 | |
---|
[8155] | 29 | void ScriptManager::loadParams(const TiXmlElement* root) |
---|
| 30 | { |
---|
| 31 | //BaseObject::loadParams(root); |
---|
[8138] | 32 | |
---|
[8155] | 33 | const TiXmlElement* scripts = root->FirstChildElement("Scripts"); |
---|
[8093] | 34 | |
---|
[8156] | 35 | if( scripts == NULL) |
---|
[8155] | 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) |
---|
[8093] | 48 | { |
---|
[8155] | 49 | } |
---|
[8093] | 50 | |
---|
| 51 | |
---|
[8155] | 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 | } |
---|
[8093] | 58 | } |
---|
| 59 | |
---|
[8155] | 60 | void ScriptManager::init() |
---|
| 61 | { |
---|
| 62 | currentWorld.assign(""); |
---|
[8093] | 63 | |
---|
[8155] | 64 | } |
---|
[8093] | 65 | |
---|
[8155] | 66 | |
---|
[8156] | 67 | void ScriptManager::createScriptList(const TiXmlElement* scripts) |
---|
[8093] | 68 | { |
---|
[8155] | 69 | const TiXmlElement* script = scripts->FirstChildElement(); |
---|
| 70 | const TiXmlElement* object; |
---|
[8093] | 71 | |
---|
[8155] | 72 | PRINTF(4)("Creating scriptlist\n"); |
---|
[8131] | 73 | |
---|
[8155] | 74 | while( script != NULL) |
---|
| 75 | { |
---|
| 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 | if(!currentScript.name.empty()) // if LoadParam didn't fail... fill the object list with the needed objects |
---|
| 81 | { |
---|
| 82 | object = script->FirstChildElement("object"); |
---|
| 83 | |
---|
| 84 | while(object != NULL) |
---|
| 85 | { |
---|
| 86 | addObjectToScript(object->Value(), currentScript); |
---|
| 87 | object = object->NextSiblingElement("object"); |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | scriptList.push_back(currentScript); |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | script = script->NextSiblingElement("Script"); |
---|
| 94 | |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | |
---|
[8093] | 98 | } |
---|
| 99 | |
---|
[8155] | 100 | void ScriptManager::setCurrentScriptFile(std::string& file) |
---|
| 101 | { |
---|
| 102 | if( !fileIsInScriptList(file) ) |
---|
| 103 | currentScript.name = file; |
---|
| 104 | else |
---|
| 105 | currentScript.name.assign(""); |
---|
| 106 | } |
---|
[8131] | 107 | |
---|
[8155] | 108 | void ScriptManager::addObjectToScript(std::string& object, LuaScript& script) |
---|
[8131] | 109 | { |
---|
[8155] | 110 | if(!objectIsInObjectList(object,script)) |
---|
| 111 | script.objectList.push_back(object); |
---|
[8131] | 112 | } |
---|
| 113 | |
---|
[8155] | 114 | |
---|
| 115 | bool ScriptManager::fileIsInScriptList(std::string& file) |
---|
[8131] | 116 | { |
---|
[8157] | 117 | for(std::list<LuaScript>::const_iterator it = scriptList.begin(); it != scriptList.end(); it++ ) |
---|
[8155] | 118 | { |
---|
| 119 | if( (*it).name.compare(file) == 0) |
---|
| 120 | { |
---|
| 121 | return true; |
---|
| 122 | } |
---|
| 123 | } |
---|
| 124 | return false; |
---|
[8131] | 125 | } |
---|
| 126 | |
---|
| 127 | |
---|
[8155] | 128 | bool ScriptManager::objectIsInObjectList(std::string& object, const LuaScript& script) |
---|
[8131] | 129 | { |
---|
[8157] | 130 | for(std::list<std::string>::const_iterator it = script.objectList.begin(); it != script.objectList.end(); it++ ) |
---|
[8155] | 131 | { |
---|
| 132 | if( (*it).compare(object) == 0) |
---|
| 133 | { |
---|
| 134 | return true; |
---|
| 135 | } |
---|
| 136 | } |
---|
| 137 | return false; |
---|
[8131] | 138 | } |
---|
| 139 | |
---|
| 140 | |
---|