[module] two-state control handlers

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
raingloom
Prole
Posts: 36
Joined: Wed Apr 22, 2015 12:35 am
Location: Always elsewhere

[module] two-state control handlers

Post by raingloom »

It's a common situation to use multiple keys for one control, but hardcoding `if keys.down or keys.s then` is harder to maintain and limits on-the-run configurability, so I whipped up a simple module that makes this easier.

current version (as of writing this) on GitHub
entire project(if the file was moved and I was too lazy to change the link, you should look here)

Simple test code that dumps all key and control states:

Code: Select all

local Controls = require 'Controls'--require 'raincoat.Game.Controls'


function love.load( arg )
	Controls.addKeyToControl( 'forward', 'up', 'w' )
	Controls.addKeyToControl( 'backward', 'down', 's' )
	Controls.addKeyToControl( 'steer-left', 'left', 'a' )
	Controls.addKeyToControl( 'steer-right', 'right', 'd' )
end


function love.keypressed( key, isRepeat )
	Controls.updateKey( key, true )
end


function love.keyreleased( key )
	Controls.updateKey( key, false )
end


function love.draw()
	local buf, i = { 'controls' }, 2
	for k, v in pairs( Controls.controls ) do
		buf[ i ], i = string.format( '\t%s: %s', k, v ), i + 1
	end
	buf[ i ], i = 'keys', i + 1
	for k, v in pairs( Controls.keys ) do
		buf[ i ], i = string.format( '\t%s: %s', k, v ), i + 1
	end
	love.graphics.print( table.concat( buf, '\n' ) )
end
It should be able to handle run-time configuration changes and mappings of the following types:
-key to control
-keys to control
-key to controls

Sorry for the lack of comments in the code, if anyone needs them, I'll add them, but I tried writing it to be easy to read anyways.
I may or may not add analog control states in the future. They will most likely be a separate module.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 3 guests