Redimentary player stats.
authorSimon Cross <hodgestar@gmail.com>
Sat, 5 Mar 2016 22:32:22 +0000 (00:32 +0200)
committerSimon Cross <hodgestar@gmail.com>
Sat, 5 Mar 2016 22:32:22 +0000 (00:32 +0200)
koperkapel/scenes/level.py

index c68ac6dc03731a70269e7d1a5e928ddbafd60531..22662a2513017d5fea8c9848cc762f2cfea0fa18 100644 (file)
@@ -13,12 +13,31 @@ from ..vehicles.base import Vehicle
 from ..weapons import weapon_by_name
 
 
+class PlayerStats:
+    def __init__(self, world):
+        roaches = world.roaches
+        self.health = sum(r.health for r in roaches)
+        self.smart = self._count_attr("smart", roaches)
+        self.fast = self._count_attr("fast", roaches)
+        self.strong = self._count_attr("strong", roaches)
+
+    def __str__(self):
+        return "<PlayerStats health=%d smart=%d fast=%d strong=%d>" % (
+            self.health, self.smart, self.fast, self.strong)
+
+    def _count_attr(self, attr, roaches):
+        attrs = [r[attr] for r in roaches]
+        attrs = [a for a in attrs if a]
+        return len(attrs)
+
+
 class BaseLevelScene(Scene):
     """ Level scene. """
 
     def __init__(self):
         super().__init__()
         self._level = None
+        self._stats = None
 
     def enter(self, world):
         self._level = levels.load(world.level.name)
@@ -93,6 +112,7 @@ class GameLevelScene(BaseLevelScene):
             for generator in self._generators:
                 generator.unpause()
             return
+        self._update_player_stats(world)
         super().enter(world)
         self._roaches = self.actors.add_layer("roaches", level=10)
         self._friends = self.actors.add_layer("friendly roaches", level=9)
@@ -280,11 +300,15 @@ class GameLevelScene(BaseLevelScene):
                 bullet.game_pos[0] * TILE_SIZE + (TILE_SIZE // 2),
                 bullet.game_pos[1] * TILE_SIZE + (TILE_SIZE // 2))
         self._check_for_bullet_hits()
+        self._update_player_stats(world)
         more = self._check_held_keys(dt)
         if more:
             events.extend(more)
         return events
 
+    def _update_player_stats(self, world):
+        self._stats = PlayerStats(world)
+
     def _check_enemies(self):
         if len(self._level.enemies) != len(self._enemies):
             # New nemy has spawned