changeset 465:5f5955635d2c

Move continues to PlayerState, and make sure they aren’t reinitialized before each stage.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 11 Sep 2013 16:03:55 +0200
parents 36bc577b2392
children b16d34fca5b4
files eosd pytouhou/game/game.pyx pytouhou/game/player.pxd pytouhou/game/player.pyx pytouhou/games/eosd.py
diffstat 5 files changed, 26 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/eosd
+++ b/eosd
@@ -123,7 +123,7 @@ def main(path, data, stage_num, rank, ch
         if debug:
             if not verbosity:
                 verbosity = 'DEBUG'
-            continues = float('inf')
+            continues = -1  # Infinite lives
 
         if verbosity:
             logging.basicConfig(level=logging.__getattribute__(verbosity))
@@ -146,7 +146,7 @@ def main(path, data, stage_num, rank, ch
 
         difficulty = 16
         default_power = [0, 64, 128, 128, 128, 128, 0][stage_num - 1]
-        states = [PlayerState(character=character, power=default_power)]
+        states = [PlayerState(character=character, power=default_power, continues=continues)]
 
         game_class = GameBossRush if boss_rush else Game
 
@@ -188,7 +188,7 @@ def main(path, data, stage_num, rank, ch
 
             hints_stage = hints.stages[stage_num - 1] if hints else None
 
-            game = game_class(resource_loader, states, stage_num, rank, difficulty, common, prng=prng, continues=continues, hints=hints_stage)
+            game = game_class(resource_loader, states, stage_num, rank, difficulty, common, prng=prng, hints=hints_stage)
 
             if not enable_particles:
                 def new_particle(pos, anim, amp, number=1, reverse=False, duration=24):
@@ -208,7 +208,7 @@ def main(path, data, stage_num, rank, ch
                 if not story or stage_num == (7 if boss_rush else 6 if rank > 0 else 5):
                     break
                 stage_num += 1
-                states = [player.state.copy() for player in game.players]  # if player.state.lives >= 0]
+                states = [player.state for player in game.players]
             except GameOver:
                 print('Game over')
                 break
--- a/pytouhou/game/game.pyx
+++ b/pytouhou/game/game.pyx
@@ -26,8 +26,7 @@ from pytouhou.game.face import Face
 cdef class Game:
     def __init__(self, players, long stage, long rank, long difficulty, bullet_types,
                  laser_types, item_types, long nb_bullets_max=0, long width=384,
-                 long height=448, prng=None, interface=None, double continues=0,
-                 hints=None):
+                 long height=448, prng=None, interface=None, hints=None):
         self.width, self.height = width, height
 
         self.nb_bullets_max = nb_bullets_max
@@ -50,7 +49,6 @@ cdef class Game:
         self.interface = interface
         self.hints = hints
 
-        self.continues = continues
         self.stage = stage
         self.rank = rank
         self.difficulty = difficulty
--- a/pytouhou/game/player.pxd
+++ b/pytouhou/game/player.pxd
@@ -4,7 +4,11 @@ from pytouhou.game.game cimport Game
 cdef class PlayerState:
     cdef public double x, y
     cdef public bint touchable, focused
-    cdef public long character, score, effective_score, lives, bombs, power, graze, points, invulnerable_time, power_bonus
+    cdef public long character, score, effective_score, lives, bombs, power
+    cdef public long graze, points
+
+    cdef long invulnerable_time, power_bonus, continues, continues_used, miss,
+    cdef long bombs_used
 
 
 cdef class Player(Element):
--- a/pytouhou/game/player.pyx
+++ b/pytouhou/game/player.pyx
@@ -27,7 +27,8 @@ class GameOver(Exception):
 
 
 cdef class PlayerState:
-    def __init__(self, long character=0, long score=0, long power=0, long lives=2, long bombs=3):
+    def __init__(self, long character=0, long score=0, long power=0,
+                 long lives=2, long bombs=3, long continues=0):
         self.character = character # ReimuA/ReimuB/MarisaA/MarisaB/...
 
         self.score = score
@@ -35,6 +36,11 @@ cdef class PlayerState:
         self.lives = lives
         self.bombs = bombs
         self.power = power
+        self.continues = continues
+
+        self.continues_used = 0
+        self.miss = 0
+        self.bombs_used = 0
 
         self.graze = 0
         self.points = 0
@@ -49,11 +55,6 @@ cdef class PlayerState:
         self.power_bonus = 0 # Never goes over 30.
 
 
-    def copy(self):
-        return PlayerState(self.character, self.score,
-                           self.power, self.lives, self.bombs)
-
-
 cdef class Player(Element):
     def __init__(self, PlayerState state, Game game, anm):
         Element.__init__(self)
@@ -250,13 +251,18 @@ cdef class Player(Element):
                     if laser is not None:
                         laser.cancel()
 
+                self.state.miss += 1
                 self.state.lives -= 1
                 if self.state.lives < 0:
                     #TODO: display a menu to ask the players if they want to continue.
-                    self._game.continues -= 1
-                    if self._game.continues < 0:
+                    if self.state.continues == 0:
                         raise GameOver
 
+                    # Don’t decrement if it’s infinite.
+                    if self.state.continues >= 0:
+                        self.state.continues -= 1
+                    self.state.continues_used += 1
+
                     for i in xrange(5):
                         self._game.drop_bonus(self.state.x, self.state.y, 4,
                                               end_pos=(self._game.prng.rand_double() * 288 + 48,
--- a/pytouhou/games/eosd.py
+++ b/pytouhou/games/eosd.py
@@ -82,7 +82,7 @@ class EoSDCommon(object):
 class EoSDGame(Game):
     def __init__(self, resource_loader, player_states, stage, rank, difficulty,
                  common, nb_bullets_max=640, width=384, height=448, prng=None,
-                 continues=0, hints=None):
+                 hints=None):
 
         self.etama = common.etama #XXX
         try:
@@ -124,7 +124,7 @@ class EoSDGame(Game):
         Game.__init__(self, players, stage, rank, difficulty,
                       common.bullet_types, common.laser_types,
                       common.item_types, nb_bullets_max, width, height, prng,
-                      common.interface, continues, hints)
+                      common.interface, hints)