From ef6c6ea8c2d13a9729d2d47350fd64459b93531e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erkki=20Sepp=C3=A4l=C3=A4?= Date: Sun, 26 Oct 2014 10:29:55 +0200 Subject: [PATCH] Added ability to configure joystick axis --- entities/plane.lua | 17 ++++------ level_state.lua | 17 ++++++++-- menu_state.lua | 80 ++++++++++++++++++++++++++++++++++++++++++++-- settings.lua | 19 ++++++++++- 4 files changed, 117 insertions(+), 16 deletions(-) diff --git a/entities/plane.lua b/entities/plane.lua index 587ceb2..12e63ba 100644 --- a/entities/plane.lua +++ b/entities/plane.lua @@ -109,10 +109,8 @@ Plane = Class{ orientationAngle = 0, - controlX = 0, - controlY = 0, - throttleX = 0, - throttleY = 0, + rotation = 0, + throttle = 0, kbdShooting = false, joyShooting = false, @@ -309,7 +307,7 @@ Plane = Class{ if self.decelerating then self.motorPower = math.max(0.0, self.motorPower - dt * decel_speed) end - self.motorPower = math.max(0.0, math.min(max_motorPower, self.motorPower + dt * self.throttleY * accel_speed)) + self.motorPower = math.max(0.0, math.min(max_motorPower, self.motorPower + dt * self.throttle * accel_speed)) -- -- local base = -- let base = V.base (V.vec_of_ang (~-(body#get_angle))) in @@ -429,7 +427,7 @@ Plane = Class{ elseif self.turningCw then dy = -1 else - dy = self.controlY + dy = self.rotation end if dy ~= 0 then dx, dy = VectorLight.mul(fwd_vel * turn_coeff, 0, dy) @@ -499,11 +497,10 @@ Plane = Class{ self.turningCcw = isTurning end; - analog = function(self, controlY, throttleX, throttleY) + analog = function(self, rotation, throttle) if self.health > 0 then - self.controlY = controlY - self.throttleX = throttleX - self.throttleY = -throttleY + self.rotation = rotation + self.throttle = throttle end end; diff --git a/level_state.lua b/level_state.lua index df9b03f..08d307c 100644 --- a/level_state.lua +++ b/level_state.lua @@ -126,10 +126,21 @@ function level_state:update(dt) end end lastJoystickButtons[player] = buttons - local x1, y1, x2, y2 = j:getAxes() - if y2 ~= nil then - players[player]:joystick(y2 - x2, x1, y1) + + local getAxis = function(info) + local value = 0 + if j:getAxisCount() >= info.axis then + value = j:getAxis(info.axis) + if info.flipped then + value = -value + end + end + return value end + + local rotation = getAxis(AXISMAP[player].rotation[1]) + getAxis(AXISMAP[player].rotation[2]) + local throttle = getAxis(AXISMAP[player].throttle[1]) + getAxis(AXISMAP[player].throttle[2]) + players[player]:joystick(rotation, throttle) end end diff --git a/menu_state.lua b/menu_state.lua index 4ce3841..60bb8f1 100644 --- a/menu_state.lua +++ b/menu_state.lua @@ -3,6 +3,7 @@ require 'level_state' require 'settings' require 'entities/label' require 'entities/valuecontroller' +require 'entities/visrectangle' require 'utils' menu_state = {} @@ -16,7 +17,7 @@ local menuTime = 0 local currentlyChosen = nil -local bindingName = { +local bindingInfo = { ccw = "Turn CW", cw = "Turn CCW", shoot = "Shoot", @@ -25,8 +26,15 @@ local bindingName = { flip = "Flip" } +local joyBindingInfo = { + rotation = "Turn", + throttle = "Engine power" +} + local bindingOrder = { "ccw", "cw", "shoot", "accelerate", "decelerate", "flip" } +local joyBindingOrder = { "rotation", "throttle" } + function menu_state:enter() menu_state.entity_list = {} love.graphics.setBackgroundColor(0, 0, 0, 0) @@ -37,7 +45,7 @@ function menu_state:enter() Label(string.format("Player %d", player), midfont, { 255, 255, 255, 255 }, "left", 500, 0, x, y, menu_state) y = y + 60 for idx, key in ipairs(bindingOrder) do - Label(bindingName[key], font, { 255, 255, 255, 255 }, "left", 190, 25, x, y, menu_state) + Label(bindingInfo[key], font, { 255, 255, 255, 255 }, "left", 190, 25, x, y, menu_state) for bindingIdx, binding in ipairs(KEYMAP[player][key]) do local label = Label(binding, font, { 255, 255, 255, 255 }, "center", 100, 25, x + 200 + 100 * (bindingIdx - 1), y, menu_state) label:onClick(function() @@ -46,10 +54,78 @@ function menu_state:enter() end y = y + 30 end + for idx, axis in ipairs(joyBindingOrder) do + local axisInfo = AXISMAP[player][axis] + Label(joyBindingInfo[axis], font, { 255, 255, 255, 255 }, "left", 190, 25, x, y, menu_state) + for bindingIdx, info in ipairs(axisInfo) do + local prefix + if info.flipped then + prefix = "-" + else + prefix = "+" + end + local visAxisBaseX = (100 + 30) / 2 + x + 200 + 100 * (bindingIdx - 1) + + local signLabel = Label(prefix, font, { 255, 255, 255, 255 }, "center", 20, 25, x + 200 + 100 * (bindingIdx - 1), y, menu_state) + + signLabel:onClick(function() + menu_state:switchSign(player, axis, bindingIdx, signLabel) + end) + + local axisLabel = Label(info.axis, font, { 255, 255, 255, 255 }, "center", 70, 25, x + 30 + 200 + 100 * (bindingIdx - 1), y, menu_state) + + axisLabel:onClick(function() + menu_state:switchAxis(player, axis, bindingIdx, axisLabel) + end) + + local visAxis = VisRectangle({ 40, 40, 0, 128 }, 10, 10, nil, visAxisBaseX, y + 11, menu_state) + + ValueController(function () + local joysticks = love.joystick.getJoysticks() + local axisIdx = AXISMAP[player][axis][bindingIdx].axis + if #joysticks >= player then + local direction = joysticks[player]:getAxis(axisIdx) + visAxis.x = visAxisBaseX + 30 * direction + end + end, menu_state) + end + y = y + 30 + end x = x + 500 end end +function menu_state:switchSign(player, axis, bindingIdx, signLabel) + AXISMAP[player][axis][bindingIdx].flipped = not AXISMAP[player][axis][bindingIdx].flipped + local prefix; + if AXISMAP[player][axis][bindingIdx].flipped then + prefix = "-" + else + prefix = "+" + end + signLabel.label = prefix + save_settings() +end + +function menu_state:switchAxis(player, axis, bindingIdx, axisLabel) + local joysticks = love.joystick.getJoysticks() + + if #joysticks >= player then + local joystick = joysticks[player] + local axisIdx = AXISMAP[player][axis][bindingIdx].axis + + axisIdx = axisIdx + 1 + + if axisIdx > joystick:getAxisCount() then + axisIdx = 1 + end + + AXISMAP[player][axis][bindingIdx].axis = axisIdx + axisLabel.label = AXISMAP[player][axis][bindingIdx].axis + save_settings() + end +end + local blinkColor = function(t) local seconds, subseconds = math.modf(menuTime) return 255 * ((math.sin(math.pow((math.log(subseconds + 1) / math.log(2)), 2) * math.pi * 2) * 0.5 + 0.5)); diff --git a/settings.lua b/settings.lua index b1fc292..f919b22 100644 --- a/settings.lua +++ b/settings.lua @@ -31,13 +31,30 @@ KEYMAP = { } } +AXISMAP = { + [1] = { + rotation = { { flipped = true, axis = 3 }, + { flipped = false, axis = 4 } }, + throttle = { { flipped = false, axis = 1 }, + { flipped = false, axis = 2 } }, + }, + [2] = { + rotation = { { flipped = true, axis = 3 }, + { flipped = false, axis = 4 } }, + throttle = { { flipped = false, axis = 1 }, + { flipped = false, axis = 2 } }, + } +} + + function save_settings() - table.save({KEYMAP = KEYMAP}, "fysplane.cfg") + table.save({KEYMAP = KEYMAP, AXISMAP = AXISMAP}, "fysplane.cfg") end if love.filesystem.isFile("fysplane.cfg") then local m = table.load("fysplane.cfg") KEYMAP = m.KEYMAP + AXISMAP = m.AXISMAP end PLANE_HEALTH = 500