Skip to content

Commit

Permalink
Ported over from LÖVE 0.8 to 0.9. Joystick handling still shoddy, esp…
Browse files Browse the repository at this point in the history
…ecially with non-gamepads
  • Loading branch information
SimonLarsen committed Dec 17, 2013
1 parent b37050f commit f4b38c5
Show file tree
Hide file tree
Showing 16 changed files with 105 additions and 106 deletions.
2 changes: 1 addition & 1 deletion anim.lua
Expand Up @@ -89,7 +89,7 @@ end
-- @param Scale factor (x-axis).
-- @param Scale factor (y-axis).
function Animation:draw(x, y, r, sx, sy)
love.graphics.drawq(self.image, self.quads[self.frame], x, y, r or 0, sx or 1, sy or 1, self.ox, self.oy)
love.graphics.draw(self.image, self.quads[self.frame], x, y, r or 0, sx or 1, sy or 1, self.ox, self.oy)
end

--- Play the animation
Expand Down
39 changes: 23 additions & 16 deletions conf.lua
@@ -1,24 +1,31 @@
function love.conf(t)
t.title = "DuckMarines"
t.author = "Unnamed"
t.url = "http://tangramgames.dk/duckmarines"
t.identity = "duckmarines"
t.version = "0.8.0"
t.identity = "duckmarines"
t.version = "0.9.0"
t.console = false
t.release = false
t.screen.width = 700
t.screen.height = 442
t.screen.fullscreen = false
t.screen.vsync = true
t.screen.fsaa = 0
t.modules.joystick = true

t.window.title = "Duck Marines"
t.window.icon = nil
t.window.width = 700
t.window.height = 442
t.window.borderless = false
t.window.resizable = false
t.window.fullscreen = false
t.window.fullscreentype = "normal"
t.window.vsync = true
t.window.fsaa = 0
t.window.display = 1

t.modules.audio = true
t.modules.keyboard = true
t.modules.event = true
t.modules.image = true
t.modules.graphics = true
t.modules.timer = true
t.modules.image = true
t.modules.joystick = true
t.modules.keyboard = true
t.modules.math = true
t.modules.mouse = true
t.modules.sound = true
t.modules.physics = false
t.modules.sound = true
t.modules.system = true
t.modules.timer = true
t.modules.window = true
end
8 changes: 4 additions & 4 deletions countdownState.lua
Expand Up @@ -31,14 +31,14 @@ function CountdownState:draw()
if self.time < 3 then
local quad = math.floor(self.time)
love.graphics.setColor(0, 0, 0, 128)
love.graphics.drawq(self.img, self.quads[quad], (WIDTH-100)/2, 176)
love.graphics.draw(self.img, self.quads[quad], (WIDTH-100)/2, 176)
love.graphics.setColor(255,255,255,255)
love.graphics.drawq(self.img, self.quads[quad], (WIDTH-100)/2, 168)
love.graphics.draw(self.img, self.quads[quad], (WIDTH-100)/2, 168)
else
love.graphics.setColor(0, 0, 0, 128)
love.graphics.drawq(self.img, self.quads[3], (WIDTH-300)/2, 176)
love.graphics.draw(self.img, self.quads[3], (WIDTH-300)/2, 176)
love.graphics.setColor(255,255,255,255)
love.graphics.drawq(self.img, self.quads[3], (WIDTH-300)/2, 168)
love.graphics.draw(self.img, self.quads[3], (WIDTH-300)/2, 168)
end
end

Expand Down
2 changes: 1 addition & 1 deletion duckDashState.lua
Expand Up @@ -54,7 +54,7 @@ function DuckDashState:draw()
end

local frame = math.floor(self.frame)
love.graphics.drawq(self.anim, self.anim_quads[frame], 296, 81)
love.graphics.draw(self.anim, self.anim_quads[frame], 296, 81)
end

function DuckDashState:isTransparent()
Expand Down
44 changes: 21 additions & 23 deletions input.lua
Expand Up @@ -166,12 +166,12 @@ JoystickInput.__index = JoystickInput
setmetatable(JoystickInput, Input)

JoystickInput.SPEED = 300
JoystickInput.DEADZONE = 0.20
JoystickInput.DEADZONE = 0.5

function JoystickInput.create(id)
function JoystickInput.create(joystick)
local self = setmetatable(Input.create(), JoystickInput)

self.id = id
self.joystick = joystick
self.down = false

return self
Expand All @@ -181,46 +181,44 @@ function JoystickInput:getMovement(dt, lock)
local dx = 0
local dy = 0

local axis1 = love.joystick.getAxis(self.id, 1)
local axis2 = love.joystick.getAxis(self.id, 2)
local leftx = self.joystick:getGamepadAxis("leftx")
local lefty = self.joystick:getGamepadAxis("lefty")

local axis3 = love.joystick.getAxis(self.id, 3)
local axis4 = love.joystick.getAxis(self.id, 4)
local rightx = self.joystick:getGamepadAxis("rightx")
local righty = self.joystick:getGamepadAxis("righty")

if axis1 and axis2 then
if not self:isDown() or lock == false then
if axis1 and math.abs(axis1) > JoystickInput.DEADZONE then
dx = axis1 * self.SPEED * dt
if leftx and lefty then
if not self:isDown() then
if leftx and math.abs(leftx) > JoystickInput.DEADZONE then
dx = leftx * self.SPEED * dt
end
if axis2 and math.abs(axis2) > JoystickInput.DEADZONE then
dy = axis2 * self.SPEED * dt
if lefty and math.abs(lefty) > JoystickInput.DEADZONE then
dy = lefty * self.SPEED * dt
end
elseif self.down == true then
if math.abs(axis1) > JoystickInput.DEADZONE or math.abs(axis2) > JoystickInput.DEADZONE then
self.down = false
self.action = vecToDir(axis1, axis2)
else
if math.abs(leftx) > JoystickInput.DEADZONE or math.abs(lefty) > JoystickInput.DEADZONE then
self.action = vecToDir(leftx, lefty)
end
end
end

if axis3 and axis4 then
if math.abs(axis3) > JoystickInput.DEADZONE or math.abs(axis4) > JoystickInput.DEADZONE then
self.action = vecToDir(axis3, axis4)
if rightx and righty then
if math.abs(rightx) > JoystickInput.DEADZONE or math.abs(righty) > JoystickInput.DEADZONE then
self.action = vecToDir(rightx, righty)
end
end

return dx, dy, false
end

function JoystickInput:joystickpressed(joystick, button)
if joystick == self.id then
self.down = true
if joystick:getID() == self.joystick:getID() then
self.clicked = true
end
end

function JoystickInput:isDown()
return love.joystick.isDown(self.id, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
return self.joystick:isGamepadDown("a", "b", "x", "y")
end

function JoystickInput:getType() return Input.TYPE_JOYSTICK end
2 changes: 1 addition & 1 deletion inputSelectState.lua
Expand Up @@ -56,7 +56,7 @@ function InputSelectState:joystickpressed(joy, button)
local found = false
for i=1,4 do
if self.inputs[i] and self.inputs[i]:getType() == Input.TYPE_JOYSTICK then
if self.inputs[i].id == joy then
if self.inputs[i].joystick == joy then
found = true
break
end
Expand Down
20 changes: 10 additions & 10 deletions levelSelectionState.lua
Expand Up @@ -77,11 +77,11 @@ end

function LevelSelectionState:updateMapList()
local items = {}
local files = love.filesystem.enumerate("res/maps")
local files = love.filesystem.getDirectoryItems("res/maps")
for i,v in ipairs(files) do
table.insert(items, v:upper())
end
files = love.filesystem.enumerate("usermaps")
files = love.filesystem.getDirectoryItems("usermaps")
for i,v in ipairs(files) do
table.insert(items, "CUSTOM: " .. v:upper())
end
Expand All @@ -105,17 +105,17 @@ function LevelSelectionState:selectionChanged(text, source)
for ix=0,11 do
local tile = map:getTile(ix, iy)
if tile >= 10 and tile <= 14 then
self.batch:addq(quadSub, ix*23, iy*23+1)
self.batch:add(quadSub, ix*23, iy*23+1)
elseif tile == 2 then
self.batch:addq(quadPit, ix*23, iy*23)
self.batch:add(quadPit, ix*23, iy*23)
elseif tile == 4 then
self.batch:addq(quadSpawnUp, ix*23-1, iy*23+2)
self.batch:add(quadSpawnUp, ix*23-1, iy*23+2)
elseif tile == 5 then
self.batch:addq(quadSpawnRight, ix*23-1, iy*23+9)
self.batch:add(quadSpawnRight, ix*23-1, iy*23+9)
elseif tile == 6 then
self.batch:addq(quadSpawnDown, ix*23-1, iy*23+9)
self.batch:add(quadSpawnDown, ix*23-1, iy*23+9)
elseif tile == 7 then
self.batch:addq(quadSpawnLeft, ix*23-8, iy*23+9)
self.batch:add(quadSpawnLeft, ix*23-8, iy*23+9)
end
end
end
Expand All @@ -124,10 +124,10 @@ function LevelSelectionState:selectionChanged(text, source)
for ix=0,11 do
local wall = map:getWall(ix, iy)
if iy > 0 and wall % 2 == 1 then
self.batch:addq(quadFenceHor, ix*23-3, iy*23-3)
self.batch:add(quadFenceHor, ix*23-3, iy*23-3)
end
if ix > 0 and wall > 1 then
self.batch:addq(quadFenceVer, ix*23-3, iy*23-3)
self.batch:add(quadFenceVer, ix*23-3, iy*23-3)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion loadLevelState.lua
Expand Up @@ -41,7 +41,7 @@ end

function LoadLevelState:updateFileList()
local items = {}
local files = love.filesystem.enumerate("usermaps")
local files = love.filesystem.getDirectoryItems("usermaps")
for i,v in ipairs(files) do
table.insert(items, v:upper())
end
Expand Down
11 changes: 3 additions & 8 deletions main.lua
Expand Up @@ -58,8 +58,8 @@ local focused

function love.load()
-- Setup screen
love.graphics.setMode(WIDTH*SCALE, HEIGHT*SCALE, false, true)
love.graphics.setDefaultImageFilter("nearest", "nearest")
love.window.setMode(WIDTH*SCALE, HEIGHT*SCALE, {fullscreen=false, vsync=true})
love.graphics.setDefaultFilter("nearest", "nearest")

-- Setup user data
if love.filesystem.exists("usermaps") == false then
Expand All @@ -74,7 +74,6 @@ function love.load()
-- Setup mouse
--love.mouse.setGrab(true)
love.mouse.setVisible(false)
love.mouse.setPosition(WIDTH/2, HEIGHT/2)
focused = true

-- Setup gamestate stack
Expand Down Expand Up @@ -135,9 +134,5 @@ function love.focus(f)
end

function setScreenMode()
love.graphics.setMode(WIDTH, HEIGHT, config.fullscreen, config.vsync)
love.timer.sleep(0.25)
love.mouse.setPosition(WIDTH/2, HEIGHT/2)
love.mouse.setGrab(true)
love.mouse.setVisible(false)
love.window.setMode(WIDTH, HEIGHT, {fullscreen=config.fullscreen, vsync=config.vsync})
end
9 changes: 4 additions & 5 deletions mainMenuState.lua
Expand Up @@ -7,11 +7,10 @@ function MainMenuState.create(config)

self.config = config

self.inputs[1] = KeyboardInput.create()
self.inputs[2] = JoystickInput.create(1)
self.inputs[3] = JoystickInput.create(2)
self.inputs[4] = JoystickInput.create(3)
self.inputs[5] = JoystickInput.create(4)
table.insert(self.inputs, KeyboardInput.create())
for i,v in ipairs(love.joystick.getJoysticks()) do
table.insert(self.inputs, JoystickInput.create(v))
end

self.cursors[1] = Cursor.create(WIDTH/2, HEIGHT/2, 1)
for i=1,5 do
Expand Down
16 changes: 8 additions & 8 deletions map.lua
Expand Up @@ -45,9 +45,9 @@ function Map:updateSpriteBatch(debug)
for iy = 0, 8 do
for ix = 0, 11 do
if (ix+iy) % 2 == 1 then
self.backBatch:addq(groundQuad1, ix*48, iy*48)
self.backBatch:add(groundQuad1, ix*48, iy*48)
else
self.backBatch:addq(groundQuad2, ix*48, iy*48)
self.backBatch:add(groundQuad2, ix*48, iy*48)
end

local tile = self:getTile(ix, iy)
Expand All @@ -60,7 +60,7 @@ function Map:updateSpriteBatch(debug)
local cx = (tile % 10) * 48
local cy = math.floor(tile / 10) * 48
quad:setViewport(cx, cy, 48, 48)
self.backBatch:addq(quad, ix*48, iy*48)
self.backBatch:add(quad, ix*48, iy*48)
end
end
end
Expand All @@ -75,20 +75,20 @@ function Map:updateSpriteBatch(debug)
if wall > 0 then
-- Right fence
if ix < 12 and wall % 2 == 1 then
self.frontBatch:addq(fenceHorzQuad, ix*48, iy*48-5)
self.frontBatch:add(fenceHorzQuad, ix*48, iy*48-5)
if self:getWall(ix+1, iy) == 0 then
self.frontBatch:addq(postQuad, ix*48+45, iy*48-8)
self.frontBatch:add(postQuad, ix*48+45, iy*48-8)
end
end
-- Downwards fence
if iy < 9 and wall > 1 then
self.frontBatch:addq(fenceVertQuad, ix*48-2, iy*48-3)
self.frontBatch:add(fenceVertQuad, ix*48-2, iy*48-3)
if self:getWall(ix, iy+1) == 0 then
self.frontBatch:addq(postQuad, ix*48-3, iy*48+40)
self.frontBatch:add(postQuad, ix*48-3, iy*48+40)
end
end
-- Post
self.frontBatch:addq(postQuad, ix*48-3, iy*48-8)
self.frontBatch:add(postQuad, ix*48-3, iy*48-8)
end
end
end
Expand Down
18 changes: 9 additions & 9 deletions menu.lua
Expand Up @@ -68,15 +68,15 @@ function Menu:draw()
-- Draw button graphics
for i,v in ipairs(self.buttons) do
if v.type == Menu.TEXT_BUTTON then
love.graphics.drawq(self.imgButton, self.quadTopLeft, v.x, v.y)
love.graphics.drawq(self.imgButton, self.quadTopRight, v.x+v.width-3, v.y)
love.graphics.drawq(self.imgButton, self.quadBottomLeft, v.x, v.y+v.height-3)
love.graphics.drawq(self.imgButton, self.quadBottomRight, v.x+v.width-3, v.y+v.height-3)
love.graphics.draw(self.imgButton, self.quadTopLeft, v.x, v.y)
love.graphics.draw(self.imgButton, self.quadTopRight, v.x+v.width-3, v.y)
love.graphics.draw(self.imgButton, self.quadBottomLeft, v.x, v.y+v.height-3)
love.graphics.draw(self.imgButton, self.quadBottomRight, v.x+v.width-3, v.y+v.height-3)

love.graphics.drawq(self.imgButton, self.quadTopEdge, v.x+3, v.y, 0, v.width-6, 1)
love.graphics.drawq(self.imgButton, self.quadBottomEdge, v.x+3, v.y+v.height-3, 0, v.width-6, 1)
love.graphics.drawq(self.imgButton, self.quadLeftEdge, v.x, v.y+3, 0, 1, v.height-6)
love.graphics.drawq(self.imgButton, self.quadRightEdge, v.x+v.width-3, v.y+3, 0, 1, v.height-6)
love.graphics.draw(self.imgButton, self.quadTopEdge, v.x+3, v.y, 0, v.width-6, 1)
love.graphics.draw(self.imgButton, self.quadBottomEdge, v.x+3, v.y+v.height-3, 0, v.width-6, 1)
love.graphics.draw(self.imgButton, self.quadLeftEdge, v.x, v.y+3, 0, 1, v.height-6)
love.graphics.draw(self.imgButton, self.quadRightEdge, v.x+v.width-3, v.y+3, 0, 1, v.height-6)

love.graphics.setColor(255, 194, 49, 255)
love.graphics.rectangle("fill", v.x+3, v.y+3, v.width-6, v.height-6)
Expand All @@ -89,7 +89,7 @@ function Menu:draw()
love.graphics.printf(v.text, v.x, (v.y+v.height/2-9), v.width, "center")
love.graphics.setColor(255,255,255,255)
elseif v.type == Menu.IMAGE_BUTTON then
love.graphics.drawq(v.img, v.quad, v.x, v.y)
love.graphics.draw(v.img, v.quad, v.x, v.y)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion saveLevelState.lua
Expand Up @@ -41,7 +41,7 @@ end

function SaveLevelState:updateFileList()
local items = {}
local files = love.filesystem.enumerate("usermaps")
local files = love.filesystem.getDirectoryItems("usermaps")
for i,v in ipairs(files) do
table.insert(items, v:upper())
end
Expand Down
16 changes: 8 additions & 8 deletions selectionList.lua
Expand Up @@ -35,14 +35,14 @@ function SelectionList:draw()
love.graphics.setColor(255,255,255,255)

-- Draw buttons
love.graphics.drawq(self.imgButton, self.quadButtonLeft, self.x, self.y)
love.graphics.drawq(self.imgButton, self.quadButtonLeft, self.x, self.y+self.height-14)
love.graphics.drawq(self.imgButton, self.quadButtonRight, self.x+self.width-2, self.y)
love.graphics.drawq(self.imgButton, self.quadButtonRight, self.x+self.width-2, self.y+self.height-14)
love.graphics.drawq(self.imgButton, self.quadButtonMiddle, self.x+2, self.y, 0, self.width-4, 1)
love.graphics.drawq(self.imgButton, self.quadButtonMiddle, self.x+2, self.y+self.height-14, 0, self.width-4, 1)
love.graphics.drawq(self.imgButton, self.quadButtonUp, self.x+self.width/2-11, self.y)
love.graphics.drawq(self.imgButton, self.quadButtonDown, self.x+self.width/2-11, self.y+self.height-14)
love.graphics.draw(self.imgButton, self.quadButtonLeft, self.x, self.y)
love.graphics.draw(self.imgButton, self.quadButtonLeft, self.x, self.y+self.height-14)
love.graphics.draw(self.imgButton, self.quadButtonRight, self.x+self.width-2, self.y)
love.graphics.draw(self.imgButton, self.quadButtonRight, self.x+self.width-2, self.y+self.height-14)
love.graphics.draw(self.imgButton, self.quadButtonMiddle, self.x+2, self.y, 0, self.width-4, 1)
love.graphics.draw(self.imgButton, self.quadButtonMiddle, self.x+2, self.y+self.height-14, 0, self.width-4, 1)
love.graphics.draw(self.imgButton, self.quadButtonUp, self.x+self.width/2-11, self.y)
love.graphics.draw(self.imgButton, self.quadButtonDown, self.x+self.width/2-11, self.y+self.height-14)

-- Draw sides
love.graphics.setColor(255, 194, 49)
Expand Down

0 comments on commit f4b38c5

Please sign in to comment.