Use count down bar.
[tabakrolletjie.git] / tabakrolletjie / scenes / night.py
index 692ecf1924b828ebcc094b1ba5afb289a992bdb5..e78ba815c3294219efb0a4a421e0850969a725c9 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,19 +17,23 @@ 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
+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()
         self._obstacles = ObstacleManager(self._space, gamestate)
         self._lights = LightManager(self._space, gamestate)
         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:
@@ -62,6 +66,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))
@@ -81,6 +89,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)
@@ -147,15 +156,20 @@ class NightScene(BaseScene):
             (shadowed_text("Press any key to continue", FONTS["sans"], 24),
              (350, 240)))
 
+    def check_battery(self):
+        if self._battery.current == 0:
+            self._lights.battery_dead()
+
     @debug_timer("night.tick")
     def tick(self, gamestate):
         if self._do_ticks and not self._paused:
             if self._total_ticks < NIGHT_LENGTH:
                 self._mould.tick(gamestate, self._space, self._lights)
                 self._lights.tick()
-                if self._total_ticks % 60 == 0:
+                if self._total_ticks % FPS == 0:
                     self._battery.current -= int(
                         self._lights.total_power_usage())
+                    self.check_battery()
                 self._total_ticks += 1
             else:
                 self._end_night()