From 5b5836320f5c8c4894dff0bd76df1acaf11bd700 Mon Sep 17 00:00:00 2001 From: Neil Date: Thu, 8 Sep 2016 22:38:03 +0200 Subject: [PATCH] Add basic turnip life-cycle --- tabakrolletjie/gamestate.py | 23 +++++++++++++++++++ tabakrolletjie/scenes/day.py | 41 ++++++++++++++++++++++++++++++---- tabakrolletjie/scenes/night.py | 12 ++++++++++ tabakrolletjie/turnip.py | 29 ++++++++++++++++++++++++ 4 files changed, 101 insertions(+), 4 deletions(-) create mode 100644 tabakrolletjie/turnip.py diff --git a/tabakrolletjie/gamestate.py b/tabakrolletjie/gamestate.py index 14da6d8..03b5f23 100644 --- a/tabakrolletjie/gamestate.py +++ b/tabakrolletjie/gamestate.py @@ -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) diff --git a/tabakrolletjie/scenes/day.py b/tabakrolletjie/scenes/day.py index 8ac0ed1..fd5a83a 100644 --- a/tabakrolletjie/scenes/day.py +++ b/tabakrolletjie/scenes/day.py @@ -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 diff --git a/tabakrolletjie/scenes/night.py b/tabakrolletjie/scenes/night.py index edaa15a..1c4097a 100644 --- a/tabakrolletjie/scenes/night.py +++ b/tabakrolletjie/scenes/night.py @@ -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 index 0000000..b53d451 --- /dev/null +++ b/tabakrolletjie/turnip.py @@ -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 -- 2.34.1