-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.pde
106 lines (96 loc) · 2.93 KB
/
functions.pde
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
// Returns an instance of a cell based off
// its id
public Cell idToCell(int id, Position pos) {
switch(id) {
case 0:
return new ConstantCell(pos);
case 1:
return new SwitchCell(pos);
case 2:
return new CableCell(pos);
case 3:
return new InverterCell(pos);
case 4:
return new WirelessCableCell(pos);
default:
return null;
}
}
// Returns a id from an instance of a Cell
public int cellToId(Cell c) {
if (c instanceof ConstantCell)
return 0;
if (c instanceof SwitchCell)
return 1;
if (c instanceof CableCell) {
if (c instanceof WirelessCableCell)
return 4;
return 2;
}
if (c instanceof InverterCell)
return 3;
return -1;
}
public String idToCellName(int id) {
switch(id) {
case 0:
return "Constant Cell";
case 1:
return "Switch Cell";
case 2:
return "Cable Cell";
case 3:
return "Inverter Cell";
case 4:
return "WirelessCable Cell";
default:
return "?";
}
}
// Creates a game save, see save_layout.json to see an example output
public void createSave() {
// The Save data json as a whole, this is the one the contains everything and is actually saved
JSONObject saveData = new JSONObject();
// The jsons within saveData
JSONObject gridData = grid.toJSON();
JSONObject stateUpdaterData = new JSONObject();
// StateUpdater
stateUpdaterData.setFloat("stepsPerSec", stateUpdater.getStepsPerSec());
// Finally put all of this into saveData and save
saveData.setJSONObject("grid", gridData);
saveData.setJSONObject("stateUpdater", stateUpdaterData);
// TODO, save as compressed file not in raw json
saveJSONObject(saveData, "save.json");
loadSuccessfulDisplay.setTo("Saved to: " + new File(sketchPath() + "/save.json"));
}
// Button functions (clickEvent functions)
public void loadSave() {
selectInput("Select a save to load:", "loadSelected");
}
public void loadSelected(File fileSelected) {
if (fileSelected == null) {
println("No file selection made");
} else {
// Load save
println("Loading save: " + fileSelected.getAbsolutePath());
JSONObject saveData = loadJSONObject(fileSelected);
try {
grid.parseJSON(saveData.getJSONObject("grid"));
loadSuccessfulDisplay.setTo("Loaded: " + fileSelected);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
public void setSlowStepSpd() {
stateUpdater.stepsPerSec = 2;
}
public void setFastStepSpd() {
stateUpdater.stepsPerSec = 16;
}
// Toggles viewing of the help menu. This is run when the '?' button at the top right of the screen
// is pressed
public void toggleHelpMenu() {
helpMenu.setIsEnabled(!helpMenu.getIsEnabled());
}