Mercurial > parpg-source
annotate systems/scriptingsystem.py @ 177:62aed6388159
Fixed showing the console.
author | Beliar |
---|---|
date | Tue, 06 Mar 2012 15:15:18 +0100 |
parents | 230d316cc43b |
children | 2a12e2843984 |
rev | line source |
---|---|
12
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
1 # This program is free software: you can redistribute it and/or modify |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
2 # it under the terms of the GNU General Public License as published by |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
3 # the Free Software Foundation, either version 3 of the License, or |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
4 # (at your option) any later version. |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
5 # |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
6 # This program is distributed in the hope that it will be useful, |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
9 # GNU General Public License for more details. |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
10 # |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
11 # You should have received a copy of the GNU General Public License |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
12 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
13 |
160 | 14 from collections import deque |
15 from copy import deepcopy | |
16 | |
65
e856b604b650
Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents:
41
diff
changeset
|
17 from parpg.bGrease import System |
157 | 18 |
19 class Script(object): | |
20 """Script object""" | |
21 | |
160 | 22 def __init__(self, actions, system): |
157 | 23 """Constructor""" |
24 assert(isinstance(actions, deque)) | |
25 self.actions = actions | |
160 | 26 assert(isinstance(system, ScriptingSystem)) |
159
1b66e1ce226b
script commands are stored in the System now instead of the the script object.
KarstenBock@gmx.net
parents:
157
diff
changeset
|
27 self.system = system |
160 | 28 self.reset() |
29 | |
30 def reset(self): | |
31 """Resets the state of the script""" | |
32 self.running_actions = deepcopy(self.actions) | |
157 | 33 self.running = False |
34 self.finished = False | |
35 self.time = 0 | |
36 self.wait = 0 | |
37 self.cur_action = None | |
160 | 38 |
157 | 39 def update(self, time): |
40 """Advance the script""" | |
41 if not self.running: | |
42 return | |
43 if self.cur_action and not self.cur_action.executed: | |
44 return | |
45 self.time += time | |
46 if self.wait <= self.time: | |
47 self.time = 0 | |
48 try: | |
174
230d316cc43b
Moved the getEnvironment method from the ScriptingSystem to the GameState and renamed it to getGameEnvironment.
Beliar
parents:
173
diff
changeset
|
49 globals, locals = self.system.game_state.getGameEnvironment() |
160 | 50 action_data = self.running_actions.popleft() |
51 action = self.system.actions[action_data[0]] | |
174
230d316cc43b
Moved the getEnvironment method from the ScriptingSystem to the GameState and renamed it to getGameEnvironment.
Beliar
parents:
173
diff
changeset
|
52 action_params = eval( |
230d316cc43b
Moved the getEnvironment method from the ScriptingSystem to the GameState and renamed it to getGameEnvironment.
Beliar
parents:
173
diff
changeset
|
53 action_data[1], |
230d316cc43b
Moved the getEnvironment method from the ScriptingSystem to the GameState and renamed it to getGameEnvironment.
Beliar
parents:
173
diff
changeset
|
54 globals, locals |
230d316cc43b
Moved the getEnvironment method from the ScriptingSystem to the GameState and renamed it to getGameEnvironment.
Beliar
parents:
173
diff
changeset
|
55 ) |
164 | 56 if not (isinstance(action_params, list) |
57 or isinstance(action_params, tuple)): | |
58 action_params = [action_params] | |
59 self.cur_action = action(self.system.world, *action_params) | |
160 | 60 self.wait = action_data[2] |
61 if len(action_data) >= 4: | |
62 vals = ( | |
174
230d316cc43b
Moved the getEnvironment method from the ScriptingSystem to the GameState and renamed it to getGameEnvironment.
Beliar
parents:
173
diff
changeset
|
63 eval(action_data[4], globals, locals) |
160 | 64 if len(action_data) > 4 |
65 else () | |
66 ) | |
67 command = action_data[3] | |
159
1b66e1ce226b
script commands are stored in the System now instead of the the script object.
KarstenBock@gmx.net
parents:
157
diff
changeset
|
68 self.system.commands[command]( |
1b66e1ce226b
script commands are stored in the System now instead of the the script object.
KarstenBock@gmx.net
parents:
157
diff
changeset
|
69 *vals, |
1b66e1ce226b
script commands are stored in the System now instead of the the script object.
KarstenBock@gmx.net
parents:
157
diff
changeset
|
70 action=self.cur_action |
1b66e1ce226b
script commands are stored in the System now instead of the the script object.
KarstenBock@gmx.net
parents:
157
diff
changeset
|
71 ) |
157 | 72 else: |
73 self.cur_action.execute() | |
74 except IndexError: | |
75 self.finished = True | |
76 self.running = False | |
77 | |
12
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
78 |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
79 class ScriptingSystem(System): |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
80 """ |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
81 System responsible for managing scripts attached to entities to define |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
82 their behavior. |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
83 """ |
157 | 84 |
160 | 85 def __init__(self, commands, actions): |
157 | 86 """Constructor""" |
159
1b66e1ce226b
script commands are stored in the System now instead of the the script object.
KarstenBock@gmx.net
parents:
157
diff
changeset
|
87 self.commands = commands |
160 | 88 self.actions = actions |
89 self.game_state = None | |
161
d224bbce512a
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
160
diff
changeset
|
90 self.reset() |
157 | 91 |
161
d224bbce512a
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
160
diff
changeset
|
92 def reset(self): |
d224bbce512a
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
160
diff
changeset
|
93 """Resets the script and condition collections""" |
d224bbce512a
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
160
diff
changeset
|
94 self.scripts = {} |
d224bbce512a
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
160
diff
changeset
|
95 self.conditions = [] |
173
3abd31885f0f
Added a method to the ScriptingSystem that returns the environment(locals, globals) in which scripts are run.
Beliar <KarstenBock@gmx.net>
parents:
171
diff
changeset
|
96 |
157 | 97 def step(self, dt): |
98 """Execute a time step for the system. Must be defined | |
99 by all system classes. | |
100 | |
101 :param dt: Time since last step invocation | |
102 :type dt: float | |
103 """ | |
160 | 104 for condition_data in self.conditions: |
105 condition = condition_data[0] | |
106 script_name = condition_data[1] | |
107 if not self.scripts.has_key(script_name): | |
108 return | |
109 script = self.scripts[script_name] | |
174
230d316cc43b
Moved the getEnvironment method from the ScriptingSystem to the GameState and renamed it to getGameEnvironment.
Beliar
parents:
173
diff
changeset
|
110 if (eval(condition, *self.game_state.getGameEnvironment()) |
230d316cc43b
Moved the getEnvironment method from the ScriptingSystem to the GameState and renamed it to getGameEnvironment.
Beliar
parents:
173
diff
changeset
|
111 and not script.running): |
160 | 112 script.running = True |
113 for script in self.scripts.itervalues(): | |
157 | 114 assert(isinstance(script, Script)) |
115 if script.finished: | |
160 | 116 script.reset() |
157 | 117 elif script.running: |
118 script.update(dt) | |
160 | 119 |
120 def setScript(self, name, actions): | |
121 """Sets a script. | |
122 @param name: The name of the script | |
123 @param actions: What the script does | |
161
d224bbce512a
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
160
diff
changeset
|
124 @type actions: deque or iterable |
160 | 125 """ |
161
d224bbce512a
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
160
diff
changeset
|
126 if not(isinstance(actions, deque)): |
d224bbce512a
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
160
diff
changeset
|
127 actions = deque(actions) |
160 | 128 self.scripts[name] = Script(actions, |
129 self | |
130 ) | |
131 | |
132 def addCondition(self, condition, script_name): | |
133 """Adds a condition. | |
134 @param condition: Condition which will be evaluated | |
135 @param script_name: Name of the script that will be executed if the | |
136 condition evaluates to True. | |
137 """ | |
138 self.conditions.append((condition, script_name)) | |
139 | |
140 | |
141 def runScript(self, name): | |
142 """Runs a script with the given name | |
143 @param name: The name of the script""" | |
144 if self.scripts.has_key(name): | |
145 self.scripts[name].running = True | |
146 | |
171
565ffdd98d68
Added math functions to the scripting system functions.
Beliar <KarstenBock@gmx.net>
parents:
164
diff
changeset
|
147 |