From 3b75d8ffcfe21e1f700fe64f0fe3156d810d838e Mon Sep 17 00:00:00 2001 From: Andrew Minnich Date: Thu, 8 Mar 2018 23:26:21 -0500 Subject: [PATCH] pairs --- baton.lua | 55 +++++++++++++++++++++++++++++++++++++++++++++++++------ main.lua | 3 ++- 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/baton.lua b/baton.lua index 2c67f49..598e3d5 100644 --- a/baton.lua +++ b/baton.lua @@ -22,7 +22,7 @@ function sf.joy.button(joystick, button) end end --- player class -- +-- player class - internal functions -- local Player = {} Player.__index = Player @@ -126,13 +126,48 @@ function Player:_updateControls() end end +function Player:_updatePairs() + for _, pair in pairs(self._pairs) do + -- get raw x and y + pair.rawX = self._controls[pair.controls[2]].rawValue - self._controls[pair.controls[1]].rawValue + pair.rawY = self._controls[pair.controls[4]].rawValue - self._controls[pair.controls[3]].rawValue + + -- limit to 1 + local len = (pair.rawX^2 + pair.rawY^2) ^ .5 + if len > 1 then + pair.rawX, pair.rawY = pair.rawX / len, pair.rawY / len + end + + -- deadzone + if self.config.squareDeadzone then + pair.x = math.abs(pair.rawX) > self.config.deadzone and pair.rawX or 0 + pair.y = math.abs(pair.rawY) > self.config.deadzone and pair.rawY or 0 + elseif len > self.config.deadzone then + pair.x, pair.y = pair.rawX, pair.rawY + else + pair.x, pair.y = 0, 0 + end + + -- down/pressed/released + pair.downPrevious = pair.down + pair.down = pair.x ~= 0 or pair.y ~= 0 + pair.pressed = pair.down and not pair.downPrevious + pair.released = pair.downPrevious and not pair.down + end +end + +-- player class - public API -- + function Player:update() self:_setActiveDevice() self:_updateControls() + self:_updatePairs() end function Player:getRaw(name) - if self._controls[name] then + if self._pairs[name] then + return self._pairs[name].rawX, self._pairs[name].rawY + elseif self._controls[name] then return self._controls[name].rawValue else error('No control with name "' .. name .. '" defined', 3) @@ -140,7 +175,9 @@ function Player:getRaw(name) end function Player:get(name) - if self._controls[name] then + if self._pairs[name] then + return self._pairs[name].x, self._pairs[name].y + elseif self._controls[name] then return self._controls[name].value else error('No control with name "' .. name .. '" defined', 3) @@ -148,7 +185,9 @@ function Player:get(name) end function Player:down(name) - if self._controls[name] then + if self._pairs[name] then + return self._pairs[name].down + elseif self._controls[name] then return self._controls[name].down else error('No control with name "' .. name .. '" defined', 3) @@ -156,7 +195,9 @@ function Player:down(name) end function Player:pressed(name) - if self._controls[name] then + if self._pairs[name] then + return self._pairs[name].pressed + elseif self._controls[name] then return self._controls[name].pressed else error('No control with name "' .. name .. '" defined', 3) @@ -164,7 +205,9 @@ function Player:pressed(name) end function Player:released(name) - if self._controls[name] then + if self._pairs[name] then + return self._pairs[name].released + elseif self._controls[name] then return self._controls[name].released else error('No control with name "' .. name .. '" defined', 3) diff --git a/main.lua b/main.lua index bd84451..5487a24 100644 --- a/main.lua +++ b/main.lua @@ -20,5 +20,6 @@ function love.update(dt) end function love.draw() - love.graphics.print(tostring(input:released 'left')) + local x, y = input:get 'move' + love.graphics.print(tostring(x) .. '\n' .. tostring(y)) end \ No newline at end of file