diff 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
line wrap: on
line diff
--- a/pytouhou/game/item.py
+++ b/pytouhou/game/item.py
@@ -15,9 +15,11 @@
 
 from math import cos, sin, atan2, pi
 
+from pytouhou.utils.interpolator import Interpolator
+
 
 class Item(object):
-    def __init__(self, pos, item_type, angle, speed, player, game):
+    def __init__(self, start_pos, item_type, game, angle=pi/2, speed=8., player=None, end_pos=None):
         self._game = game
         self._sprite = item_type.sprite
         self._removed = False
@@ -29,12 +31,22 @@ class Item(object):
 
         self.player = player
 
-        self.x, self.y = pos
+        self.x, self.y = start_pos
         self.angle = angle
         self.speed = speed
         dx, dy = cos(angle) * speed, sin(angle) * speed
         self.delta = dx, dy
 
+        if not player:
+            #TODO: find the formulae in the binary.
+            self.speed_interpolator = None
+            if end_pos:
+                self.pos_interpolator = Interpolator(start_pos, 0,
+                                                     end_pos, 60)
+            else:
+                self.speed_interpolator = Interpolator((-2.,), 0,
+                                                       (0.,), 60)
+
         self._sprite.angle = angle
 
 
@@ -46,12 +58,24 @@ class Item(object):
     def update(self):
         dx, dy = self.delta
 
+        if self.frame == 60:
+            self.speed_interpolator = Interpolator((0.,), 60,
+                                                   (3.,), 180)
+
         if self.player is not None:
             self.angle = atan2(self.player.y - self.y, self.player.x - self.x)
-            dx, dy = cos(self.angle) * self.speed, sin(self.angle) * self.speed
+            self.x += cos(self.angle) * self.speed
+            self.y += sin(self.angle) * self.speed
+        elif self.speed_interpolator is None:
+            self.pos_interpolator.update(self.frame)
+            self.x, self.y = self.pos_interpolator.values
         else:
-            pass #TODO: item falls!
+            self.speed_interpolator.update(self.frame)
+            self.speed, = self.speed_interpolator.values
+            dx, dy = cos(self.angle) * self.speed, sin(self.angle) * self.speed
+            self.delta = dx, dy
+            self.x += dx
+            self.y += dy
 
-        self.x += dx
-        self.y += dy
+        self.frame += 1