Last active
June 11, 2021 11:18
-
-
Save jiaaro/09d2c10856b6d65002d5aadd5d2ea1b6 to your computer and use it in GitHub Desktop.
gamepad emulation of playdate crank
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- uses: | |
-- https://github.com/rxi/classic | |
-- https://github.com/rxi/lume | |
local Object = require 'classic' | |
local lume = require 'lume' | |
---@class Controller | |
local Controller = Object:extend() | |
local function getStickAngle(x, y) | |
if math.abs(x) < .5 and math.abs(y) < .5 then | |
return | |
end | |
return lume.angle(0, 0, x, y) | |
end | |
local AnalogStickCrank = Object:extend() | |
function AnalogStickCrank:new(joystick, axisx, axisy) | |
self.joystick = joystick | |
self.axisx = axisx | |
self.axisy = axisy | |
self.pos = 0 | |
self.rotation_this_frame = 0 | |
self.last_angle = nil | |
end | |
function AnalogStickCrank:update(dt) | |
if not self.joystick then | |
return | |
end | |
local x = self.joystick:getGamepadAxis(self.axisx) | |
local y = self.joystick:getGamepadAxis(self.axisy) | |
local angle = getStickAngle(x, y) | |
if not angle then | |
self.last_angle = angle | |
self.rotation_this_frame = 0 | |
return | |
end | |
if not self.last_angle then | |
self.last_angle = angle | |
end | |
self.rotation_this_frame = (angle - self.last_angle) | |
self.pos = self.pos + self.rotation_this_frame | |
self.last_angle = angle | |
end | |
function Controller:new(joystick) | |
self.joystick = joystick | |
self.crankl = AnalogStickCrank(self.joystick, 'leftx', 'lefty') | |
self.crankr = AnalogStickCrank(self.joystick, 'rightx', 'righty') | |
end | |
function Controller:update(dt) | |
self.crankl:update(dt) | |
self.crankr:update(dt) | |
end | |
---@return Controller | |
return Controller |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Controller = require 'controller' | |
local lg = love.graphics | |
function love.load() | |
_G.controller = Controller(love.joystick.getJoysticks()[1]) | |
end | |
function love.update(dt) | |
local rotation_this_frame = controller.crankr.rotation_this_frame or 0 | |
end | |
function love.draw() | |
lg.print(string.format("crank rotation: %0.3f", controller.crankr.pos), 5, 5) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment