diff pytouhou/game/player.py @ 130:11ab06f4c4c6

Introduce characters!
author Thibaut Girka <thib@sitedethib.com>
date Sat, 10 Sep 2011 22:48:56 +0200
parents 694f25881d0f
children c7f0fd9d2145
line wrap: on
line diff
--- a/pytouhou/game/player.py
+++ b/pytouhou/game/player.py
@@ -13,13 +13,84 @@
 ##
 
 
-class Player(object):
-    def __init__(self):
+from math import cos, sin, atan2, pi
+
+from pytouhou.game.sprite import Sprite
+from pytouhou.vm.anmrunner import ANMRunner
+
+
+SQ2 = 2. ** 0.5 / 2.
+
+
+class PlayerState(object):
+    def __init__(self, character=0, score=0, power=0, lives=0, bombs=0):
+        self.character = character # ReimuA/ReimuB/MarisaA/MarisaB/...
+
+        self.score = score
+        self.lives = lives
+        self.bombs = bombs
+        self.power = power
+
+        self.graze = 0
+        self.points = 0
+
         self.x = 192.0
         self.y = 384.0
-        self.score = 0
-        self.graze = 0
-        self.power = 0
-        self.lives = 0
-        self.bombs = 0
-        self.character = 0 # ReimuA/ReimuB/MarisaA/MarisaB/...
+
+
+class Player(object):
+    def __init__(self, state, character):
+        self._sprite = None
+        self._anmrunner = None
+
+        self.state = state
+        self.character = character
+        self.anm_wrapper = character.anm_wrapper
+        self.direction = None
+
+        self.set_anim(0)
+
+
+    @property
+    def x(self):
+        return self.state.x
+
+
+    @property
+    def y(self):
+        return self.state.y
+
+
+    def set_anim(self, index):
+        self._sprite = Sprite()
+        self._anmrunner = ANMRunner(self.anm_wrapper, index, self._sprite)
+        self._anmrunner.run_frame()
+
+
+    def update(self, keystate):
+        try:
+            dx, dy = {16: (0.0, -1.0), 32: (0.0, 1.0), 64: (-1.0, 0.0), 128: (1.0, 0.0),
+                      16|64: (-SQ2, -SQ2), 16|128: (SQ2, -SQ2),
+                      32|64: (-SQ2, SQ2), 32|128:  (SQ2, SQ2)}[keystate & (16|32|64|128)]
+        except KeyError:
+            speed = 0.0
+            dx, dy = 0.0, 0.0
+        else:
+            speed = self.character.focused_speed if keystate & 4 else self.character.speed
+            dx, dy = dx * speed, dy * speed
+
+        if dx < 0 and self.direction != -1:
+            self.set_anim(1)
+            self.direction = -1
+        elif dx > 0 and self.direction != +1:
+            self.set_anim(3)
+            self.direction = +1
+        elif dx == 0 and self.direction is not None:
+            self.set_anim({-1: 2, +1: 4}[self.direction])
+            self.direction = None
+
+        self.state.x += dx
+        self.state.y += dy
+
+        self._anmrunner.run_frame()
+