Mercurial > parpg-source
annotate systems/scriptingsystem.py @ 157:79d6b17b80a3
Implemented simple script system.
author | KarstenBock@gmx.net |
---|---|
date | Sat, 12 Nov 2011 16:27:39 +0100 |
parents | e856b604b650 |
children | 1b66e1ce226b |
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 | |
20 def __init__(self, condition, actions, commands): | |
21 """Constructor""" | |
22 assert(isinstance(actions, deque)) | |
23 self.condition = condition | |
24 self.actions = actions | |
25 self.commands = commands | |
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] | |
48 self.commands[command](*vals, action=self.cur_action) | |
49 else: | |
50 self.cur_action.execute() | |
51 except IndexError: | |
52 self.finished = True | |
53 self.running = False | |
54 | |
12
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
55 |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
56 class ScriptingSystem(System): |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
57 """ |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
58 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
|
59 their behavior. |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
60 """ |
157 | 61 |
62 def __init__(self, funcs): | |
63 """Constructor""" | |
64 self.funcs = funcs | |
65 self.vals = {} | |
66 self.scripts = [] | |
67 | |
68 def step(self, dt): | |
69 """Execute a time step for the system. Must be defined | |
70 by all system classes. | |
71 | |
72 :param dt: Time since last step invocation | |
73 :type dt: float | |
74 """ | |
75 for script in self.scripts: | |
76 assert(isinstance(script, Script)) | |
77 if script.finished: | |
78 self.scripts.remove(script) | |
79 elif script.running: | |
80 script.update(dt) | |
81 elif eval(script.condition, self.funcs, self.vals): | |
82 script.running = True |