Added joystick support. Mapping works with the XBox original controller.

This commit is contained in:
Erkki Seppälä 2014-10-22 21:38:06 +03:00
parent 1cd538f9a0
commit 4eb0612c25
6 changed files with 85 additions and 12 deletions

View file

@ -71,4 +71,7 @@ ComputerPlayer = Class{
release = function(self, key) release = function(self, key)
end; end;
joystick = function(self, ...)
end;
} }

View file

@ -1,9 +1,6 @@
-- LÖVE configuration values -- LÖVE configuration values
function love.conf(t) function love.conf(t)
-- Disable unneeded modules
t.modules.joystick = false
t.window.title = "Fysplane" t.window.title = "Fysplane"
t.window.width = 1280 t.window.width = 1280
t.window.height = 768 t.window.height = 768

View file

@ -108,6 +108,14 @@ Plane = Class{
orientationAngle = 0, orientationAngle = 0,
controlX = 0,
controlY = 0,
throttleX = 0,
throttleY = 0,
kbdShooting = false,
joyShooting = false,
init = function(self, x, y, xDir, yDir, r, g, b, level) init = function(self, x, y, xDir, yDir, r, g, b, level)
local density = 50 local density = 50
PhysicsEntity.init(self, x, y, level, "dynamic", 0.2) PhysicsEntity.init(self, x, y, level, "dynamic", 0.2)
@ -166,7 +174,11 @@ Plane = Class{
die = function(self) die = function(self)
self.health = 0 self.health = 0
if self.joyShooting or self.kbdShooting then
self.machinegun:stopShooting() self.machinegun:stopShooting()
end
self.joyShooting = false
self.kbdShooting = false
Animation(self.body:getX(), self.body:getY(), self.level, explosionFrames) Animation(self.body:getX(), self.body:getY(), self.level, explosionFrames)
self:getOwner():setPlane(nil) self:getOwner():setPlane(nil)
self.motorSound:stop() self.motorSound:stop()
@ -203,11 +215,17 @@ Plane = Class{
shoot = function(self, down) shoot = function(self, down)
if self.health > 0 then if self.health > 0 then
if down then if down then
if not kbdShooting and not joyShooting then
self.machinegun:startShooting() self.machinegun:startShooting()
end
kbdShooting = true
else else
kbdShooting = false
if not kbdShooting and not joyShooting then
self.machinegun:stopShooting() self.machinegun:stopShooting()
end end
end end
end
end; end;
wasHitBy = function(self, by) wasHitBy = function(self, by)
@ -269,9 +287,10 @@ Plane = Class{
if self.accelerating then if self.accelerating then
self.motorPower = math.min(max_motorPower, self.motorPower + dt * accel_speed) self.motorPower = math.min(max_motorPower, self.motorPower + dt * accel_speed)
end end
if self.decelerating then if self.decelerating or self.brake then
self.motorPower = math.max(0.0, self.motorPower - dt * accel_speed) self.motorPower = math.max(0.0, self.motorPower - dt * decel_speed)
end end
self.motorPower = math.max(0.0, math.min(max_motorPower, self.motorPower + dt * self.throttleY * accel_speed))
-- -- local base = -- -- local base =
-- let base = V.base (V.vec_of_ang (~-(body#get_angle))) in -- let base = V.base (V.vec_of_ang (~-(body#get_angle))) in
@ -385,13 +404,16 @@ Plane = Class{
-- elseif self.turningCw then -- elseif self.turningCw then
-- dx, dy = VectorLight.rotate(self.angle, 0, -50000) -- dx, dy = VectorLight.rotate(self.angle, 0, -50000)
-- end -- end
dy = 0
if self.turningCcw then if self.turningCcw then
dx, dy = 0, 1 dy = 1
elseif self.turningCw then elseif self.turningCw then
dx, dy = 0, -1 dy = -1
else
dy = self.controlY
end end
if self.turningCw or self.turningCcw then if dy ~= 0 then
dx, dy = VectorLight.mul(fwd_vel * turn_coeff, dx, dy) dx, dy = VectorLight.mul(fwd_vel * turn_coeff, 0, dy)
-- dx, dy = VectorLight.mul(turn_coeff * 10000 -- * sign(fwd_vel) -- dx, dy = VectorLight.mul(turn_coeff * 10000 -- * sign(fwd_vel)
-- , dx, dy) -- , dx, dy)
-- print("dx", dx, "dy", dy, "tail_speed", tail_speed) -- print("dx", dx, "dy", dy, "tail_speed", tail_speed)
@ -441,5 +463,28 @@ Plane = Class{
ccw = function(self, isTurning) ccw = function(self, isTurning)
self.turningCcw = isTurning self.turningCcw = isTurning
end; end;
analog = function(self, controlX, controlY, throttleX, throttleY, fire, brake)
if self.health > 0 then
self.controlX = controlX
self.controlY = controlY
self.throttleX = throttleX
self.throttleY = -throttleY
self.fire = fire
self.brake = brake
if fire then
if not kbdShooting and not joyShooting then
self.machinegun:startShooting()
end
joyShooting = true
else
joyShooting = false
if not kbdShooting and not joyShooting then
self.machinegun:stopShooting()
end
end
end
end;
} }

View file

@ -36,6 +36,8 @@ local POWERUPS = {
ChaingunPowerUp ChaingunPowerUp
} }
local joysticks = {}
function level_state:init() function level_state:init()
end end
@ -59,6 +61,10 @@ function level_state:enter(previous, level_file)
level_time = 0.0 level_time = 0.0
updated_time = 0.0 updated_time = 0.0
for i, joystick in ipairs(love.joystick.getJoysticks()) do
joysticks[i] = joystick
end
love.graphics.setBackgroundColor({0, 0, 0, 255}) love.graphics.setBackgroundColor({0, 0, 0, 255})
current_level = Level() current_level = Level()
current_level.world:setCallbacks(begin_contact, end_contact, pre_solve, post_solve) current_level.world:setCallbacks(begin_contact, end_contact, pre_solve, post_solve)
@ -100,6 +106,21 @@ function level_state:update(dt)
level_time = level_time + dt level_time = level_time + dt
for player = 1, #players, 1 do
local j = joysticks[player]
if j then
local buttons = {}
for i = 1, j:getButtonCount(), 1 do
buttons[i] = j:isDown(i)
end
local x1, y1, x2, y2 = j:getAxes()
if #buttons >= 8 and y2 ~= nil then
-- button mapping suitable for XBox original controller
players[player]:joystick(x2, y2, x1, y1, buttons[8], buttons[7])
end
end
end
dt = PHYSICS_STEP dt = PHYSICS_STEP
while updated_time < level_time do while updated_time < level_time do
updated_time = updated_time + dt updated_time = updated_time + dt

View file

@ -35,4 +35,7 @@ NullPlayer = Class{
release = function(self, key) release = function(self, key)
end; end;
joystick = function(self, ...)
end;
} }

View file

@ -67,4 +67,8 @@ Player = Class{
end end
end end
end; end;
joystick = function(self, ...)
self.plane:analog(...)
end
} }