annotate pytouhou/utils/interpolator.py @ 50:811cefefb5c8

Fix a few bugs and add support for a few instructions
author Thibaut Girka <thib@sitedethib.com>
date Mon, 22 Aug 2011 21:16:47 +0200
parents 6ebf9539c077
children ab826bc29aa2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
1 class Interpolator(object):
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
2 def __init__(self, values=()):
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
3 self.values = tuple(values)
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
4 self.start_values = tuple(values)
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
5 self.end_values = tuple(values)
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
6 self.start_frame = 0
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
7 self.end_frame = 0
50
811cefefb5c8 Fix a few bugs and add support for a few instructions
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
8 self._frame = 0
13
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
9
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
10
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
11 def set_interpolation_start(self, frame, values):
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
12 self.start_values = tuple(values)
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
13 self.start_frame = frame
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
14
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
15
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
16 def set_interpolation_end(self, frame, values):
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
17 self.end_values = tuple(values)
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
18 self.end_frame = frame
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
19
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
20
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
21 def set_interpolation_end_frame(self, end_frame):
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
22 self.end_frame = end_frame
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
23
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
24
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
25 def set_interpolation_end_values(self, values):
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
26 self.end_values = tuple(values)
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
27
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
28
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
29 def update(self, frame):
50
811cefefb5c8 Fix a few bugs and add support for a few instructions
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
30 self._frame = frame
13
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
31 if frame >= self.end_frame:
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
32 self.values = tuple(self.end_values)
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
33 self.start_values = tuple(self.end_values)
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
34 self.start_frame = frame
50
811cefefb5c8 Fix a few bugs and add support for a few instructions
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
35 return frame == self.end_frame
13
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
36 else:
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
37 truc = float(frame - self.start_frame) / float(self.end_frame - self.start_frame)
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
38 self.values = tuple(start_value + truc * (end_value - start_value)
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
39 for (start_value, end_value) in zip(self.start_values, self.end_values))
50
811cefefb5c8 Fix a few bugs and add support for a few instructions
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
40 return True
20
6ebf9539c077 Handle more enemies types and movements
Thibaut Girka <thib@sitedethib.com>
parents: 13
diff changeset
41