annotate pytouhou/game/item.py @ 153:37df8c618c2e

Add falling items.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 09 Oct 2011 16:54:49 -0700
parents 86807b8a63bd
children ebfd328e700c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
1 # -*- encoding: utf-8 -*-
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
2 ##
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com>
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
4 ##
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
5 ## This program is free software; you can redistribute it and/or modify
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
6 ## it under the terms of the GNU General Public License as published
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
7 ## by the Free Software Foundation; version 3 only.
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
8 ##
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
9 ## This program is distributed in the hope that it will be useful,
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
12 ## GNU General Public License for more details.
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
13 ##
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
14
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
15
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
16 from math import cos, sin, atan2, pi
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
17
153
37df8c618c2e Add falling items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 152
diff changeset
18 from pytouhou.utils.interpolator import Interpolator
37df8c618c2e Add falling items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 152
diff changeset
19
150
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
20
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
21 class Item(object):
153
37df8c618c2e Add falling items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 152
diff changeset
22 def __init__(self, start_pos, item_type, game, angle=pi/2, speed=8., player=None, end_pos=None):
151
5cf927cbd9c5 Merge GameState into Game. TODO: Merge PlayerState into Player
Thibaut Girka <thib@sitedethib.com>
parents: 150
diff changeset
23 self._game = game
150
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
24 self._sprite = item_type.sprite
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
25 self._removed = False
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
26 self._item_type = item_type
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
27
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
28 self.hitbox_half_size = item_type.hitbox_size / 2.
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
29
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
30 self.frame = 0
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
31
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
32 self.player = player
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
33
153
37df8c618c2e Add falling items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 152
diff changeset
34 self.x, self.y = start_pos
150
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
35 self.angle = angle
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
36 self.speed = speed
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
37 dx, dy = cos(angle) * speed, sin(angle) * speed
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
38 self.delta = dx, dy
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
39
153
37df8c618c2e Add falling items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 152
diff changeset
40 if not player:
37df8c618c2e Add falling items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 152
diff changeset
41 #TODO: find the formulae in the binary.
37df8c618c2e Add falling items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 152
diff changeset
42 self.speed_interpolator = None
37df8c618c2e Add falling items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 152
diff changeset
43 if end_pos:
37df8c618c2e Add falling items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 152
diff changeset
44 self.pos_interpolator = Interpolator(start_pos, 0,
37df8c618c2e Add falling items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 152
diff changeset
45 end_pos, 60)
37df8c618c2e Add falling items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 152
diff changeset
46 else:
37df8c618c2e Add falling items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 152
diff changeset
47 self.speed_interpolator = Interpolator((-2.,), 0,
37df8c618c2e Add falling items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 152
diff changeset
48 (0.,), 60)
37df8c618c2e Add falling items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 152
diff changeset
49
150
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
50 self._sprite.angle = angle
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
51
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
52
152
86807b8a63bd Add collisions with enemies and items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 151
diff changeset
53 def collect(self, player):
86807b8a63bd Add collisions with enemies and items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 151
diff changeset
54 player.state.score += self._item_type.score
86807b8a63bd Add collisions with enemies and items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 151
diff changeset
55 self._removed = True
86807b8a63bd Add collisions with enemies and items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 151
diff changeset
56
86807b8a63bd Add collisions with enemies and items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 151
diff changeset
57
150
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
58 def update(self):
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
59 dx, dy = self.delta
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
60
153
37df8c618c2e Add falling items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 152
diff changeset
61 if self.frame == 60:
37df8c618c2e Add falling items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 152
diff changeset
62 self.speed_interpolator = Interpolator((0.,), 60,
37df8c618c2e Add falling items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 152
diff changeset
63 (3.,), 180)
37df8c618c2e Add falling items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 152
diff changeset
64
150
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
65 if self.player is not None:
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
66 self.angle = atan2(self.player.y - self.y, self.player.x - self.x)
153
37df8c618c2e Add falling items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 152
diff changeset
67 self.x += cos(self.angle) * self.speed
37df8c618c2e Add falling items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 152
diff changeset
68 self.y += sin(self.angle) * self.speed
37df8c618c2e Add falling items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 152
diff changeset
69 elif self.speed_interpolator is None:
37df8c618c2e Add falling items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 152
diff changeset
70 self.pos_interpolator.update(self.frame)
37df8c618c2e Add falling items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 152
diff changeset
71 self.x, self.y = self.pos_interpolator.values
150
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
72 else:
153
37df8c618c2e Add falling items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 152
diff changeset
73 self.speed_interpolator.update(self.frame)
37df8c618c2e Add falling items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 152
diff changeset
74 self.speed, = self.speed_interpolator.values
37df8c618c2e Add falling items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 152
diff changeset
75 dx, dy = cos(self.angle) * self.speed, sin(self.angle) * self.speed
37df8c618c2e Add falling items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 152
diff changeset
76 self.delta = dx, dy
37df8c618c2e Add falling items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 152
diff changeset
77 self.x += dx
37df8c618c2e Add falling items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 152
diff changeset
78 self.y += dy
150
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
79
153
37df8c618c2e Add falling items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 152
diff changeset
80 self.frame += 1
150
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
81