From 2c844664ed953dd3e6941f741ea7057a8f34e218 Mon Sep 17 00:00:00 2001 From: Neil Date: Sat, 10 Sep 2016 12:37:57 +0200 Subject: [PATCH] Right-click and drag light rotation --- tabakrolletjie/scenes/day.py | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/tabakrolletjie/scenes/day.py b/tabakrolletjie/scenes/day.py index 589f8b2..933f16b 100644 --- a/tabakrolletjie/scenes/day.py +++ b/tabakrolletjie/scenes/day.py @@ -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): -- 2.34.1