forked from Mari0-CE/Mari0-Community-Edition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
physics.lua
1402 lines (1208 loc) · 40.3 KB
/
physics.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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[[
PHYSICS LIBRARY THING
WRITTEN BY MAURICE GUÉGAN FOR MARI0
DON'T STEAL MY SHIT
Licensed under WTFPL
]]--
--MASK REFERENCE LIST
---1: *ALWAYS NOT COLLIDE*
---2: WORLD
---3: MARIO
---4: GOOMBA
---5: KOOPA
---6: MUSHROOM/ONEUP/FLOWER/STAR
---7: GEL DISPENSER
---8: GEL
---9: BOX
--10: SCREENBOUNDARIES
--11: BULLETBILL
--12: PORTALWALLS
--13: FIREBALLS
--14: HAMMERS
--15: PLATFORMS/SEESAWS
--16: BOWSER
--17: FIRE
--18: VINE
--19: SPRING
--20: HAMMERBROS
--21: LAKITO
--22: BUTTON --Not used anymore
--23: CASTLEFIRE
--24: CHEEP
--25: DOOR
--26: FAITHPLATE
--27: FLYINGFISH
--28: LIGHTBRIDGE
--29: PLANT
--30: SQUID
--31: UPFIRE
function prerotatecall(a, b)
local prerotatecalls = {"goomba", "koopa", "hammerbros", "lakito", "cheep", "flyingfish", "squid"}
if tablecontains(prerotatecalls, a) then
return true
end
return false
end
function physicsupdate(dt)
local lobjects = objects
for j, w in pairs(lobjects) do
if j ~= "tile" then
for i, v in pairs(w) do
if v.static == false and v.active then
--GRAVITY
v.speedy = v.speedy + (v.gravity or yacceleration)*dt*0.5
if v.speedy > maxyspeed then
v.speedy = maxyspeed
end
--Standard conversion!
if v.gravitydirection and v.gravitydirection ~= math.pi/2 then
v.speedx, v.speedy = convertfromstandard(v, v.speedx, v.speedy)
end
--PORTALS LOL
local passed = false
if v.portalable ~= false then
if not checkportalVER(v, v.x+v.speedx*dt) then
if checkportalHOR(v, v.y+v.speedy*dt) then
passed = true
end
else
passed = true
end
if passed and j == "player" then
playsound("portalenter")
end
end
--COLLISIONS ROFL
local horcollision = false
local vercollision = false
local latetable = {"portalwall", "castlefirefire", "platform"}
--VS OTHER OBJECTS PRE TILE GROUP
for h, u in pairs(lobjects) do
if h ~= "tile" then
local pass = true
for k, l in pairs(latetable) do
if h == l then
pass = false
break
end
end
if pass then
local hor, ver = handlegroup(i, h, u, v, j, dt, passed)
if hor then
horcollision = true
end
if ver then
vercollision = true
end
--Check for emancipation grill
checkforemances(dt, v)
end
end
end
--VS TILES (Because I only wanna check close ones)
local xstart = math.floor(v.x+v.speedx*dt-2/16)+1
local ystart = math.floor(v.y+v.speedy*dt-2/16)+1
local xfrom = xstart
local xto = xstart+math.ceil(v.width)
local dir = 1
if v.speedx < 0 then
xfrom, xto = xto, xfrom
dir = -1
end
for x = xfrom, xto, dir do
for y = ystart, ystart+math.ceil(v.height) do
--check if invisible block
if inmap(x, y) and (not tilequads[map[x][y][1]]:getproperty("invisible", x, y) or j == "player") then
local t = lobjects["tile"][x .. "-" .. y]
if t then
-- Same object Active Not masked
if (i ~= g or j ~= h) and t.active and v.mask[t.category] ~= true then
local collision1, collision2 = checkcollision(v, t, "tile", x .. "-" .. y, j, i, dt, passed)
if collision1 then
horcollision = true
elseif collision2 then
vercollision = true
end
end
end
end
end
end
--VS: LATE OBJECTS
for g, h in pairs(latetable) do
local u = objects[h]
local hor, ver = handlegroup(i, h, u, v, j, dt, passed)
if hor then
horcollision = true
end
if ver then
vercollision = true
end
--Check for emancipation grill
checkforemances(dt, v)
end
--Move the object
if vercollision == false then
v.y = v.y + v.speedy*dt
end
if horcollision == false then
v.x = v.x + v.speedx*dt
end
if v.gravitydirection and v.gravitydirection ~= math.pi/2 then
v.speedx, v.speedy = converttostandard(v, v.speedx, v.speedy)
end
if vercollision == false then
if v.previouslyonground and v.startfall and not v.jumping then
v.previouslyonground = false
if v.speedy >= 0 then
v:startfall(i)
end
end
elseif not v.previouslyonground and v.startfall then
v.previouslyonground = true
end
--check if object is inside portal
if v.portalable ~= false then
inportal(v)
end
--GRAVITY
v.speedy = v.speedy + (v.gravity or yacceleration)*dt*0.5
end
end
end
end
end
function checkforemances(dt, v)
if v.emancipatecheck then
for h, u in pairs(emancipationgrills) do
if u.active then
if u.dir == "hor" then
if inrange(v.x+6/16, u.startx-1, u.endx, true) and inrange(u.y-14/16, v.y, v.y+v.speedy*dt, true) then
if v.emancipate then v:emancipate(h) end
end
else
if inrange(v.y+6/16, u.starty-1, u.endy, true) and inrange(u.x-14/16, v.x, v.x+v.speedx*dt, true) then
if v.emancipate then v:emancipate(h) end
end
end
end
end
end
end
function handlegroup(i, h, u, v, j, dt, passed)
local horcollision = false
local vercollision = false
for g, t in pairs(u) do
-- Same object? Active Not masked
if (i ~= g or j ~= h) and t.active and (v.mask == nil or v.mask[t.category] ~= true) and (t.mask == nil or t.mask[v.category] ~= true) then
local collision1, collision2 = checkcollision(v, t, h, g, j, i, dt, passed)
if collision1 then
horcollision = true
elseif collision2 then
vercollision = true
end
end
end
return horcollision, vercollision
end
function checkcollision(v, t, h, g, j, i, dt, passed) --v: b1table | t: b2table | h: b2type | g: b2id | j: b1type | i: b1id
local hadhorcollision = false
local hadvercollision = false
if h ~= "tile" or (not tilequads[map[t.cox][t.coy][1]]:getproperty("slantupleft", t.cox, t.coy) and not tilequads[map[t.cox][t.coy][1]]:getproperty("slantupright", t.cox, t.coy)) then
if math.abs(v.x+v.speedx*dt-t.x) < math.max(v.width, t.width)+1 and math.abs(v.y+v.speedy*dt-t.y) < math.max(v.height, t.height)+1 then
--check if it's a passive collision (Object is colliding anyway)
if not passed and aabb(v.x, v.y, v.width, v.height, t.x, t.y, t.width, t.height) then --passive collision! (oh noes!)
if passivecollision(v, t, h, g, j, i, dt) then
hadvercollision = true
end
elseif aabb(v.x + v.speedx*dt, v.y + v.speedy*dt, v.width, v.height, t.x, t.y, t.width, t.height) then
if aabb(v.x + v.speedx*dt, v.y, v.width, v.height, t.x, t.y, t.width, t.height) then --Collision is horizontal!
if horcollision(v, t, h, g, j, i, dt) then
hadhorcollision = true
end
elseif aabb(v.x, v.y+v.speedy*dt, v.width, v.height, t.x, t.y, t.width, t.height) then --Collision is vertical!
if vercollision(v, t, h, g, j, i, dt) then
hadvercollision = true
end
else
--We're fucked, it's a diagonal collision! run!
--Okay actually let's take this slow okay. Let's just see if we're moving faster horizontally than vertically, aight?
local grav = yacceleration
if self and self.gravity then
grav = self.gravity
end
if math.abs(v.speedy-grav*dt) < math.abs(v.speedx) then
--vertical collision it is.
if vercollision(v, t, h, g, j, i, dt) then
hadvercollision = true
end
else
--okay so we're moving mainly vertically, so let's just pretend it was a horizontal collision? aight cool.
if horcollision(v, t, h, g, j, i, dt) then
hadhorcollision = true
end
end
end
end
end
else
if math.abs(v.x-t.x) < math.max(v.width, t.width)+1 and math.abs(v.y-t.y) < math.max(v.height, t.height)+1 then
--check if it's a passive collision (Object is colliding anyway)
local slant = "ul"
if tilequads[map[t.cox][t.coy][1]]:getproperty("slantupright", t.cox, t.coy) then
slant = "ur"
end
if not passed and aabt(v.x, v.y, v.width, v.height, t.x, t.y, t.width, t.height, slant) then --passive collision! (oh noes!)
if trianglepassivecollision(v, t, h, g, j, i, dt) then
hadvercollision = true
end
elseif aabt(v.x + v.speedx*dt, v.y + v.speedy*dt, v.width, v.height, t.x, t.y, t.width, t.height, slant) then
if trianglevercollision(v, t, h, g, j, i, dt) then
hadvercollision = true
end
end
end
end
return hadhorcollision, hadvercollision
end
function trianglevercollision(v, t, h, g, j, i, dt)
if v.floorcollide then
if v:floorcollide(j, v, g, i) ~= false then
if v.speedy then
v.speedy = 0
end
if t.slant == "ur" then
v.y = t.y-v.height + math.max(0, math.min(1, (v.x+v.speedx*dt)-(t.x)))
elseif t.slant == "ul" then
v.y = t.y-v.height + math.max(0, math.min(1, (t.x+t.width)-((v.x+v.speedx*dt)+v.width)))
end
return true
end
else
if v.speedy then
v.speedy = 0
end
if t.slant == "ur" then
v.y = t.y-v.height+(t.x-v.x)+2/16
elseif t.slant == "ul" then
v.y = t.y-v.height + math.max(0, math.min(1, (t.x+t.width)-((v.x+v.speedx*dt)+v.width)))
end
return true
end
end
function trianglepassivecollision(v, t, h, g, j, i, dt)
trianglevercollision(v, t, h, g, j, i, dt)
end
function passivecollision(v, t, h, g, j, i, dt)
if v.passivecollide then
v:passivecollide(h, t, i, g)
if t.passivecollide then
t:passivecollide(j, v, i, g)
end
else
if v.floorcollide then
if v:floorcollide(h, t, i, g) ~= false then
if v.speedy > 0 then
v.speedy = 0
end
v.y = t.y - v.height
return true
end
else
if v.speedy > 0 then
v.speedy = 0
end
v.y = t.y - v.height
return true
end
end
return false
end
function horcollision(v, t, h, g, j, i, dt)
if v.speedx < 0 then
--move object RIGHT (because it was moving left)
if collisionexists("right", t) then
if callcollision("right", t, j, v, g, i) ~= false then
if t.speedx and t.speedx > 0 then
t.speedx = 0
end
end
else
if t.speedx and t.speedx > 0 then
t.speedx = 0
end
end
if collisionexists("left", v) then
if callcollision("left", v, h, t, i, g) ~= false then
if v.speedx < 0 then
v.speedx = 0
end
v.x = t.x + t.width
return true
end
else
if v.speedx < 0 then
v.speedx = 0
end
v.x = t.x + t.width
return true
end
else
--move object LEFT (because it was moving right)
if collisionexists("left", t) then
if callcollision("left", t, j, v, g, i) ~= false then
if t.speedx and t.speedx < 0 then
t.speedx = 0
end
end
else
if t.speedx and t.speedx < 0 then
t.speedx = 0
end
end
if collisionexists("right", v) then
if callcollision("right", v, h, t, i, g) ~= false then
if v.speedx > 0 then
v.speedx = 0
end
v.x = t.x - v.width
return true
end
else
if v.speedx > 0 then
v.speedx = 0
end
v.x = t.x - v.width
return true
end
end
return false
end
function vercollision(v, t, h, g, j, i, dt)
if v.speedy < 0 then
--move object DOWN (because it was moving up)
if collisionexists("floor", t) then
if callcollision("floor", t, j, v, g, i) ~= false then
if t.speedy and t.speedy > 0 then
t.speedy = 0
end
end
else
if t.speedy and t.speedy > 0 then
t.speedy = 0
end
end
if collisionexists("ceil", v) then
if callcollision("ceil", v, h, t, i, g) ~= false then
if v.speedy < 0 then
v.speedy = 0
end
v.y = t.y + t.height
return true
end
else
if v.speedy < 0 then
v.speedy = 0
end
v.y = t.y + t.height
return true
end
else
if collisionexists("ceil", t) then
if callcollision("ceil", t, j, v, g, i) ~= false then
if t.speedy and t.speedy < 0 then
t.speedy = 0
end
end
else
if t.speedy and t.speedy < 0 then
t.speedy = 0
end
end
if collisionexists("floor", v) then
if callcollision("floor", v, h, t, i, g) ~= false then
if v.speedy > 0 then
v.speedy = 0
end
v.y = t.y - v.height
return true
end
else
if v.speedy > 0 then
v.speedy = 0
end
v.y = t.y - v.height
return true
end
end
return false
end
function collisionscalls(dir, obj, a, b, c, d)
local r
if obj.gravitydirection > math.pi/4*1 and obj.gravitydirection <= math.pi/4*3 then
if dir == "floor" then
if obj.floorcollide then
r = obj:floorcollide(a, b, c, d)
end
elseif dir == "left" then
if obj.leftcollide then
r = obj:leftcollide(a, b, c, d)
end
elseif dir == "ceil" then
if obj.ceilcollide then
r = obj:ceilcollide(a, b, c, d)
end
elseif dir == "right" then
if obj.rightcollide then
r = obj:rightcollide(a, b, c, d)
end
end
elseif obj.gravitydirection > math.pi/4*3 and obj.gravitydirection <= math.pi/4*5 then
if dir == "floor" then
if obj.rightcollide then
r = obj:rightcollide(a, b, c, d)
end
elseif dir == "left" then
if obj.floorcollide then
r = obj:floorcollide(a, b, c, d)
end
elseif dir == "ceil" then
if obj.leftcollide then
r = obj:leftcollide(a, b, c, d)
end
elseif dir == "right" then
if obj.ceilcollide then
r = obj:ceilcollide(a, b, c, d)
end
end
elseif obj.gravitydirection > math.pi/4*5 and obj.gravitydirection <= math.pi/4*7 then
if dir == "floor" then
if obj.ceilcollide then
r = obj:ceilcollide(a, b, c, d)
end
elseif dir == "left" then
if obj.rightcollide then
r = obj:rightcollide(a, b, c, d)
end
elseif dir == "ceil" then
if obj.floorcollide then
r = obj:floorcollide(a, b, c, d)
end
elseif dir == "right" then
if obj.leftcollide then
r = obj:leftcollide(a, b, c, d)
end
end
else
if dir == "floor" then
if obj.leftcollide then
r = obj:leftcollide(a, b, c, d)
end
elseif dir == "left" then
if obj.ceilcollide then
r = obj:ceilcollide(a, b, c, d)
end
elseif dir == "ceil" then
if obj.rightcollide then
r = obj:rightcollide(a, b, c, d)
end
elseif dir == "right" then
if obj.floorcollide then
r = obj:floorcollide(a, b, c, d)
end
end
end
return r
end
function callcollision(dir, obj, a, b, c, d)
if not obj.gravitydirection then
if dir == "floor" then
return obj:floorcollide(a, b, c, d)
elseif dir == "left" then
return obj:leftcollide(a, b, c, d)
elseif dir == "ceil" then
return obj:ceilcollide(a, b, c, d)
elseif dir == "right" then
return obj:rightcollide(a, b, c, d)
end
end
obj.speedx, obj.speedy = converttostandard(obj, obj.speedx, obj.speedy)
local r
--PRE ROTATION CALLS!!! WOAAAH
if prerotatecall(a, b) then
r = collisionscalls(dir, obj, a, b, c, d)
end
if a == "tile" then
local purplegel = false
local x, y = b.cox, b.coy
--Find a more suitable block if available
if dir == "floor" or dir == "ceil" then
if inmap(x+1, y) and tilequads[map[x+1][y][1]]:getproperty("collision", x+1, y) and obj.x+obj.width/2+.5 > x + .5 then
x = x + 1
elseif inmap(x-1, y) and tilequads[map[x-1][y][1]]:getproperty("collision", x-1, y) and obj.x+obj.width/2+.5 < x - .5 then
x = x - 1
end
end
if dir == "left" or dir == "right" then
if inmap(x, y+1) and tilequads[map[x][y+1][1]]:getproperty("collision", x, y+1) and obj.y+obj.height/2+.5 > y + .5 then
y = y + 1
elseif inmap(x, y-1) and tilequads[map[x][y-1][1]]:getproperty("collision", x, y-1) and obj.y+obj.height/2+.5 < y - .5 then
y = y - 1
end
end
if not obj.runanimationprogress or obj.size == 1 then --cheap check for player
if dir == "left" and map[x][y]["gels"]["right"] == 4 then
if obj.gravitydirection == 0 then
obj.speedx = -obj.speedx
end
obj.gravitydirection = math.pi
purplegel = true
elseif dir == "floor" and map[x][y]["gels"]["top"] == 4 then
if obj.gravitydirection == math.pi*1.5 then
obj.speedx = -obj.speedx
end
obj.gravitydirection = math.pi/2
purplegel = true
allowskip = false
elseif dir == "right" and map[x][y]["gels"]["left"] == 4 then
if obj.gravitydirection == math.pi then
obj.speedx = -obj.speedx
end
obj.gravitydirection = 0
purplegel = true
elseif dir == "ceil" and map[x][y]["gels"]["bottom"] == 4 then
if obj.gravitydirection == math.pi/2 then
obj.speedx = -obj.speedx
end
obj.gravitydirection = math.pi*1.5
purplegel = true
end
end
if purplegel then
obj.speedy = 0
elseif obj.gravitydirection ~= math.pi/2 then
--check if wall is a side. and stuff.
local resetgravity = false
if obj.gravitydirection > math.pi/4*1 and obj.gravitydirection <= math.pi/4*3 then --down
if dir == "ceil" or dir == "floor" then
resetgravity = true
end
elseif obj.gravitydirection > math.pi/4*3 and obj.gravitydirection <= math.pi/4*5 then --left
if dir == "right" or dir == "left" then
resetgravity = true
end
elseif obj.gravitydirection > math.pi/4*5 and obj.gravitydirection <= math.pi/4*7 then --up
if dir == "floor" or dir == "ceil" then
resetgravity = true
end
else --right
if dir == "left" or dir == "right" then
resetgravity = true
end
end
if resetgravity then
obj.gravitydirection = math.pi/2
obj.speedy = 0
obj.speedx = -obj.speedx
end
end
elseif obj.gravitydirection ~= math.pi/2 and (not b.gravitydirection or b.gravitydirection ~= obj.gravitydirection) then
--Player vs box hardcode fix
if (a == "box" and obj.category == 3) or (a == "player" and obj.category == 9) then
else
obj.gravitydirection = math.pi/2
obj.speedy = 0
obj.speedx = -obj.speedx
end
end
--AFTER ROTATION CALLS!!! WOAAAH
if not prerotatecall(a, b) then
r = collisionscalls(dir, obj, a, b, c, d)
end
obj.speedx, obj.speedy = convertfromstandard(obj, obj.speedx, obj.speedy)
return r
end
function collisionexists(dir, obj)
if not obj.gravitydirection or (obj.gravitydirection > math.pi/4*1 and obj.gravitydirection <= math.pi/4*3) then
if dir == "floor" then
return obj.floorcollide
elseif dir == "left" then
return obj.leftcollide
elseif dir == "ceil" then
return obj.ceilcollide
elseif dir == "right" then
return obj.rightcollide
end
elseif obj.gravitydirection > math.pi/4*3 and obj.gravitydirection <= math.pi/4*5 then
if dir == "floor" then
return obj.rightcollide
elseif dir == "left" then
return obj.floorcollide
elseif dir == "ceil" then
return obj.leftcollide
elseif dir == "right" then
return obj.ceilcollide
end
elseif obj.gravitydirection > math.pi/4*5 and obj.gravitydirection <= math.pi/4*7 then
if dir == "floor" then
return obj.ceilcollide
elseif dir == "left" then
return obj.rightcollide
elseif dir == "ceil" then
return obj.floorcollide
elseif dir == "right" then
return obj.leftcollide
end
else
if dir == "floor" then
return obj.leftcollide
elseif dir == "left" then
return obj.ceilcollide
elseif dir == "ceil" then
return obj.rightcollide
elseif dir == "right" then
return obj.floorcollide
end
end
end
function adjustcollside(side, gravitydirection)
if side == "left" then
if gravitydirection > math.pi/4*1 and gravitydirection <= math.pi/4*3 then --down
return "left"
elseif gravitydirection > math.pi/4*3 and gravitydirection <= math.pi/4*5 then --left
return "up"
elseif gravitydirection > math.pi/4*5 and gravitydirection <= math.pi/4*7 then --up
return "right"
else --right
return "down"
end
elseif side == "up" then
if gravitydirection > math.pi/4*1 and gravitydirection <= math.pi/4*3 then --down
return "up"
elseif gravitydirection > math.pi/4*3 and gravitydirection <= math.pi/4*5 then --left
return "right"
elseif gravitydirection > math.pi/4*5 and gravitydirection <= math.pi/4*7 then --up
return "down"
else --right
return "left"
end
elseif side == "right" then
if gravitydirection > math.pi/4*1 and gravitydirection <= math.pi/4*3 then --down
return "right"
elseif gravitydirection > math.pi/4*3 and gravitydirection <= math.pi/4*5 then --left
return "down"
elseif gravitydirection > math.pi/4*5 and gravitydirection <= math.pi/4*7 then --up
return "left"
else --right
return "up"
end
elseif side == "down" then
if gravitydirection > math.pi/4*1 and gravitydirection <= math.pi/4*3 then --down
return "down"
elseif gravitydirection > math.pi/4*3 and gravitydirection <= math.pi/4*5 then --left
return "left"
elseif gravitydirection > math.pi/4*5 and gravitydirection <= math.pi/4*7 then --up
return "up"
else --right
return "right"
end
end
end
function aabb(ax, ay, awidth, aheight, bx, by, bwidth, bheight)
return ax+awidth > bx and ax < bx+bwidth and ay+aheight > by and ay < by+bheight
end
function aabt(ax, ay, awidth, aheight, bx, by, bwidth, bheight, bdir)
if bdir == "ur" then
return ax+awidth > bx and ax < bx+bwidth and ay+aheight > by and ay < by+bheight and ay + aheight - by > ax - bx
elseif bdir == "ul" then
return ax+awidth > bx and ax < bx+bwidth and ay+aheight > by and ay < by+bheight and ay + aheight - by > bheight - ax - awidth + bx
elseif bdir == "dl" then
return ax+awidth > bx and ax < bx+bwidth and ay+aheight > by and ay < by+bheight and ay - by < ax+awidth - bx
elseif bdir == "dr" then
return ax+awidth > bx and ax < bx+bwidth and ay+aheight > by and ay < by+bheight and ay - by < bwidth- ax + bx
end
end
function checkrect(x, y, width, height, list, statics)
local out = {}
local inobj
if type(list) == "table" and list[1] == "exclude" then
inobj = {unpack(list)}
table.remove(inobj, 1)
list = "all"
end
for i, v in pairs(objects) do
local contains = false
if list and list ~= "all" then
for j = 1, #list do
if list[j] == i then
contains = true
end
end
end
if list == "all" or contains then
for j, w in pairs(v) do
if statics or w.static ~= true or list ~= "all" then
local skip = false
if inobj then
for i, v in pairs(inobj) do
if v == w then
skip = true
end
if (v.mask ~= nil and v.mask[w.category] == true) or (w.mask ~= nil and w.mask[v.category] == true) then
skip = true
end
end
end
if not skip then
if w.active then
if aabb(x, y, width, height, w.x, w.y, w.width, w.height) then
table.insert(out, i)
table.insert(out, j)
end
end
end
end
end
end
end
return out
end
function inportal(self)
if self.mask[2] then
return
end
for i, v in pairs(portals) do
if v.x1 ~= false and v.x2 ~= false then
local portal1xplus = 0
local portal2xplus = 0
local portal1Y = v.y1
local portal2Y = v.y2
local portal1yplus = 0
local portal2yplus = 0
local portal1X = v.x1
local portal2X = v.x2
--Get the extra block of each portal
if v.facing1 == "up" then
portal1xplus = 1
elseif v.facing1 == "down" then
portal1xplus = -1
end
if v.facing2 == "up" then
portal2xplus = 1
elseif v.facing2 == "down" then
portal2xplus = -1
end
if v.facing1 == "right" then
portal1yplus = 1
elseif v.facing1 == "left" then
portal1yplus = -1
end
if v.facing2 == "right" then
portal2yplus = 1
elseif v.facing2 == "left" then
portal2yplus = -1
end
local x = math.floor(self.x+self.width/2)+1
local y = math.floor(self.y+self.height/2)+1
if (x == portal1X or x == portal1X + portal1xplus) and (y == portal1Y or y == portal1Y + portal1yplus) then
local entryportalX = v.x1
local entryportalY = v.y1
local entryportalfacing = v.facing1
local exitportalX = v.x2
local exitportalY = v.y2
local exitportalfacing = v.facing2
self.x, self.y, self.speedx, self.speedy, self.rotation = portalcoords(self.x, self.y, self.speedx, self.speedy, self.width, self.height, self.rotation, self.animationdirection, entryportalX, entryportalY, entryportalfacing, exitportalX, exitportalY, exitportalfacing, self, true)
elseif (x == portal2X or x == portal2X + portal2xplus) and (y == portal2Y or y == portal2Y + portal2yplus) then
local entryportalX = v.x2
local entryportalY = v.y2
local entryportalfacing = v.facing2
local exitportalX = v.x1
local exitportalY = v.y1
local exitportalfacing = v.facing1
self.x, self.y, self.speedx, self.speedy, self.rotation = portalcoords(self.x, self.y, self.speedx, self.speedy, self.width, self.height, self.rotation, self.animationdirection, entryportalX, entryportalY, entryportalfacing, exitportalX, exitportalY, exitportalfacing, self)
end
end
end
return false
end
function checkportalHOR(self, nextY) --handles horizontal (up- and down facing) portal teleportation
for i, v in pairs(portals) do
if v.x1 ~= false and v.x2 ~= false then
local portal1xplus = 0
local portal2xplus = 0
local portal1Y = v.y1
local portal2Y = v.y2
--Get the extra block of each portal
if v.facing1 == "up" then
portal1xplus = 1
portal1Y = portal1Y - 1
elseif v.facing1 == "down" then
portal1xplus = -1
end
if v.facing2 == "up" then
portal2xplus = 1
portal2Y = portal2Y - 1
elseif v.facing2 == "down" then
portal2xplus = -1
end
--first part checks whether object is in the portal's x range, second part whether object just moved through the portal's Y value
if ((v.x1 == math.floor(self.x+1) or v.x1+portal1xplus == math.floor(self.x+1)) and inrange(portal1Y, self.y+self.height/2, nextY+self.height/2))
or ((v.x2 == math.floor(self.x+1) or v.x2+portal2xplus == math.floor(self.x+1)) and inrange(portal2Y, self.y+self.height/2, nextY+self.height/2)) then
--check which portal is entry
local entryportalX, entryportalY, entryportalfacing
local exitportalX, exitportalY, exitportalfacing
local entryportalxplus, entryportalyplus, exitportalxplus, exitportalyplus
if (v.x1 == math.floor(self.x+1) or v.x1+portal1xplus == math.floor(self.x+1)) and inrange(portal1Y, self.y+self.height/2, nextY+self.height/2) then
entryportalX = v.x1
entryportalY = v.y1
entryportalfacing = v.facing1
entryportalxplus = portal1xplus
exitportalX = v.x2
exitportalY = v.y2
exitportalfacing = v.facing2
exitportalxplus = portal2xplus
else
entryportalX = v.x2
entryportalY = v.y2
entryportalfacing = v.facing2
entryportalxplus = portal2xplus
exitportalX = v.x1
exitportalY = v.y1
exitportalfacing = v.facing1
exitportalxplus = portal1xplus
end
--check if movement makes that portal even a possibility
if entryportalfacing == "up" then
if self.speedy < 0 then
return false
end
elseif entryportalfacing == "down" then
if self.speedy > 0 then
return false
end
end
if entryportalfacing == "left" or entryportalfacing == "right" then
return false
end
local testx, testy, testspeedx, testspeedy, testrotation = portalcoords(self.x, self.y, self.speedx, self.speedy, self.width, self.height, self.rotation, self.animationdirection, entryportalX, entryportalY, entryportalfacing, exitportalX, exitportalY, exitportalfacing, self, true)
if #checkrect(testx, testy, self.width, self.height, {"exclude", self}, false) == 0 then --Check if exit position is free
self.x, self.y, self.speedx, self.speedy, self.rotation = testx, testy, testspeedx, testspeedy, testrotation