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
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
15 from collections import deque
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
16
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
17 class Script(object):
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
18 """Script object"""
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
19
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
20 def __init__(self, condition, actions, commands):
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
21 """Constructor"""
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
22 assert(isinstance(actions, deque))
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
23 self.condition = condition
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
24 self.actions = actions
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
25 self.commands = commands
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
26 self.running = False
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
27 self.finished = False
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
28 self.time = 0
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
29 self.wait = 0
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
30 self.cur_action = None
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
31
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
32 def update(self, time):
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
33 """Advance the script"""
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
34 if not self.running:
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
35 return
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
36 if self.cur_action and not self.cur_action.executed:
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
37 return
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
38 self.time += time
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
39 if self.wait <= self.time:
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
40 self.time = 0
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
41 try:
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
42 action = self.actions.popleft()
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
43 self.cur_action = action[0]
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
44 self.wait = action[1]
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
45 if len(action) >= 3:
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
46 vals = action[3:] if len(action) > 3 else ()
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
47 command = action[2]
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
48 self.commands[command](*vals, action=self.cur_action)
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
49 else:
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
50 self.cur_action.execute()
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
51 except IndexError:
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
52 self.finished = True
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
53 self.running = False
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
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
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
61
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
62 def __init__(self, funcs):
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
63 """Constructor"""
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
64 self.funcs = funcs
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
65 self.vals = {}
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
66 self.scripts = []
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
67
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
68 def step(self, dt):
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
69 """Execute a time step for the system. Must be defined
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
70 by all system classes.
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
71
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
72 :param dt: Time since last step invocation
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
73 :type dt: float
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
74 """
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
75 for script in self.scripts:
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
76 assert(isinstance(script, Script))
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
77 if script.finished:
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
78 self.scripts.remove(script)
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
79 elif script.running:
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
80 script.update(dt)
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
81 elif eval(script.condition, self.funcs, self.vals):
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
82 script.running = True