Something went wrong. Try again.
A Love2D remaster of my bug-catching-cult-classic Bait and Switch
Something went wrong. Try again.
2.7 kB · 116 lines
Lua
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116local input = {}
local config = require("src.config")
input.buttonsDown = {}input.buttonsPressed = {}input.buttonsReleased = {}
input.mouse = { x = 0, y = 0, scaledX = 0, scaledY = 0}
input.actions = { net = 1, pause = "escape"}
function input.load() input.movements = config.controls.movement input.actions.net = config.controls.action.net input.screenScale = love.graphics.getWidth() / config.screen.width input.screenOffsetX = 0 input.screenOffsetY = 0end
function input.update() input.mouse.x, input.mouse.y = love.mouse.getPosition() input.mouse.scaledX = (input.mouse.x - input.screenOffsetX) / input.screenScale input.mouse.scaledY = (input.mouse.y - input.screenOffsetY) / input.screenScaleend
function input.clear() input.buttonsPressed = {} input.buttonsReleased = {}end
function input.isMovementDown(direction) if not input.movements[direction] then return false end for _, key in ipairs(input.movements[direction]) do if love.keyboard.isDown(key) then return true end end return falseend
function input.getMovementVector() local dx, dy = 0, 0 if input.isMovementDown("up") then dy = dy - 1 end if input.isMovementDown("down") then dy = dy + 1 end if input.isMovementDown("left") then dx = dx - 1 end if input.isMovementDown("right") then dx = dx + 1 end local len = math.sqrt(dx * dx + dy * dy) if len > 0 then dx, dy = dx / len, dy / len end return dx, dyend
function input.isActionDown(action) local key = input.actions[action] if type(key) == "number" then return love.mouse.isDown(key) elseif type(key) == "string" then return love.keyboard.isDown(key) end return falseend
function input.isActionPressed(action) return input.buttonsPressed[input.actions[action]] or falseend
function input.isActionReleased(action) return input.buttonsReleased[input.actions[action]] or falseend
function input.getMousePosition() return input.mouse.scaledX, input.mouse.scaledYend
function input.getRawMousePosition() return input.mouse.x, input.mouse.yend
function input.keypressed(key) input.buttonsDown[key] = true input.buttonsPressed[key] = trueend
function input.keyreleased(key) input.buttonsDown[key] = nil input.buttonsReleased[key] = trueend
function input.mousepressed(x, y, button) input.buttonsDown[button] = true input.buttonsPressed[button] = trueend
function input.mousereleased(x, y, button) input.buttonsDown[button] = nil input.buttonsReleased[button] = trueend
return input