Add basic turnip life-cycle
authorNeil <neil@dip.sun.ac.za>
Thu, 8 Sep 2016 20:38:03 +0000 (22:38 +0200)
committerNeil <neil@dip.sun.ac.za>
Thu, 8 Sep 2016 20:38:03 +0000 (22:38 +0200)
tabakrolletjie/gamestate.py
tabakrolletjie/scenes/day.py
tabakrolletjie/scenes/night.py
tabakrolletjie/turnip.py [new file with mode: 0644]

index 14da6d880f84d5e562cd30867d005b29fa6d7403..03b5f23a1d424fe937124b0b1f8390ab86cfc58c 100644 (file)
@@ -7,11 +7,34 @@ class GameState(object):
     def __init__(self):
         self._state = {
             "station": None,
+            "turnips": [],
         }
+        self.harvested = 0
 
     @property
     def station(self):
         return self._state["station"]
 
+    @property
+    def turnips(self):
+        return self._state["turnips"]
+
+    @turnips.setter
+    def turnips(self, turnip_list):
+        self._state["turnips"] = turnip_list
+
+    @property
+    def seeds(self):
+        if 'seeds' in self._state:
+            return self._state['seeds']
+        elif self._state["station"] and 'seeds' in self._state["station"]["config"]:
+            self._state['seeds'] = self._state["station"]["config"]["seeds"]
+            return self._state['seeds']
+        return 0
+
+    @seeds.setter
+    def seeds(self, value):
+        self._state['seeds'] = value
+
     def load_station(self, station):
         self._state["station"] = loader.load_station(station)
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
index edaa15a6e3b2ba559565b96f6a6ee0db6c5c3194..1c4097a72d26837e6536b055e210ac80fb43b2cb 100644 (file)
@@ -10,6 +10,7 @@ from ..obstacles import ObstacleManager
 from ..enemies import Boyd
 from ..events import SceneChangeEvent
 from ..utils import debug_timer
+from ..turnip import Turnip
 
 
 class NightScene(BaseScene):
@@ -18,11 +19,18 @@ class NightScene(BaseScene):
         self._obstacles = ObstacleManager(self._space, gamestate)
         self._lights = LightManager(self._space, gamestate)
         self._mould = Boyd(gamestate, self._space)
+        self._turnips = []
+        for turnip_data in gamestate.turnips:
+            turnip = Turnip(**turnip_data)
+            self._turnips.append(turnip)
+
 
     @debug_timer("night.render")
     def render(self, surface, gamestate):
         surface.fill((0, 0, 155))
         self._mould.render(surface)
+        for turnip in self._turnips:
+            turnip.render(surface)
         self._lights.render_light(surface)
         self._obstacles.render(surface)
         self._lights.render_fittings(surface)
@@ -44,3 +52,7 @@ class NightScene(BaseScene):
     def tick(self, gamestate):
         self._mould.tick(gamestate, self._space, self._lights)
         self._lights.tick()
+
+    def exit(self, gamestate):
+        turnip_data = [turnip.serialize() for turnip in self._turnips]
+        gamestate.turnips = turnip_data
diff --git a/tabakrolletjie/turnip.py b/tabakrolletjie/turnip.py
new file mode 100644 (file)
index 0000000..b53d451
--- /dev/null
@@ -0,0 +1,29 @@
+# Brassica rapa extraterrestrialus, the common space turnip
+
+from .loader import loader
+import random
+
+class Turnip(object):
+
+    def __init__(self, **kwargs):
+        self._age = kwargs.get('age', 0)
+        self._pos = kwargs.get('pos', (0, 0))
+        self._update_image()
+
+    def _update_image(self):
+        self._image = loader.load_image('32', 'turnip%d.png' % (self._age + 1))
+
+    def render(self, surface):
+        surface.blit(self._image, self._pos, None)
+
+    def serialize(self):
+        return {'age': self._age, 'pos': self._pos}
+
+    def grow(self):
+        self._age += 1
+        if self._age >= 3:
+            # Mature, so harvest it for seeds
+            return random.randint(1, 3)
+        else:
+            self._update_image()
+        return None