-
Notifications
You must be signed in to change notification settings - Fork 26
/
button.lua
168 lines (148 loc) · 4.02 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
button = class:new()
function button:init(x, y, t, 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.t = t or 1
if self.t == 2 or self.t == 3 then
self.checktable = {"box"}
else
self.checktable = {"player", "box", "core", "powblock", "smallspring", "pbutton", "enemy"}
for i, v in pairs(enemies) do
if v ~= "icicle" and v ~= "chainchomp" then
table.insert(self.checktable, v)
end
end
end
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.mask = {true}
self.drawable = false
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.checktable)
--ignore certain entities (needed for cube and sphere buttons for example)
local delete
for j = 1, #colls, 2 do
local rem = false
local obj = objects[colls[j]][colls[j+1]]
if (self.t == 2 and (obj.t ~= "box1" and obj.t ~= "box2")) or (self.t == 3 and obj.t ~= "edgeless") or obj.doesntpressbuttons then
if not delete then delete = {} end
table.insert(delete, j)
table.insert(delete, j+1)
end
end
if delete then
table.sort(delete, function(a,b) return a>b end)
for i, v in pairs(delete) do
table.remove(colls, v)
end
end
--update lights on cubes
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
if not delete then delete = {} end
table.insert(delete, i)
end
end
if delete then
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
end
--send out put if being pressed
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[spriteset][self.t][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[spriteset][self.t][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[spriteset][self.t][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[spriteset][self.t][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