virtualcursor.lua 947 B

123456789101112131415161718192021222324252627282930313233
  1. VirtualCursor = class()
  2. function VirtualCursor:init()
  3. self.cursorSpeed = 0
  4. end
  5. function VirtualCursor:getUV()
  6. return ctx.u, ctx.v
  7. end
  8. function VirtualCursor:update()
  9. local joysticks = love.joystick.getJoysticks()
  10. if #joysticks > 0 then
  11. local u, v = self:getUV()
  12. table.each(joysticks, function(joystick)
  13. local cursorSpeed = .625 * u
  14. local x, y = love.mouse.getPosition()
  15. local xx, yy = joystick:getGamepadAxis('leftx'), joystick:getGamepadAxis('lefty')
  16. local len = (xx * xx + yy * yy) ^ .5
  17. if len < .2 then len = 0 end
  18. local vx, vy = xx / len, yy / len
  19. self.cursorSpeed = lume.lerp(self.cursorSpeed, len > 0 and cursorSpeed or 0, 18 * ls.tickrate)
  20. vx = math.clamp(vx, -1, 1)
  21. vy = math.clamp(vy, -1, 1)
  22. vx = vx * self.cursorSpeed * len
  23. vy = vy * self.cursorSpeed * len
  24. love.mouse.setPosition(x + vx * ls.tickrate, y + vy * ls.tickrate)
  25. end)
  26. end
  27. end