Added reconfigurable keybindings
This commit is contained in:
parent
113101894b
commit
59b851d087
8 changed files with 176 additions and 53 deletions
|
@ -36,16 +36,3 @@ Entity = Class{
|
||||||
self.level.entity_list[self.id] = nil
|
self.level.entity_list[self.id] = nil
|
||||||
end;
|
end;
|
||||||
}
|
}
|
||||||
|
|
||||||
function drawEntities()
|
|
||||||
-- Do we need ordering? If so, use ipairs instead of pairs
|
|
||||||
for key, entity in pairs(entity_list) do
|
|
||||||
entity:draw()
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function updateEntities(dt)
|
|
||||||
for key, entity in pairs(entity_list) do
|
|
||||||
entity:update(dt)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
38
entities/label.lua
Normal file
38
entities/label.lua
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
Class = require 'hump.class'
|
||||||
|
require 'entities/entity'
|
||||||
|
|
||||||
|
Label = Class {
|
||||||
|
__include = Entity,
|
||||||
|
|
||||||
|
init = function(self, label, font, color, align, xsize, ysize, ...)
|
||||||
|
Entity.init(self, ...)
|
||||||
|
self.label = label
|
||||||
|
self.color = color
|
||||||
|
self.font = font
|
||||||
|
self.align = align
|
||||||
|
self.xsize = xsize
|
||||||
|
self.ysize = ysize
|
||||||
|
self.onClickFunction = function() end
|
||||||
|
end;
|
||||||
|
|
||||||
|
onClick = function(self, fn)
|
||||||
|
self.onClickFunction = fn
|
||||||
|
return self
|
||||||
|
end;
|
||||||
|
|
||||||
|
draw = function(self)
|
||||||
|
love.graphics.push()
|
||||||
|
love.graphics.setColor(self.color)
|
||||||
|
love.graphics.translate(self.x, self.y)
|
||||||
|
love.graphics.setFont(self.font)
|
||||||
|
love.graphics.printf(self.label, 0, 0, self.xsize, self.align)
|
||||||
|
--love.graphics.rectangle("fill", 0, 0, self.xsize, self.ysize)
|
||||||
|
love.graphics.pop()
|
||||||
|
end;
|
||||||
|
|
||||||
|
mousePressed = function(self, x, y, button)
|
||||||
|
if x >= self.x and y >= self.y and x <= self.x + self.xsize and y <= self.y + self.ysize then
|
||||||
|
self.onClickFunction(button)
|
||||||
|
end
|
||||||
|
end;
|
||||||
|
}
|
27
entities/valuecontroller.lua
Normal file
27
entities/valuecontroller.lua
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
Class = require 'hump.class'
|
||||||
|
require 'entities/entity'
|
||||||
|
|
||||||
|
ValueController = Class {
|
||||||
|
__include = Entity,
|
||||||
|
|
||||||
|
init = function(self, fn, level)
|
||||||
|
Entity.init(self, 0, 0, level)
|
||||||
|
self.fn = fn
|
||||||
|
self.onDelete = function() end
|
||||||
|
self.t = 0
|
||||||
|
end;
|
||||||
|
|
||||||
|
update = function(self, dt)
|
||||||
|
self.t = self.t + dt
|
||||||
|
self.fn(self.t)
|
||||||
|
end;
|
||||||
|
|
||||||
|
draw = function(...)
|
||||||
|
Entity.draw(...)
|
||||||
|
end;
|
||||||
|
|
||||||
|
delete = function(self, ...)
|
||||||
|
self.onDelete()
|
||||||
|
Entity.delete(self, ...)
|
||||||
|
end;
|
||||||
|
}
|
|
@ -22,6 +22,7 @@ Level = Class{
|
||||||
self.background = love.graphics.newImage('resources/graphics/sky.png')
|
self.background = love.graphics.newImage('resources/graphics/sky.png')
|
||||||
self.bgCanvas = love.graphics.newCanvas()
|
self.bgCanvas = love.graphics.newCanvas()
|
||||||
love.graphics.setCanvas(self.bgCanvas)
|
love.graphics.setCanvas(self.bgCanvas)
|
||||||
|
love.graphics.setColor(255, 255, 255, 255)
|
||||||
love.graphics.draw(self.background)
|
love.graphics.draw(self.background)
|
||||||
love.graphics.setCanvas()
|
love.graphics.setCanvas()
|
||||||
|
|
||||||
|
|
|
@ -178,12 +178,14 @@ function level_state:keypressed(key, unicode)
|
||||||
or love.keyboard.isDown("rctrl")) then
|
or love.keyboard.isDown("rctrl")) then
|
||||||
|
|
||||||
debugEnabled = not debugEnabled
|
debugEnabled = not debugEnabled
|
||||||
elseif key == " " then
|
|
||||||
paused = not paused
|
|
||||||
else
|
else
|
||||||
|
local found = false
|
||||||
for id, player in pairs(players) do
|
for id, player in pairs(players) do
|
||||||
player:press(key)
|
found = player:press(key) or found
|
||||||
end
|
end
|
||||||
|
if not found and (key == " " or key == "p") then
|
||||||
|
paused = not paused
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
126
menu_state.lua
126
menu_state.lua
|
@ -1,54 +1,104 @@
|
||||||
Gamestate = require 'hump.gamestate'
|
Gamestate = require 'hump.gamestate'
|
||||||
require 'level_state'
|
require 'level_state'
|
||||||
require 'settings'
|
require 'settings'
|
||||||
|
require 'entities/label'
|
||||||
|
require 'entities/valuecontroller'
|
||||||
|
|
||||||
menu_state = {}
|
menu_state = {}
|
||||||
|
|
||||||
local font = love.graphics.newFont(18)
|
local font = love.graphics.newFont(18)
|
||||||
|
local midfont = love.graphics.newFont(40)
|
||||||
local titlefont = love.graphics.newFont(72)
|
local titlefont = love.graphics.newFont(72)
|
||||||
local background = love.graphics.newImage('resources/graphics/sky.png')
|
local background = love.graphics.newImage('resources/graphics/sky.png')
|
||||||
|
|
||||||
|
local menuTime = 0
|
||||||
|
|
||||||
|
local currentlyChosen = nil
|
||||||
|
|
||||||
|
local bindingName = {
|
||||||
|
ccw = "Turn CW",
|
||||||
|
cw = "Turn CCW",
|
||||||
|
shoot = "Shoot",
|
||||||
|
accelerate = "Engine power up",
|
||||||
|
decelerate = "Engine power down",
|
||||||
|
flip = "Flip"
|
||||||
|
}
|
||||||
|
|
||||||
|
local bindingOrder = { "ccw", "cw", "shoot", "accelerate", "decelerate", "flip" }
|
||||||
|
|
||||||
function menu_state:enter()
|
function menu_state:enter()
|
||||||
|
menu_state.entity_list = {}
|
||||||
love.graphics.setBackgroundColor(0, 0, 0, 0)
|
love.graphics.setBackgroundColor(0, 0, 0, 0)
|
||||||
|
|
||||||
|
local x = 40
|
||||||
|
for player = 1, 2, 1 do
|
||||||
|
local y = 300
|
||||||
|
Label(string.format("Player %d", player), midfont, { 255, 255, 255, 255 }, "left", 500, 0, x, y, menu_state)
|
||||||
|
y = y + 60
|
||||||
|
for idx, key in ipairs(bindingOrder) do
|
||||||
|
local binding = KEYMAP[player][key]
|
||||||
|
local l1 = Label(bindingName[key], font, { 255, 255, 255, 255 }, "left", 190, 25, x, y, menu_state)
|
||||||
|
local l2 = Label(binding, font, { 255, 255, 255, 255 }, "left", 50, 25, x + 200, y, menu_state)
|
||||||
|
local adjust = function()
|
||||||
|
menu_state:adjustBindings(l1, l2, player, key)
|
||||||
|
end
|
||||||
|
l1:onClick(adjust)
|
||||||
|
l2:onClick(adjust)
|
||||||
|
y = y + 30
|
||||||
|
end
|
||||||
|
x = x + 400
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local blinkColor = function(t)
|
||||||
|
local seconds, subseconds = math.modf(menuTime)
|
||||||
|
return 255 * ((math.sin(math.pow((math.log(subseconds + 1) / math.log(2)), 2) * math.pi * 2) * 0.5 + 0.5));
|
||||||
|
end
|
||||||
|
|
||||||
|
function menu_state:adjustBindings(name, binding, player, key)
|
||||||
|
if currentlyChosen ~= nil then
|
||||||
|
currentlyChosen:delete()
|
||||||
|
end
|
||||||
|
|
||||||
|
currentlyChosen = ValueController(function ()
|
||||||
|
name.color[4] = blinkColor();
|
||||||
|
binding.color[4] = blinkColor();
|
||||||
|
end, menu_state)
|
||||||
|
currentlyChosen.data = { name = name, binding = binding, player = player, key = key, label = binding }
|
||||||
|
currentlyChosen.onDelete = function()
|
||||||
|
name.color[4] = 255;
|
||||||
|
binding.color[4] = 255;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function menu_state:draw()
|
function menu_state:draw()
|
||||||
love.graphics.setColor(128, 128, 128, 255)
|
love.graphics.setColor(128, 128, 128, 255)
|
||||||
love.graphics.draw(background)
|
love.graphics.draw(background)
|
||||||
|
|
||||||
|
local seconds, subseconds = math.modf(menuTime)
|
||||||
|
|
||||||
love.graphics.setColor(255,255,255,255)
|
love.graphics.setColor(255,255,255,255)
|
||||||
love.graphics.setFont(titlefont)
|
love.graphics.setFont(titlefont)
|
||||||
|
|
||||||
love.graphics.printf("FYSPLANE", 0, 100, love.window.getWidth(), "center")
|
love.graphics.printf("FYSPLANE", 0, 100, love.window.getWidth(), "center")
|
||||||
|
|
||||||
love.graphics.setFont(font)
|
|
||||||
love.graphics.printf("PLAYER 1\
|
|
||||||
\
|
|
||||||
Turn CW: " .. KEYMAP[1]['cw'] .. "\
|
|
||||||
Turn CCW: " .. KEYMAP[1]['ccw'] .. "\
|
|
||||||
Shoot: " .. KEYMAP[1]['shoot'] .. "\
|
|
||||||
Engine power up: " .. KEYMAP[1]['accelerate'] .. "\
|
|
||||||
Engine power down: " .. KEYMAP[1]['decelerate'] .. "\
|
|
||||||
Flip: " .. KEYMAP[1]['flip'] .. "\
|
|
||||||
", 40, 400, 400, "left")
|
|
||||||
|
|
||||||
love.graphics.printf("PLAYER 2\
|
|
||||||
\
|
|
||||||
Turn CW: " .. KEYMAP[2]['cw'] .. "\
|
|
||||||
Turn CCW: " .. KEYMAP[2]['ccw'] .. "\
|
|
||||||
Shoot: " .. KEYMAP[2]['shoot'] .. "\
|
|
||||||
Engine power up: " .. KEYMAP[2]['accelerate'] .. "\
|
|
||||||
Engine power down: " .. KEYMAP[2]['decelerate'] .. "\
|
|
||||||
Flip: " .. KEYMAP[2]['flip'] .. "\
|
|
||||||
", love.window.getWidth() - 40 - 400, 400, 400, "left")
|
|
||||||
|
|
||||||
love.graphics.printf("PRESS ANY KEY TO BEGIN…", 0, 700, love.window.getWidth(), "center")
|
love.graphics.printf("PRESS ANY KEY TO BEGIN…", 0, 700, love.window.getWidth(), "center")
|
||||||
|
|
||||||
|
for key, entity in pairs(menu_state.entity_list) do
|
||||||
|
entity:draw()
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function menu_state:update(dt)
|
function menu_state:update(dt)
|
||||||
|
menuTime = menuTime + dt
|
||||||
|
|
||||||
|
for key, entity in pairs(menu_state.entity_list) do
|
||||||
|
if entity.update then
|
||||||
|
entity:update(dt)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@ -58,17 +108,27 @@ end
|
||||||
|
|
||||||
|
|
||||||
function menu_state:keypressed(key, unicode)
|
function menu_state:keypressed(key, unicode)
|
||||||
if key == "1" then
|
if currentlyChosen == nil then
|
||||||
level_state.mode = "solo"
|
if key == "1" then
|
||||||
Gamestate.switch(level_state)
|
level_state.mode = "solo"
|
||||||
elseif key == "c" then
|
Gamestate.switch(level_state)
|
||||||
level_state.mode = "computer"
|
elseif key == "c" then
|
||||||
Gamestate.switch(level_state)
|
level_state.mode = "computer"
|
||||||
|
Gamestate.switch(level_state)
|
||||||
|
elseif key == "escape" then
|
||||||
|
love.event.quit()
|
||||||
|
else
|
||||||
|
level_state.computer = "2player"
|
||||||
|
Gamestate.switch(level_state)
|
||||||
|
end
|
||||||
elseif key == "escape" then
|
elseif key == "escape" then
|
||||||
love.event.quit()
|
currentlyChosen:delete()
|
||||||
|
currentlyChosen = nil
|
||||||
else
|
else
|
||||||
level_state.computer = "2player"
|
KEYMAP[currentlyChosen.data.player][currentlyChosen.data.key] = key
|
||||||
Gamestate.switch(level_state)
|
currentlyChosen.data.label.label = key
|
||||||
|
currentlyChosen:delete()
|
||||||
|
currentlyChosen = nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -79,7 +139,11 @@ end
|
||||||
|
|
||||||
|
|
||||||
function menu_state:mousepressed(x, y, button)
|
function menu_state:mousepressed(x, y, button)
|
||||||
|
for key, entity in pairs(menu_state.entity_list) do
|
||||||
|
if entity.mousePressed then
|
||||||
|
entity:mousePressed(x, y, button)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function menu_state:mousereleased(x, y, button)
|
function menu_state:mousereleased(x, y, button)
|
||||||
|
|
|
@ -52,23 +52,29 @@ Player = Class{
|
||||||
end;
|
end;
|
||||||
|
|
||||||
press = function(self, key)
|
press = function(self, key)
|
||||||
|
local found = false
|
||||||
for action, keycode in pairs(self.keys) do
|
for action, keycode in pairs(self.keys) do
|
||||||
if key == keycode then
|
if key == keycode then
|
||||||
|
found = true
|
||||||
if self.actions[action] and self.plane then
|
if self.actions[action] and self.plane then
|
||||||
self.actions[action](true)
|
self.actions[action](true)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
return found
|
||||||
end;
|
end;
|
||||||
|
|
||||||
release = function(self, key)
|
release = function(self, key)
|
||||||
|
local found = false
|
||||||
for action, keycode in pairs(self.keys) do
|
for action, keycode in pairs(self.keys) do
|
||||||
if key == keycode then
|
if key == keycode then
|
||||||
|
found = true
|
||||||
if self.actions[action] and self.plane then
|
if self.actions[action] and self.plane then
|
||||||
self.actions[action](false)
|
self.actions[action](false)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
return found
|
||||||
end;
|
end;
|
||||||
|
|
||||||
joystick = function(self, ...)
|
joystick = function(self, ...)
|
||||||
|
|
10
settings.lua
10
settings.lua
|
@ -13,21 +13,19 @@ KEYMAP = {
|
||||||
[1] = {
|
[1] = {
|
||||||
ccw = 'k',
|
ccw = 'k',
|
||||||
cw = 'l',
|
cw = 'l',
|
||||||
flip = 'i',
|
flip = ',',
|
||||||
shoot = 'o',
|
shoot = 'o',
|
||||||
accelerate = 'j',
|
accelerate = 'j',
|
||||||
decelerate = 'm',
|
decelerate = 'm'
|
||||||
flip = ','
|
|
||||||
},
|
},
|
||||||
|
|
||||||
[2] = {
|
[2] = {
|
||||||
ccw = 'a',
|
ccw = 'a',
|
||||||
cw = 's',
|
cw = 's',
|
||||||
flip = 'q',
|
flip = 'a',
|
||||||
shoot = 'w',
|
shoot = 'w',
|
||||||
accelerate = 'x',
|
accelerate = 'x',
|
||||||
decelerate = 'z',
|
decelerate = 'z'
|
||||||
flip = 'a'
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue