Add basic turnip life-cycle
[tabakrolletjie.git] / tabakrolletjie / scenes / day.py
index 8ac0ed170ac40368bc77705a071ad8bfd7d07e61..fd5a83a133043c7b65b7c0f88587c92d3ba8c8ae 100644 (file)
@@ -13,6 +13,7 @@ from ..utils import debug_timer
 
 from ..constants import SCREEN_SIZE
 from ..widgets import ImageButton
+from ..turnip import Turnip
 
 
 class DayScene(BaseScene):
@@ -20,6 +21,20 @@ class DayScene(BaseScene):
         self._space = pymunk.Space()
         self._obstacles = ObstacleManager(self._space, gamestate)
         self._lights = LightManager(self._space, gamestate)
+        self._turnips = []
+        self._seeds = gamestate.seeds
+        self._harvested = gamestate.harvested
+        self._tool = None
+        for turnip_data in gamestate.turnips:
+            turnip = Turnip(**turnip_data)
+            # Turnips grow at dawn
+            seeds = turnip.grow()
+            if seeds:
+                self._seeds += seeds
+                self._harvested += 1
+            else:
+                self._turnips.append(turnip)
+        print 'Seeds', self._seeds
         # Toolbar
         self._tools = [
             ImageButton('32', 'seed.png', name='seed',
@@ -30,6 +45,10 @@ class DayScene(BaseScene):
 
     def exit(self, gamestate):
         self._unset_cursor()
+        gamestate.seeds = self._seeds
+        gamestate.harvested = self._harvested
+        turnip_data = [turnip.serialize() for turnip in self._turnips]
+        gamestate.turnips = turnip_data
 
     @debug_timer("day.render")
     def render(self, surface, gamestate):
@@ -37,6 +56,8 @@ class DayScene(BaseScene):
         self._lights.render_light(surface)
         self._obstacles.render(surface)
         self._lights.render_fittings(surface)
+        for turnip in self._turnips:
+            turnip.render(surface)
         for tool in self._tools:
             tool.render(surface)
         self._draw_cursor(surface)
@@ -54,16 +75,28 @@ class DayScene(BaseScene):
                 # Check tools
                 for tool in self._tools:
                     if tool.pressed(ev):
-                        print 'tool', tool.name
                         if tool.name == 'reset tool':
                             self._unset_cursor()
+                            self._tool = None
                         else:
+                            self._tool = tool.name
                             self._set_cursor(tool.name)
                         return
-                # Not tool, so check lights
-                self._lights.toggle_nearest(ev.pos, surfpos=True)
-                print self._lights.lit_by(ev.pos, surfpos=True)
+                if self._tool == "seed":
+                    if self._seeds > 0:
+                        # plant seed
+                        turnip = Turnip(age=0, pos=ev.pos)
+                        self._turnips.append(turnip)
+                        self._seeds -= 1
+                        self._update_toolbar
+                else:
+                    # Not tool, so check lights
+                    self._lights.toggle_nearest(ev.pos, surfpos=True)
+                    print self._lights.lit_by(ev.pos, surfpos=True)
 
     @debug_timer("day.tick")
     def tick(self, gamestate):
         self._lights.tick()
+
+    def _update_toolbar(self):
+        pass