Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/script_engine/src/lib/script_engine/script_manager.cc @ 8170

Last change on this file since 8170 was 8170, checked in by snellen, 18 years ago

added scripttrigger

File size: 2.7 KB
RevLine 
[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]9ScriptManager::ScriptManager()
[8093]10{
[8155]11  this->init();
[8093]12}
13
14ScriptManager::~ScriptManager()
15{
16
17
18}
19
[8138]20ScriptManager* ScriptManager::getInstance()
21{
22
23  if (!ScriptManager::singletonRef)
24    ScriptManager::singletonRef = new ScriptManager();
25  return ScriptManager::singletonRef;
26
27}
28
[8155]29void 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
47void ScriptManager::tick(float timestep)
[8093]48{
[8155]49}
[8093]50
51
[8155]52void 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]60void ScriptManager::init()
61{
62  currentWorld.assign("");
[8093]63
[8155]64}
[8093]65
[8155]66
[8156]67void  ScriptManager::createScriptList(const TiXmlElement* scripts)
[8093]68{
[8155]69  const TiXmlElement* script = scripts->FirstChildElement();
[8093]70
[8155]71  PRINTF(4)("Creating scriptlist\n");
[8131]72
[8155]73  while( script != NULL)
74  {
[8170]75    { // scope to make the LoadParam work
[8155]76    LoadParam(script, "file", this, ScriptManager, setCurrentScriptFile)
77        .describe("the fileName of the script, that should be loaded into this world")
78        .defaultValues("");
[8164]79    }
[8155]80
81    if(!currentScript.name.empty()) // if LoadParam didn't fail... fill the object list with the needed objects
82    {
83
[8164]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
[8155]91    scriptList.push_back(currentScript);
92    }
93
94    script = script->NextSiblingElement("Script");
95
96  }
97
98
[8093]99}
100
[8163]101void  ScriptManager::setCurrentScriptFile(const std::string& file)
[8155]102 {
103   if( !fileIsInScriptList(file) )
104     currentScript.name = file;
105   else
106     currentScript.name.assign("");
107 }
[8131]108
[8164]109void  ScriptManager::addObjectToScript(const std::string object)
[8131]110{
[8164]111  if(!objectIsInObjectList(object,currentScript))
112    currentScript.objectList.push_back(object);
[8131]113}
114
[8155]115
[8163]116bool ScriptManager::fileIsInScriptList(const std::string& file)
[8131]117{
[8157]118  for(std::list<LuaScript>::const_iterator it = scriptList.begin(); it != scriptList.end(); it++ )
[8155]119  {
120    if( (*it).name.compare(file) == 0)
121    {
122      return true;
123    }
124  }
125  return false;
[8131]126}
127
128
[8163]129bool ScriptManager::objectIsInObjectList(const std::string& object, const LuaScript& script)
[8131]130{
[8157]131  for(std::list<std::string>::const_iterator it = script.objectList.begin(); it != script.objectList.end(); it++ )
[8155]132  {
133    if( (*it).compare(object) == 0)
134    {
135      return true;
136    }
137  }
138  return false;
[8131]139}
140
141
Note: See TracBrowser for help on using the repository browser.