-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoadSettings.java
executable file
·101 lines (87 loc) · 2.89 KB
/
LoadSettings.java
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
import greenfoot.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;
/**
* Load Screen for when people want to load their progress (coins,hp,level)
*
* @author Jimmy
* @version June 13 2024
*/
public class LoadSettings extends World {
private final Button load1 = new Button();
/**
* Constructor for objects of class LoadSettings.
*/
public LoadSettings() {
super(1280, 720, 1);
// Background
setBackground("WorldBackground.jpg");
// Title
Font font = new Font("Arial", 64);
SuperDisplayLabel titleLabel = new SuperDisplayLabel(Color.WHITE, Color.BLACK, font);
addObject(titleLabel, 600, 200);
GreenfootImage titleImage = new GreenfootImage("ArcaneOdysseyLogo.png");
titleLabel.setImage(titleImage);
titleLabel.setLocation(640, 150);
// Load Button 1
GreenfootImage loadImage1 = new GreenfootImage("saveFile1.png");
load1.setImage(loadImage1);
addObject(load1, 640, 300);
}
public void act() {
checkPressed();
}
private void checkPressed() {
if (Greenfoot.mouseClicked(load1)) {
int[] savedOptions = loadSave();
int savedHP = savedOptions[0];
int savedCoins = savedOptions[1];
int savedLevel = savedOptions[2];
if (savedLevel == 0) {
Level0 level = new Level0();
Greenfoot.setWorld(level);
level.setHP(savedHP);
level.setCoins(savedCoins);
}
if (savedLevel == 1) {
Level1 level = new Level1();
Greenfoot.setWorld(level);
level.setHP(savedHP);
level.setCoins(savedCoins);
}
}
}
public int[] loadSave() {
ArrayList<String> data = new ArrayList<String>();
Scanner scan = null;
try {
scan = new Scanner(new File("saveFile1.csv"));
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
while (scan.hasNextLine()) {
data.add(scan.nextLine());
}
int[] options = new int[3];
for (String line : data) {
StringTokenizer st = new StringTokenizer(line, ",");
ArrayList<String> lineData = new ArrayList<String>();
while (st.hasMoreTokens()) {
lineData.add(st.nextToken());
}
options[0] = (Integer.parseInt(lineData.get(0))); // totalHP
options[1] = (Integer.parseInt(lineData.get(1))); // totalCoins
options[2] = (Integer.parseInt(lineData.get(2))); // level
}
return options;
}
public void stopped() {
TitleScreen.stopBGM();
}
public void started() {
TitleScreen.playBGM();
}
}