mirror of
https://github.com/TangentFoxy/baton.git
synced 2025-07-28 02:52:19 +00:00
simplify some expressions
This commit is contained in:
39
baton.lua
39
baton.lua
@@ -55,21 +55,16 @@ end
|
|||||||
|
|
||||||
function sourceFunction.joystick.axis(joystick, value)
|
function sourceFunction.joystick.axis(joystick, value)
|
||||||
local axis, direction = parseAxis(value)
|
local axis, direction = parseAxis(value)
|
||||||
if tonumber(axis) then
|
value = tonumber(axis) and joystick:getAxis(tonumber(axis))
|
||||||
value = joystick:getAxis(tonumber(axis))
|
or joystick:getGamepadAxis(axis)
|
||||||
else
|
|
||||||
value = joystick:getGamepadAxis(axis)
|
|
||||||
end
|
|
||||||
if direction == '-' then value = -value end
|
if direction == '-' then value = -value end
|
||||||
return value > 0 and value or 0
|
return value > 0 and value or 0
|
||||||
end
|
end
|
||||||
|
|
||||||
function sourceFunction.joystick.button(joystick, button)
|
function sourceFunction.joystick.button(joystick, button)
|
||||||
if tonumber(button) then
|
local isDown = tonumber(button) and joystick:isDown(tonumber(button))
|
||||||
return joystick:isDown(tonumber(button)) and 1 or 0
|
or joystick:isGamepadDown(button)
|
||||||
else
|
return isDown and 1 or 0
|
||||||
return joystick:isGamepadDown(button) and 1 or 0
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function sourceFunction.joystick.hat(joystick, value)
|
function sourceFunction.joystick.hat(joystick, value)
|
||||||
@@ -166,16 +161,8 @@ end
|
|||||||
|
|
||||||
function Player:_updateControls()
|
function Player:_updateControls()
|
||||||
for _, control in pairs(self._controls) do
|
for _, control in pairs(self._controls) do
|
||||||
-- get raw value
|
|
||||||
control.rawValue = self:_getControlRawValue(control)
|
control.rawValue = self:_getControlRawValue(control)
|
||||||
|
control.value = control.rawValue >= self.config.deadzone and control.rawValue or 0
|
||||||
-- get value
|
|
||||||
control.value = 0
|
|
||||||
if control.rawValue >= self.config.deadzone then
|
|
||||||
control.value = control.rawValue
|
|
||||||
end
|
|
||||||
|
|
||||||
-- down/pressed/released
|
|
||||||
control.downPrevious = control.down
|
control.downPrevious = control.down
|
||||||
control.down = control.value > 0
|
control.down = control.value > 0
|
||||||
control.pressed = control.down and not control.downPrevious
|
control.pressed = control.down and not control.downPrevious
|
||||||
@@ -186,11 +173,14 @@ end
|
|||||||
function Player:_updatePairs()
|
function Player:_updatePairs()
|
||||||
for _, pair in pairs(self._pairs) do
|
for _, pair in pairs(self._pairs) do
|
||||||
-- get raw x and y
|
-- get raw x and y
|
||||||
pair.rawX = self._controls[pair.controls[2]].rawValue - self._controls[pair.controls[1]].rawValue
|
local l = self._controls[pair.controls[1]].rawValue
|
||||||
pair.rawY = self._controls[pair.controls[4]].rawValue - self._controls[pair.controls[3]].rawValue
|
local r = self._controls[pair.controls[2]].rawValue
|
||||||
|
local u = self._controls[pair.controls[3]].rawValue
|
||||||
|
local d = self._controls[pair.controls[4]].rawValue
|
||||||
|
pair.rawX, pair.rawY = r - l, d - u
|
||||||
|
|
||||||
-- limit to 1
|
-- limit to 1
|
||||||
local len = (pair.rawX^2 + pair.rawY^2) ^ .5
|
local len = math.sqrt(pair.rawX^2 + pair.rawY^2)
|
||||||
if len > 1 then
|
if len > 1 then
|
||||||
pair.rawX, pair.rawY = pair.rawX / len, pair.rawY / len
|
pair.rawX, pair.rawY = pair.rawX / len, pair.rawY / len
|
||||||
end
|
end
|
||||||
@@ -199,10 +189,9 @@ function Player:_updatePairs()
|
|||||||
if self.config.squareDeadzone then
|
if self.config.squareDeadzone then
|
||||||
pair.x = math.abs(pair.rawX) > self.config.deadzone and pair.rawX or 0
|
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
|
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
|
else
|
||||||
pair.x, pair.y = 0, 0
|
pair.x = len > self.config.deadzone and pair.rawX or 0
|
||||||
|
pair.y = len > self.config.deadzone and pair.rawY or 0
|
||||||
end
|
end
|
||||||
|
|
||||||
-- down/pressed/released
|
-- down/pressed/released
|
||||||
|
Reference in New Issue
Block a user