-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.lua
95 lines (86 loc) · 2.64 KB
/
input.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
local function selectSlot( slotNum )
print( "Selected slot #" .. tostring( slotNum ) )
local item = game.config.hotBar[ slotNum ]
if item then
local block = game.block.type[item] or false
if block then
game.curBlockType = block
else
game.curBlockType = game.block.type.stone
end
else
game.config.hotBar[ slotNum ] = "none"
end
end
local actions =
{
quit = function() love.event.quit() end,
placeBlock = function() game.block.create( game.mouseXCoord, game.mouseYCoord, game.curBlockType ) end,
destroyBlock = function() game.block.remove( game.mouseXCoord, game.mouseYCoord ) end,
["+moveUp"] = function() game.moving.up = true end,
["-moveUp"] = function() game.moving.up = false end,
["+moveDown"] = function() game.moving.down = true end,
["-moveDown"] = function() game.moving.down = false end,
["+moveLeft"] = function() game.moving.left = true end,
["-moveLeft"] = function() game.moving.left = false end,
["+moveRight"] = function() game.moving.right = true end,
["-moveRight"] = function() game.moving.right = false end,
["+moveUp"] = function() game.moving.up = true end,
["-moveUp"] = function() game.moving.up = false end,
["+sprint"] = function() game.moving.sprinting = true end,
["-sprint"] = function() game.moving.sprinting = false end
}
local bindings =
{
escape = "quit",
mouse1 = "destroyBlock",
mouse2 = "placeBlock",
w = "+moveUp",
s = "+moveDown",
a = "+moveLeft",
d = "+moveRight",
["lshift"] = "+sprint"
}
for slotNum = 0, 9 do
local slot = "slot" .. tostring( slotNum )
actions[ slot ] = function() selectSlot( slotNum ) end
bindings[tostring( slotNum )] = slot
end
local function handleInput( actionName )
local action = actions[ actionName ]
if action then
print( actionName .. " is a valid action." )
action()
else
print( actionName .. " is not a valid action." )
return
end
end
local function bindPress( key )
local actionName = bindings[ key ]
if actionName then
print( key .. " is bound to " .. actionName )
handleInput( actionName )
else
print( key .. " is not bound to any action. " )
end
end
local function bindRelease( key )
local actionName = bindings[ key ]
if actionName then
actionName = string.gsub( actionName, "+", "-" )
print( key .. "(release) is bound to " .. actionName )
handleInput( actionName )
else
print( key .. "(release) is not bound to any action. " )
end
end
function love.keypressed( key )
bindPress( key )
end
function love.keyreleased( key )
bindRelease( key )
end
function love.mousepressed( mouseX, mouseY, button, touch )
bindPress( "mouse" .. tostring( button ) )
end