Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function s_cursor:update()
- local last_coords = self.coords:copy()
- -- get inputs
- self:handle_inputs()
- -- update state
- if self.coords ~= last_coords then
- self.target = mgr_game.layout:get_screen_coords_for_level(self.coords)
- self.animator = _gfx.animator.new(150, self.current, self.target, playdate.easingFunctions.outCirc)
- end
- if self.current ~= self.target then
- -- animate towards our target
- local last_coords = self.current:copy()
- self.current = self.animator:currentValue()
- -- determine delta
- self.delta.x = self.current.x - last_coords.x
- self.delta.y = self.current.y - last_coords.y
- -- squash and stretch based on velocity
- -- magic numbers 1.75, 0.57 are chosen to prevent the square from becoming a 1-pixel line
- -- 1.75 x 0.57 is approximately 1, preserving the visual volume of the square
- if (math.abs(self.delta.x) - math.abs(self.delta.y)) > 0 then
- self.scale.x = math.min(1 + math.abs(self.delta.x / g_box_size), 1.75)
- self.scale.y = math.max(1 - math.abs(self.delta.x / g_box_size), 0.57)
- else
- self.scale.x = math.max(1 - math.abs(self.delta.y / g_box_size), 0.57)
- self.scale.y = math.min(1 + math.abs(self.delta.y / g_box_size), 1.75)
- end
- local grid_size = g_box_size + g_padding
- local cx = (self.current.x + grid_size/2) - (grid_size * self.scale.x / 2) + 1
- local cy = (self.current.y + grid_size/2) - (grid_size * self.scale.y / 2) + 1
- local cwidth = (g_box_size * self.scale.x)
- local cheight = (g_box_size * self.scale.y)
- self:setBounds(cx, cy, cwidth, cheight)
- end
- -- if we've run out of animation, reset to nil
- if self.animator and self.animator:ended() then
- self.animator = nil
- self.current = self.target:copy()
- self.delta = _gtry.point.new(0, 0)
- self.scale = _gtry.point.new(1, 1)
- self:setBounds(self.current.x, self.current.y, g_box_size, g_box_size)
- end
- self:markDirty()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement