annotate pytouhou/game/bullet.py @ 84:1a0c78e5a941

Oops O:)
author Thibaut Girka <thib@sitedethib.com>
date Sat, 03 Sep 2011 22:58:13 +0200
parents
children 3804f07d3b0e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
84
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
1 # -*- encoding: utf-8 -*-
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
2 ##
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com>
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
4 ##
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
5 ## This program is free software; you can redistribute it and/or modify
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
6 ## it under the terms of the GNU General Public License as published
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
7 ## by the Free Software Foundation; version 3 only.
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
8 ##
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
9 ## This program is distributed in the hope that it will be useful,
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
12 ## GNU General Public License for more details.
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
13 ##
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
14
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
15 from math import cos, sin, atan2, pi
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
16
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
17 from pytouhou.vm.anmrunner import ANMRunner
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
18 from pytouhou.game.sprite import Sprite
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
19
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
20
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
21 class Bullet(object):
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
22 __slots__ = ('x', 'y', 'angle', 'speed',
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
23 'flags', 'attributes', 'anim_idx', 'sprite_idx_offset', 'player',
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
24 '_game_state', '_sprite', '_anmrunner',
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
25 '_removed', '_launched')
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
26
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
27 def __init__(self, pos, anim_idx, sprite_idx_offset,
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
28 angle, speed, attributes, flags, player, game_state):
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
29 self._game_state = game_state
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
30 self._sprite = None
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
31 self._anmrunner = None
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
32 self._removed = False
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
33 self._launched = False
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
34
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
35 self.player = player
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
36
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
37 self.anim_idx = anim_idx
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
38 self.sprite_idx_offset = sprite_idx_offset
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
39
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
40 #TODO
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
41 #if flags & (2|4|8):
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
42 # index = {2: 11, 4: 12, 8: 13}[flags & (2|4|8)]
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
43 # self._sprite = Sprite()
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
44 # self._anmrunner = ANMRunner(self._game_state.resources.etama_anm_wrappers[0],
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
45 # index, self._sprite, sprite_idx_offset)
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
46
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
47 self.flags = flags
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
48 self.attributes = list(attributes)
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
49
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
50 self.x, self.y = pos
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
51 self.angle = angle
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
52 self.speed = speed
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
53
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
54
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
55 def is_visible(self, screen_width, screen_height):
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
56 if self._sprite:
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
57 tx, ty, tw, th = self._sprite.texcoords
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
58 if self._sprite.corner_relative_placement:
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
59 raise Exception #TODO
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
60 else:
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
61 tx, ty, tw, th = 0., 0., 0., 0.
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
62
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
63 max_x = tw / 2.
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
64 max_y = th / 2.
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
65 min_x = -max_x
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
66 min_y = -max_y
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
67
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
68 if any((min_x > screen_width - self.x,
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
69 max_x < -self.x,
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
70 min_y > screen_height - self.y,
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
71 max_y < -self.y)):
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
72 return False
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
73 return True
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
74
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
75
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
76 def get_objects_by_texture(self):
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
77 objects_by_texture = {}
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
78 key = self._sprite.anm.first_name, self._sprite.anm.secondary_name
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
79 key = (key, self._sprite.blendfunc)
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
80 if not key in objects_by_texture:
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
81 objects_by_texture[key] = (0, [], [], [])
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
82 vertices = tuple((x + self.x, y + self.y, z) for x, y, z in self._sprite._vertices)
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
83 objects_by_texture[key][1].extend(vertices)
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
84 objects_by_texture[key][2].extend(self._sprite._uvs)
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
85 objects_by_texture[key][3].extend(self._sprite._colors)
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
86 return objects_by_texture
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
87
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
88
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
89 def update(self):
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
90 if not self._sprite or self._sprite._removed:
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
91 self._launched = True
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
92 self._sprite = Sprite()
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
93 self._anmrunner = ANMRunner(self._game_state.resources.etama_anm_wrappers[0], #TODO
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
94 self.anim_idx, self._sprite, self.sprite_idx_offset)
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
95 #TODO: self._anmrunner.sprite_idx_offset = self.sprite_idx_offset
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
96
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
97 if self._anmrunner and not self._anmrunner.run_frame():
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
98 self._anmrunner = None
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
99
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
100 if self._sprite:
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
101 self._sprite.update()
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
102 if self._sprite._changed: #TODO
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
103 angle = pi/2.-self.angle if self._sprite.automatic_orientation else 0.
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
104 self._sprite.update_vertices_uvs_colors(angle_base=angle)
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
105
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
106 #TODO: flags
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
107 x, y = self.x, self.y
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
108
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
109 dx, dy = cos(self.angle) * self.speed, sin(self.angle) * self.speed
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
110 self.x, self.y = x + dx, y + dy
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
111