# HG changeset patch # User KarstenBock@gmx.net # Date 1321558568 -3600 # Node ID d224bbce512af55c3d5f586cc869473f1233afe7 # Parent 75c0b728ccf31a3c42841907dea706c229dab053 Implemented loading scripts from files. diff -r 75c0b728ccf3 -r d224bbce512a gamemodel.py --- a/gamemodel.py Sun Nov 13 17:19:14 2011 +0100 +++ b/gamemodel.py Thu Nov 17 20:36:08 2011 +0100 @@ -353,7 +353,30 @@ for agent in agents: if not agent == None: self.addAgent(map_name, agent) - + + def readScriptsOfMap(self, map_name, world): + """Read the scripts of the map + @param map_name: Name of the map + @type map_name: str + @param world: The current active world + @type world: parpg.world.World""" + map_scripts_file = ( + self.map_files[map_name].replace(".xml", "_scripts.yaml") + ) + if vfs.VFS.exists(map_scripts_file): + scripts_file = vfs.VFS.open(map_scripts_file) + scripts_data = yaml.load(scripts_file) + scripts = (scripts_data["Scripts"]) + conditions = ( + scripts_data["Conditions"] if + scripts_data.has_key("Conditions") else () + ) + scripting = world.systems.scripting + for name, actions in scripts.iteritems(): + scripting.setScript(name, actions) + for condition in conditions: + scripting.addCondition(*condition) + def readAllAgents(self): """Read the agents of the all_agents_file and store them""" agents_file = vfs.VFS.open(self.all_agents_file) @@ -601,10 +624,12 @@ self.game_state.clearObjects() self.game_state.maps = {} - def setActiveMap(self, map_name): + def setActiveMap(self, map_name, world): """Sets the active map that is to be rendered. @type map_name: String @param map_name: The name of the map to load + @param world: The active world + @type world: parpg.world.World @return: None""" # Turn off the camera on the old map before we turn on the camera # on the new map. diff -r 75c0b728ccf3 -r d224bbce512a gamescenecontroller.py --- a/gamescenecontroller.py Sun Nov 13 17:19:14 2011 +0100 +++ b/gamescenecontroller.py Thu Nov 17 20:36:08 2011 +0100 @@ -355,10 +355,10 @@ ) for obj in deleted: obj.delete() + self.model.loadMap(self.model.target_map_name) + self.setupScripts(self.model.target_map_name) - self.model.loadMap(self.model.target_map_name) - - self.model.setActiveMap(self.model.target_map_name) + self.model.setActiveMap(self.model.target_map_name, self) self.model.placeAgents(self) self.model.placePC(self) @@ -372,6 +372,11 @@ self.view.hud.initializeInventory() self.pause(False) + def setupScripts(self, map_name): + """Read scripts for the current map""" + self.systems.scripting.reset() + self.model.readScriptsOfMap(map_name, self) + def handleScrolling(self): """ Merge kb and mouse related scroll data, limit the speed and @@ -538,6 +543,7 @@ self.model.loadMap(self.model.game_state.current_map_name) self.model.placeAgents(self) self.model.placePC(self) + self.setupScripts(self.model.game_state.current_map_name) self.view.hud.initializeInventory() def quitGame(self): diff -r 75c0b728ccf3 -r d224bbce512a systems/scriptingsystem.py --- a/systems/scriptingsystem.py Sun Nov 13 17:19:14 2011 +0100 +++ b/systems/scriptingsystem.py Thu Nov 17 20:36:08 2011 +0100 @@ -82,12 +82,17 @@ """Constructor""" self.funcs = {} self.vals = {} - self.scripts = {} self.commands = commands - self.conditions = [] self.actions = actions self.game_state = None + self.reset() + def reset(self): + """Resets the script and condition collections""" + self.scripts = {} + self.conditions = [] + + def step(self, dt): """Execute a time step for the system. Must be defined by all system classes. @@ -121,7 +126,10 @@ """Sets a script. @param name: The name of the script @param actions: What the script does + @type actions: deque or iterable """ + if not(isinstance(actions, deque)): + actions = deque(actions) self.scripts[name] = Script(actions, self )