fysplane/menu_state.lua

153 lines
3.8 KiB
Lua
Raw Normal View History

2014-10-17 19:28:17 +00:00
Gamestate = require 'hump.gamestate'
require 'level_state'
require 'settings'
2014-10-23 18:42:30 +00:00
require 'entities/label'
require 'entities/valuecontroller'
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)
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
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" }
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-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
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
2014-10-17 19:28:17 +00:00
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
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
2014-10-17 19:28:17 +00:00
function menu_state:draw()
love.graphics.setColor(128, 128, 128, 255)
love.graphics.draw(background)
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)
love.graphics.setFont(titlefont)
love.graphics.printf("FYSPLANE", 0, 100, love.window.getWidth(), "center")
love.graphics.printf("PRESS ANY KEY 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-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-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)
elseif key == "escape" then
love.event.quit()
else
level_state.computer = "2player"
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-23 18:42:30 +00:00
KEYMAP[currentlyChosen.data.player][currentlyChosen.data.key] = key
currentlyChosen.data.label.label = key
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
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