-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathundertale.java
More file actions
1631 lines (1394 loc) · 54 KB
/
undertale.java
File metadata and controls
1631 lines (1394 loc) · 54 KB
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
/*
* ICS3U - Final Culminating Project
* By Orion Chen & Nicky Lam
* For Mr. Chow
*
* UNDERTALE
* This project is a re-make of the famous indie game UNDERTALE.
* It is a game about exploration, finding items, and talking to new
* characters that you meet in the game. However, we obviously do not
* have the expertise to implement all the things in the original game.
* In this re-make, you can explore 2 different maps of the game and
* find heals in each setting. You are then able to fight 1 boss
* at the very last setting in the second map.
* After that, you will be taken to a trophy room and the game ends there.
*
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.*;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.util.*;
public class undertale extends JPanel implements KeyListener, MouseListener, Runnable{
public static int gameState = 0;
public static int setting = 1;
public static int mouseX;
public static int mouseY;
public static int mapX = 0;
public static int mapY = 0;
public static int change = 1;
// Chara variables
public static int charaX = 475;
public static int charaY = 220;
public static int charaSpeed = 5;
// Chara theoretical position when map is moving
// and not herself
public static int globalPos;
public static int fps = 20;
// Variables for movement
public static boolean up = false;
public static boolean left = false;
public static boolean down = false;
public static boolean right = false;
// Chara starting positions when entering or exiting
// [map][setting][1 = start, 2 = exit]
public static corner[][][] allPos = new corner[4][5][3];
// Creating a path to import the pictures
public static BufferedImage[] charaImages = new BufferedImage[10];
public static int curChara = 2; // know which frame of chara to show
// Images
public static BufferedImage titleScreen;
public static BufferedImage aboutScreen;
public static boolean about = false;
// Map images
public static BufferedImage[] ruinsImages = new BufferedImage[5];
public static ArrayList<dimension>[] ruinsBounds = new ArrayList[5];
public static ArrayList<dimension>[] ruinsExits = new ArrayList[5];
public static BufferedImage[] snowdenImages = new BufferedImage[5];
public static ArrayList<dimension>[] snowdenBounds = new ArrayList[5];
public static ArrayList<dimension>[] snowdenExits = new ArrayList[5];
public static BufferedImage replace;
public static BufferedImage[] flowey = new BufferedImage[2];
public static ArrayList<dimension>[] floweyBounds = new ArrayList[2];
public static ArrayList<dimension>[] floweyExits = new ArrayList[2];
// gameState is the map, setting is the current place
// [map][setting]
public static ArrayList<dimension>[][] allBounds = new ArrayList[4][5];
public static ArrayList<dimension>[][] allExits = new ArrayList[4][5];
public static BufferedImage[][] allMaps = new BufferedImage[4][5];
public static dimension[][] allInteractables = new dimension[4][5];
public static boolean[][] visitedInteractables = new boolean[4][5];
// Set of coords in specific maps where the map camera needs to move
public static dimension[][] moveMap = new dimension[3][5];
// Images that will be faded out and into
BufferedImage fadeStart;
BufferedImage fadeEnd;
// Battle
// Variables that let the program if user is currently
// battling or has defeated the boss
public static boolean battling = false;
public static boolean winner = false;
// Player images
public static BufferedImage player;
public static BufferedImage brokenHeart;
// Menu images
public static BufferedImage[] menuImages = new BufferedImage[4];
// Menu options
public static BufferedImage[] selectionImages = new BufferedImage[4];
public static corner[] optionPos = new corner[4];
// Slash animation
public static BufferedImage[] slashImage = new BufferedImage[4];
public static corner[] slashCoords = new corner[4];
// Player boundaries in battle
public static ArrayList<dimension> gameBounds = new ArrayList<dimension>();
// Font
public static Font damageFont;
// Slider
public static BufferedImage bar;
// Number of heals
public static int heals = 0;
// Boss image
public static BufferedImage BD;
public static BufferedImage deadBoss;
// When user runs out of hp
public static BufferedImage gameOver;
public static BufferedImage gameOver2;
public static int z = 0;
public static boolean over = false;
// Attacks
public static BufferedImage bone;
public static BufferedImage bone2;
public static corner[] bonePositions = new corner[11];
public static corner[] bonePositions2 = new corner[12];
// Keycode specifically for battle
public static int xe;
// To display winning text when claiming trophy
public static boolean ended = false;
// Textfile streaming
public static Scanner sc = new Scanner(System.in);
public static ArrayList<String>[][] allScripts = new ArrayList[4][5];
// startScript must be 1-indexed
public static boolean[][] startScript;
public static boolean bossDefeated = false;
public static ArrayList<String> healScript = new ArrayList<String>();
public static Scanner healText;
public static boolean grabbedHeal = false;
public static Font speakingFont;
// Character images when speaking
public static BufferedImage dialogueBox;
public static BufferedImage orion;
public static BufferedImage nicky;
public static BufferedImage sans;
public static BufferedImage clarence;
public static BufferedImage BDspeaking;
public static Thread thread;
// other
animation animation = new animation(this);
charaAnimation charaAnimation = new charaAnimation(this);
dialogue dialogue = new dialogue(this);
audio audio = new audio(this);
battle battle = new battle(this);
public undertale() {
// 10 pixels less height and width than the background because
// for some reason there is extra space when defining the frame
// to be the same size as the background image
setPreferredSize(new Dimension(990, 615));
setBackground(new Color(0, 0, 0));
// Import images
try {
titleScreen = ImageIO.read(new File("assets/undertalestartmenu.png"));
aboutScreen = ImageIO.read(new File("assets/aboutScreen.png"));
// Maps
snowdenImages[1] = ImageIO.read(new File("assets/maps/snowden/snowden1.png"));
snowdenImages[2] = ImageIO.read(new File("assets/maps/snowden/snowden2.png"));
snowdenImages[3] = ImageIO.read(new File("assets/maps/snowden/snowden3.png"));
snowdenImages[4] = ImageIO.read(new File("assets/maps/snowden/snowden4.png"));
replace = ImageIO.read(new File("assets/maps/snowden/realsnowden4.png"));
ruinsImages[1] = ImageIO.read(new File("assets/maps/ruins/ruins1.png"));
ruinsImages[2] = ImageIO.read(new File("assets/maps/ruins/ruins2.png"));
ruinsImages[3] = ImageIO.read(new File("assets/maps/ruins/ruins3.png"));
ruinsImages[4] = ImageIO.read(new File("assets/maps/ruins/ruins4.png"));
charaImages[0] = ImageIO.read(new File("assets/charaAnimation/AcharaB1.png"));
charaImages[1] = ImageIO.read(new File("assets/charaAnimation/AcharaL1.png"));
charaImages[2] = ImageIO.read(new File("assets/charaAnimation/BcharaF1.png"));
charaImages[3] = ImageIO.read(new File("assets/charaAnimation/BcharaR1.png"));
charaImages[4] = ImageIO.read(new File("assets/charaAnimation/charaB2.png"));
charaImages[5] = ImageIO.read(new File("assets/charaAnimation/charaB3.png"));
charaImages[6] = ImageIO.read(new File("assets/charaAnimation/charaF2.png"));
charaImages[7] = ImageIO.read(new File("assets/charaAnimation/charaF3.png"));
charaImages[8] = ImageIO.read(new File("assets/charaAnimation/charaL2.png"));
charaImages[9] = ImageIO.read(new File("assets/charaAnimation/charaR2.png"));
flowey[1] = ImageIO.read(new File("assets/story/Flowey2.png"));
// Dialogue
dialogueBox = ImageIO.read(new File("assets/scripts/dialogueBox.png"));
speakingFont = Font.createFont(Font.TRUETYPE_FONT, new File("assets/fonts/DTM-Sans.ttf")).deriveFont(30f);
orion = ImageIO.read(new File("assets/scripts/orion.png"));
nicky = ImageIO.read(new File("assets/scripts/nicky.png"));
clarence = ImageIO.read(new File("assets/scripts/clarence.png"));
sans = ImageIO.read(new File("assets/scripts/sans.png"));
BDspeaking = ImageIO.read(new File("assets/scripts/BDspeaking.png"));
healText = new Scanner(new File("assets/scripts/heal_script.txt"));
// BATTLE.JAVA
// Menu / Buttons
menuImages[1] = ImageIO.read(new File("assets/battleImages/menus/menu1.png"));
menuImages[2] = ImageIO.read(new File("assets/battleImages/menus/menu2.png"));
menuImages[3] = ImageIO.read(new File("assets/battleImages/menus/menu3.png"));
selectionImages[1] = ImageIO.read(new File("assets/battleImages/options/option1.png"));
selectionImages[2] = ImageIO.read(new File("assets/battleImages/options/option2.png"));
selectionImages[3] = ImageIO.read(new File("assets/battleImages/options/option3.png"));
player = ImageIO.read(new File("assets/battleImages/characters/player.png"));
bar = ImageIO.read(new File("assets/battleImages/menus/bar.jpg"));
bone = ImageIO.read(new File("assets/battleImages/attacks/bone.png"));
bone2 = ImageIO.read(new File("assets/battleImages/attacks/bone2.png"));
BD = ImageIO.read(new File("assets/battleImages/characters/boss.png"));
brokenHeart = ImageIO.read(new File("assets/battleImages/characters/deadPlayer.png"));
gameOver = ImageIO.read(new File("assets/battleImages/menus/gameOver.png"));
gameOver2 = ImageIO.read(new File("assets/battleImages/menus/gameOver2.png"));
deadBoss = ImageIO.read(new File("assets/battleImages/menus/deadBoss.png"));
// slash animation
slashImage[0] = ImageIO.read(new File("assets/battleImages/slash/slash1.1.png"));
slashImage[1] = ImageIO.read(new File("assets/battleImages/slash/slash2.1.png"));
slashImage[2] = ImageIO.read(new File("assets/battleImages/slash/slash3.1.png"));
slashImage[3] = ImageIO.read(new File("assets/battleImages/slash/slash4.1.png"));
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
// Register the font
ge.registerFont(speakingFont);
}
catch (Exception e) {
System.out.println("Image does not exist");
}
// Create thread
thread = new Thread(this);
thread.start();
// Add listeners
addKeyListener(this);
this.setFocusable(true);
addMouseListener(this);
// Create boundaries & exits
for (int i = 1; i < 5; i++) {
ruinsBounds[i] = new ArrayList<dimension>();
ruinsExits[i] = new ArrayList<dimension>();
snowdenBounds[i] = new ArrayList<dimension>();
snowdenExits[i] = new ArrayList<dimension>();
}
floweyBounds[1] = new ArrayList<dimension>();
floweyExits[1] = new ArrayList<dimension>();
for (int i = 1; i < 3; i++) { // map
for (int x = 0; x < 3; x++) { // setting
allBounds[i][x] = new ArrayList<dimension>();
allExits[i][x] = new ArrayList<dimension>();
}
}
// Positions of Chara when entering a new setting
// ruins1
allPos[1][1][1] = new corner(480, 220); // Initial position after menu
allPos[1][1][2] = new corner(15, 340); // In front of exit
// ruins2
allPos[1][2][1] = new corner(900, 255); // In front of entrance
allPos[1][2][2] = new corner(130, 185); // In front of exit
// ruins3
allPos[1][3][1] = new corner(85, 240); // In front of entrance
allPos[1][3][2] = new corner(870, 415); // In front of exit
// ruins4
allPos[1][4][1] = new corner(475, 450); // In front of entrance
allPos[1][4][2] = new corner(470, 80); // In front of exit
// snowden1
allPos[2][1][1] = new corner(140, 400); // In front of entrance
allPos[2][1][2] = new corner(905, 400); // In front of exit
// snowden2
allPos[2][2][1] = new corner(10, 360); // In front of entrance
allPos[2][2][2] = new corner(930, 345); // In front of exit
// snowden3
allPos[2][3][1] = new corner(920, 380); // In front of entrance
allPos[2][3][2] = new corner(60, 155); // In front of exit
// snowden4
allPos[2][4][1] = new corner(0, 265); // In front of entrance
allPos[2][4][2] = new corner(865, 140); // In front of exit
// Flowey
allPos[3][1][1] = new corner(450, 120); // In front of entrance
allPos[3][1][2] = new corner(0,0); // There no other settings beyond this, so no exit
// Adding boundaries
// ruins1
ruinsBounds[1].add(new dimension(new corner (135, -10), new corner (800, 375)));
ruinsBounds[1].add(new dimension(new corner(-35, 260), new corner(135, 375)));
ruinsExits[1].add(new dimension(new corner(1000, 625), new corner(1000, 625))); // there is no entrance so set to max val
ruinsExits[1].add(new dimension(new corner(-35, 260), new corner(-35, 375))); // exit
// ruins2
ruinsBounds[2].add(new dimension(new corner (45, 175), new corner (940, 300)));
ruinsBounds[2].add(new dimension(new corner (100, 150), new corner (170, 175)));
ruinsExits[2].add(new dimension(new corner(940,175), new corner(940, 300))); // entrance
ruinsExits[2].add(new dimension(new corner(100,150), new corner(170, 150))); // exit
// ruins3
ruinsBounds[3].add(new dimension(new corner(55, 180), new corner(135,285)));
ruinsBounds[3].add(new dimension(new corner(135, 125), new corner(765,460)));
ruinsBounds[3].add(new dimension(new corner(765, 355), new corner(900,460)));
ruinsExits[3].add(new dimension(new corner(55,180), new corner(55,285))); // entrance
ruinsExits[3].add(new dimension(new corner(900,355), new corner(900,460))); // exit
// ruins4
ruinsBounds[4].add(new dimension(new corner(450, 275), new corner(495,480)));
ruinsBounds[4].add(new dimension(new corner(250, 270), new corner(695,280)));
ruinsBounds[4].add(new dimension(new corner(395, 60), new corner(550,270)));
ruinsBounds[4].add(new dimension(new corner(465,40), new corner(475,60)));
ruinsExits[4].add(new dimension(new corner(445,480), new corner(500,480))); // entrance
ruinsExits[4].add(new dimension(new corner(465,40), new corner(475, 40))); // exit
// snowden1
snowdenBounds[1].add(new dimension(new corner(210, 265), new corner(365, 320)));
snowdenBounds[1].add(new dimension(new corner(210, 320), new corner(945, 470)));
snowdenBounds[1].add(new dimension(new corner(90, 370), new corner(210, 470)));
snowdenExits[1].add(new dimension(new corner(90, 370), new corner (90, 415))); // entrance
snowdenExits[1].add(new dimension(new corner(945, 320), new corner(945, 470))); // exit
// snowden2
snowdenBounds[2].add(new dimension(new corner(-25, 250), new corner(950, 445)));
snowdenBounds[2].add(new dimension(new corner(70, 0), new corner(605, 250)));
snowdenBounds[2].add(new dimension(new corner(605, 180), new corner(820, 250)));
snowdenExits[2].add(new dimension(new corner(-25, 250), new corner(-25, 445))); // entrance
snowdenExits[2].add(new dimension(new corner(950, 250), new corner(950, 445))); // exit
// snowden3
snowdenBounds[3].add(new dimension(new corner(805, 310), new corner(945, 400)));
snowdenBounds[3].add(new dimension(new corner(525, 160), new corner(805, 400)));
snowdenBounds[3].add(new dimension(new corner(125, 250), new corner(525, 400)));
snowdenBounds[3].add(new dimension(new corner(0, 160), new corner(390, 250)));
snowdenBounds[3].add(new dimension(new corner(45, 130), new corner(75, 160)));
snowdenExits[3].add(new dimension(new corner(945, 310), new corner(945, 400))); // entrance
snowdenExits[3].add(new dimension(new corner(45, 130), new corner(75, 130))); // exit
// snowden4
snowdenBounds[4].add(new dimension(new corner(-40, 185), new corner(355,290)));
snowdenBounds[4].add(new dimension(new corner(65, 10), new corner(355,290)));
snowdenBounds[4].add(new dimension(new corner(355, 130), new corner(450,140)));
snowdenBounds[4].add(new dimension(new corner(455, 130), new corner(495, 140)));
// add this boundary below when the boss is beat so the player can progress
//snowdenBounds[4].add(new dimension(new corner(495, 130), new corner(880, 140)));
snowdenBounds[4].add(new dimension(new corner(880, 130), new corner(910,155)));
snowdenBounds[4].add(new dimension(new corner(880, 155), new corner(895,175)));
snowdenBounds[4].add(new dimension(new corner(880, 175), new corner(910,280)));
snowdenExits[4].add(new dimension(new corner(-40, 190), new corner(-40, 305))); // entrance
snowdenExits[4].add(new dimension(new corner(910, 130), new corner(910, 155))); // exit
// Flowey
floweyBounds[1].add(new dimension(new corner(10, 15), new corner(300, 495)));
floweyBounds[1].add(new dimension(new corner(300, 110), new corner(615, 245)));
floweyBounds[1].add(new dimension(new corner(300, 245), new corner(380, 430)));
floweyBounds[1].add(new dimension(new corner(535, 245), new corner(615, 430)));
floweyBounds[1].add(new dimension(new corner(300, 430), new corner(615, 495)));
floweyBounds[1].add(new dimension(new corner(615, 15), new corner(910, 495)));
floweyBounds[1].add(new dimension(new corner(440, 75), new corner(470, 110)));
floweyExits[1].add(new dimension(new corner(1, 1), new corner(1,1)));
floweyExits[1].add(new dimension(new corner(440, 75), new corner(470,75)));
// Putting them all in a list
allBounds[1] = ruinsBounds;
allBounds[2] = snowdenBounds;
allBounds[3] = floweyBounds;
allMaps[1] = ruinsImages;
allMaps[2] = snowdenImages;
allMaps[3] = flowey;
allExits[1] = ruinsExits;
allExits[2] = snowdenExits;
allExits[3] = floweyExits;
// Coords to move maps
moveMap[1][3] = new dimension(new corner(0, 290), new corner(0, 900));
moveMap[1][4] = new dimension(new corner(0, 435), new corner(0, 900));
moveMap[2][1] = new dimension(new corner(370, 0), new corner(1200, 0));
moveMap[2][4] = new dimension(new corner(445, 0), new corner(1115, 0));
// Slash animation coords
slashCoords[0] = new corner(410, 0);
slashCoords[1] = new corner(410, 40);
slashCoords[2] = new corner(400, 70);
slashCoords[3] = new corner(410, 70);
// Interactables
// ruins
allInteractables[1][3] = new dimension(new corner(230, 860), new corner(355, 970)); // the bottom red bush, use globalPos
// snowden
allInteractables[2][1] = new dimension(new corner(210, 265), new corner(345, 265)); // the bush
allInteractables[2][2] = new dimension(new corner(705, 180), new corner(770, 180)); // sans
allInteractables[2][3] = new dimension(new corner(390, 195), new corner(525, 250)); // clarence (the snowman)
allInteractables[2][4] = new dimension(new corner(1225, 130), new corner(1225, 140)); // the boss, must use globalPos
allInteractables[3][1] = new dimension(new corner(380, 245), new corner(535, 430)); // the trophy
// scripts (one indexed)
startScript = new boolean[4][5];
startScript[1] = new boolean[] {false, true, false, true, false};
startScript[2] = new boolean[] {false, true, true, true, true};
startScript[3] = new boolean[] {false, true, false, false, false};
for (int i = 1; i < 4; i++) {
for (int x = 1; x < 5; x++) {
try {
allScripts[i][x] = new ArrayList<String>();
Scanner currentFile = new Scanner(new File("assets/scripts/" + i + "_" + x + "_script.txt"));
while (currentFile.hasNextLine()){
allScripts[i][x].add(currentFile.nextLine());
}
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
}
}
}
while (healText.hasNextLine()) {
healScript.add(healText.nextLine());
}
// Font
try {
damageFont = Font.createFont(Font.TRUETYPE_FONT, new File("assets/fonts/DTM-Sans.ttf")).deriveFont(50f);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
//register the font
ge.registerFont(damageFont); }
catch (IOException | FontFormatException e) {
e.printStackTrace(); }
// Use the font
this.setFont(damageFont);
// Menu options
optionPos[1] = new corner(235, 539);
optionPos[2] = new corner(442, 539);
optionPos[3] = new corner(650, 539);
// Boundaries when in battle
gameBounds.add(new dimension(new corner(310, 295), new corner(655,445)));
}
public static void main(String[] args) {
JFrame frame = new JFrame("UNDERTALE");
undertale panel = new undertale();
frame.add(panel);
frame.pack();
frame.setVisible(true);
frame.setResizable(false);
}
/*
* The method that paints the screen accordingly to each game state or
* if we are in battle mode. It also paints the dialogue for the game.
*
*
*/
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, animation.alpha));
if (!battling) {
// When the boss has been defeated, the program should sleep for two seconds
// as an image is drawn with text
if (bossDefeated) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
audio.x = 1;
bossDefeated = false;
}
// Playing the music of each map
try {
audio.playMusic(gameState);
} catch (LineUnavailableException | UnsupportedAudioFileException | IOException e1) {
e1.printStackTrace();
}
// Whenever interacting with an object or person, the footstep sound should stop
// **When going through the gate to the last room, the footsteps continuously play,
// **so this is needed
if(gameState == 3 && audio.footP || (grabbedHeal && audio.footP)) {audio.clip2.stop(); audio.footP = false; System.out.println("lionel x roman");}
// Fade
if (animation.fading) {
if (!animation.faded) {
g2d.drawImage(fadeStart, mapX, mapY, null);
// fade out the character as well
if (gameState - 1 != 0 && 1 <= gameState && gameState <= 3) {
g2d.drawImage(charaImages[curChara], charaX, charaY, null);
}
} else {
// change chara position
if(gameState != 0) {
charaX = allPos[gameState][setting][change].x;
charaY = allPos[gameState][setting][change].y;
}
// Used to determine if map needs to move at a certain point
// if Chara spawns at the bottom
if (charaY > 312 && gameState == 1 && setting == 3) {
globalPos = 1025;
// Globalpos in ruins3 should be tracking her Y-value
} else if (charaY < 312 && gameState == 1 && setting == 3) {
globalPos = charaY;
}
// if Chara spawns in the lower half of ruins4
else if (charaY > 312 && gameState == 1 && setting == 4) {
globalPos = 1075;
}
// if Chara spawns in the upper half of ruins4
else if (charaY < 312 && gameState == 1 && setting == 4) {
globalPos = 240;
}
// if Chara spawns on the right side of any snowden map
// besides the fourth
else if (gameState == 2 && charaX > 625 && setting != 4) {
globalPos = (1250 - (640 - charaX));
}
// if Chara spawns on the left side of any snowden map
// besides the fourth
else if (gameState == 2 && charaX < 625 && setting != 4) {
globalPos = charaX;
}
g2d.drawImage(fadeEnd, mapX, mapY, null);
// fade in the character as well
if (1 <= gameState && gameState <= 3) {
g2d.drawImage(charaImages[curChara], charaX, charaY, null);
}
}
}
// draw about screen
else if(about) {
g2d.drawImage(aboutScreen, 0, 0, null);
}
// the start menu
else if (gameState == 0 && !about) {
g2d.drawImage(titleScreen, 0, 0, null);
}
// exploration
else if (1 <= gameState && gameState <= 3) {
// snowden4 has the boss in it, so he should disappear when defeated
if(winner) {allMaps[2][4] = replace;}
g2d.drawImage(allMaps[gameState][setting], mapX, mapY, null);
// animation
try {
Thread.sleep(60);
g2d.drawImage(charaImages[curChara], charaX, charaY, null);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
else if (battling) {
// font
g.setColor(new Color(255, 255, 255));
g.setFont(speakingFont);
// menu background
if (battle.menuState <= 3 || (battle.menuState == 5 && !winner)) {
if (battle.menuState != 5) {
g.drawImage(menuImages[battle.menuState], 0, 0, null);
}
// needed so screen does not flicker when the boss is attacked
else {
g.drawImage(menuImages[2], 0, 0, null);
}
g.drawImage(BD, 430, 85, null);
}
// stats
if (!(battle.menuState > 3) || (battle.menuState == 5 && !winner)) {
g.drawString(battle.health + "", 345, 518);
g.drawString("50", 425, 518);
g.drawString(heals + "", 645, 518);
g.drawString("5", 710, 518);
g.drawString("BOSS HEALTH: ", 385, 75);
g.drawString(battle.bossHealth + "", 575, 75);
}
// if u lose or win
if (battle.menuState > 3 || battle.slashDraw) {
// lose
if (battle.menuState == 4) {
// We use if statements to ensure that the order of
// execution is correct as the thread.sleep causes
// issues with the order. Menus / Text need to be redrawn
// as the text for HP freezes at "1", then the sleep runs.
if (z == 0) {
if (battle.x == 0) {
// redrawing all the background images
g.drawImage(menuImages[3], 0, 0, null);
g.drawImage(BD, 430, 85, null);
g.drawString(battle.health + "", 345, 518);
g.drawString("50", 425, 518);
g.drawString(heals + "", 645, 518);
g.drawString("5", 710, 518);
g.drawString("BOSS HEALTH: ", 385, 75);
g.drawString(battle.bossHealth + "", 575, 75);
battle.x++;
System.out.println("runed");
}
else if (battle.x == 1) {
// clearing the screen for heart crack (dying screen)
super.paintComponent(g);
g.drawImage(brokenHeart, battle.playerX, battle.playerY, null);
battle.x++;
System.out.println("runed2");
}
else if (battle.x == 2) {
battle.x++;
battle.dead = true;
System.out.println("runed3");
}
}
// when player dies
if (battle.dead) {
// over makes sures that no key inputs work until the text
// "press z to continue" appears
over = true;
try {
audio.playMusic(gameState);
} catch (LineUnavailableException | UnsupportedAudioFileException | IOException e1) {
e1.printStackTrace();
}
try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
battle.dead = false;
}
// z controls when inputs work
else if (battle.x == 3) {
z++;
if (z < 30) {
g.drawImage(gameOver2, 0, 0, null);
}
if (z >= 30) {
g.drawImage(gameOver, 0, 0, null);
over = false;
}
}
}
// When dealing damage to the boss, it will create
// a slash animation. To do so, you have to redraw
// all the images and the slash images
else if (battle.slashDraw) {
if (battle.num <= 3) {
g.drawImage(menuImages[2], 0, 0, null);
g.drawImage(bar, battle.barX, 314, null);
g.drawImage(BD, 430, 85, null);
g.drawString(battle.health + "", 345, 518);
g.drawString("50", 425, 518);
g.drawString(heals + "", 645, 518);
g.drawString("5", 710, 518);
g.drawString("BOSS HEALTH: ", 385, 75);
g.drawString(battle.bossHealth + "", 575, 75);
g.drawString("MINUS " + battle.damage + " HP", 620, 150);
// hit sound
if (battle.playHit) {
try {
audio.hitSound(battle.damage);
} catch (LineUnavailableException | UnsupportedAudioFileException | IOException e1) {
e1.printStackTrace();
}
battle.playHit = false;
}
g.drawImage(slashImage[battle.num], slashCoords[battle.num].x, slashCoords[battle.num].y, null);
}
// Reset variable values to ensure
// that the animation can be reused
else {
if (battle.menuState != 5) {
g.drawImage(bar, battle.barX, 314, null);
}
battle.slashDraw = false;
battle.slash = false;
battle.num = 0;
System.out.println("bared: " + battle.barX);
}
// If the boss is defeated
if (battle.menuState == 5 && !battle.slashDraw) {
// specific image of boss dead
g.drawImage(deadBoss, 0, 0, null);
// make sure that you cannot fight the boss again
allInteractables[2][4] = new dimension(new corner(-1, -1), new corner(-1, -1));
snowdenBounds[4].add(new dimension(new corner(495, 130), new corner(880, 140)));
battling = false;
// both variables indicate that the user has won
// winner is for interactive purposes and ensuring
// that certain images are not drawn
winner = true;
// bossDefeated is specifically used for making the
// program sleep at a certain point
bossDefeated = true;
}
}
}
else {
// highlight selection
if (battle.menuState == 1) {
g.setFont(speakingFont);
if (battle.textState == 1) {
g.drawString("What will Chara do?", 75, 360);
}
// text that will be displayed when using a heal
else if (battle.textState == 2) {
if (battle.health == 50) {
g.drawString("You are MAX HP!", 75, 360);
g.drawString("Press 'Z' again to continue", 75, 390);
}
else if (heals == 0) {
g.drawString("You are out of heals!", 75, 360);
g.drawString("Press 'Z' again to continue", 75, 390);
}
else {
g.drawString("You healed 10 HP! You have " + heals + " left", 75, 360);
g.drawString("Press 'Z' again to continue", 75, 390);
}
}
// fleeing
else if (battle.textState == 3) {
g.drawString("You fled the scene", 75, 360);
g.drawString("Press 'Z' again to continue", 75, 390);
}
g.drawImage(selectionImages[battle.selectionState], optionPos[battle.selectionState].x, optionPos[battle.selectionState].y, null);
}
// screen with the slider
else if (battle.menuState == 2) {
// Reset bar values
if (battle.count == 0) {
battle.count = 1;
battle.barX = 60;
battle.damage = 0;
battle.stopBar = false;
}
// when bar has been stopped
if (battle.stopBar) {
try {
System.out.println("damaged");
Thread.sleep(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
// deciding the variation of the attack (coming from left or right)
if (Math.random() >= 0.50)
battle.variation = 1;
else battle.variation = 2;
System.out.println("variation = " + battle.variation);
// deciding which attack
if (battle.attack == 1) {
firstAttack();
}
else if (battle.attack == 2) {
secondAttack();
}
battle.menuState = 3;
battle.stopBar = false;
}
// condition is to ensure that the bar is not drawn
// when using a heal
else if (battle.selectionState != 2) {
g.drawImage(bar, battle.barX, 314, null);
// stopping the bar when it has reached the end
// or user has clicked space
if (battle.barX >= 920 || battle.damage >= 1) {
battle.stopBar = true;
}
// if bar has not been stopped, add 20 to its x-value
if (!battle.stopBar) {
battle.barX += 20;
}
}
}
// the battle itself
else if (battle.menuState == 3) {
g.drawImage(player, battle.playerX, battle.playerY, null);
// drawing the attacks
if (battle.attack == 1) {
for (int i = 1; i < 11; i++) {
corner cur = bonePositions[i];
if (297 < cur.x && cur.x < 670) {
g.drawImage(bone, cur.x, cur.y, null);
}
}
}
else if (battle.attack == 2) {
for (int i = 1; i < 11; i++) {
corner cur = bonePositions2[i];
if (battle.variation == 1) {
if (cur.x < 680) {
g.drawImage(bone2, cur.x, cur.y, null);
}
}
else {
if (cur.x > 297) {
g.drawImage(bone2, cur.x, cur.y, null);
}
}
}
}
}
}
}
// put this at the bottom because game must draw first and then dialogue
/*
* This if statement checks if there is dialogue on the screen.
* If there is, it will draw a box and the dialogue from the appropriate text-file
* from gameState and setting.
*/
if (dialogue.speaking && !animation.fading && 1 <= gameState && gameState <= 3) {
g.setColor(new Color(255, 255, 255));
g.setFont(speakingFont);
g.drawImage(dialogueBox, 40, 420, null);
// orion appears every time gameState is odd
// nicky appears every time gameState is even
// they appear everywhere except for
// gameState 2, settings 2-4
if (!(gameState == 2 && (setting != 1))) {
if (setting == 1) g.drawImage(orion, 53, 430, null);
else if (setting == 3 || setting == 4) g.drawImage(nicky, 53, 430, null);
}
else if (gameState == 2 && setting == 2)
g.drawImage(sans, 100, 430, null);
else if (gameState == 2 && setting == 3)
g.drawImage(clarence, 100, 450, null);
else if (gameState == 2 && setting == 4)
g.drawImage(BDspeaking, 53, 430, null);
for (int i = 0; i < 3; i++) {
// text when grabbing a heal
if (grabbedHeal) {
g.drawString(healScript.get(i), 300, 485);
g.drawString("You now have " + heals + ((heals > 1) ? " heals." : " heal."), 300, 485 + 40);
}
// text when grabbing the trophy
else if (ended) {
g.drawString("You got a #1 Trophy!", 300, 485);
g.drawString("Thank you for playing our game.", 300, 525);
}
else if (i + dialogue.startLine < allScripts[gameState][setting].size()) {
g.drawString(allScripts[gameState][setting].get(i + dialogue.startLine), 300, 485 + 40 * i);
}
else {
dialogue.startLine = 0;
dialogue.speaking = false;
break;
}
}
// turn starting scripts off
if (startScript[gameState][setting]) {
startScript[gameState][setting] = false;
}
}
}
/*
* The keyboard method that updates the player's position during exploration.
* Or if battle, it will call a keyboard method in battle.java that pretty much
* does the same thing but it adjusted to battle conditions
*/
@Override
public void keyPressed(KeyEvent e) {
xe = 0;
// Over is only true when the player dies in the boss fight
// The losing screen makes the program freeze, so no inputs
// should work during this time
if(!over) {xe = e.getKeyCode();}
// Key code for Chara's animation
int x = e.getKeyCode();
if(x >= 37 && x <= 40) {charaAnimation.x = e.getKeyCode();}
if(!battling) {
System.out.println("key has pressed");
if (!animation.fading && 1 <= gameState && gameState <= 3) {
// change animation to run method as it depends on each click
// for animation to change directions
if (dialogue.speaking) {
System.out.println("dialg");
dialogue.keyPressed(e);
}
// when playing clicks the interact button on a interactable that has not been used
else if ((e.getKeyChar() == 'z' || e.getKeyChar() == 'Z') && !visitedInteractables[gameState][setting]) {
// stop footstep audio if its playing
if(audio.footP) {audio.footS = true;}
try {
audio.footstep(gameState);
}
catch (LineUnavailableException | UnsupportedAudioFileException | IOException e1) {
e1.printStackTrace();
}
if (allInteractables[gameState][setting] != null) {
int newX = charaX;
int newY = charaY;
if (gameState == 1 && setting == 3) {
newY = globalPos;
}
else if (gameState == 2 && setting == 4) {
newX = globalPos;
}
if (interact(newX, newY, allInteractables[gameState][setting])) {
System.out.println("interacted");
if(!(!winner && gameState == 2 && setting == 4)){
visitedInteractables[gameState][setting] = true;
}
if (!battling)
dialogue.speaking = true;
}
}
}
else {
// movement
if(e.getKeyCode() == 38)