Add turnips to the space and check for collisions when planting them
authorNeil <neil@dip.sun.ac.za>
Thu, 8 Sep 2016 20:59:36 +0000 (22:59 +0200)
committerNeil <neil@dip.sun.ac.za>
Thu, 8 Sep 2016 20:59:36 +0000 (22:59 +0200)
tabakrolletjie/constants.py
tabakrolletjie/scenes/day.py
tabakrolletjie/scenes/night.py
tabakrolletjie/turnip.py

index 4cd1f9d5572ec4e512b549f6e34ef3337a083404..66150336c4e0120bcadf741ac5a8c154e132c77c 100644 (file)
@@ -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 = {
index fd5a83a133043c7b65b7c0f88587c92d3ba8c8ae..453ccf1001bb92fdbff2a14e04b11ba28da2d5e5 100644 (file)
@@ -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)
index 1c4097a72d26837e6536b055e210ac80fb43b2cb..3f63f872dd408b8a16963b343821b60b3eca6c6c 100644 (file)
@@ -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)
 
 
index b53d451f3705a707a5ceec55bab04eb551bd4f43..27cb4de9b20c3f3af8da42fe24b347c051fcb8ec 100644 (file)
@@ -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: