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