Add turnips to the space and check for collisions when planting them
[tabakrolletjie.git] / tabakrolletjie / scenes / day.py
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)