bg
[sso] / shaken_and_spit_out / pgfw / Animation.py
1 from GameChild import GameChild
2
3 class Animation(GameChild):
4
5 def __init__(self, parent, method=None, interval=None, unfiltered=False):
6 GameChild.__init__(self, parent)
7 self.unfiltered = unfiltered
8 self.default_method = method or self.build_frame
9 self.accounts = {}
10 self.register(self.default_method, interval=interval)
11 self.current_elapsed = 0
12 self.last_update = 0
13
14 def build_frame(self):
15 pass
16
17 def register(self, *args, **kwargs):
18 interval = None
19 if kwargs.has_key("interval"):
20 interval = kwargs["interval"]
21 for method in args:
22 if method not in self.accounts:
23 self.accounts[method] = Account(interval, self)
24 else:
25 self.accounts[method].set_interval(interval)
26
27 def play(self, method=None, interval=None, delay=0, play_once=False,
28 **kwargs):
29 account = self.accounts[self.get_default(method)]
30 account.set_delay(delay)
31 account.set_args(kwargs)
32 account.set_play_once(play_once)
33 if interval:
34 account.set_interval(interval)
35 account.play()
36
37 def get_default(self, method):
38 if not method:
39 method = self.default_method
40 return method
41
42 def halt(self, method=None):
43 if not method:
44 for account in self.accounts.values():
45 account.halt()
46 else:
47 if self.accounts.has_key(method):
48 self.accounts[method].halt()
49
50 def is_playing(self, method=None, check_all=False, include_delay=False):
51 if check_all:
52 return any(self.is_account_playing(account, include_delay) for \
53 method, account in self.accounts.iteritems())
54 return self.is_account_playing(self.accounts[self.get_default(method)],
55 include_delay)
56
57 def is_account_playing(self, account, include_delay):
58 return account.playing and (not include_delay or not account.delay)
59
60 def update(self):
61 for method, account in self.accounts.iteritems():
62 if account.update():
63 method(**account.args)
64
65
66 class Account:
67
68 def __init__(self, interval, animation):
69 self.animation = animation
70 self.time_filter = animation.get_game().time_filter
71 self.set_interval(interval)
72 self.set_delay(0)
73 self.set_play_once(False)
74 self.interval_index = 0
75 self.last_frame = 0
76 self.halt()
77
78 def set_interval(self, interval):
79 if isinstance(interval, int) or isinstance(interval, str):
80 interval = [interval]
81 self.interval = interval
82
83 def set_delay(self, delay):
84 self.delay = delay
85
86 def set_play_once(self, play_once):
87 self.play_once = play_once
88
89 def set_args(self, args):
90 self.args = args
91
92 def play(self):
93 self.playing = True
94
95 def halt(self):
96 self.last_update = None
97 self.playing = False
98
99 def update(self):
100 if self.playing:
101 if self.animation.unfiltered:
102 ticks = self.time_filter.get_unfiltered_ticks()
103 else:
104 ticks = self.time_filter.get_ticks()
105 self.update_delay(ticks)
106 if not self.delay:
107 interval = self.interval
108 if interval:
109 if ticks - self.last_frame < self.get_current_interval():
110 return False
111 self.last_frame = ticks
112 self.increment_interval_index()
113 if self.play_once:
114 self.halt()
115 return True
116
117 def get_current_interval(self):
118 return self.interval[self.interval_index]
119
120 def increment_interval_index(self):
121 index = self.interval_index + 1
122 if index >= len(self.interval):
123 index = 0
124 self.interval_index = index
125
126 def update_delay(self, ticks):
127 delay = self.delay
128 if delay > 0:
129 last_update = self.last_update or ticks
130 delay -= ticks - last_update
131 if delay < 0:
132 delay = 0
133 self.last_update = ticks
134 self.delay = delay