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
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
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
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
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
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]
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
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
52 else:
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
53 self.cur_action.execute()
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
54 except IndexError:
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
55 self.finished = True
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
56 self.running = False
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
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
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
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
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
66 """Constructor"""
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
67 self.funcs = funcs
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
68 self.vals = {}
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
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
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 def step(self, dt):
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
73 """Execute a time step for the system. Must be defined
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
74 by all system classes.
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
75
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
76 :param dt: Time since last step invocation
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
77 :type dt: float
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
78 """
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
79 for script in self.scripts:
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
80 assert(isinstance(script, Script))
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
81 if script.finished:
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
82 self.scripts.remove(script)
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
83 elif script.running:
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
84 script.update(dt)
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
85 elif eval(script.condition, self.funcs, self.vals):
79d6b17b80a3 Implemented simple script system.
KarstenBock@gmx.net
parents: 65
diff changeset
86 script.running = True