Advertisement
Guest User

Squash n Stretch based on Velocity

a guest
Sep 15th, 2024
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.09 KB | Gaming | 0 0
  1. function s_cursor:update()
  2.     local last_coords = self.coords:copy()
  3.  
  4.     -- get inputs
  5.     self:handle_inputs()
  6.  
  7.     -- update state
  8.     if self.coords ~= last_coords then
  9.         self.target = mgr_game.layout:get_screen_coords_for_level(self.coords)
  10.         self.animator = _gfx.animator.new(150, self.current, self.target, playdate.easingFunctions.outCirc)
  11.     end
  12.  
  13.     if self.current ~= self.target then
  14.         -- animate towards our target
  15.         local last_coords = self.current:copy()
  16.         self.current = self.animator:currentValue()
  17.  
  18.         -- determine delta
  19.         self.delta.x = self.current.x - last_coords.x
  20.         self.delta.y = self.current.y - last_coords.y
  21.  
  22.         -- squash and stretch based on velocity
  23.         -- magic numbers 1.75, 0.57 are chosen to prevent the square from becoming a 1-pixel line
  24.         -- 1.75 x 0.57 is approximately 1, preserving the visual volume of the square
  25.         if (math.abs(self.delta.x) - math.abs(self.delta.y)) > 0 then
  26.             self.scale.x = math.min(1 + math.abs(self.delta.x / g_box_size), 1.75)
  27.             self.scale.y = math.max(1 - math.abs(self.delta.x / g_box_size), 0.57)
  28.         else
  29.             self.scale.x = math.max(1 - math.abs(self.delta.y / g_box_size), 0.57)
  30.             self.scale.y = math.min(1 + math.abs(self.delta.y / g_box_size), 1.75)
  31.         end
  32.  
  33.         local grid_size = g_box_size + g_padding
  34.         local cx = (self.current.x + grid_size/2) - (grid_size * self.scale.x / 2) + 1
  35.         local cy = (self.current.y + grid_size/2) - (grid_size * self.scale.y / 2) + 1
  36.         local cwidth = (g_box_size * self.scale.x)
  37.         local cheight = (g_box_size * self.scale.y)
  38.  
  39.         self:setBounds(cx, cy, cwidth, cheight)
  40.     end
  41.  
  42.     -- if we've run out of animation, reset to nil
  43.     if self.animator and self.animator:ended() then
  44.         self.animator = nil
  45.         self.current = self.target:copy()
  46.         self.delta = _gtry.point.new(0, 0)
  47.         self.scale = _gtry.point.new(1, 1)
  48.  
  49.         self:setBounds(self.current.x, self.current.y, g_box_size, g_box_size)
  50.     end
  51.  
  52.     self:markDirty()
  53. end
Tags: lua Playdate
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement