-
Notifications
You must be signed in to change notification settings - Fork 17
/
button.lua
138 lines (118 loc) · 3.19 KB
/
button.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
button = class("button")
function button:init(x, y, r)
self.cox = x
self.coy = y
--PHYSICS STUFF
self.x = x-15/16
self.y = y-3/16
self.width = 30/16
self.height = 3/16
self.static = true
self.active = false
self.category = 22
self.dir = "down"
self.objtable = {"player", "enemy", "box"}
self.mask = {true}
self.drawable = false
--Input list
self.input1state = "off"
self.r = {unpack(r)}
table.remove(self.r, 1)
table.remove(self.r, 1)
--DIRECTION
if #self.r > 0 and self.r[1] ~= "link" then
self.dir = self.r[1]
table.remove(self.r, 1)
end
self.out = false
self.outtable = {}
self.pressings = {}
end
function button:update(dt)
local x, y, width, height
if self.dir == "down" then
x = self.x+5/16
y = self.y-2/16
width = 20/16
height = 1
elseif self.dir == "left" then
x = self.x-12/16
y = self.y-7/16
width = 1
height = 20/16
elseif self.dir == "right" then
x = self.x+10/16
y = self.y-7/16
width = 1
height = 20/16
elseif self.dir == "up" then
x = self.x+5/16
y = self.y-24/16
width = 20/16
height = 1
end
local colls = checkrect(x, y, width, height, self.objtable)
for j = 1, #colls, 2 do
local add = true
for i, v in pairs(self.pressings) do
if objects[colls[j]][colls[j+1]] == v then
add = false
end
end
if add then
table.insert(self.pressings, objects[colls[j]][colls[j+1]])
if objects[colls[j]][colls[j+1]].onbutton then
objects[colls[j]][colls[j+1]]:onbutton(true)
end
end
end
local delete = {}
for i, v in pairs(self.pressings) do
local rem = true
for j = 1, #colls, 2 do
if objects[colls[j]][colls[j+1]] == v then
rem = false
end
end
if rem then
table.insert(delete, i)
end
end
table.sort(delete, function(a,b) return a>b end)
for i, v in pairs(delete) do
if self.pressings[v].onbutton then
self.pressings[v]:onbutton(false)
end
table.remove(self.pressings, v)
end
if (#colls > 0) ~= self.out then
self.out = not self.out
for i = 1, #self.outtable do
if self.outtable[i][1].input then
if self.out then
self.outtable[i][1]:input("on", self.outtable[i][2])
else
self.outtable[i][1]:input("off", self.outtable[i][2])
end
end
end
end
end
function button:draw()
local quad = 1
if self.out then
quad = 2
end
if self.dir == "down" then
love.graphics.draw(buttonimg, buttonquad[quad], math.floor((self.x-1/16-xscroll)*16*scale), ((self.y-yscroll)*16-10)*scale, 0, scale, scale)
elseif self.dir == "left" then
love.graphics.draw(buttonimg, buttonquad[quad], math.floor((self.x+4/16-xscroll)*16*scale), ((self.y-yscroll)*16-21)*scale, math.pi/2, scale, scale)
elseif self.dir == "right" then
love.graphics.draw(buttonimg, buttonquad[quad], math.floor((self.x+10/16-xscroll)*16*scale), ((self.y-yscroll)*16+11)*scale, -math.pi/2, scale, scale)
elseif self.dir == "up" then
love.graphics.draw(buttonimg, buttonquad[quad], math.floor((self.x+31/16-xscroll)*16*scale), ((self.y-yscroll)*16-16)*scale, math.pi, scale, scale)
end
end
function button:addoutput(a, t)
table.insert(self.outtable, {a, t})
end