annotate pytouhou/game/background.py @ 15:07fba4e1da65

Refactor
author Thibaut Girka <thib@sitedethib.com>
date Fri, 05 Aug 2011 21:21:06 +0200
parents 07a7f28c8aaa
children 66ce9bb440ac
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 from io import BytesIO
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
2 import os
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
3 import struct
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
4 from itertools import chain
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
5
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
6 from pytouhou.utils.interpolator import Interpolator
15
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents: 14
diff changeset
7 from pytouhou.game.sprite import Sprite
13
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
8
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
9
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
10 class Background(object):
15
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents: 14
diff changeset
11 def __init__(self, stage, anim):
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents: 14
diff changeset
12 self.stage = stage
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents: 14
diff changeset
13 self.anim = anim
13
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
14 self.objects = []
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
15 self.object_instances = []
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
16 self._uvs = b''
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
17 self._vertices = b''
15
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents: 14
diff changeset
18 self.nb_vertices = 0
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents: 14
diff changeset
19
13
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
20 self.build_objects()
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
21 self.build_object_instances()
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
22
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
23
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
24 def build_object_instances(self):
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
25 self.object_instances = []
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
26 for obj, ox, oy, oz in self.stage.object_instances:
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
27 obj_id = self.stage.objects.index(obj)
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
28
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
29 obj_instance = []
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
30 for face_vertices, face_uvs in self.objects[obj_id]:
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
31 obj_instance.append((tuple((x + ox, y + oy, z + oz)
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
32 for x, y, z in face_vertices),
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
33 face_uvs))
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
34 self.object_instances.append(obj_instance)
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
35 # Z-sorting
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
36 def keyfunc(obj):
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
37 return min(z for face in obj for x, y, z in face[0])
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
38 self.object_instances.sort(key=keyfunc, reverse=True)
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
39
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
40
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
41 def object_instances_to_vertices_uvs(self):
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
42 vertices = tuple(vertex for obj in self.object_instances
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
43 for face in obj for vertex in face[0])
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
44 uvs = tuple(uv for obj in self.object_instances
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
45 for face in obj for uv in face[1])
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
46 return vertices, uvs
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
47
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
48
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
49 def build_objects(self):
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
50 self.objects = []
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
51 for i, obj in enumerate(self.stage.objects):
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
52 faces = []
15
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents: 14
diff changeset
53 for script_index, ox, oy, oz, width_override, height_override in obj.quads:
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents: 14
diff changeset
54 sprite = Sprite(self.anim, script_index)
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents: 14
diff changeset
55 sprite.update(0, width_override, height_override)
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents: 14
diff changeset
56 uvs, vertices = sprite._uvs, tuple((x + ox, y + oy, z + oz) for x, y, z in sprite._vertices)
13
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
57 faces.append((vertices, uvs))
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
58 self.objects.append(faces)
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
59
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
60
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
61 def update(self, frame):
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
62 if not self._uvs or not self._vertices:
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
63 vertices, uvs = self.object_instances_to_vertices_uvs()
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
64 self.nb_vertices = len(vertices)
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
65 vertices_format = 'f' * (3 * self.nb_vertices)
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
66 uvs_format = 'f' * (2 * self.nb_vertices)
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
67 self._vertices = struct.pack(vertices_format, *chain(*vertices))
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
68 self._uvs = struct.pack(uvs_format, *chain(*uvs))
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
69
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
70 self.position_interpolator = Interpolator((0, 0, 0))
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
71 self.fog_interpolator = Interpolator((0, 0, 0, 0, 0))
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
72 self.position2_interpolator = Interpolator((0, 0, 0))
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
73
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
74 for frame_num, message_type, args in self.stage.script:
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
75 if frame_num == frame:
14
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 13
diff changeset
76 if message_type == 0:
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 13
diff changeset
77 self.position_interpolator.set_interpolation_start(frame_num, args)
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 13
diff changeset
78 elif message_type == 1:
13
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
79 self.fog_interpolator.set_interpolation_end_values(args)
14
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 13
diff changeset
80 elif message_type == 2:
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 13
diff changeset
81 self.position2_interpolator.set_interpolation_end_values(args)
13
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
82 elif message_type == 3:
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
83 duration, = args
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
84 self.position2_interpolator.set_interpolation_end_frame(frame_num + duration)
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
85 elif message_type == 4:
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
86 duration, = args
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
87 self.fog_interpolator.set_interpolation_end_frame(frame_num + duration)
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
88 if frame_num > frame and message_type == 0:
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
89 self.position_interpolator.set_interpolation_end(frame_num, args)
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
90 break
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
91
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
92 self.position2_interpolator.update(frame)
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
93 self.fog_interpolator.update(frame)
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
94 self.position_interpolator.update(frame)
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
95