Serialize light state across day and night transitions
[tabakrolletjie.git] / tabakrolletjie / scenes / day.py
index f0db831da1cd068fece25d289a98266c3c3e954a..e2d1e19bcb6634b62366fb83efe1bad7fafbbb94 100644 (file)
@@ -11,7 +11,7 @@ import pymunk.pygame_util
 
 from .base import BaseScene
 from ..battery import BatteryManager
-from ..lights import LightManager, light_fitting_by_type
+from ..lights import LightManager, light_fitting_by_type, check_space_for_light
 from ..infobar import InfoBar
 from ..obstacles import ObstacleManager
 from ..events import SceneChangeEvent
@@ -19,9 +19,9 @@ from ..utils import debug_timer, shadowed_text
 from ..loader import loader
 from ..transforms import Overlay, Alpha, ColourWedges
 
-from ..constants import SCREEN_SIZE, FONTS, DEBUG
+from ..constants import SCREEN_SIZE, FONTS, FPS, NIGHT_HOURS_PER_TICK, DEBUG
 from ..widgets import ImageButton
-from ..turnip import Turnip, TurnipInvalidPosition, check_turnips
+from ..turnip import Turnip, TurnipInvalidPosition
 
 
 class DayScene(BaseScene):
@@ -129,6 +129,7 @@ class DayScene(BaseScene):
 
     def end_day(self, gamestate):
         self._battery.apply_recharge()
+        gamestate.update_lights(self._lights)
         from .night import NightScene
         SceneChangeEvent.post(scene=NightScene())
 
@@ -138,7 +139,9 @@ class DayScene(BaseScene):
 
     @property
     def power_usage(self):
-        return int(self._lights.total_power_usage())
+        power = self._lights.total_power_usage()
+        power = power / (FPS * NIGHT_HOURS_PER_TICK)
+        return int(round(power))
 
     @debug_timer("day.render")
     def render(self, surface, gamestate):
@@ -183,7 +186,7 @@ class DayScene(BaseScene):
             # since that looks weird, but we don't want to center
             # the turnip under the mouse either, since that
             # causes issues as well, so we compromise
-            pos = (ev.pos[0] - 8, ev.pos[1] - 8)
+            pos = (ev.pos[0] - 18, ev.pos[1] - 18)
             try:
                 turnip = Turnip(age=0, pos=pos, space=self._space)
                 self._turnips.append(turnip)
@@ -213,11 +216,8 @@ class DayScene(BaseScene):
         if gamestate.seeds > cost:
             pos = pymunk.pygame_util.from_pygame(
                 ev.pos, pygame.display.get_surface())
-            # Bail if we're too close to an existing light
-            if self._lights.nearest(pos, max_distance=25):
-                return
-            # Also check turnips
-            if check_turnips(self._space, pos, max_distance=25):
+            # Bail if we're too close to an existing light, obstacle or turnip
+            if check_space_for_light(self._space, pos, max_distance=25):
                 return
             gamestate.seeds -= cost
             cfg["position"] = pos