ARGH
[matches/honours.git] / research / transmission_spectroscopy / simulator / pgu-0.18 / pgu / engine.py
1 """a state engine. 
2 """
3 import pygame
4 from pygame.locals import *
5
6 class State:
7     """Template Class -- for a state.
8
9     Arguments:
10         game -- The state engine.
11         value -- I usually pass in a custom value to a state
12     
13     For all of the template methods, they should return None unless they return 
14     a new State to switch the engine to.
15
16     """
17     def __init__(self,game,value=None):
18         self.game,self.value = game,value
19
20     def init(self): 
21         """Template Method - Initialize the state, called once the first time a state is selected."""
22         return
23
24     def paint(self,screen): 
25         """Template Method - Paint the screen.  Called once after the state is selected.
26         
27         State is responsible for calling pygame.display.flip() or whatever.
28
29         """
30         return
31         
32     def repaint(self): 
33         """Template Method - Request a repaint of this state."""
34         self._paint = 1
35
36     def update(self,screen):
37         """Template Method - Update the screen.
38         
39         State is responsible for calling pygame.display.update(updates) or whatever.
40
41         """
42         return
43
44     def loop(self):
45         """Template Method - Run a logic loop, called once per frame."""
46         return
47
48     def event(self,e):
49         """Template Method - Recieve an event."""
50         return
51
52
53 class Quit(State):
54     """A state to quit the state engine."""
55     
56     def init(self): 
57         self.game.quit = 1
58
59
60 class Game:
61     """Template Class - The state engine."""
62
63     def fnc(self,f,v=None):
64         s = self.state
65         if not hasattr(s,f): return 0
66         f = getattr(s,f)
67         if v != None: r = f(v)
68         else: r = f()
69         if r != None:
70             self.state = r
71             self.state._paint = 1
72             return 1
73         return 0
74         
75     def run(self,state,screen=None):
76         """Run the state engine, this is a infinite loop (until a quit occurs).
77         
78         Arguments:
79             game -- a state engine
80             screen -- the screen
81
82         """
83         self.quit = 0
84         self.state = state
85         if screen != None: self.screen = screen
86         
87         self.init()
88         
89         while not self.quit:
90             self.loop()
91
92     def loop(self):
93         s = self.state
94         if not hasattr(s,'_init') or s._init:
95             s._init = 0
96             if self.fnc('init'): return
97         else: 
98             if self.fnc('loop'): return
99         if not hasattr(s,'_paint') or s._paint:
100             s._paint = 0
101             if self.fnc('paint',self.screen): return
102         else: 
103             if self.fnc('update',self.screen): return
104         
105         for e in pygame.event.get():
106             #NOTE: this might break API?
107             #if self.event(e): return
108             if not self.event(e):
109                 if self.fnc('event',e): return
110         self.tick()
111         return
112             
113     def init(self):
114         """Template Method - called at the beginning of State.run() to initialize things."""
115         return
116         
117     def tick(self):
118         """Template Method - called once per frame, usually for timer purposes."""
119         pygame.time.wait(10)
120     
121     def event(self,e):
122         """Template Method - called with each event, so the engine can capture special events.
123         
124         Rturn a True value if the event is captured and does not need to be passed onto the current
125         state
126
127         """
128         if e.type is QUIT: 
129             self.state = Quit(self)
130             return 1
131
132

UCC git Repository :: git.ucc.asn.au