alunizaje/assets/scripts/gui/controls.lua

87 lines
2.6 KiB
Lua
Raw Normal View History

2017-01-03 23:49:53 +01:00
local anim8 = require 'assets/scripts/vendor/anim8'
2016-11-12 13:49:24 +01:00
local controls = {}
2017-01-03 23:49:53 +01:00
local BUTTONS = {}
2016-11-12 13:49:24 +01:00
2016-12-08 20:02:27 +01:00
function controls.load(game)
2017-01-03 23:49:53 +01:00
-- Button left
2017-01-05 10:49:04 +01:00
local img_button_left = love.graphics.newImage('assets/sprites/gui/button_left.png')
local left_x = PADDING
2017-01-06 21:36:57 +01:00
local left_y = game.window.height - img_button_left:getHeight() - PADDING
2017-01-03 23:49:53 +01:00
controls.new_button('button_left', left_x, left_y, img_button_left)
-- Button right
2017-01-05 10:49:04 +01:00
local img_button_right = love.graphics.newImage('assets/sprites/gui/button_right.png')
local right_x = game.window.width - (img_button_right:getWidth() / 2) - PADDING
2017-01-06 21:36:57 +01:00
local right_y = game.window.height - img_button_right:getHeight() - PADDING
2017-01-03 23:49:53 +01:00
controls.new_button('button_right', right_x, right_y, img_button_right)
-- Button up
2017-01-05 10:49:04 +01:00
local img_button_up = love.graphics.newImage('assets/sprites/gui/button_up.png')
local up_x = game.window.width - (img_button_up:getWidth() / 2) - PADDING
2017-01-06 21:36:57 +01:00
local up_y = right_y - img_button_up:getHeight() - PADDING
2017-01-03 23:49:53 +01:00
controls.new_button('button_up', up_x, up_y, img_button_up)
2016-12-08 20:02:27 +01:00
end
function controls.draw()
2017-01-03 23:49:53 +01:00
for key, button in pairs(BUTTONS) do
button.animation:draw(button.img, button.x, button.y)
end
2016-12-08 20:02:27 +01:00
end
2017-01-03 22:05:46 +01:00
function controls.keypressed(key, scancode, isrepeat)
if key == 'escape' or key == 'q' then
CONTROL_QUIT = true
end
if key == 'right' then
CONTROL_RIGHT = true
elseif key == 'left' then
CONTROL_LEFT = true
end
if key == 'up' then
CONTROL_UP = true
end
end
function controls.keyreleased(key, scancode)
CONTROL_UP, CONTROL_RIGHT, CONTROL_LEFT, CONTROL_QUIT = false, false, false, false
end
function controls.mousepressed(x, y, button, istouch)
2017-01-03 23:49:53 +01:00
local buttons_pressends = {}
for key, button in pairs(BUTTONS) do
if button.x <= x and x <= (button.x + (button.img:getWidth() / 2)) and button.y <= y and y <= (button.y + button.img:getHeight()) then
table.insert(buttons_pressends, button.name)
button.animation:gotoFrame(2)
mouse_actions(button.name)
end
end
2017-01-03 22:05:46 +01:00
end
function controls.mousereleased(x, y, button, istouch)
2017-01-03 23:49:53 +01:00
CONTROL_UP, CONTROL_RIGHT, CONTROL_LEFT, CONTROL_QUIT = false, false, false, false
for key, button in pairs(BUTTONS) do
button.animation:gotoFrame(1)
end
end
function controls.new_button(name, x, y, img)
img_temp = img
g = anim8.newGrid(img_temp:getWidth() / 2, img_temp:getHeight(), img_temp:getWidth(), img_temp:getHeight())
table.insert(BUTTONS, {
name=name,
x=x,
y=y,
img=img_temp,
animation = anim8.newAnimation(g('1-2', 1), 1)
})
end
function mouse_actions(name)
if name == 'button_left' then
CONTROL_LEFT = true
elseif name == 'button_right' then
CONTROL_RIGHT = true
elseif name == 'button_up' then
CONTROL_UP = true
end
2017-01-03 22:05:46 +01:00
end
2016-11-12 13:49:24 +01:00
return controls