Mercurial > parpg-core
annotate src/parpg/systems/scriptingsystem.py @ 201:c0915e63a557
Added "Say" action.
author | KarstenBock@gmx.net |
---|---|
date | Thu, 15 Dec 2011 21:14:13 +0100 |
parents | cf6345ec8988 |
children |
rev | line source |
---|---|
34
5ac50245e42c
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 |
5ac50245e42c
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 |
5ac50245e42c
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 |
5ac50245e42c
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. |
5ac50245e42c
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
5 # |
5ac50245e42c
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, |
5ac50245e42c
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 |
5ac50245e42c
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 |
5ac50245e42c
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. |
5ac50245e42c
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
10 # |
5ac50245e42c
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 |
5ac50245e42c
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/>. |
5ac50245e42c
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
13 |
192 | 14 from collections import deque |
15 from copy import deepcopy | |
16 | |
92
0f659c7675f6
Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents:
66
diff
changeset
|
17 from parpg.bGrease import System |
189 | 18 |
19 class Script(object): | |
20 """Script object""" | |
21 | |
192 | 22 def __init__(self, actions, system): |
189 | 23 """Constructor""" |
24 assert(isinstance(actions, deque)) | |
25 self.actions = actions | |
192 | 26 assert(isinstance(system, ScriptingSystem)) |
191
c97e48257f51
script commands are stored in the System now instead of the the script object.
KarstenBock@gmx.net
parents:
189
diff
changeset
|
27 self.system = system |
192 | 28 self.reset() |
29 | |
30 def reset(self): | |
31 """Resets the state of the script""" | |
32 self.running_actions = deepcopy(self.actions) | |
189 | 33 self.running = False |
34 self.finished = False | |
35 self.time = 0 | |
36 self.wait = 0 | |
37 self.cur_action = None | |
192 | 38 |
189 | 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: | |
192 | 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 ) | |
197 | 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) | |
192 | 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] | |
191
c97e48257f51
script commands are stored in the System now instead of the the script object.
KarstenBock@gmx.net
parents:
189
diff
changeset
|
67 self.system.commands[command]( |
c97e48257f51
script commands are stored in the System now instead of the the script object.
KarstenBock@gmx.net
parents:
189
diff
changeset
|
68 *vals, |
c97e48257f51
script commands are stored in the System now instead of the the script object.
KarstenBock@gmx.net
parents:
189
diff
changeset
|
69 action=self.cur_action |
c97e48257f51
script commands are stored in the System now instead of the the script object.
KarstenBock@gmx.net
parents:
189
diff
changeset
|
70 ) |
189 | 71 else: |
72 self.cur_action.execute() | |
73 except IndexError: | |
74 self.finished = True | |
75 self.running = False | |
76 | |
34
5ac50245e42c
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
77 |
5ac50245e42c
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
78 class ScriptingSystem(System): |
5ac50245e42c
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
79 """ |
5ac50245e42c
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 |
5ac50245e42c
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
81 their behavior. |
5ac50245e42c
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
82 """ |
189 | 83 |
192 | 84 def __init__(self, commands, actions): |
189 | 85 """Constructor""" |
192 | 86 self.funcs = {} |
189 | 87 self.vals = {} |
191
c97e48257f51
script commands are stored in the System now instead of the the script object.
KarstenBock@gmx.net
parents:
189
diff
changeset
|
88 self.commands = commands |
192 | 89 self.actions = actions |
90 self.game_state = None | |
193
728eabc921f2
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
192
diff
changeset
|
91 self.reset() |
189 | 92 |
193
728eabc921f2
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
192
diff
changeset
|
93 def reset(self): |
728eabc921f2
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
192
diff
changeset
|
94 """Resets the script and condition collections""" |
728eabc921f2
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
192
diff
changeset
|
95 self.scripts = {} |
728eabc921f2
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
192
diff
changeset
|
96 self.conditions = [] |
728eabc921f2
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
192
diff
changeset
|
97 |
728eabc921f2
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
192
diff
changeset
|
98 |
189 | 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 """ | |
192 | 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(): | |
189 | 122 assert(isinstance(script, Script)) |
123 if script.finished: | |
192 | 124 script.reset() |
189 | 125 elif script.running: |
126 script.update(dt) | |
192 | 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 | |
193
728eabc921f2
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
192
diff
changeset
|
132 @type actions: deque or iterable |
192 | 133 """ |
193
728eabc921f2
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
192
diff
changeset
|
134 if not(isinstance(actions, deque)): |
728eabc921f2
Implemented loading scripts from files.
KarstenBock@gmx.net
parents:
192
diff
changeset
|
135 actions = deque(actions) |
192 | 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 |