Level switching logic
This commit is contained in:
parent
efe9f386bd
commit
d4c4c7bfd0
3 changed files with 140 additions and 3 deletions
95
input.lua
Normal file
95
input.lua
Normal file
|
@ -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()
|
30
levelhandler.lua
Normal file
30
levelhandler.lua
Normal file
|
@ -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
|
18
main.lua
18
main.lua
|
@ -2,17 +2,29 @@ require 'grid'
|
||||||
require 'point'
|
require 'point'
|
||||||
require 'line'
|
require 'line'
|
||||||
require 'mouse'
|
require 'mouse'
|
||||||
require 'makegrid'
|
require 'levelhandler'
|
||||||
|
Input = require 'input'
|
||||||
|
|
||||||
function love.load()
|
function love.load()
|
||||||
love.graphics.setLineStyle( Config.lineStyle )
|
love.graphics.setLineStyle( Config.lineStyle )
|
||||||
love.graphics.setLineWidth( Config.lineWidth )
|
love.graphics.setLineWidth( Config.lineWidth )
|
||||||
|
|
||||||
GameGrid = MakeGrid( require 'levels/test2' )
|
GameGrid = LevelHandler:first()
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.update( dt )
|
function love.update( dt )
|
||||||
Mouse:update()
|
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
|
end
|
||||||
|
|
||||||
function love.draw()
|
function love.draw()
|
||||||
|
@ -29,6 +41,6 @@ function love.draw()
|
||||||
end
|
end
|
||||||
|
|
||||||
if GameGrid:isCompleted() then
|
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
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue