Mercurial > parpg-source
annotate systems/scriptingsystem.py @ 159:1b66e1ce226b
script commands are stored in the System now instead of the the script object.
author | KarstenBock@gmx.net |
---|---|
date | Sun, 13 Nov 2011 13:37:24 +0100 |
parents | 79d6b17b80a3 |
children | 75c0b728ccf3 |
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 |
65
e856b604b650
Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents:
41
diff
changeset
|
14 from parpg.bGrease import System |
157 | 15 from collections import deque |
16 | |
17 class Script(object): | |
18 """Script object""" | |
19 | |
159
1b66e1ce226b
script commands are stored in the System now instead of the the script object.
KarstenBock@gmx.net
parents:
157
diff
changeset
|
20 def __init__(self, condition, actions, system): |
157 | 21 """Constructor""" |
22 assert(isinstance(actions, deque)) | |
23 self.condition = condition | |
24 self.actions = actions | |
159
1b66e1ce226b
script commands are stored in the System now instead of the the script object.
KarstenBock@gmx.net
parents:
157
diff
changeset
|
25 self.system = system |
157 | 26 self.running = False |
27 self.finished = False | |
28 self.time = 0 | |
29 self.wait = 0 | |
30 self.cur_action = None | |
31 | |
32 def update(self, time): | |
33 """Advance the script""" | |
34 if not self.running: | |
35 return | |
36 if self.cur_action and not self.cur_action.executed: | |
37 return | |
38 self.time += time | |
39 if self.wait <= self.time: | |
40 self.time = 0 | |
41 try: | |
42 action = self.actions.popleft() | |
43 self.cur_action = action[0] | |
44 self.wait = action[1] | |
45 if len(action) >= 3: | |
46 vals = action[3:] if len(action) > 3 else () | |
47 command = action[2] | |
159
1b66e1ce226b
script commands are stored in the System now instead of the the script object.
KarstenBock@gmx.net
parents:
157
diff
changeset
|
48 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
|
49 *vals, |
1b66e1ce226b
script commands are stored in the System now instead of the the script object.
KarstenBock@gmx.net
parents:
157
diff
changeset
|
50 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
|
51 ) |
157 | 52 else: |
53 self.cur_action.execute() | |
54 except IndexError: | |
55 self.finished = True | |
56 self.running = False | |
57 | |
12
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
58 |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
59 class ScriptingSystem(System): |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
60 """ |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
61 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
|
62 their behavior. |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
63 """ |
157 | 64 |
159
1b66e1ce226b
script commands are stored in the System now instead of the the script object.
KarstenBock@gmx.net
parents:
157
diff
changeset
|
65 def __init__(self, funcs, commands): |
157 | 66 """Constructor""" |
67 self.funcs = funcs | |
68 self.vals = {} | |
69 self.scripts = [] | |
159
1b66e1ce226b
script commands are stored in the System now instead of the the script object.
KarstenBock@gmx.net
parents:
157
diff
changeset
|
70 self.commands = commands |
157 | 71 |
72 def step(self, dt): | |
73 """Execute a time step for the system. Must be defined | |
74 by all system classes. | |
75 | |
76 :param dt: Time since last step invocation | |
77 :type dt: float | |
78 """ | |
79 for script in self.scripts: | |
80 assert(isinstance(script, Script)) | |
81 if script.finished: | |
82 self.scripts.remove(script) | |
83 elif script.running: | |
84 script.update(dt) | |
85 elif eval(script.condition, self.funcs, self.vals): | |
86 script.running = True |