2014-10-17 19:28:17 +00:00
|
|
|
Gamestate = require 'hump.gamestate'
|
2014-10-19 10:54:54 +00:00
|
|
|
require 'level_state'
|
|
|
|
require 'settings'
|
2014-10-23 18:42:30 +00:00
|
|
|
require 'entities/label'
|
|
|
|
require 'entities/valuecontroller'
|
2014-10-26 08:29:55 +00:00
|
|
|
require 'entities/visrectangle'
|
2014-10-24 06:43:42 +00:00
|
|
|
require 'utils'
|
2014-10-17 19:28:17 +00:00
|
|
|
|
|
|
|
menu_state = {}
|
|
|
|
|
|
|
|
local font = love.graphics.newFont(18)
|
2014-10-23 18:42:30 +00:00
|
|
|
local midfont = love.graphics.newFont(40)
|
2014-10-19 10:54:54 +00:00
|
|
|
local titlefont = love.graphics.newFont(72)
|
|
|
|
local background = love.graphics.newImage('resources/graphics/sky.png')
|
2014-10-17 19:28:17 +00:00
|
|
|
|
2014-10-23 18:42:30 +00:00
|
|
|
local menuTime = 0
|
|
|
|
|
|
|
|
local currentlyChosen = nil
|
|
|
|
|
2014-10-26 08:29:55 +00:00
|
|
|
local bindingInfo = {
|
2014-10-23 18:42:30 +00:00
|
|
|
ccw = "Turn CW",
|
|
|
|
cw = "Turn CCW",
|
|
|
|
shoot = "Shoot",
|
|
|
|
accelerate = "Engine power up",
|
|
|
|
decelerate = "Engine power down",
|
|
|
|
flip = "Flip"
|
|
|
|
}
|
|
|
|
|
2014-10-26 08:29:55 +00:00
|
|
|
local joyBindingInfo = {
|
|
|
|
rotation = "Turn",
|
|
|
|
throttle = "Engine power"
|
|
|
|
}
|
|
|
|
|
2014-10-23 18:42:30 +00:00
|
|
|
local bindingOrder = { "ccw", "cw", "shoot", "accelerate", "decelerate", "flip" }
|
|
|
|
|
2014-10-26 08:29:55 +00:00
|
|
|
local joyBindingOrder = { "rotation", "throttle" }
|
|
|
|
|
2014-10-17 19:28:17 +00:00
|
|
|
function menu_state:enter()
|
2014-10-23 18:42:30 +00:00
|
|
|
menu_state.entity_list = {}
|
2014-10-17 19:28:17 +00:00
|
|
|
love.graphics.setBackgroundColor(0, 0, 0, 0)
|
2014-10-27 07:01:31 +00:00
|
|
|
|
|
|
|
love.window.setMode(levelWidth(), levelHeight(), {resizable=true, vsync=true})
|
|
|
|
|
2014-10-23 18:42:30 +00:00
|
|
|
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
|
2014-10-26 08:29:55 +00:00
|
|
|
Label(bindingInfo[key], font, { 255, 255, 255, 255 }, "left", 190, 25, x, y, menu_state)
|
2014-10-24 06:05:00 +00:00
|
|
|
for bindingIdx, binding in ipairs(KEYMAP[player][key]) do
|
2014-10-26 08:36:40 +00:00
|
|
|
local label = Label(function() return KEYMAP[player][key][bindingIdx] end,
|
|
|
|
font, { 255, 255, 255, 255 }, "center", 100, 25, x + 200 + 100 * (bindingIdx - 1), y, menu_state)
|
2014-10-24 06:05:00 +00:00
|
|
|
label:onClick(function()
|
|
|
|
menu_state:adjustBindings(label, bindingIdx, player, key)
|
|
|
|
end)
|
2014-10-23 18:42:30 +00:00
|
|
|
end
|
|
|
|
y = y + 30
|
|
|
|
end
|
2014-10-26 08:29:55 +00:00
|
|
|
for idx, axis in ipairs(joyBindingOrder) do
|
|
|
|
local axisInfo = AXISMAP[player][axis]
|
|
|
|
Label(joyBindingInfo[axis], font, { 255, 255, 255, 255 }, "left", 190, 25, x, y, menu_state)
|
|
|
|
for bindingIdx, info in ipairs(axisInfo) do
|
|
|
|
local visAxisBaseX = (100 + 30) / 2 + x + 200 + 100 * (bindingIdx - 1)
|
|
|
|
|
2014-10-26 08:36:40 +00:00
|
|
|
local signLabel = Label(function()
|
|
|
|
if AXISMAP[player][axis][bindingIdx].flipped then
|
|
|
|
return "-"
|
|
|
|
else
|
|
|
|
return "+"
|
|
|
|
end
|
|
|
|
end, font, { 255, 255, 255, 255 }, "center", 20, 25, x + 200 + 100 * (bindingIdx - 1), y, menu_state)
|
2014-10-26 08:29:55 +00:00
|
|
|
|
|
|
|
signLabel:onClick(function()
|
2014-10-26 08:36:40 +00:00
|
|
|
menu_state:switchSign(player, axis, bindingIdx)
|
2014-10-26 08:29:55 +00:00
|
|
|
end)
|
|
|
|
|
2014-10-26 08:36:40 +00:00
|
|
|
local axisLabel = Label(function()
|
|
|
|
return AXISMAP[player][axis][bindingIdx].axis
|
|
|
|
end, font, { 255, 255, 255, 255 }, "center", 70, 25, x + 30 + 200 + 100 * (bindingIdx - 1), y, menu_state)
|
2014-10-26 08:29:55 +00:00
|
|
|
|
|
|
|
axisLabel:onClick(function()
|
2014-10-26 08:36:40 +00:00
|
|
|
menu_state:switchAxis(player, axis, bindingIdx)
|
2014-10-26 08:29:55 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
local visAxis = VisRectangle({ 40, 40, 0, 128 }, 10, 10, nil, visAxisBaseX, y + 11, menu_state)
|
|
|
|
|
|
|
|
ValueController(function ()
|
|
|
|
local joysticks = love.joystick.getJoysticks()
|
|
|
|
local axisIdx = AXISMAP[player][axis][bindingIdx].axis
|
|
|
|
if #joysticks >= player then
|
2014-10-26 10:33:12 +00:00
|
|
|
local j = joysticks[player]
|
|
|
|
if axisIdx > 0 and axisIdx <= j:getAxisCount() then
|
|
|
|
local direction = j:getAxis(axisIdx)
|
|
|
|
visAxis.x = visAxisBaseX + 30 * direction
|
|
|
|
end
|
2014-10-26 08:29:55 +00:00
|
|
|
end
|
|
|
|
end, menu_state)
|
|
|
|
end
|
|
|
|
y = y + 30
|
|
|
|
end
|
2014-10-24 06:58:04 +00:00
|
|
|
x = x + 500
|
2014-10-23 18:42:30 +00:00
|
|
|
end
|
2014-10-17 19:28:17 +00:00
|
|
|
end
|
|
|
|
|
2014-10-26 08:36:40 +00:00
|
|
|
function menu_state:switchSign(player, axis, bindingIdx)
|
2014-10-26 08:29:55 +00:00
|
|
|
AXISMAP[player][axis][bindingIdx].flipped = not AXISMAP[player][axis][bindingIdx].flipped
|
|
|
|
save_settings()
|
|
|
|
end
|
|
|
|
|
2014-10-26 08:36:40 +00:00
|
|
|
function menu_state:switchAxis(player, axis, bindingIdx)
|
2014-10-26 08:29:55 +00:00
|
|
|
local joysticks = love.joystick.getJoysticks()
|
|
|
|
|
|
|
|
if #joysticks >= player then
|
|
|
|
local joystick = joysticks[player]
|
|
|
|
local axisIdx = AXISMAP[player][axis][bindingIdx].axis
|
|
|
|
|
|
|
|
axisIdx = axisIdx + 1
|
|
|
|
if axisIdx > joystick:getAxisCount() then
|
2014-10-26 10:33:12 +00:00
|
|
|
axisIdx = 0
|
2014-10-26 08:29:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
AXISMAP[player][axis][bindingIdx].axis = axisIdx
|
|
|
|
save_settings()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-23 18:42:30 +00:00
|
|
|
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
|
|
|
|
|
2014-10-24 06:05:00 +00:00
|
|
|
function menu_state:adjustBindings(label, bindingIdx, player, key)
|
2014-10-23 18:42:30 +00:00
|
|
|
if currentlyChosen ~= nil then
|
|
|
|
currentlyChosen:delete()
|
|
|
|
end
|
|
|
|
|
|
|
|
currentlyChosen = ValueController(function ()
|
2014-10-24 06:05:00 +00:00
|
|
|
label.color[4] = blinkColor();
|
2014-10-23 18:42:30 +00:00
|
|
|
end, menu_state)
|
2014-10-24 06:05:00 +00:00
|
|
|
currentlyChosen.data = { bindingIdx = bindingIdx, player = player, key = key, label = label }
|
2014-10-23 18:42:30 +00:00
|
|
|
currentlyChosen.onDelete = function()
|
2014-10-24 06:05:00 +00:00
|
|
|
label.color[4] = 255;
|
2014-10-23 18:42:30 +00:00
|
|
|
end
|
|
|
|
end
|
2014-10-17 19:28:17 +00:00
|
|
|
|
|
|
|
function menu_state:draw()
|
2014-10-19 10:54:54 +00:00
|
|
|
love.graphics.setColor(128, 128, 128, 255)
|
2014-10-26 18:36:43 +00:00
|
|
|
love.graphics.draw(background, 0, 0, 0,
|
|
|
|
levelWidth() / background:getWidth(),
|
|
|
|
levelHeight() / background:getHeight())
|
|
|
|
|
2014-10-19 10:54:54 +00:00
|
|
|
|
2014-10-23 18:42:30 +00:00
|
|
|
local seconds, subseconds = math.modf(menuTime)
|
|
|
|
|
2014-10-17 19:28:17 +00:00
|
|
|
love.graphics.setColor(255,255,255,255)
|
2014-10-19 10:54:54 +00:00
|
|
|
love.graphics.setFont(titlefont)
|
|
|
|
|
|
|
|
love.graphics.printf("FYSPLANE", 0, 100, love.window.getWidth(), "center")
|
|
|
|
|
2014-10-30 20:01:29 +00:00
|
|
|
love.graphics.setFont(midfont);
|
|
|
|
love.graphics.printf("1 for solo game", 0, 600, love.window.getWidth(), "center")
|
|
|
|
love.graphics.printf("2 for two-player game", 0, 630, love.window.getWidth(), "center")
|
|
|
|
love.graphics.printf("c for game against the computer", 0, 660, love.window.getWidth(), "center")
|
|
|
|
|
|
|
|
love.graphics.setFont(titlefont)
|
|
|
|
love.graphics.printf("PRESS SPACE TO BEGIN…", 0, 700, love.window.getWidth(), "center")
|
2014-10-23 18:42:30 +00:00
|
|
|
|
|
|
|
for key, entity in pairs(menu_state.entity_list) do
|
|
|
|
entity:draw()
|
|
|
|
end
|
|
|
|
|
2014-10-17 19:28:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function menu_state:update(dt)
|
2014-10-23 18:42:30 +00:00
|
|
|
menuTime = menuTime + dt
|
2014-10-17 19:28:17 +00:00
|
|
|
|
2014-10-24 06:43:42 +00:00
|
|
|
if currentlyChosen ~= nil then
|
|
|
|
local button = nil
|
2014-10-24 06:53:28 +00:00
|
|
|
local joysticks = love.joystick.getJoysticks()
|
|
|
|
if #joysticks >= currentlyChosen.data.player then
|
|
|
|
local buttons = getJoystickButtons(joysticks[currentlyChosen.data.player])
|
|
|
|
|
|
|
|
for i = 1, #buttons, 1 do
|
|
|
|
if buttons[i] then
|
|
|
|
button = i
|
|
|
|
end
|
2014-10-24 06:43:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if button ~= nil then
|
|
|
|
local key = string.format("button%d", button)
|
|
|
|
KEYMAP[currentlyChosen.data.player][currentlyChosen.data.key][currentlyChosen.data.bindingIdx] = key
|
|
|
|
currentlyChosen.data.label.label = key
|
|
|
|
currentlyChosen:delete()
|
|
|
|
currentlyChosen = nil
|
|
|
|
save_settings()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-10-23 18:42:30 +00:00
|
|
|
for key, entity in pairs(menu_state.entity_list) do
|
|
|
|
if entity.update then
|
|
|
|
entity:update(dt)
|
|
|
|
end
|
|
|
|
end
|
2014-10-17 19:28:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function menu_state:focus(bool)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function menu_state:keypressed(key, unicode)
|
2014-10-27 07:01:31 +00:00
|
|
|
love.window.setMode(levelWidth(), levelHeight(), {resizable=false, vsync=true})
|
2014-10-23 18:42:30 +00:00
|
|
|
if currentlyChosen == nil then
|
|
|
|
if key == "1" then
|
|
|
|
level_state.mode = "solo"
|
|
|
|
Gamestate.switch(level_state)
|
|
|
|
elseif key == "c" then
|
|
|
|
level_state.mode = "computer"
|
|
|
|
Gamestate.switch(level_state)
|
2014-10-26 14:46:59 +00:00
|
|
|
elseif key == "2" then
|
|
|
|
level_state.mode = "2player"
|
|
|
|
Gamestate.switch(level_state)
|
2014-10-23 18:42:30 +00:00
|
|
|
elseif key == "escape" then
|
|
|
|
love.event.quit()
|
2014-10-30 20:01:29 +00:00
|
|
|
elseif key == " " then
|
2014-10-23 18:42:30 +00:00
|
|
|
Gamestate.switch(level_state)
|
|
|
|
end
|
2014-10-22 18:44:51 +00:00
|
|
|
elseif key == "escape" then
|
2014-10-23 18:42:30 +00:00
|
|
|
currentlyChosen:delete()
|
|
|
|
currentlyChosen = nil
|
2014-10-19 11:02:32 +00:00
|
|
|
else
|
2014-10-24 06:27:17 +00:00
|
|
|
if key == "backspace" then
|
|
|
|
key = "none"
|
|
|
|
end
|
2014-10-24 06:05:00 +00:00
|
|
|
KEYMAP[currentlyChosen.data.player][currentlyChosen.data.key][currentlyChosen.data.bindingIdx] = key
|
2014-10-23 18:42:30 +00:00
|
|
|
currentlyChosen:delete()
|
|
|
|
currentlyChosen = nil
|
2014-10-23 18:55:57 +00:00
|
|
|
save_settings()
|
2014-10-19 11:02:32 +00:00
|
|
|
end
|
2014-10-17 19:28:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function menu_state:keyreleased(key, unicode)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2014-10-24 06:14:09 +00:00
|
|
|
function menu_state:joystickpressed(key, button)
|
|
|
|
end
|
|
|
|
|
2014-10-17 19:28:17 +00:00
|
|
|
|
|
|
|
function menu_state:mousepressed(x, y, button)
|
2014-10-23 18:42:30 +00:00
|
|
|
for key, entity in pairs(menu_state.entity_list) do
|
|
|
|
if entity.mousePressed then
|
|
|
|
entity:mousePressed(x, y, button)
|
|
|
|
end
|
|
|
|
end
|
2014-10-17 19:28:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function menu_state:mousereleased(x, y, button)
|
|
|
|
|
|
|
|
end
|