-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathgameA.lua
1210 lines (991 loc) · 36.3 KB
/
gameA.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
function gameA_load()
gamestate = "gameA"
pause = false
skipupdate = true
difficulty_speed = 100
cuttingtimer = lineclearduration
scorescore = 0
levelscore = 0
linesscore = 0
linescleared = 0
lastscoreadd = 0
scoreaddtimer = scoreaddtime
densityupdatetimer = 0
nextpiecerot = 0
newlevelbeep = false
--PHYSICS--
meter = 30
world = love.physics.newWorld(0, -720, 960, 1200, 0, 500, true )
tetrikind = {}
wallshapes = {}
tetrishapes = {}
tetribodies = {}
offsetshapes = {}
tetrishapescopy = {}
data = {}
wallbodies = love.physics.newBody(world, 32, -64, 0, 0) --WALLS
wallshapes[0] = love.physics.newPolygonShape( wallbodies,-8, -64, -8,672, 24,672, 24,-64)
wallshapes[0]:setData({"left"})
wallshapes[0]:setFriction(0.00001)
wallshapes[1] = love.physics.newPolygonShape( wallbodies,352,-64, 352,672, 384,672, 384,-64)
wallshapes[1]:setData({"right"})
wallshapes[1]:setFriction(0.00001)
wallshapes[2] = love.physics.newPolygonShape( wallbodies,24,640, 24,672, 352,672, 352,640)
wallshapes[2]:setData({"ground"})
wallshapes[3] = love.physics.newPolygonShape( wallbodies,-8,-96, 384,-96, 384,-64, -8,-64)
wallshapes[3]:setData({"ceiling"})
world:setCallbacks(collideA)
-----------
--FIRST "nextpiece"-
nextpiece = math.random(7)
checklinedensity(false)
game_addTetriA()
nextpiece = math.random(7)
----------------
end
function game_addTetriA() --creates new block (using createtetriA) at 1 and sets its velocity
--NEW BLOCK--
randomblock = nextpiece
createtetriA(randomblock, 1, 224, blockstartY)
tetribodies[1]:setLinearVelocity(0, difficulty_speed)
end
function createtetriA(i, uniqueid, x, y) --creates block, including body, shapes, image, imagedata and whatnot.
tetriimagedata[uniqueid] = newImageData( "graphics/pieces/"..i..".png", scale)
tetriimages[uniqueid] = padImagedata( tetriimagedata[uniqueid] )
tetrikind[uniqueid] = i
tetrishapes[uniqueid] = {}
if i == 1 then --I
tetribodies[uniqueid] = love.physics.newBody(world, x, y, 0, blockrot)
tetrishapes[uniqueid][1] = love.physics.newRectangleShape( tetribodies[uniqueid], -48,0, 32, 32)
tetrishapes[uniqueid][2] = love.physics.newRectangleShape( tetribodies[uniqueid], -16,0, 32, 32)
tetrishapes[uniqueid][3] = love.physics.newRectangleShape( tetribodies[uniqueid], 16,0, 32, 32)
tetrishapes[uniqueid][4] = love.physics.newRectangleShape( tetribodies[uniqueid], 48,0, 32, 32)
elseif i == 2 then --J
tetribodies[uniqueid] = love.physics.newBody(world, x, y, 0, blockrot)
tetrishapes[uniqueid][1] = love.physics.newRectangleShape( tetribodies[uniqueid], -32,-16, 32, 32)
tetrishapes[uniqueid][2] = love.physics.newRectangleShape( tetribodies[uniqueid], 0,-16, 32, 32)
tetrishapes[uniqueid][3] = love.physics.newRectangleShape( tetribodies[uniqueid], 32,-16, 32, 32)
tetrishapes[uniqueid][4] = love.physics.newRectangleShape( tetribodies[uniqueid], 32,16, 32, 32)
elseif i == 3 then --L
tetribodies[uniqueid] = love.physics.newBody(world, x, y, 0, blockrot)
tetrishapes[uniqueid][1] = love.physics.newRectangleShape( tetribodies[uniqueid], -32,-16, 32, 32)
tetrishapes[uniqueid][2] = love.physics.newRectangleShape( tetribodies[uniqueid], 0,-16, 32, 32)
tetrishapes[uniqueid][3] = love.physics.newRectangleShape( tetribodies[uniqueid], 32,-16, 32, 32)
tetrishapes[uniqueid][4] = love.physics.newRectangleShape( tetribodies[uniqueid], -32,16, 32, 32)
elseif i == 4 then --O
tetribodies[uniqueid] = love.physics.newBody(world, x, y, 0, blockrot)
tetrishapes[uniqueid][1] = love.physics.newRectangleShape( tetribodies[uniqueid], -16,-16, 32, 32)
tetrishapes[uniqueid][2] = love.physics.newRectangleShape( tetribodies[uniqueid], -16,16, 32, 32)
tetrishapes[uniqueid][3] = love.physics.newRectangleShape( tetribodies[uniqueid], 16,16, 32, 32)
tetrishapes[uniqueid][4] = love.physics.newRectangleShape( tetribodies[uniqueid], 16,-16, 32, 32)
elseif i == 5 then --S
tetribodies[uniqueid] = love.physics.newBody(world, x, y, 0, blockrot)
tetrishapes[uniqueid][1] = love.physics.newRectangleShape( tetribodies[uniqueid], -32,16, 32, 32)
tetrishapes[uniqueid][2] = love.physics.newRectangleShape( tetribodies[uniqueid], 0,-16, 32, 32)
tetrishapes[uniqueid][3] = love.physics.newRectangleShape( tetribodies[uniqueid], 32,-16, 32, 32)
tetrishapes[uniqueid][4] = love.physics.newRectangleShape( tetribodies[uniqueid], 0,16, 32, 32)
elseif i == 6 then --T
tetribodies[uniqueid] = love.physics.newBody(world, x, y, 0, blockrot)
tetrishapes[uniqueid][1] = love.physics.newRectangleShape( tetribodies[uniqueid], -32,-16, 32, 32)
tetrishapes[uniqueid][2] = love.physics.newRectangleShape( tetribodies[uniqueid], 0,-16, 32, 32)
tetrishapes[uniqueid][3] = love.physics.newRectangleShape( tetribodies[uniqueid], 32,-16, 32, 32)
tetrishapes[uniqueid][4] = love.physics.newRectangleShape( tetribodies[uniqueid], 0,16, 32, 32)
elseif i == 7 then --Z
tetribodies[uniqueid] = love.physics.newBody(world, x, y, 0, blockrot)
tetrishapes[uniqueid][1] = love.physics.newRectangleShape( tetribodies[uniqueid], 0,16, 32, 32)
tetrishapes[uniqueid][2] = love.physics.newRectangleShape( tetribodies[uniqueid], 0,-16, 32, 32)
tetrishapes[uniqueid][3] = love.physics.newRectangleShape( tetribodies[uniqueid], 32,16, 32, 32)
tetrishapes[uniqueid][4] = love.physics.newRectangleShape( tetribodies[uniqueid], -32,-16, 32, 32)
end
tetribodies[uniqueid]:setLinearDamping(0.5)
tetribodies[uniqueid]:setMassFromShapes()
tetribodies[uniqueid]:setBullet(true)
for i, v in pairs(tetrishapes[uniqueid]) do
v:setData({1})
end
end
function gameA_draw()
--FULLSCREEN OFFSET
if fullscreen then
love.graphics.translate(fullscreenoffsetX, fullscreenoffsetY)
--scissor
love.graphics.setScissor(fullscreenoffsetX, fullscreenoffsetY, 160*scale, 144*scale)
end
--background--
love.graphics.draw(gamebackgroundcutoff, 0, 0, 0, scale, scale)
---------------
--tetrishapes--
if cuttingtimer == lineclearduration then
for i,v in pairs(tetribodies) do
if pause == false then
love.graphics.draw( tetriimages[i], v:getX()*physicsscale, v:getY()*physicsscale, v:getAngle(), 1, 1, piececenter[tetrikind[i]][1]*scale, piececenter[tetrikind[i]][2]*scale)
end
end
else
for i = 1, #tetricutimg do
if pause == false then
love.graphics.draw( tetricutimg[i], tetricutpos[i*2-1]*physicsscale, tetricutpos[i*2]*physicsscale, tetricutang[i], 1, 1, piececenter[tetricutkind[i]][1]*scale, piececenter[tetricutkind[i]][2]*scale)
end
end
--blinky lines
local section = math.ceil(cuttingtimer/(lineclearduration/lineclearblinks))
if math.mod(section, 2) == 1 or cuttingtimer == 0 then
local rr, rg, rb = unpack(getrainbowcolor(hue))
local r = 145 + rr*64
local g = 145 + rg*64
local b = 145 + rb*64
for i = 1, 18 do
if linesremoved[i] == true then
love.graphics.setColor(r, g, b)
love.graphics.rectangle("fill", 14*scale, (i-1)*8*scale, 82*scale, 8*scale)
end
end
end
end
love.graphics.setColor(255, 255, 255)
--Next piece
if pause == false then
love.graphics.draw(nextpieceimg[nextpiece], 136*scale, 120*scale, nextpiecerot, 1, 1, piececenterpreview[nextpiece][1]*scale, piececenterpreview[nextpiece][2]*scale)
end
----------------
--Last score
if scoreaddtimer < scoreaddtime then
if fullscreen then
love.graphics.setScissor(105*scale+fullscreenoffsetX, 35*scale+fullscreenoffsetY, 55*scale, 9*scale)
else
love.graphics.setScissor(105*scale, 35*scale, 55*scale, 9*scale)
end
love.graphics.setFont(whitefont)
local offsetX = 0
for i = 1, tostring(lastscoreadd):len() - 1 do
offsetX = offsetX - 8*scale
end
love.graphics.print("+" .. lastscoreadd, 136*scale+offsetX, 36*scale-scoreaddtimer/scoreaddtime*8*scale, 0, scale)
love.graphics.setFont(tetrisfont)
if fullscreen then
love.graphics.setScissor(fullscreenoffsetX, fullscreenoffsetY, 160*scale, 144*scale)
else
love.graphics.setScissor()
end
end
--line density counter
for i = 1, 18 do
local fullness = linearea[i]/1024/linecleartreshold
if fullness > 1 then
fullness = 1
end
local color
if fullness == 1 then
color = 0
else
color = 235-(fullness/1)*180
end
love.graphics.setColor(color, color, color)
love.graphics.rectangle("fill", 0, (i-1)*8*scale, math.floor(6*scale*fullness), 8*scale)
end
love.graphics.setColor(255, 255, 255)
---------
--start--
if pause == true then
love.graphics.draw(pausegraphiccutoff, 14*scale, 0, 0, scale, scale)
end
---------
--SCORES---------------------------------------
--"score"--
offsetX = 0
scorestring = tostring(scorescore)
for i = 1, scorestring:len() - 1 do
offsetX = offsetX - 8*scale
end
love.graphics.print( scorescore, 144*scale + offsetX, 24*scale, 0, scale, scale)
--"level"--
offsetX = 0
scorestring = tostring(levelscore)
for i = 1, scorestring:len() - 1 do
offsetX = offsetX - 8*scale
end
love.graphics.print( levelscore, 136*scale + offsetX, 56*scale, 0, scale, scale)
--"tiles"--
offsetX = 0
scorestring = tostring(linesscore)
for i = 1, scorestring:len() - 1 do
offsetX = offsetX - 8*scale
end
love.graphics.print( linesscore, 136*scale + offsetX, 80*scale, 0, scale, scale)
-----------------------------------------------
--FULLSCREEN OFFSET
if fullscreen then
love.graphics.translate(-fullscreenoffsetX, -fullscreenoffsetY)
--scissor
love.graphics.setScissor()
end
end
function gameA_update(dt)
--NEXTPIECE ROTATION (rotating allday erryday)
if cuttingtimer == lineclearduration then
nextpiecerot = nextpiecerot + nextpiecerotspeed*dt
while nextpiecerot > math.pi*2 do
nextpiecerot = nextpiecerot - math.pi*2
end
end
--CUTTING TIMER
if cuttingtimer < lineclearduration then
cuttingtimer = cuttingtimer + dt
if cuttingtimer >= lineclearduration then
--RANDOMIZE NEXT PIECE
nextpiece = math.random(7)
cuttingtimer = lineclearduration
skipupdate = true
scoreaddtimer = 0
if newlevelbeep then
love.audio.stop(newlevel)
love.audio.play(newlevel)
newlevelbeep = false
end
end
return
end
--SCOREADD TIMER
if cuttingtimer == lineclearduration then
if scoreaddtimer < scoreaddtime then
scoreaddtimer = scoreaddtimer + dt
if scoreaddtimer > scoreaddtime then
scoreaddtimer = scoreaddtime
end
end
end
if gamestate == "gameA" then
if controls.isDown("rotateright") then
if tetribodies[1]:getAngularVelocity() < 3 then
tetribodies[1]:applyTorque( 70 )
end
end
if controls.isDown("rotateleft") then
if tetribodies[1]:getAngularVelocity() > -3 then
tetribodies[1]:applyTorque( -70 )
end
end
if controls.isDown( "left" ) then
local x, y = tetribodies[1]:getWorldCenter()
tetribodies[1]:applyForce( -70, 0, x, y )
end
if controls.isDown( "right" ) then
local x, y = tetribodies[1]:getWorldCenter()
tetribodies[1]:applyForce( 70, 0, x, y )
end
local x, y = tetribodies[1]:getLinearVelocity( )
if controls.isDown( "down" ) then
--commented part limits the blackfallspeed
if y > 500 then
tetribodies[1]:setLinearVelocity(x, 500)
else
local cx, cy = tetribodies[1]:getWorldCenter()
tetribodies[1]:applyForce( 0, 20, cx, cy )
end
else
if y > difficulty_speed then
tetribodies[1]:setLinearVelocity(x, y-2000*dt)
end
end
end
endblock = false
world:update(dt)
if endblock then
endblockA()
end
--DENSITY UPDATE TIMER
if densityupdatetimer >= densityupdateinterval then
while densityupdatetimer >= densityupdateinterval and cuttingtimer == lineclearduration do
checklinedensity(false)
densityupdatetimer = densityupdatetimer - densityupdateinterval
end
end
densityupdatetimer = densityupdatetimer + dt
if gamestate == "failingA" then
clearcheck = true
for i,v in pairs(tetribodies) do
if v:getY() < 648 then
clearcheck = false
end
end
if clearcheck then
failed_load()
end
end
end
function getintersectX(shape, y) --returns left and right collision points to a certain shape on a Y coordinate (or -1, -0.9 if no collision)
local lefttime = shape:testSegment( 55, y, 385, y)
local righttime = shape:testSegment( 385, y, 55, y)
if lefttime ~= nil and righttime ~= nil then
local leftx = 330 * lefttime + 55
local rightx = 385 - 330 * righttime
return leftx, rightx
else
return -1, -0.9
end
end
function removeline(lineno) --Does all necessary things to clear a line. Refineshape and cutimage included.
upperline = (lineno - 1) * 32
lowerline = lineno * 32
globaline = lineno
coordinateproperties = {}
numberofbodies = highestbody()
local ioffset = 0
tetribodies[1] = "dummy :D"
for i = 2, numberofbodies do --every body
v = tetribodies[i-ioffset]
if i-ioffset > numberofbodies then
print("oh yeah")
break
end
if i-ioffset > 1 then
refined = false
coordinateproperties[i-ioffset] = {}
tetrishapescopy = {}
upperleftx = 640
lowerleftx = 640
upperrightx = 0
lowerrightx = 0
for j, w in pairs(tetrishapes[i-ioffset]) do
x1, x2 = getintersectX(tetrishapes[i-ioffset][j], upperline)
if x1 < upperleftx and x1 ~= -1 then
upperleftx = x1
end
if x2 > upperrightx then
upperrightx = x2
end
x1, x2 = getintersectX(tetrishapes[i-ioffset][j], lowerline)
if x1 < lowerleftx and x1 ~= -1 then
lowerleftx = x1
end
if x2 > lowerrightx then
lowerrightx = x2
end
end
for j, w in pairs(tetrishapes[i-ioffset]) do --Every shape
above = false
inside = false
below = false
coordinateproperties[i-ioffset][j] = {}
coordinates = getPoints2table(w)
for y = 1, #coordinates, 2 do --Every Point
if coordinates[y+1] < upperline then --POINT ABOVE CUTRECT
coordinateproperties[i-ioffset][j][math.ceil(y/2)] = 1
above = true
elseif coordinates[y+1] >= upperline and coordinates[y+1] <= lowerline then --POINT INSIDE CUTRECT!
coordinateproperties[i-ioffset][j][math.ceil(y/2)] = 2
inside = true
elseif coordinates[y+1] > lowerline then --POINT BELOW CUTRECT
coordinateproperties[i-ioffset][j][math.ceil(y/2)] = 3
below = true
end
end
if above == true and inside == true and below == false then
tetrishapescopy[#tetrishapescopy+1]=refineshape(upperline, 1, i-ioffset, v, j, w)
refined = true
elseif above == true and inside == true and below == true then
tetrishapescopy[#tetrishapescopy+1]=refineshape(upperline, 1, i-ioffset, v, j, w)
tetrishapescopy[#tetrishapescopy+1]=refineshape(lowerline, -1, i-ioffset, v, j, w)
refined = true
elseif above == false and inside == true and below == false then
--nothing because it'll get removed (don't delete the elseif though cause it'll go though the "else")
refined = true
elseif above == false and inside == true and below == true then
tetrishapescopy[#tetrishapescopy+1]=refineshape(lowerline, -1, i-ioffset, v, j, w)
refined = true
elseif above == true and inside == false and below == true then
tetrishapescopy[#tetrishapescopy+1]=refineshape(upperline, 1, i-ioffset, v, j, w)
tetrishapescopy[#tetrishapescopy+1]=refineshape(lowerline, -1, i-ioffset, v, j, w)
refined = true
else
cotable = getPoints2table(tetrishapes[i-ioffset][j])
for var = 1, #cotable, 2 do
cotable[var], cotable[var+1] = tetribodies[i-ioffset]:getLocalPoint(cotable[var], cotable[var+1])
end
tetrishapescopy[#tetrishapescopy+1] = love.physics.newPolygonShape(tetribodies[i-ioffset], unpack(cotable))
end
end
if refined == true then
--create for either above our below; or both if body is cut in center.
--gotta set the bodyids here and reuse them in the "check for disconnect shapes" further down
for a, b in pairs(tetrishapes[i-ioffset]) do --remove all shapes
if tetrishapes[i-ioffset][a] then
tetrishapes[i-ioffset][a]:destroy()
tetrishapes[i-ioffset][a] = nil
end
end
tetrishapes[i-ioffset] = {}
if #tetrishapescopy == 0 then --body empty
if tetribodies[i-ioffset] then
tetribodies[i-ioffset]:destroy()
table.remove(tetribodies, i-ioffset)
table.remove(tetrishapes, i-ioffset)
table.remove(tetrikind, i-ioffset)
table.remove(tetriimages, i-ioffset)
table.remove(tetriimagedata, i-ioffset)
numberofbodies = numberofbodies - 1
ioffset = ioffset + 1
end
else
----check for disconnected shapes
--apply group numbers to shapes then loop through each existing value, creating a new body for everything > 1 (max should be 3 I think).
shapegroups = {}
numberofgroups = 0
for a, b in pairs(tetrishapescopy) do --through all shapes
shapegroups[a] = 0
currentcoords = getPoints2table(b)
for shapecounter = 1, a - 1 do --Through all previously set groups
coords = getPoints2table(tetrishapescopy[shapecounter])
for currentcoordsvar = 1, #currentcoords/2 do --through all coords in the current shape
for coordsvar = 1, #coords/2 do --through all coords in all previously set groups (Holy shit 6 stacked "for" loops; I code like an asshole!)
if math.abs(currentcoords[currentcoordsvar*2-1] - coords[coordsvar*2-1]) < 2 and math.abs(currentcoords[currentcoordsvar*2] - coords[coordsvar*2]) < 2 then
shapegroups[a] = shapegroups[shapecounter]
end
end
end
end
if shapegroups[a] == 0 then --create new group
numberofgroups = numberofgroups + 1
shapegroups[a] = numberofgroups
end
end
--All shapes have now an assigned group number in "shapegroups[shape] = groupnumber" :) yay.
for a = 1, numberofgroups do
if a == 1 then --reassign the old bodyid
rotation = tetribodies[i-ioffset]:getAngle()
tetribodies[i-ioffset]:destroy()
tetribodies[i-ioffset] = love.physics.newBody(world, tetribodies[i-ioffset]:getX(), tetribodies[i-ioffset]:getY(), tetribodies[i-ioffset]:getMass(), blockrot)
tetribodies[i-ioffset]:setAngle(rotation)
tetrishapes[i-ioffset] = {}
for b, c in pairs(tetrishapescopy) do
if shapegroups[b] == a then
cotable = getPoints2table(tetrishapescopy[b])
for var = 1, #cotable, 2 do
cotable[var], cotable[var+1] = tetribodies[i-ioffset]:getLocalPoint(cotable[var], cotable[var+1])
end
tetrishapes[i-ioffset][#tetrishapes[i-ioffset]+1] = love.physics.newPolygonShape(tetribodies[i-ioffset], unpack(cotable))
tetrishapes[i-ioffset][#tetrishapes[i-ioffset]]:setData({i-ioffset}) --set the shape name for collision
end
end
--save old imagedata to local var first in case we create a new bodyid..
backupimagedata = love.image.newImageData( tetriimagedata[i-ioffset]:getWidth(), tetriimagedata[i-ioffset]:getHeight())
backupimagedata:paste(tetriimagedata[i-ioffset], 0, 0, 0, 0, tetriimagedata[i-ioffset]:getWidth(), tetriimagedata[i-ioffset]:getHeight() )
--cut the image.
cutimage(i-ioffset, numberofgroups)
--mass confusion
tetribodies[i-ioffset]:setMassFromShapes()
local mass = tetribodies[i-ioffset]:getMass()
if mass < minmass then
for i, v in pairs(tetrishapes[i-ioffset]) do
v:setDensity( minmass/mass )
end
tetribodies[i-ioffset]:setMassFromShapes()
for i, v in pairs(tetrishapes[i-ioffset]) do
v:setDensity( 1 )
end
end
else --create new bodyid
tetribodies[highestbody()+1] = love.physics.newBody(world, tetribodies[i-ioffset]:getX(), tetribodies[i-ioffset]:getY(), tetribodies[i-ioffset]:getMass(), blockrot)
tetribodies[highestbody()]:setAngle(tetribodies[i-ioffset]:getAngle())
tetrishapes[highestbody()] = {}
for b, c in pairs(tetrishapescopy) do
if shapegroups[b] == a then
cotable = getPoints2table(tetrishapescopy[b])
for var = 1, #cotable, 2 do
cotable[var], cotable[var+1] = tetribodies[i-ioffset]:getLocalPoint(cotable[var], cotable[var+1])
end
tetrishapes[highestbody()][#tetrishapes[highestbody()]+1] = love.physics.newPolygonShape(tetribodies[highestbody()], unpack(cotable))
tetrishapes[highestbody()][#tetrishapes[highestbody()]]:setData({highestbody()}) --set the shape name for collision
end
end
linearspeedX, linearspeedY = tetribodies[i-ioffset]:getLinearVelocity()
tetribodies[highestbody()]:setLinearVelocity(linearspeedX, linearspeedY)
tetribodies[highestbody()]:setLinearDamping(0.5)
tetribodies[highestbody()]:setBullet(true)
tetribodies[highestbody()]:setAngularVelocity(tetribodies[i-ioffset]:getAngularVelocity())
tetriimagedata[highestbody()] = love.image.newImageData( backupimagedata:getWidth(), backupimagedata:getHeight())
tetriimagedata[highestbody()]:paste( backupimagedata, 0, 0, 0, 0, backupimagedata:getWidth(), backupimagedata:getHeight() )
tetriimages[highestbody()] = padImagedata( tetriimagedata[highestbody()] )
tetrikind[highestbody()] = tetrikind[i-ioffset]
debugimagedata = love.image.newImageData( backupimagedata:getWidth(), backupimagedata:getHeight())
debugimagedata:paste(backupimagedata, 0, 0, 0, 0, backupimagedata:getWidth(), backupimagedata:getHeight() )
debugimage = padImagedata( debugimagedata )
--cut the image
cutimage(highestbody(), numberofgroups)
--mass confusion
tetribodies[highestbody()]:setMassFromShapes()
local mass = tetribodies[highestbody()]:getMass()
if mass < minmass then
for i, v in pairs(tetrishapes[highestbody()]) do
v:setDensity( minmass/mass )
end
tetribodies[highestbody()]:setMassFromShapes()
for i, v in pairs(tetrishapes[highestbody()]) do
v:setDensity( 1 )
end
end
end
end
end --if body empty
end --if refined
--clean up the tables..
for a, b in pairs(tetrishapescopy) do
if tetrishapescopy[a] then
tetrishapescopy[a]:destroy()
tetrishapescopy[a] = nil
end
end
tetrishapescopy = {}
end --if i-ioffset > 1
end
end
function cutimage(bodyid, numberofgroups) --cuts the image of a body based on its shapes (2nd argument might be obsolete)
local width = tetriimagedata[bodyid]:getWidth()
local height = tetriimagedata[bodyid]:getHeight()
--[[ This old method used the angle between the two cutting points and all the other points to determine if the point should be removed.
But it sometimes bugged out for reasons I couldn't figure out so I'm going for the less good looking easy approach. Boo :(
local bodyang = tetribodies[bodyid]:getAngle()
bodyang = math.mod(bodyang, math.pi)
local highestx = -1
local lowestx = 160*scale
for i, v in pairs(tetrishapes[bodyid]) do
x1, x2 = getintersectX(v, upperline-0.01)
if x1 < lowestx and x1 ~= -1 then
lowestx = x1
end
if x2 > highestx thena
highestx = x2
end
end
--get if to chose lower or upper line
local posy = getPoints2table(tetrishapes[bodyid][1])
posy = posy[2]
if posy > (upperline + lowerline) / 2 then
line = lowerline
else
line = upperline
end
--convert points to local coordinates
local dummy1, dummy2 = tetribodies[bodyid]:getLocalPoint( lowestx, line )
dummy1, dummy2 = dummy1 + width/2, dummy2 + height/2
local point1 = {dummy1, dummy2}
local dummy1, dummy2 = tetribodies[bodyid]:getLocalPoint( highestx, line )
dummy1, dummy2 = dummy1 + width/2, dummy2 + height/2
local point2 = {dummy1, dummy2}
local leftlimit = 160*scale
local rightlimit = -1
--find out the limits of there's more than 1 body being created
if numberofgroups > 1 then
for s = 1, #tetrishapes[bodyid] do
local cotable = getPoints2table(tetrishapes[bodyid][s])
for i = 1, #cotable, 2 do
local x, y = tetribodies[bodyid]:getLocalPoint(cotable[i], cotable[i+1])
x = x+width/2
if x < leftlimit then
leftlimit = x
elseif x > rightlimit then
rightlimit = x
end
end
end
else
leftlimit = -1
rightlimit = 160*scale
end
local ang = math.atan2(point2[1] - point1[1], point2[2] - point1[2])
ang = -ang + math.pi]]--
for y = 0, height-1 do
for x = 0, width-1 do
--[[local ang2 = math.atan2(x - point1[1], y - point1[2]) --PART OF OLD METHOD
ang2 = -ang2 + math.pi
if (ang2 > ang and ang2 < ang + math.pi) or ang2 < ang - math.pi or (x < leftlimit or x > rightlimit) then
tetriimagedata[bodyid]:setPixel(x, y, 255, 255, 255, 0)
end]]
local dummy1, dummy2 = tetribodies[bodyid]:getWorldPoint((x-width/2+.5)*(4/scale), (y-height/2+.5)*(4/scale))
local deletepixel = true
for i, v in pairs(tetrishapes[bodyid]) do
if v:testPoint( dummy1, dummy2 ) then
deletepixel = false
break
end
end
if deletepixel then
tetriimagedata[bodyid]:setPixel(x, y, 255, 255, 255, 0)
end
end
end
tetriimages[bodyid] = padImagedata( tetriimagedata[bodyid] )
end
function refineshape(line, mult, bodyid, body, shapeid, shape) --refines a shape using the old coordinates and the cutting line
local leftx, rightx = getintersectX(tetrishapes[bodyid][shapeid], line)
if leftx ~= -1 then --Not sure what to do if not
local coords = getPoints2table(tetrishapes[bodyid][shapeid])
--remove all points inside the cutting zone
local lastcutoff
local i=2
while i <= #coords do
if coords[i]*mult > line*mult then
table.remove(coords, i)
table.remove(coords, i-1)
lastcutoff = i
i=0
end
i=i+2
end
--add new points (Only if they aren't identical to existing points)
if lastcutoff then
if mult == 1 then
if samepos(coords, line, leftx) == false then
table.insert(coords, lastcutoff-1,leftx)
table.insert(coords, lastcutoff,line)
end
if samepos(coords, line, rightx) == false then
table.insert(coords, lastcutoff-1,rightx)
table.insert(coords, lastcutoff,line)
end
else
if samepos(coords, line, rightx) == false then
table.insert(coords, lastcutoff-1,rightx)
table.insert(coords, lastcutoff,line)
end
if samepos(coords, line, leftx) == false then
table.insert(coords, lastcutoff-1,leftx)
table.insert(coords, lastcutoff,line)
end
end
end
--create the new shape
if #coords/2 >= 3 and #coords/2 <= 8 then --shape still has 3 or more points, and not over 8.
if largeenough(coords) then
local newcoords={}
for i=1,#coords,2 do
newcoords[i],newcoords[i+1] = body:getLocalPoint(coords[i], coords[i+1])
end
return love.physics.newPolygonShape(body, unpack(newcoords))
end
else
print("#coords")
end
else
local coords = getPoints2table(tetrishapes[bodyid][shapeid])
local newcoords={}
for i=1,#coords,2 do
newcoords[i],newcoords[i+1] = body:getLocalPoint(coords[i], coords[i+1])
end
return love.physics.newPolygonShape(body, unpack(newcoords))
end
end
function checklinedensity(active) --checks all 18 lines and, if active == true, calls removeline. Also does scoring, sounds and stuff.
--loop through every shape and add each area to a nax
linearea = {}
for i = 1, 18 do
linearea[i] = 0
end
for i = 2, #tetribodies do
for j, k in pairs(tetrishapes[i]) do
local coords = getPoints2table(k)
--Get first and last involved line
local firstline = 19
local lastline = 0
for point = 2, #coords, 2 do
if math.ceil(round(coords[point]) / 32) < firstline then
firstline = math.ceil(round(coords[point]) / 32)
elseif math.ceil(round(coords[point]) / 32) > lastline then
lastline = math.ceil(round(coords[point]) / 32)
end
end
for line = firstline, lastline do
if line >= 1 and line <= 18 then
coords = getPoints2table(k)
if line > firstline then
local offset = 0
repeat
leftx, rightx = getintersectX(tetrishapes[i][j], (line-1)*32+offset)
offset = offset + 1
until leftx ~= -1 or offset >= 32
--remove all points above the line
local coi=2
local lastcutoff = nil
while coi <= #coords do
if coords[coi] <= (line-1)*32 then
table.remove(coords, coi)
table.remove(coords, coi-1)
lastcutoff = coi
coi=0
end
coi=coi+2
end
--add points of top line (if points were cut off)
if lastcutoff then
table.insert(coords, lastcutoff-1,rightx)
table.insert(coords, lastcutoff,(line-1)*32)
table.insert(coords, lastcutoff-1,leftx)
table.insert(coords, lastcutoff,(line-1)*32)
end
end
if line < lastline then
local offset = 0
repeat
leftx, rightx = getintersectX(tetrishapes[i][j], (line)*32 - offset)
offset = offset + 1
until leftx ~= -1 or offset >= 32
--remove all points below the line
local coi=2
local lastcutoff = nil
while coi <= #coords do
if coords[coi] >= (line)*32 then
table.remove(coords, coi)
table.remove(coords, coi-1)
lastcutoff = coi
coi=0
end
coi=coi+2
end
--add points of bottom line (if points were cut off)
if lastcutoff then
table.insert(coords, lastcutoff-1,leftx)
table.insert(coords, lastcutoff,(line)*32)
table.insert(coords, lastcutoff-1,rightx)
table.insert(coords, lastcutoff,(line)*32)
end
end
linearea[line] = linearea[line] + polygonarea(coords)
end
end
end
end
if active then
local removedlines = false
local numberoflines = 0
linesremoved = {}
for i = 1, 18 do
if linearea[i] > 1024*linecleartreshold then
if removedlines == false then
cuttingtimer = 0
removedlines = true
--Save position, image, kind and image of each kind so I can draw them even after changing the actual parts.
tetricutpos = {}
tetricutang = {}
tetricutkind = {}
tetricutimg = {}
for i, v in pairs(tetribodies) do -- = 2, #tetribodies do
if tetribodies[i].getX then
table.insert(tetricutpos, tetribodies[i]:getX())
table.insert(tetricutpos, tetribodies[i]:getY())
table.insert(tetricutang, tetribodies[i]:getAngle())
table.insert(tetricutkind, tetrikind[i])
table.insert(tetricutimg, padImagedata(tetriimagedata[i]))
end
end
--[[for j, k in pairs(tetribodies) do --CANCEL ALL BLOCK MOVEMENT
if k.setLinearVelocity then
k:setLinearVelocity(0, 0)
k:setAngularVelocity(0, 0)
end
end]]
end
linesremoved[i] = true
numberoflines = numberoflines + 1
linesscore = linesscore + 1
end
end
if removedlines then
if numberoflines >= 4 then
love.audio.stop(fourlineclear)
love.audio.play(fourlineclear)
else
love.audio.stop(lineclear)
love.audio.play(lineclear)
end
--Possible scoring functions:
-- (numberoflines^2*40)+(numberoflines*50)* averagearea^8 (mine)
-- (NUMOFLINES*3)^(AREA^10)*20+NUMOFLINES^2*40 (murks)
--old scoring
--[[
if numberoflines == 1 then
scorescore = scorescore + 40
elseif numberoflines == 2 then
scorescore = scorescore + 100
elseif numberoflines == 3 then
scorescore = scorescore + 300
elseif numberoflines == 4 then
scorescore = scorescore + 800
else
scorescore = scorescore + numberoflines*300
end
]]
--calculate average area
local averagearea = 0
for i = 1, 18 do
if linesremoved[i] then
averagearea = averagearea + linearea[i]
end
end
averagearea = averagearea / numberoflines / 10240
local scoreadd = math.ceil((numberoflines*3)^(averagearea^10)*20+numberoflines^2*40)
scorescore = scorescore + scoreadd
lastscoreadd = scoreadd
scoreaddtimer = 0
--Level
linescleared = linescleared + numberoflines
if math.floor(linescleared/10) > levelscore then
levelscore = levelscore + 1
difficulty_speed = 100 + levelscore*7
newlevelbeep = true
end
--Draw the screen before removing lines.
love.graphics.clear()
gameA_draw()
love.graphics.present( )
for i = 1, 18 do