Mercurial > parpg-source
annotate systems/scriptingsystem.py @ 162:ee2d6835d87a
Added RunScriptAction class.
author | KarstenBock@gmx.net |
---|---|
date | Sat, 19 Nov 2011 16:00:32 +0100 |
parents | d224bbce512a |
children | ed24962cdf5e |
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: | |
160 | 49 action_data = self.running_actions.popleft() |
50 action = self.system.actions[action_data[0]] | |
51 action_params = eval(action_data[1], | |
52 self.system.funcs, | |
53 self.system.vals | |
54 ) | |
55 self.cur_action = action(self.system.world, action_params) | |
56 self.wait = action_data[2] | |
57 if len(action_data) >= 4: | |
58 vals = ( | |
59 eval(action_data[4], self.system.funcs, self.system.vals) | |
60 if len(action_data) > 4 | |
61 else () | |
62 ) | |
63 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
|
64 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
|
65 *vals, |
1b66e1ce226b
script commands are stored in the System now instead of the the script object.
KarstenBock@gmx.net
parents:
157
diff
changeset
|
66 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
|
67 ) |
157 | 68 else: |
69 self.cur_action.execute() | |
70 except IndexError: | |
71 self.finished = True | |
72 self.running = False | |
73 | |
12
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
74 |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
75 class ScriptingSystem(System): |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
76 """ |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
77 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
|
78 their behavior. |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
79 """ |
157 | 80 |
160 | 81 def __init__(self, commands, actions): |
157 | 82 """Constructor""" |
160 | 83 self.funcs = {} |
157 | 84 self.vals = {} |
159
1b66e1ce226b
script commands are stored in the System now instead of the the script object.
KarstenBock@gmx.net
parents:
157
diff
changeset
|
85 self.commands = commands |
160 | 86 self.actions = actions |
87 self.game_state = None | |
161
d224bbce512a
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
160
diff
changeset
|
88 self.reset() |
157 | 89 |
161
d224bbce512a
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
160
diff
changeset
|
90 def reset(self): |
d224bbce512a
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
160
diff
changeset
|
91 """Resets the script and condition collections""" |
d224bbce512a
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
160
diff
changeset
|
92 self.scripts = {} |
d224bbce512a
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
160
diff
changeset
|
93 self.conditions = [] |
d224bbce512a
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
160
diff
changeset
|
94 |
d224bbce512a
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
160
diff
changeset
|
95 |
157 | 96 def step(self, dt): |
97 """Execute a time step for the system. Must be defined | |
98 by all system classes. | |
99 | |
100 :param dt: Time since last step invocation | |
101 :type dt: float | |
102 """ | |
160 | 103 self.vals.clear() |
104 self.vals.update( | |
105 self.game_state.getObjectDictOfMap( | |
106 self.game_state.current_map_name) | |
107 ) | |
108 self.funcs.clear() | |
109 self.funcs.update(self.game_state.funcs) | |
110 for condition_data in self.conditions: | |
111 condition = condition_data[0] | |
112 script_name = condition_data[1] | |
113 if not self.scripts.has_key(script_name): | |
114 return | |
115 script = self.scripts[script_name] | |
116 if eval(condition, self.funcs, self.vals) and not script.running: | |
117 script.running = True | |
118 for script in self.scripts.itervalues(): | |
157 | 119 assert(isinstance(script, Script)) |
120 if script.finished: | |
160 | 121 script.reset() |
157 | 122 elif script.running: |
123 script.update(dt) | |
160 | 124 |
125 def setScript(self, name, actions): | |
126 """Sets a script. | |
127 @param name: The name of the script | |
128 @param actions: What the script does | |
161
d224bbce512a
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
160
diff
changeset
|
129 @type actions: deque or iterable |
160 | 130 """ |
161
d224bbce512a
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
160
diff
changeset
|
131 if not(isinstance(actions, deque)): |
d224bbce512a
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
160
diff
changeset
|
132 actions = deque(actions) |
160 | 133 self.scripts[name] = Script(actions, |
134 self | |
135 ) | |
136 | |
137 def addCondition(self, condition, script_name): | |
138 """Adds a condition. | |
139 @param condition: Condition which will be evaluated | |
140 @param script_name: Name of the script that will be executed if the | |
141 condition evaluates to True. | |
142 """ | |
143 self.conditions.append((condition, script_name)) | |
144 | |
145 | |
146 def runScript(self, name): | |
147 """Runs a script with the given name | |
148 @param name: The name of the script""" | |
149 if self.scripts.has_key(name): | |
150 self.scripts[name].running = True | |
151 | |
152 |