Right-click and drag light rotation
authorNeil <neil@dip.sun.ac.za>
Sat, 10 Sep 2016 10:37:57 +0000 (12:37 +0200)
committerNeil <neil@dip.sun.ac.za>
Sat, 10 Sep 2016 10:38:03 +0000 (12:38 +0200)
tabakrolletjie/scenes/day.py

index 589f8b25d03d5fad28cf20889fe36b5fea05ae03..933f16bad399df9dd430b3737e0b5f2ee21bd5e9 100644 (file)
@@ -1,5 +1,7 @@
 """ Be prepared. """
 
+import math
+
 import pygame.display
 import pygame.locals as pgl
 
@@ -34,6 +36,7 @@ class DayScene(BaseScene):
         self._paused = False
         self._tool = None
         self._light_color = None
+        self._dragging = None
         for turnip_data in gamestate.turnips:
             turnip = Turnip(space=self._space, **turnip_data)
             # Turnips grow at dawn
@@ -113,6 +116,15 @@ class DayScene(BaseScene):
                 # TODO: Add error sound or something
                 pass
 
+    def _update_light_angle(self, pos):
+        # Update the angle of the given light
+        pos = pymunk.pygame_util.to_pygame(pos, pygame.display.get_surface())
+        distance = pos - self._dragging.position
+        angle = math.atan2(distance[1], distance[0])
+        # Set light angle to this position
+        self._dragging.ray_manager.direction = math.degrees(angle)
+        # TODO: Update gamestate with new angle
+
     def _place_spotlight(self, gamestate, colour, ev):
         if self._seeds > 5:
             pos = pymunk.pygame_util.from_pygame(ev.pos,
@@ -126,7 +138,8 @@ class DayScene(BaseScene):
                 "type": "spotlight",
                 "colour": colour,
                 "position": pos,
-                "angle_limits": [0, 90],
+                "direction": 45,
+                "spread": 90,
                 "intensity": 0.5,
                 "radius_limits": [0, 100],
             }
@@ -138,7 +151,7 @@ class DayScene(BaseScene):
             pos = pymunk.pygame_util.from_pygame(ev.pos,
                                                  pygame.display.get_surface())
             # Bail if we're too close to an existing light
-            if self._lights.nearest(pos, max_distance=25):
+            if self._lights.nearest(ev.pos, surfpos=True, max_distance=25):
                 return
             self._seeds -= 3
             self._update_toolbar(gamestate)
@@ -203,9 +216,22 @@ class DayScene(BaseScene):
                     # Not tool, so check lights
                     self._lights.toggle_nearest(ev.pos, surfpos=True)
                     print self._lights.lit_by(ev.pos, surfpos=True)
-            elif ev.button == 3 and self._tool:
-                self._tool = None
-                self._unset_cursor()
+            elif ev.button == 3:
+                light = self._lights.nearest(ev.pos, surfpos=True,
+                                             max_distance=5.0)
+                if light:
+                    # Start drag to rotate light
+                    self._dragging = light
+                elif self._tool:
+                    # Unset tool
+                    self._tool = None
+                    self._unset_cursor()
+        elif ev.type == pgl.MOUSEMOTION:
+            if self._dragging:
+                # Calculate angle between current position and mouse pos
+                self._update_light_angle(ev.pos)
+        elif ev.type == pgl.MOUSEBUTTONUP:
+            self._dragging = None
 
     @debug_timer("day.tick")
     def tick(self, gamestate):