forked from alesan99/mari0_ae
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbox.lua
519 lines (469 loc) · 12 KB
/
box.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
box = class:new()
function box:init(x, y, t)
--PHYSICS STUFF
self.cox = x
self.coy = y
self.x = x-14/16
self.y = y-12/16
self.speedy = 0
self.speedx = 0
self.width = 12/16
self.height = 12/16
self.static = false
self.active = true
self.category = 9
self.parent = nil
self.portaloverride = true
self.mask = { true,
false, false, false, false, false,
false, false, false, true, true,
false, false, true, false, false,
true, true, false, false, true,
false, true, true, false, false,
true, false, true, true, true,
false, true}
self.emancipatecheck = true
self.userect = adduserect(self.x, self.y, 12/16, 12/16, self)
self.t = t or "box1"
--IMAGE STUFF
self.drawable = true
self.graphic = boximage
if self.t == "box2" then
self.boxframe = 2
elseif self.t == "edgeless" then
self.boxframe = 3
else
self.boxframe = 1
end
self.quad = boxquad[spriteset][self.boxframe][1]
self.overlaygraphic = false
self.overlayquad = false
self.offsetX = 6
self.offsetY = 2
self.quadcenterX = 6
self.quadcenterY = 6
self.rotation = 0 --for portals
self.rotationspeed = 0
self.falling = false
self.destroying = false
self.outtable = {}
self.portaledframe = false
self.gel = false
self.pipespawnmax = 1
self.gravitydir = "down"
self.doesntchangeplayergravity = true
end
function box:update(dt)
if edgewrapping then --wrap around screen
local minx, maxx = -self.width, mapwidth
if self.x < minx then
self.x = maxx
elseif self.x > maxx then
self.x = minx
end
end
if self.insidetile then
self.insidetile = self.insidetile - 1
if self.insidetile <= 0 then
self.insidetile = false
end
end
local friction = boxfrictionair
if self.falling == false and self.t ~= "edgeless" then
friction = boxfriction
end
if (inmap(math.floor(self.x+1+self.width), math.floor(self.y+self.height+20/16)) or inmap(math.floor(self.x+1), math.floor(self.y+self.height+20/16))) then
local t, t2
local cox, coy = math.floor(self.x+1), math.floor(self.y+self.height+20/16)
local cox2 = math.floor(self.x+1+self.width)
if inmap(cox, coy) then
t = map[cox][coy]
end
if inmap(cox2, coy) then
t2 = map[cox2][coy]
end
if (t and ((t[2] and entityquads[t[2]] and entityquads[t[2]].t == "ice") or (t[1] and tilequads[t[1]] and tilequads[t[1]]:getproperty("ice", cox, coy)))) or
(t2 and ((t2[2] and entityquads[t2[2]] and entityquads[t2[2]].t == "ice") or (t2[1] and tilequads[t2[1]] and tilequads[t2[1]]:getproperty("ice", cox, coy)))) then
friction = icefriction
end
end
if not self.pushed then
if self.gravitydir == "right" or self.gravity == "left" then
if self.speedy > 0 then
self.speedy = self.speedy - friction*dt
if self.speedy < 0 then
self.speedy = 0
end
else
self.speedy = self.speedy + friction*dt
if self.speedy > 0 then
self.speedy = 0
end
end
else
if self.speedx > 0 then
self.speedx = self.speedx - friction*dt
if self.speedx < 0 then
self.speedx = 0
end
else
self.speedx = self.speedx + friction*dt
if self.speedx > 0 then
self.speedx = 0
end
end
end
else
self.pushed = false
end
--rotate back to 0 (portals)
if self.gel and (not self.parent) and self.rotationspeed ~= 0 then
self.rotation = (self.rotation + (self.rotationspeed)*dt)%(math.pi*2)
while self.rotation < 0 do
self.rotation = self.rotation + math.pi*2
end
elseif self.t ~= "edgeless" then
self.rotation = math.fmod(self.rotation, math.pi*2)
if self.rotation > 0 then
self.rotation = self.rotation - portalrotationalignmentspeed*dt
if self.rotation < 0 then
self.rotation = 0
end
elseif self.rotation < 0 then
self.rotation = self.rotation + portalrotationalignmentspeed*dt
if self.rotation > 0 then
self.rotation = 0
end
end
else
self.rotation = self.rotation + (self.speedx*math.pi)*dt
end
if self.parent then
local oldx = self.x
local oldy = self.y
self.x = self.parent.x+math.sin(-self.parent.pointingangle)*0.3
self.y = self.parent.y-math.cos(-self.parent.pointingangle)*0.3
if self.portaledframe == false then
for h, u in pairs(emancipationgrills) do
if u.active then
if u.dir == "hor" then
if inrange(self.x+6/16, u.startx-1, u.endx, true) and inrange(u.y-14/16, oldy, self.y, true) then
self:emancipate(h)
end
else
if inrange(self.y+6/16, u.starty-1, u.endy, true) and inrange(u.x-14/16, oldx, self.x, true) then
self:emancipate(h)
end
end
end
end
end
end
self.userect.x = self.x
self.userect.y = self.y
if self.funnel then
self.gravity = yacceleration
self.funnel = false
end
--check if offscreen
if self.y > mapheight+2 then
self:destroy()
end
self.portaledframe = false
if self.destroying then
return true
else
return false
end
end
function box:leftcollide(a, b)
if self:globalcollide(a, b) then
return false
end
if self.falling and self.gravitydir == "left" then
self.falling = false
end
if self.gel == 1 then
self:bluegel("right")
end
if a == "button" then
self.y = b.y - self.height
self.x = b.x + b.width - 0.01
if self.speedy > 0 then
self.speedy = 0
end
return false
elseif a == "player" then
self.pushed = true
return false
elseif a == "tile" then
if inmap(b.cox, b.coy) and map[b.cox][b.coy]["gels"] and
map[b.cox][b.coy]["gels"]["right"] then
if map[b.cox][b.coy]["gels"]["right"] == 1 then
self.speedx = math.max(0, -self.speedx)
self.speedy = math.min(self.speedy, -horbouncespeedy)
elseif map[b.cox][b.coy]["gels"]["right"] == 4 then
self:purplegel("left")
end
end
end
end
function box:rightcollide(a, b)
if self:globalcollide(a, b) then
return false
end
if self.falling and self.gravitydir == "right" then
self.falling = false
end
if self.gel == 1 then
self:bluegel("left")
end
if a == "button" then
self.y = b.y - self.height
self.x = b.x - self.width+0.01
if self.speedy > 0 then
self.speedy = 0
end
return false
elseif a == "player" then
self.pushed = true
return false
elseif a == "tile" then
if inmap(b.cox, b.coy) and map[b.cox][b.coy]["gels"] and
map[b.cox][b.coy]["gels"]["left"] then
if map[b.cox][b.coy]["gels"]["left"] == 1 then
self.speedx = math.min(0, -self.speedx)
self.speedy = math.min(self.speedy, -horbouncespeedy)
elseif map[b.cox][b.coy]["gels"]["left"] == 4 then
self:purplegel("right")
end
end
end
end
function box:floorcollide(a, b)
if self:globalcollide(a, b) then
return false
end
if self.falling and self.gravitydir == "down" then
self.falling = false
end
if a == "goomba" or a == "bulletbill" then
b:stomp("box")
addpoints(200, self.x, self.y)
playsound(stompsound)
self.falling = true
return false
end
if self.gel == 1 then
self:bluegel("top")
elseif a == "tile" then
if inmap(b.cox, b.coy) and map[b.cox][b.coy]["gels"] and
map[b.cox][b.coy]["gels"]["top"] then
if map[b.cox][b.coy]["gels"]["top"] == 1 then
self.speedy = math.min(0, -self.speedy)
elseif map[b.cox][b.coy]["gels"]["top"] == 4 then
self:purplegel("down")
end
end
end
if b.SLOPE and (self.t == "edgeless" or self.gel == 2) then
if b.dir == "left" then
self.speedx = self.speedx - 32*b.incline*gdt
else
self.speedx = self.speedx + 32*b.incline*gdt
end
end
if a == "enemy" and b.killedbyboxes then
if b.stompable then
b:stomp()
else
b:shotted("right", false, false, false)
end
addpoints(200, self.x, self.y)
playsound("stomp")
self.falling = true
self.speedy = -10
return false
end
if a == "player" then
if self.gravitydir == "right" or self.gravity == "left" then
self.pushed = true
return false
end
end
end
function box:ceilcollide(a, b)
if self:globalcollide(a, b) then
return false
end
if self.falling and self.gravitydir == "up" then
self.falling = false
end
if a == "player" then
if self.gravitydir == "right" or self.gravity == "left" then
self.pushed = true
return false
end
elseif a == "tile" then
if inmap(b.cox, b.coy) and map[b.cox][b.coy]["gels"] and
map[b.cox][b.coy]["gels"]["bottom"] then
if map[b.cox][b.coy]["gels"]["bottom"] == 4 then
self:purplegel("top")
end
end
end
end
function box:passivecollide(a, b)
if self.gel == 1 then
self:bluegel("top")
end
if a == "player" then
if self.gravitydir == "right" or self.gravity == "left" then
if self.y+self.height > b.y+b.height then
self.y = b.y+b.height
else
self.y = b.y-self.height
end
else
if self.x+self.width > b.x+b.width then
self.x = b.x+b.width
else
self.x = b.x-self.width
end
end
end
if a == "tile" then
--boxes usually clip walls horizontally at around 85-99 speedx
--if (not self.insidetile) then
local threshold = 88
if self.speedx > threshold then
self.x = b.x-self.width
self.speedx = 0
elseif self.speedx < -threshold then
self.x = b.x+b.width
self.speedx = 0
end
--end
--self.insidetile = 2
end
end
function box:globalcollide(a, b)
if a == "gel" then
--cover in gel
self.rotationspeed = 0
if b.id == 5 then
self.gel = false
self.overlaygraphic = false
self.overlayquad = false
else
self.gel = b.id
self.overlaygraphic = boxgelimg
self.overlayquad = boxgelquad[spriteset][1][self.gel]
if self.t == "edgeless" then
self.overlayquad = boxgelquad[spriteset][2][self.gel]
end
end
return true
end
end
function box:bluegel(dir)
if dir == "top" then
--if self.speedy > gdt*yacceleration*10 then
self.speedy = math.min(-20, -self.speedy)
self.falling = true
self.speedx = math.random(-10,10)
self.rotationspeed = self.speedx
return true
--end
elseif dir == "left" then
if self.speedx > horbounceminspeedx then
self.speedx = math.min(-horbouncemaxspeedx, -self.speedx*horbouncemul)
self.speedy = math.min(self.speedy, -horbouncespeedy)
return true
end
elseif dir == "right" then
if self.speedx < -horbounceminspeedx then
self.speedx = math.min(horbouncemaxspeedx, -self.speedx*horbouncemul)
self.speedy = math.min(self.speedy, -horbouncespeedy)
return false
end
end
end
function box:purplegel(dir)
local oldgravitydir = self.gravitydir
if dir == "top" then
self.gravitydir = "up"
elseif dir == "bottom" then
self.gravitydir = "down"
elseif dir == "left" then
if self.gravitydir ~= "left" then
self.speedy = self.speedy/2
end
self.gravitydir = "left"
elseif dir == "right" then
if self.gravitydir ~= "right" then
self.speedy = self.speedy/2
end
self.gravitydir = "right"
end
self.falling = false
end
function box:startfall()
self.falling = true
end
function box:emancipate()
if self.destroying then
return false
end
local speedx, speedy = self.speedx, self.speedy
if self.parent then
self.parent:cubeemancipate()
speedx = speedx + self.parent.speedx
speedy = speedy + self.parent.speedy
end
self.drawable = false
table.insert(emancipateanimations, emancipateanimation:new(self.x, self.y, self.width, self.height, self.graphic, self.quad, speedx, speedy, self.rotation, self.offsetX, self.offsetY, self.quadcenterX, self.quadcenterY))
self:destroy()
end
function box:destroy()
self.userect.delete = true
self.destroying = true
for i = 1, #self.outtable do
if self.outtable[i][1].input then
self.outtable[i][1]:input("toggle", self.outtable[i][2])
end
end
end
function box:addoutput(a, t)
table.insert(self.outtable, {a, t})
end
function box:used(id)
self.parent = objects["player"][id]
self.active = false
self.speedx = 0
self.speedy = 0
objects["player"][id]:pickupbox(self)
end
function box:dropped(gravitydir)
local gravitydirection = gravitydir
self.parent = nil
self.active = true
if gravitydirection then
self.gravitydir = gravitydirection
if self.gravitydir == "left" or self.gravitydir == "right" then
self.speedy = 0
else
self.speedx = 0
end
end
end
function box:portaled()
self.portaledframe = true
end
function box:onbutton(s)
if s then
self.quad = boxquad[spriteset][self.boxframe][2]
else
self.quad = boxquad[spriteset][self.boxframe][1]
end
end