-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDoom.lua
1326 lines (1161 loc) · 33.5 KB
/
Doom.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
-- Made by Xella
-- Ugly old code
-- Couldn't be bothered to fix everything when updating to the new Pine3D
-- Don't mod this mess. Please just make your own game which has readable code
local path = "/"..fs.getDir(shell.getRunningProgram())
local Pine3D = require("Pine3D-minified")
os.loadAPI(path.."/blittle")
local objects = {}
local gun = paintutils.loadImage(path.."/images/gun")
local gunf = paintutils.loadImage(path.."/images/gunf")
local bgun = paintutils.loadImage(path.."/images/bgun")
local bgunf = paintutils.loadImage(path.."/images/bgunf")
local heart = paintutils.loadImage(path.."/images/heart")
local bheart = paintutils.loadImage(path.."/images/bheart")
local hearts = 5
local fire = paintutils.loadImage(path.."/images/fire")
local bfire = paintutils.loadImage(path.."/images/bfire")
local shootCooldown = 3 / 16
local lastShot = os.clock() - shootCooldown
local resetGame = false
local playerX = 0
local playerY = 0
local playerZ = -2
local playerSpeed = 3
local playerTurnSpeed = 100
local keysDown = {}
local gunbobX, gunbobY = 0, 0
local username = "Unnamed"
local submitScore = true
local graphics = "Good"
local score = 0
local scoreTime = 0
local endGame = false
local levelCount = 0
local reloadedLevel = false
local xDoor = true
local endless = false
local enemySpeed = 2.5
local FoV = 90
local playerDirectionHor = 0
local playerDirectionVer = 0
local finishMode
local termWidth, termHeight = term.getSize()
function round(num, numDecimalPlaces)
local mult = 10^(numDecimalPlaces or 0)
return math.floor(num * mult + 0.5) / mult
end
local ThreeDFrame = Pine3D.newFrame()
local blittleOn = true
ThreeDFrame:highResMode(blittleOn)
ThreeDFrame:setBackgroundColor(colors.lightGray)
local environmentObjects = {
ThreeDFrame:newObject(Pine3D.models:plane({size = 100, y = -0.1, color = colors.orange}))
}
local latestScore = {}
local function min(a, b)
if (a <= b) then
return a
end
return b
end
local function loadSettings()
if (fs.exists(path.."/settings") == false) then
local file = fs.open(path.."/settings", "w")
file.writeLine("Unnamed")
file.writeLine("true")
file.writeLine("Good")
file.close()
end
local file = fs.open(path.."/settings", "r")
username = file.readLine()
submitScore = file.readLine()
graphics = file.readLine()
file.close()
if (graphics == "Good") then
blittleOn = true
else
blittleOn = false
end
ThreeDFrame:highResMode(blittleOn)
end
local function saveSettings()
local file = fs.open(path.."/settings", "w")
file.writeLine(username)
file.writeLine(submitScore)
file.writeLine(graphics)
file.close()
end
local function loadLevel(levelname)
if (reloadedLevel == false) then
objects = {}
end
local level = paintutils.loadImage(path.."/levels/"..levelname)
for z, row in pairs(level) do
for x, value in pairs(row) do
if (value ~= nil and value > 0) then
if (value == 1) then
playerX = x
playerY = 0
playerZ = z
end
if (reloadedLevel == false) then
local object = nil
if (value == colors.orange) then
object = ThreeDFrame:newObject("models/wallz", x, 0, z)
object.solid = true
elseif (value == colors.magenta) then
object = ThreeDFrame:newObject("models/wallx", x, 0, z)
object.solid = true
elseif (value == colors.lightBlue) then
object = ThreeDFrame:newObject("models/wallxz", x, 0, z)
object.solid = true
elseif (value == colors.red) then
object = ThreeDFrame:newObject("models/doorz", x, 0, z)
object.model = "doorz"
elseif (value == colors.green) then
object = ThreeDFrame:newObject("models/doorx", x, 0, z)
object.model = "doorx"
elseif (value == colors.yellow) then
object = ThreeDFrame:newObject("models/enemy1", x, 0, z)
object.model = "enemy1"
object.lastHit = os.clock()
elseif (value == colors.lime) then
object = ThreeDFrame:newObject("models/enemy2", x, 0, z)
object.model = "enemy2"
object.lastHit = os.clock() - 1/20
elseif (value == colors.pink) then
object = ThreeDFrame:newObject("models/emerald", x, 0, z, 0, 45, 0)
object.solid = true
end
if object then
objects[#objects+1] = object
end
end
end
end
end
end
local function loadRandomLevel(xDoor)
if (xDoor == false) then
local levels = {"level3", "level5", "level7"}
loadedLevel = levels[math.random(1, table.getn(levels))]
else
local levels = {"level2", "level4", "level6", "level8"}
loadedLevel = levels[math.random(1, table.getn(levels))]
end
loadLevel(loadedLevel)
end
local function free(x, y, z)
for objectNr, object in pairs(objects) do
if object.solid then
if (x >= object[1] - 0.5 and x <= object[1] + 0.5) then
if (y >= object[2] and y <= object[2] + 1) then
if (z >= object[3] - 0.5 and z <= object[3] + 0.5) then
return false
end
end
end
end
end
return true
end
local function rendering()
ThreeDFrame:setCamera(playerX, playerY + 0.5, playerZ, 0, playerDirectionHor, playerDirectionVer)
while true do
--ThreeDFrame:loadGround(backgroundColor1)
--ThreeDFrame:loadSky(backgroundColor2)
ThreeDFrame:drawObjects(environmentObjects)
ThreeDFrame:drawObjects(objects)
if (blittleOn) then
if (lastShot > os.clock() - shootCooldown) then
ThreeDFrame.buffer:image(29+gunbobX+(termWidth-51), 8+gunbobY+(termHeight-19), bfire, true)
ThreeDFrame.buffer:image(32+gunbobX+(termWidth-51), 10+gunbobY+(termHeight-19), bgunf, true)
else
ThreeDFrame.buffer:image(32+gunbobX+(termWidth-51), 10+gunbobY+(termHeight-19), bgun, true)
end
for i = 1, hearts do
ThreeDFrame.buffer:image(2 + (i-1) * 6, 2, bheart, true)
end
else
if (lastShot > os.clock() - shootCooldown) then
ThreeDFrame.buffer:image(29+gunbobX+(termWidth-51), 8+gunbobY+(termHeight-19), fire, false)
ThreeDFrame.buffer:image(32+gunbobX+(termWidth-51), 10+gunbobY+(termHeight-19), gunf, false)
else
ThreeDFrame.buffer:image(32+gunbobX+(termWidth-51), 10+gunbobY+(termHeight-19), gun, false)
end
for i = 1, hearts do
ThreeDFrame.buffer:image(2 + (i-1) * 6, 2, heart, false)
end
end
ThreeDFrame:drawBuffer()
os.queueEvent("FakeEvent")
os.pullEvent("FakeEvent")
end
end
local function shoot()
local bulletDirHor = playerDirectionHor
local bulletX = playerX
local bulletZ = playerZ
local step = 0.15
local hit = false
for i = 1, 10 / step do
bulletX = bulletX + math.cos(math.rad(playerDirectionHor)) * step
bulletZ = bulletZ + math.sin(math.rad(playerDirectionHor)) * step
if (free(bulletX, 0, bulletZ) == false) then
hit = true
break
end
for objectNr, object in pairs(objects) do
if (object.model == "enemy1" or object.model == "enemy2") then
if ((math.abs(bulletX - object[1])^2 + math.abs(bulletZ - object[3]))^0.5 <= 0.5) then
object:setModel("models/corpse")
object.model = "models/corpse"
hit = true
score = score + 10
break
end
end
end
if (hit) then
break
end
end
end
local gunbobby = -1
local function inputPlayer(time)
local playerXvel = 0
local playerZvel = 0
local isMoving
if (keysDown[keys.left]) then
playerDirectionHor = playerDirectionHor - playerTurnSpeed * time
if (playerDirectionHor <= -180) then
playerDirectionHor = playerDirectionHor + 360
end
end
if (keysDown[keys.right]) then
playerDirectionHor = playerDirectionHor + playerTurnSpeed * time
if (playerDirectionHor >= 180) then
playerDirectionHor = playerDirectionHor - 360
end
end
if (keysDown[keys.down]) then
playerDirectionVer = playerDirectionVer - playerTurnSpeed * time
if (playerDirectionVer < -80) then
playerDirectionVer = -80
end
end
if (keysDown[keys.up]) then
playerDirectionVer = playerDirectionVer + playerTurnSpeed * time
if (playerDirectionVer > 80) then
playerDirectionVer = 80
end
end
if (keysDown[keys.w]) then
playerXvel = playerSpeed * math.cos(math.rad(playerDirectionHor)) + playerXvel
playerZvel = playerSpeed * math.sin(math.rad(playerDirectionHor)) + playerZvel
end
if (keysDown[keys.s]) then
playerXvel = -playerSpeed * math.cos(math.rad(playerDirectionHor)) + playerXvel
playerZvel = -playerSpeed * math.sin(math.rad(playerDirectionHor)) + playerZvel
end
if (keysDown[keys.a]) then
playerXvel = playerSpeed * math.cos(math.rad(playerDirectionHor - 90)) + playerXvel
playerZvel = playerSpeed * math.sin(math.rad(playerDirectionHor - 90)) + playerZvel
end
if (keysDown[keys.d]) then
playerXvel = playerSpeed * math.cos(math.rad(playerDirectionHor + 90)) + playerXvel
playerZvel = playerSpeed * math.sin(math.rad(playerDirectionHor + 90)) + playerZvel
end
if (keysDown[keys.space]) then
if (lastShot < os.clock() - shootCooldown) then
lastShot = os.clock()
shoot()
end
end
if (math.abs(playerXvel) > 0.5) or (math.abs(playerZvel) > 0.5) then
if gunbobby == -1 then gunbobby = 1 end
gunbobby = (gunbobby + (math.abs(playerXvel*time) + math.abs(playerZvel*time)) * 2) % 360
else
gunbobby = -1
end
if gunbobby == -1 then
gunbobX = 0
gunbobY = 0
else
gunbobX = round(math.sin(gunbobby),1)
gunbobY = round(math.abs(math.cos(gunbobby)),1)
end
if (free(playerX + playerXvel * time, playerY, playerZ)) then
playerX = playerX + playerXvel * time
end
if (free(playerX, playerY, playerZ + playerZvel * time)) then
playerZ = playerZ + playerZvel * time
end
for _, object in pairs(objects) do
if (object.model == "doorx" or object.model == "doorz") then
local dx = object[1] - playerX
local dz = object[3] - playerZ
local distance = math.sqrt(dx^2 + dz^2)
if (distance < 0.5) then
if (endless == false) then
reloadedLevel = true
if (loadedLevel == "level1") then
loadedLevel = "level2"
reloadedLevel = false
elseif (loadedLevel == "level2" and object.model == "doorx") then
loadedLevel = "level3"
reloadedLevel = false
elseif (loadedLevel == "level3" and object.model == "doorz") then
loadedLevel = "level4"
reloadedLevel = false
elseif (loadedLevel == "level4" and object.model == "doorx") then
loadedLevel = "level5"
reloadedLevel = false
elseif (loadedLevel == "level5" and object.model == "doorz") then
loadedLevel = "level6"
reloadedLevel = false
elseif (loadedLevel == "level6" and object.model == "doorx") then
loadedLevel = "level7"
reloadedLevel = false
elseif (loadedLevel == "level7" and object.model == "doorz") then
loadedLevel = "level8"
reloadedLevel = false
elseif (loadedLevel == "level8" and object.model == "doorx") then
loadedLevel = "level9"
reloadedLevel = false
backgroundColor1 = colors.lime
backgroundColor2 = colors.lightBlue
playerDirectionHor = 270
playerDirectionVer = 0
endGame = true
score = score + hearts * 16 + 400 - 8*min(50, scoreTime)
end
loadLevel(loadedLevel)
else
if (object.model == "doorx" and xDoor) then
xDoor = false
loadRandomLevel(xDoor)
levelCount = levelCount + 1
elseif (object.model == "doorz" and xDoor == false) then
xDoor = true
loadRandomLevel(xDoor)
levelCount = levelCount + 1
else
reloadedLevel = true
loadLevel(loadedLevel)
reloadedLevel = false
end
end
end
end
end
ThreeDFrame:setCamera(playerX, playerY + 0.5, playerZ, 0, playerDirectionHor, playerDirectionVer)
end
local function smoothKeyInput()
--keysDown = {}
while true do
local sEvent, key = os.pullEvent()
if sEvent == "key" then
keysDown[key] = true
if (key == keys.g) then
loadSettings()
if (blittleOn == false) then
blittleOn = true
graphics = "Good"
ThreeDFrame:highResMode(true)
else
blittleOn = false
graphics = "Bad"
ThreeDFrame:highResMode(false)
end
saveSettings()
elseif (key == keys.q) then
playerDirectionHor = playerDirectionHor + 180
playerDirectionVer = -playerDirectionVer
elseif key == keys.leftCtrl then
resetGame = true
end
elseif sEvent == "key_up" then
keysDown[key] = nil
elseif sEvent == "term_resize" then
termWidth, termHeight = term.getSize()
ThreeDFrame:setSize(1, 1, termWidth, termHeight)
end
end
end
local function lineOfSight(x, z)
local dx = x - playerX
local dz = z - playerZ
local lineDir = math.atan(dx / dz)
local distance = math.sqrt(dx^2 + dz^2)
local curX = x
local curZ = z
local step = 0.15
local sight = true
for i = 1, distance / step do
curX = curX + math.cos(lineDir) * step
curZ = curZ + math.sin(lineDir) * step
if (free(curX, 0, curZ) == false) then
sight = false
break
end
end
return sight
end
local function updateGame(time)
if (endGame == false) then
scoreTime = scoreTime + time
end
for objectNr, object in pairs(objects) do
if (object.model == "enemy1") then
local dx = object[1] - playerX
local dz = object[3] - playerZ
if (dz == 0) then
dz = 0.00001
end
local playerDir = math.deg(math.atan(dx/dz))
if (dz < 0) then
if (dx < 0) then
playerDir = playerDir - 180
else
playerDir = playerDir + 180
end
else
if (dx < 0) then
playerDir = playerDir
else
playerDir = playerDir
end
end
object:setRot(nil, math.rad(playerDir + 90), nil)
if (object.lastHit < os.clock() - 3) then
if (lineOfSight(object[1], object[3])) then
object.lastHit = os.clock()
hearts = hearts - 1
end
end
elseif (object.model == "enemy2") then
local dx = object[1] - playerX
local dz = object[3] - playerZ
if (dz == 0) then
dz = 0.00001
end
local playerDir = math.deg(math.atan(dx/dz))
if (dz < 0) then
if (dx < 0) then
playerDir = playerDir - 180
else
playerDir = playerDir + 180
end
else
if (dx < 0) then
playerDir = playerDir
else
playerDir = playerDir
end
end
object:setRot(nil, math.rad(playerDir + 90), nil)
local playerDistance = math.sqrt(dx^2 + dz^2)
if (playerDistance >= 0.5) then
local edX = -enemySpeed * math.sin(math.rad(playerDir)) * time
local edZ = -enemySpeed * math.cos(math.rad(playerDir)) * time
if (free(object[1] + edX, object[2], object[3])) then
object[1] = object[1] + edX
end
if (free(object[1], object[2], object[3] + edZ)) then
object[3] = object[3] + edZ
end
end
if (object.lastHit < os.clock() - 3) then
if (playerDistance < 1) then
object.lastHit = os.clock()
hearts = hearts - 1
end
end
end
end
end
local function deathAnimation()
local blood = {{16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384},{16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,0,0,0,0,16384,16384,16384,16384,16384,16384,0,0,0,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,0,16384},{16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,1,16384,16384,16384,16384,16384,16384,16384,0,0,0,0,0,0,16384,16384,16384,16384,0,0,0,0,16384,16384,1,16384,16384,0,0,0,0,16384,16384,16384,16384,16384,0,0,0},{16384,16384,16384,16384,16384,1,16384,16384,0,16384,16384,16384,1,1,16384,16384,0,0,16384,16384,0,0,0,0,0,0,0,16384,1,16384,16384,0,0,0,0,16384,16384,16384,16384,0,0,0,0,16384,16384,16384,16384,16384,0,0,0},{0,16384,16384,16384,1,1,16384,16384,0,0,0,16384,16384,16384,16384,16384,0,0,0,0,0,0,0,0,0,0,0,16384,1,16384,16384,0,0,0,0,16384,16384,16384,0,0,0,0,0,0,16384,16384,16384,16384,0,0,0},{0,0,0,16384,16384,16384,16384,0,0,0,0,0,0,16384,16384,0,0,0,0,0,0,0,0,0,0,0,0,16384,16384,16384,16384,0,0,0,0,0,0,0,0,0,0,0,0,0,16384,1,1,16384,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,16384,16384,0,0,0,0,0,0,0,0,0,0,0,0,0,0,16384,16384,1,16384,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,16384,16384,16384,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,16384,16384,16384,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,16384,0,0,0,0}}
local scr_x, scr_y = term.getSize()
local function fade(col1, col2, delay)
local oldeTXT, oldeBG = term.getTextColor(), term.getBackgroundColor()
term.setBackgroundColor(col1)
term.setTextColor(col2)
term.clear()
sleep(delay or 0)
for a = 1, 2 do
for y = 1, scr_y do
term.setCursorPos((y%2),y)
term.write(("\127 "):rep(math.ceil(scr_x/2)+1))
end
sleep(delay or 0)
if a == 1 then
for y = 1, scr_y do
term.setCursorPos(1,y)
term.write(("\127"):rep(scr_x))
end
else
term.clear()
end
sleep(delay or 0)
term.setBackgroundColor(col2)
term.setTextColor(col1)
end
sleep(0 or delay)
term.setTextColor(oldeTXT)
term.setBackgroundColor(oldeBG)
end
local topcenterwrite = function(txt)
term.setCursorPos((scr_x/2)-(#txt/2) + 1,1)
term.write(txt)
end
if scr_x == 51 then
for y = 1, #blood do
paintutils.drawImage(blood,1,y-#blood)
sleep(0.1)
end
end
term.setBackgroundColor(colors.red)
term.setTextColor(colors.white)
local script = {
"You feel the last of",
"your life drain away",
"as your body succumbs to",
"the deeply inflicted wounds",
"you failed to prevent.",
"",
"Your lungs collapse.",
"",
"GAME OVER"
}
local skip = false
for y = scr_y, 1, -1 do
term.scroll(-1)
if script[y-4] then
topcenterwrite(script[y-4])
end
if (skip) then
sleep(0)
skip = false
else
skip = true
end
end
--sleep(0.5)
os.pullEvent("char")
term.clear()
sleep(0.1)
fade(colors.red,colors.black,0.1)
sleep(0.2)
term.setBackgroundColor(colors.black)
term.setCursorPos(1,1)
term.clear()
end
local function gameUpdate()
local timeFromLastUpdate = os.clock()
--local avgUpdateSpeed = 0
--local updateCount = 0
--local timeOff = 0
while true do
local currentTime = os.clock()
--[[if (currentTime > timeFromLastUpdate) then
updateGame(currentTime - timeFromLastUpdate - timeOff)
inputPlayer(currentTime - timeFromLastUpdate - timeOff)
avgUpdateSpeed = (currentTime - timeFromLastUpdate) / (updateCount + 1)
updateCount = 0
timeOff = 0
else
updateGame(avgUpdateSpeed)
newTimeOff = timeOff + avgUpdateSpeed
newUpdateCount = updateCount + 1
end]]--
local dt = currentTime - timeFromLastUpdate
updateGame(dt)
inputPlayer(dt)
timeFromLastUpdate = currentTime
if resetGame then
finishMode = "reset"
break
end
--sleep(0)
os.queueEvent("gameupdate")
os.pullEvent("gameupdate")
if (hearts <= 0) then
finishMode = "death"
break
end
if (endGame) then
finishMode = "finish"
break
end
end
end
local function viewHighscores(endless)
term.setBackgroundColor(colors.black)
term.setTextColor(colors.yellow)
term.clear()
term.setCursorPos(12+(termWidth-51)/2, 10)
write("Connecting to the database...")
local rawData = {}
if (endless == false) then
rawData = http.get("https://doom.pine3d.cc/api/highscores")
else
rawData = http.get("https://doom.pine3d.cc/api/highscoresendless")
end
if (rawData == nil) then
term.setBackgroundColor(colors.black)
term.setTextColor(colors.red)
term.clear()
term.setCursorPos(2, 2)
write("Server is not reachable.")
else
local rawData2 = rawData.readAll()
term.clear()
term.setCursorPos(2+(termWidth-51)/2, 2)
write("Online Highscores:")
term.setTextColor(colors.red)
term.setCursorPos(2+(termWidth-51)/2, 4)
if (endless == false) then
write("# Nickname Score Time Date")
else
write("# Nickname Score Levels Date")
end
term.setTextColor(colors.orange)
local recordNr = 0
for record in rawData2:gmatch("[^~]*~") do
record = record:gsub("~", "")
local values = {}
for recordField in record:gmatch("[^;]*;") do
values[#values+1] = recordField
end
local val2 = values[2]:gsub(";", "")
local val3 = values[3]:gsub(";", "")
if values[1]:gsub(";", "") == latestScore[1] and tonumber(val2) == math.floor(latestScore[2]*10 + 0.5)/10 and tonumber(val3) == math.floor(latestScore[3]*100 + 0.5)/100 then
term.setTextColor(colors.white)
else
term.setTextColor(colors.orange)
end
term.setCursorPos(2+(termWidth-51)/2, 6 + recordNr)
write(recordNr + 1)
for i = 1, #values do
local value = values[i]
if (i == 1) then
term.setCursorPos(5+(termWidth-51)/2, 6 + recordNr)
elseif (i == 2) then
term.setCursorPos(26+(termWidth-51)/2, 6 + recordNr)
elseif (i == 3) then
term.setCursorPos(33+(termWidth-51)/2, 6 + recordNr)
elseif (i == 4) then
term.setCursorPos(41+(termWidth-51)/2, 6 + recordNr)
local year = ""
local month = ""
local day = ""
local partNr = 0
for part in value:gmatch("[^-]*") do
if (partNr == 0) then
year = part
elseif (partNr == 2) then
month = part
elseif (partNr == 4) then
day = part
end
partNr = partNr + 1
end
value = day.."-"..month.."-"..year
end
write(value:gsub(";", ""))
end
recordNr = recordNr + 1
if (recordNr >= 11 + (termHeight-19)) then
break
end
end
end
sleep(1)
term.setTextColor(colors.yellow)
term.setCursorPos(2+(termWidth-51)/2, termHeight-1)
write("Press any key to close...")
sleep(0.5)
while true do
local event, key = os.pullEvent()
if event == "term_resize" then
termWidth, termHeight = term.getSize()
ThreeDFrame:setSize(1, 1, termWidth, termHeight)
return viewHighscores(endless)
elseif event == "key" and key ~= keys.leftAlt and key ~= keys.f2 then
return
end
end
end
local function startGame()
loadSettings()
resetGame = false
ThreeDFrame:highResMode(blittleOn)
parallel.waitForAny(smoothKeyInput, rendering, gameUpdate)
end
local function newGameNormal()
score = 0
scoreTime = 0
hearts = 5
endGame = false
playerDirectionHor = 0
playerDirectionVer = 0
reloadedLevel = false
loadedLevel = "level1"
loadLevel(loadedLevel)
endless = false
backgroundColor1 = colors.orange
backgroundColor2 = colors.lightGray
startGame()
if finishMode == "reset" then
return newGameNormal()
end
if (hearts > 0) then
term.setBackgroundColor(colors.black)
term.setTextColor(colors.red)
term.clear()
term.setCursorPos(2, 2)
write("You have survived!")
term.setTextColor(colors.orange)
term.setCursorPos(2, 4)
write("Score: "..(math.floor(score*10+0.5)/10))
term.setCursorPos(2, 5)
write("Time: "..(math.floor(scoreTime*100+0.5)/100))
if (submitScore == "true") then
latestScore = {username, score, scoreTime}
local result = http.get("https://doom.pine3d.cc/api/newscore?name="..textutils.urlEncode(username).."&score="..score.."&time="..scoreTime)
if (result == nil) then
term.setBackgroundColor(colors.black)
term.setTextColor(colors.red)
term.clear()
term.setCursorPos(2, 2)
write("Server is not reachable. Score has not been submitted.")
end
end
sleep(2)
term.setTextColor(colors.yellow)
term.setCursorPos(2, termHeight-1)
write("Press any key to close...")
os.pullEvent("key")
if (submitScore == "true") then
viewHighscores(false)
end
else
deathAnimation()
end
end
local function newGameEndless()
score = 0
time = 0
hearts = 8
endGame = false
playerDirectionHor = 0
playerDirectionVer = 0
reloadedLevel = false
xDoor = false
loadRandomLevel(xDoor)
endless = true
levelCount = 0
backgroundColor1 = colors.orange
backgroundColor2 = colors.lightGray
startGame()
if finishMode == "reset" then
return newGameNormal()
end
score = score + levelCount * 22
deathAnimation()
term.setBackgroundColor(colors.black)
term.setTextColor(colors.red)
term.clear()
term.setCursorPos(2, 2)
write("You died!")
term.setTextColor(colors.orange)
term.setCursorPos(2, 4)
write("Score: "..(math.floor(score*10+0.5)/10))
term.setCursorPos(2, 5)
write("Levels: "..levelCount)
if (submitScore == "true") then
latestScore = {username, score, levelCount}
local result = http.get("https://doom.pine3d.cc/api/newscoreendless?name="..textutils.urlEncode(username).."&score="..score.."&levels="..levelCount)
if (result == nil) then
term.setBackgroundColor(colors.black)
term.setTextColor(colors.red)
term.clear()
term.setCursorPos(2, 2)
write("Server is not reachable. Score has not been submitted.")
end
end
sleep(2)
term.setTextColor(colors.yellow)
term.setCursorPos(2, termHeight-1)
write("Press any key to close...")
os.pullEvent("key")
if (submitScore == "true") then
viewHighscores(true)
end
end
local function newGame()
term.setBackgroundColor(colors.black)
term.setTextColor(colors.red)
term.clear()
term.setCursorPos(21+(termWidth-51)/2, 8)
write("Normal Mode")
term.setCursorPos(20+(termWidth-51)/2, 10)
write("Endless Mode")
term.setCursorPos(24+(termWidth-51)/2, 12)
write("Back")
local selected2 = 0
while true do
term.setTextColor(colors.yellow)
term.setCursorPos(19+(termWidth-51)/2, 8)
if (selected2 == 0) then
write(">")
else
write(" ")
end
term.setCursorPos(18+(termWidth-51)/2, 10)
if (selected2 == 1) then
write(">")
else
write(" ")
end
term.setCursorPos(22+(termWidth-51)/2, 12)
if (selected2 == 2) then
write(">")
else
write(" ")
end
local event, key = os.pullEvent()
if event == "key" then
if (key == keys.w or key == keys.up) then
selected2 = (selected2 - 1 + 3) % 3
elseif (key == keys.s or key == keys.down) then
selected2 = (selected2 + 1) % 3
elseif (key == keys.space or key == keys.enter) then
if (selected2 == 0) then
newGameNormal()
elseif (selected2 == 1) then
newGameEndless()
end
break
end
elseif event == "term_resize" then
termWidth, termHeight = term.getSize()
ThreeDFrame:setSize(1, 1, termWidth, termHeight)
term.setBackgroundColor(colors.black)
term.setTextColor(colors.red)
term.clear()
term.setCursorPos(21+(termWidth-51)/2, 8)
write("Normal Mode")
term.setCursorPos(20+(termWidth-51)/2, 10)
write("Endless Mode")
term.setCursorPos(24+(termWidth-51)/2, 12)
write("Back")
end
end
end
local function showHighscores()
term.setBackgroundColor(colors.black)
term.setTextColor(colors.red)
term.clear()
term.setCursorPos(21+(termWidth-51)/2, 8)
write("Normal Mode")
term.setCursorPos(20+(termWidth-51)/2, 10)
write("Endless Mode")
term.setCursorPos(24+(termWidth-51)/2, 12)
write("Back")