Merge branch 'master' of ctpug.org.za:tabakrolletjie
authorSimon Cross <hodgestar@gmail.com>
Sat, 10 Sep 2016 19:06:46 +0000 (21:06 +0200)
committerSimon Cross <hodgestar@gmail.com>
Sat, 10 Sep 2016 19:06:46 +0000 (21:06 +0200)
tabakrolletjie/constants.py
tabakrolletjie/infobar.py
tabakrolletjie/scenes/night.py

index 6ddfd9fd01083bda88ae028072ba811a1df1c762..0ea58f52a260aba732d68e84a8f8be7b2652bf0a 100644 (file)
@@ -19,6 +19,8 @@ FPS = 30
 
 # Night length in ticks
 NIGHT_LENGTH = 1500
+# Night length in hours
+NIGHT_LENGTH_HOURS = 12
 
 # Pymunk categories
 OBSTACLE_CATEGORY = 1 << 0
index 58aa3b6a27bdfe10932ea6f1283bbc2599999346..ac9f1a95923f53b4aa1b5eb702428e617d6bfe94 100644 (file)
@@ -54,3 +54,41 @@ class InfoBar(object):
         if text != self._text:
             self._text = text
             self._update_infobar()
+
+
+class CountDownBar(object):
+    """ Count Down Bar. """
+
+    def __init__(self, units="h", color=(255, 255, 255), pos=(1024, 10)):
+        self.units = units
+        self.color = color
+        self.pos = pos
+        self.template = "{remaining:d}{units}"
+        self._font = loader.load_font(FONTS['sans'], size=48)
+        self._text = None
+        self._countbar = None
+        self._dest = None
+
+    def render(self, surface, remaining):
+        self._update(remaining)
+        surface.blit(self._countbar, self._dest, None)
+
+    def _update_countbar(self):
+        text_img = self._font.render(self._text, True, self.color)
+        width = text_img.get_width() + 10
+        height = text_img.get_height() + 10
+        self._countbar = pygame.surface.Surface(
+            (width, height), pgl.SWSURFACE).convert_alpha()
+        self._countbar.fill((0, 0, 0, 64))
+        self._countbar.blit(text_img, (5, 3), None)
+        self._dest = (self.pos[0] - width, self.pos[1])
+
+    def _update(self, remaining):
+        options = {
+            "remaining": remaining,
+            "units": self.units,
+        }
+        text = self.template.format(**options)
+        if text != self._text:
+            self._text = text
+            self._update_countbar()
index 674d0742c4d1b5826b4e94232057ee3f2a224139..e4cc73384b9c433f61755cfd16fe73a35226770f 100644 (file)
@@ -8,7 +8,7 @@ import pymunk
 from .base import BaseScene
 from ..battery import BatteryManager
 from ..lights import LightManager
-from ..infobar import InfoBar
+from ..infobar import InfoBar, CountDownBar
 from ..obstacles import ObstacleManager
 from ..enemies import Boyd
 from ..events import SceneChangeEvent
@@ -17,12 +17,14 @@ from ..loader import loader
 from ..transforms import Overlay
 from ..turnip import Turnip
 from ..widgets import ImageButton
-from ..constants import NIGHT_LENGTH, DEBUG, FONTS, SCREEN_SIZE, FPS
+from ..constants import (
+    NIGHT_LENGTH, NIGHT_LENGTH_HOURS, DEBUG, FONTS, SCREEN_SIZE, FPS)
 
 
 class NightScene(BaseScene):
 
     DARKNESS = Overlay(colour=(0, 0, 0, 150))
+    HOURS_PER_TICK = float(NIGHT_LENGTH_HOURS) / NIGHT_LENGTH
 
     def enter(self, gamestate):
         self._space = pymunk.Space()
@@ -31,6 +33,7 @@ class NightScene(BaseScene):
         self._battery = BatteryManager(gamestate)
         self.check_battery()
         self._infobar = InfoBar("day", battery=self._battery, scene=self)
+        self._countdownbar = CountDownBar("h")
         self._mould = Boyd(gamestate, self._space)
         self._turnips = []
         for turnip_data in gamestate.turnips:
@@ -68,6 +71,10 @@ class NightScene(BaseScene):
     def power_usage(self):
         return int(self._lights.total_power_usage())
 
+    def remaining_hours(self):
+        return int(round(
+            (NIGHT_LENGTH - self._total_ticks) * self.HOURS_PER_TICK))
+
     @debug_timer("night.render")
     def render(self, surface, gamestate):
         surface.blit(self._soil, (0, 0))
@@ -87,6 +94,7 @@ class NightScene(BaseScene):
         self._obstacles.render(surface)
         self._lights.render_fittings(surface)
         self._infobar.render(surface, gamestate)
+        self._countdownbar.render(surface, self.remaining_hours())
 
         for tool in self._tools:
             tool.render(surface)