-
Notifications
You must be signed in to change notification settings - Fork 108
/
laser.lua
419 lines (340 loc) · 10.3 KB
/
laser.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
laser = class:new()
function laser:init(x, y, dir, r)
self.cox = x
self.coy = y
self.dir = dir
self.r = r
self.blocked = false
self.lasertable = {}
self.outtable = {}
self.framestart = 0
self.enabled = true
end
function laser:link()
self.outtable = {}
if #self.r > 2 then
for j, w in pairs(outputs) do
for i, v in pairs(objects[w]) do
if tonumber(self.r[4]) == v.cox and tonumber(self.r[5]) == v.coy then
v:addoutput(self)
self.enabled = false
end
end
end
end
end
function laser:input(t)
if t == "on" then
self.enabled = true
self:updaterange()
elseif t == "off" then
self.enabled = false
self:updaterange()
else
self.enabled = not self.enabled
self:updaterange()
end
end
function laser:update(dt)
if self.framestart < 2 then
self.framestart = self.framestart + 1
return
end
local x, y, width, height
local col = false
for i = 1, #self.lasertable, 5 do
if self.lasertable[i] == "left" then
x = self.lasertable[i+1]-1+self.lasertable[i+3]
y = self.lasertable[i+2]-0.5625
width = -self.lasertable[i+3]+1
height = 2/16
local rectcol = checkrect(x, y, width, height, {"player", "box", "goomba", "koopa"})
if #rectcol > 0 then
col = true
self.blocked = true
local biggestx = -1
local biggesti
for j = 1, #rectcol, 2 do
if objects[rectcol[j]][rectcol[j+1]].x > biggestx then
biggestx = objects[rectcol[j]][rectcol[j+1]].x
biggesti = j
end
end
obj = objects[rectcol[biggesti]][rectcol[biggesti+1]]
if obj.laser then
obj:laser("right")
end
local newtable = {}
for k = 1, i*5 do
table.insert(newtable, self.lasertable[k])
end
newtable[i+3] = obj.x - newtable[i+1] + obj.width+1
self.lasertable = newtable
self:updateoutputs()
break
end
elseif self.lasertable[i] == "right" then
x = self.lasertable[i+1]-1
y = self.lasertable[i+2]-0.5625
width = self.lasertable[i+3]+1
height = 2/16
local rectcol = checkrect(x, y, width, height, {"player", "box", "goomba", "koopa"})
if #rectcol > 0 then
col = true
self.blocked = true
local smallestx = mapwidth+1
local smallesti
for j = 1, #rectcol, 2 do
if objects[rectcol[j]][rectcol[j+1]].x < smallestx then
smallestx = objects[rectcol[j]][rectcol[j+1]].x
smallesti = j
end
end
obj = objects[rectcol[smallesti]][rectcol[smallesti+1]]
if obj.laser then
obj:laser("left")
end
local newtable = {}
for k = 1, i*5 do
table.insert(newtable, self.lasertable[k])
end
newtable[i+3] = obj.x - newtable[i+1]
self.lasertable = newtable
self:updateoutputs()
break
end
elseif self.lasertable[i] == "up" then
x = self.lasertable[i+1]-0.5625
y = self.lasertable[i+2]+self.lasertable[i+4]-1
width = 2/16
height = -self.lasertable[i+4]+1
local rectcol = checkrect(x, y, width, height, {"player", "box", "goomba", "koopa"})
if #rectcol > 0 then
col = true
self.blocked = true
local biggesty = 0
local biggesti
for j = 1, #rectcol, 2 do
if objects[rectcol[j]][rectcol[j+1]].y > biggesty then
biggesty = objects[rectcol[j]][rectcol[j+1]].y
biggesti = j
end
end
obj = objects[rectcol[biggesti]][rectcol[biggesti+1]]
if obj.laser then
obj:laser("down")
end
local newtable = {}
for k = 1, i*5 do
table.insert(newtable, self.lasertable[k])
end
newtable[i+4] = obj.y - newtable[i+2]+1
self.lasertable = newtable
self:updateoutputs()
break
end
elseif self.lasertable[i] == "down" then
x = self.lasertable[i+1]-0.5625
y = self.lasertable[i+2]-1
width = 2/16
height = self.lasertable[i+4]+1
local rectcol = checkrect(x, y, width, height, {"player", "box", "goomba", "koopa"})
if #rectcol > 0 then
col = true
self.blocked = true
local smallesty = 16
local smallesti
for j = 1, #rectcol, 2 do
if objects[rectcol[j]][rectcol[j+1]].y < smallesty then
smallesty = objects[rectcol[j]][rectcol[j+1]].y
smallesti = j
end
end
obj = objects[rectcol[smallesti]][rectcol[smallesti+1]]
if obj.laser then
obj:laser("up")
end
local newtable = {}
for k = 1, i*5 do
table.insert(newtable, self.lasertable[k])
end
newtable[i+4] = obj.y - newtable[i+2] + 1 - obj.height+1-1
self.lasertable = newtable
self:updateoutputs()
break
end
end
end
self:updateoutputs()
if col == false and self.blocked == true then
self.blocked = false
self:updaterange()
end
end
function laser:updateoutputs()
for i = 1, #self.lasertable, 5 do
if self.lasertable[i] == "left" then
for x = self.lasertable[i+1], math.ceil(self.lasertable[i+1]+self.lasertable[i+3])-1, -1 do
local y = self.lasertable[i+2]
self:checktile(x, y)
end
elseif self.lasertable[i] == "right" then
for x = self.lasertable[i+1], math.floor(self.lasertable[i+1]+self.lasertable[i+3])+1 do
local y = self.lasertable[i+2]
self:checktile(x, y)
end
elseif self.lasertable[i] == "up" then
for y = self.lasertable[i+2], self.lasertable[i+2]+self.lasertable[i+4]-1, -1 do
local x = self.lasertable[i+1]
self:checktile(x, y)
end
elseif self.lasertable[i] == "down" then
for y = self.lasertable[i+2], self.lasertable[i+2]+self.lasertable[i+4]+1 do
local x = self.lasertable[i+1]
self:checktile(x, y)
end
end
end
for i, v in pairs(self.outtable) do
v:clear()
end
end
function laser:checktile(x, y)
--check if block is a detector
for i, v in pairs(objects["laserdetector"]) do
if x == v.cox and y == v.coy and (v.cox ~= self.r[4] or v.coy ~= self.r[5]) then
table.insert(self.outtable, v)
v:input("on")
end
end
end
function laser:draw()
for i = 1, #self.lasertable, 5 do
if self.lasertable[i] == "left" then
love.graphics.setScissor(math.floor((self.lasertable[i+1]+self.lasertable[i+3]-xscroll-1)*16*scale), (self.lasertable[i+2]-1.5)*16*scale, math.max((-self.lasertable[i+3]+1)*16*scale, 0), 16*scale)
for x = self.lasertable[i+1], math.floor(self.lasertable[i+1]+self.lasertable[i+3])-1, -1 do
love.graphics.draw(laserimg, math.floor((x-xscroll-1)*16*scale), (self.lasertable[i+2]-20/16)*16*scale, 0, scale, scale)
end
elseif self.lasertable[i] == "right" then
love.graphics.setScissor(math.floor((self.lasertable[i+1]-xscroll-1)*16*scale), (self.lasertable[i+2]-1.5)*16*scale, math.max((self.lasertable[i+3]+1)*16*scale, 0), 16*scale)
for x = self.lasertable[i+1], math.ceil(self.lasertable[i+1]+self.lasertable[i+3])+1 do
love.graphics.draw(laserimg, math.floor((x-xscroll-1)*16*scale), (self.lasertable[i+2]-20/16)*16*scale, 0, scale, scale)
end
elseif self.lasertable[i] == "up" then
for y = self.lasertable[i+2], self.lasertable[i+2]+self.lasertable[i+4], -1 do
love.graphics.draw(laserimg, math.floor((self.lasertable[i+1]-xscroll-5/16)*16*scale), (y-1)*16*scale, math.pi/2, scale, scale, 8, 1)
end
elseif self.lasertable[i] == "down" then
for y = self.lasertable[i+2], self.lasertable[i+2]+self.lasertable[i+4] do
love.graphics.draw(laserimg, math.floor((self.lasertable[i+1]-xscroll-5/16)*16*scale), (y-1)*16*scale, math.pi/2, scale, scale, 8, 1)
end
end
love.graphics.setScissor()
end
local rot = 0
if self.dir == "up" then
rot = math.pi*1.5
elseif self.dir == "down" then
rot = math.pi*0.5
elseif self.dir == "left" then
rot = math.pi
end
love.graphics.draw(lasersideimg, math.floor((self.cox-xscroll-.5)*16*scale), (self.coy-1)*16*scale, rot, scale, scale, 8, 8)
end
function laser:updaterange()
self.lasertable = {}
if self.enabled == false then
self:updateoutputs()
return
end
local dir = self.dir
local startx, starty = self.cox, self.coy
local rangex, rangey = 0, 0
local x, y = self.cox, self.coy
local firstcheck = true
local quit = false
while x >= 1 and x <= mapwidth and y >= 1 and y <= 15 and tilequads[map[x][y][1]].collision == false and (x ~= startx or y ~= starty or dir ~= self.dir or firstcheck == true) and quit == false do
firstcheck = false
if dir == "right" then
x = x + 1
rangex = rangex + 1
elseif dir == "left" then
x = x - 1
rangex = rangex - 1
elseif dir == "up" then
y = y - 1
rangey = rangey - 1
elseif dir == "down" then
y = y + 1
rangey = rangey + 1
end
--check if current block is a portal
local portalx, portaly, portalfacing, infacing = getPortal(x, y)
if portalx ~= false and ((dir == "left" and infacing == "right") or (dir == "right" and infacing == "left") or (dir == "up" and infacing == "down") or (dir == "down" and infacing == "up")) then
if dir == "right" then
x = x - 1
rangex = rangex - 1
elseif dir == "left" then
x = x + 1
rangex = rangex + 1
elseif dir == "up" then
y = y + 1
rangey = rangey + 1
elseif dir == "down" then
y = y - 1
rangey = rangey - 1
end
table.insert(self.lasertable, dir)
table.insert(self.lasertable, x-rangex)
table.insert(self.lasertable, y-rangey)
table.insert(self.lasertable, rangex)
table.insert(self.lasertable, rangey)
x, y = portalx, portaly
dir = portalfacing
rangex, rangey = 0, 0
if dir == "right" then
x = portalx + 1
elseif dir == "left" then
x = portalx - 1
rangex = 0
elseif dir == "up" then
y = portaly - 1
elseif dir == "down" then
y = portaly + 1
end
end
--doors
for i, v in pairs(objects["door"]) do
if v.active then
if v.dir == "ver" then
if x == v.cox and (y == v.coy or y == v.coy-1) then
quit = true
end
elseif v.dir == "hor" then
if y == v.coy and (x == v.cox or x == v.cox+1) then
quit = true
end
end
end
end
end
if dir == "right" then
x = x - 1
rangex = rangex - 1
elseif dir == "left" then
x = x + 1
rangex = rangex + 1
elseif dir == "up" then
y = y + 1
rangey = rangey + 1
elseif dir == "down" then
y = y - 1
rangey = rangey - 1
end
table.insert(self.lasertable, dir)
table.insert(self.lasertable, x-rangex)
table.insert(self.lasertable, y-rangey)
table.insert(self.lasertable, rangex)
table.insert(self.lasertable, rangey)
self:update()
end