diff --git a/input.lua b/input.lua new file mode 100644 index 0000000..ba5b407 --- /dev/null +++ b/input.lua @@ -0,0 +1,95 @@ +-- Input system +-- TODO: replace this with universal system +local Input = { + keyboardButtons = { 'escape', 'space' }, + keyboardPressed = {}, + lastKeyboardPressed = {}, + actions = {} +} + +local function mapAction( input, action, keys ) + input.actions[action] = {} + for _, key in pairs( keys ) do + table.insert( input.actions[action], key ) + end +end + +local function loadMap( input ) + mapAction( input, 'exit', { 'escape' } ) + mapAction( input, 'nextlevel', { 'space' } ) +end + +local function predHolded( pressed, _ ) + return pressed +end + +local function predPressed( pressed, lastPressed ) + return pressed and not lastPressed +end + +local function predReleased( pressed, lastPressed ) + return not pressed and lastPressed +end + +local function testAction( input, action, predicate ) + for _, key in ipairs( input.actions[action] ) do + local pressed, lastPressed + + pressed = input.keyboardPressed + lastPressed = input.lastKeyboardPressed + + if predicate( pressed[key], lastPressed[key] ) then + return true + end + end + + return false +end + +function Input.init() + for btn in ipairs( Input.keyboardButtons ) do + Input.keyboardPressed[Input.keyboardButtons[btn]] = false + Input.lastKeyboardPressed[Input.keyboardButtons[btn]] = false + end + + loadMap( Input ) + + return Input +end + +function Input:update() + for k, _ in pairs( self.keyboardPressed ) do + self.lastKeyboardPressed[k] = self.keyboardPressed[k] + end + + for button in ipairs( self.keyboardButtons ) do + self.keyboardPressed[self.keyboardButtons[button]] = love.keyboard.isDown( self.keyboardButtons[button] ) + end +end + +function Input:actionHolded( action ) + return testAction( self, action, predHolded ) +end + +function Input:actionReleased( action ) + return testAction( self, action, predReleased ) +end + +function Input:actionPressed( action ) + return testAction( self, action, predPressed ) +end + +function Input:actionDiff( negative, positive ) + local negResult = testAction( self, negative, predHolded ) + local posResult = testAction( self, positive, predHolded ) + + if negResult and not posResult then + return -1 + elseif posResult and not negResult then + return 1 + else + return 0 + end +end + +return Input.init() diff --git a/levelhandler.lua b/levelhandler.lua new file mode 100644 index 0000000..e5941e7 --- /dev/null +++ b/levelhandler.lua @@ -0,0 +1,30 @@ +require 'makegrid' + +-- Controls switching between levels +---@class LevelHandler +---@field levels string[] +---@field current integer +LevelHandler = { + levels = { + 'test2', 'test' + }, + current = 1, +} + +-- Switches level to next or cycles if it was the last level +---@return Grid +function LevelHandler:next() + self.current = self.current + 1 + if self.current > #self.levels then + self.current = 1 + end + local levelPath = string.format('levels/%s', self.levels[self.current]) + return MakeGrid( require( levelPath ) ) +end + +-- Returns first level +---@return Grid +function LevelHandler:first() + local levelPath = string.format('levels/%s', self.levels[1]) + return MakeGrid( require( levelPath ) ) +end diff --git a/main.lua b/main.lua index 93d81f9..404a262 100644 --- a/main.lua +++ b/main.lua @@ -2,17 +2,29 @@ require 'grid' require 'point' require 'line' require 'mouse' -require 'makegrid' +require 'levelhandler' +Input = require 'input' function love.load() love.graphics.setLineStyle( Config.lineStyle ) love.graphics.setLineWidth( Config.lineWidth ) - GameGrid = MakeGrid( require 'levels/test2' ) + GameGrid = LevelHandler:first() end function love.update( dt ) Mouse:update() + Input:update() + + if Input:actionReleased( 'exit' ) then + love.event.quit() + end + + if Input:actionReleased( 'nextlevel' ) then + if GameGrid:isCompleted() then + GameGrid = LevelHandler:next() + end + end end function love.draw() @@ -29,6 +41,6 @@ function love.draw() end if GameGrid:isCompleted() then - love.graphics.print( "Grid completed", 64, 320 ) + love.graphics.print( "Grid completed. Press space to proceed to next", 64, 320 ) end end