LIGHT_CATEGORY = 1 << 1
MOULD_CATEGORY = 1 << 2
FITTINGS_CATEGORY = 1 << 3
+TURNIP_CATEGORY = 1 << 4
# Font definitions
FONTS = {
from ..constants import SCREEN_SIZE
from ..widgets import ImageButton
-from ..turnip import Turnip
+from ..turnip import Turnip, TurnipInvalidPosition
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:
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)
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)
# 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))
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: