From 03a464fef04162e3928750a31e52cf954d5af7c8 Mon Sep 17 00:00:00 2001 From: Neil Date: Thu, 8 Sep 2016 22:59:36 +0200 Subject: [PATCH] Add turnips to the space and check for collisions when planting them --- tabakrolletjie/constants.py | 1 + tabakrolletjie/scenes/day.py | 21 +++++++++++++++------ tabakrolletjie/scenes/night.py | 2 +- tabakrolletjie/turnip.py | 27 +++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 7 deletions(-) diff --git a/tabakrolletjie/constants.py b/tabakrolletjie/constants.py index 4cd1f9d..6615033 100644 --- a/tabakrolletjie/constants.py +++ b/tabakrolletjie/constants.py @@ -22,6 +22,7 @@ OBSTACLE_CATEGORY = 1 << 0 LIGHT_CATEGORY = 1 << 1 MOULD_CATEGORY = 1 << 2 FITTINGS_CATEGORY = 1 << 3 +TURNIP_CATEGORY = 1 << 4 # Font definitions FONTS = { diff --git a/tabakrolletjie/scenes/day.py b/tabakrolletjie/scenes/day.py index fd5a83a..453ccf1 100644 --- a/tabakrolletjie/scenes/day.py +++ b/tabakrolletjie/scenes/day.py @@ -13,7 +13,7 @@ from ..utils import debug_timer from ..constants import SCREEN_SIZE from ..widgets import ImageButton -from ..turnip import Turnip +from ..turnip import Turnip, TurnipInvalidPosition class DayScene(BaseScene): @@ -26,7 +26,7 @@ class DayScene(BaseScene): self._harvested = gamestate.harvested self._tool = None for turnip_data in gamestate.turnips: - turnip = Turnip(**turnip_data) + turnip = Turnip(space=self._space, **turnip_data) # Turnips grow at dawn seeds = turnip.grow() if seeds: @@ -85,10 +85,19 @@ class DayScene(BaseScene): 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 + # We don't want top-left to equal the mouse position, + # 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) + try: + turnip = Turnip(age=0, pos=pos, space=self._space) + self._turnips.append(turnip) + self._seeds -= 1 + self._update_toolbar() + except TurnipInvalidPosition as e: + # TODO: Add error sound or something + pass else: # Not tool, so check lights self._lights.toggle_nearest(ev.pos, surfpos=True) diff --git a/tabakrolletjie/scenes/night.py b/tabakrolletjie/scenes/night.py index 1c4097a..3f63f87 100644 --- a/tabakrolletjie/scenes/night.py +++ b/tabakrolletjie/scenes/night.py @@ -21,7 +21,7 @@ class NightScene(BaseScene): self._mould = Boyd(gamestate, self._space) self._turnips = [] for turnip_data in gamestate.turnips: - turnip = Turnip(**turnip_data) + turnip = Turnip(space=self._space, **turnip_data) self._turnips.append(turnip) diff --git a/tabakrolletjie/turnip.py b/tabakrolletjie/turnip.py index b53d451..27cb4de 100644 --- a/tabakrolletjie/turnip.py +++ b/tabakrolletjie/turnip.py @@ -1,14 +1,37 @@ # Brassica rapa extraterrestrialus, the common space turnip +import pymunk +import pymunk.pygame_util +import pygame.display + from .loader import loader +from .constants import TURNIP_CATEGORY, LIGHT_CATEGORY import random +TURNIP_FILTER = pymunk.ShapeFilter( + mask=pymunk.ShapeFilter.ALL_MASKS ^ LIGHT_CATEGORY, + categories=TURNIP_CATEGORY) + + +class TurnipInvalidPosition(Exception): + pass + + class Turnip(object): def __init__(self, **kwargs): self._age = kwargs.get('age', 0) self._pos = kwargs.get('pos', (0, 0)) + space = kwargs.get('space', None) self._update_image() + self._body = pymunk.Body(0, 0, pymunk.Body.STATIC) + self._shape = pymunk.Circle(self._body, 16) + self._shape.filter = TURNIP_FILTER + self._body.position = pymunk.pygame_util.from_pygame( + self._pos, pygame.display.get_surface()) + if space.shape_query(self._shape): + raise TurnipInvalidPosition() + space.add(self._body, self._shape) def _update_image(self): self._image = loader.load_image('32', 'turnip%d.png' % (self._age + 1)) @@ -19,6 +42,10 @@ class Turnip(object): def serialize(self): return {'age': self._age, 'pos': self._pos} + def remove(self): + # FIXME: Remove body from the space + pass + def grow(self): self._age += 1 if self._age >= 3: -- 2.34.1