-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaterialManager.java
178 lines (117 loc) · 5.52 KB
/
MaterialManager.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
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
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import java.util.ArrayList;
/** Manages loading all material types as well as their respective getters and setters */
public class MaterialManager {
/** The list of all known material types */
public ArrayList<Material> MATERIALS;
public ArrayList<Tile> TILES = new ArrayList<>(300);
/** Relative Path to the resource file containing the material types */
private String filePath;
/** Default constructor which will load resources from the default save file */
public MaterialManager(String filePath) {
// load default file path
this.filePath = filePath;
// load materials from file
MATERIALS = new ArrayList<Material>(1);
}
/** Adds a new Material at it's assigned position as defined by the material's ID */
public void addMaterial(Material material) {
// Should the Material Collection not have enough slots, add new ones
if (MATERIALS.size()<=material.id) {
for (int size = MATERIALS.size(); size <= material.id; size++) {
MATERIALS.add(new Material(size));
}
}
// Add new Material type at assigned ID and give progress feedback
MATERIALS.set(material.id, material);
System.out.print('.');
}
/** Will parse a JSON file and add all contained material types to the ArrayList. Uses the default file. */
public void loadMaterials() {
loadMaterials(this.filePath);
}
/** Will parse a JSON file and add all contained material types to the ArrayList. Asks for a specific file to use. */
public void loadMaterials(String filePath){
// Try to read a .JSON file and return its parsed content as JSONObject
JSONObject jsonObject = General.getJSONfromFile(filePath);
// Iterate over Materials in JSON Object
JSONArray Materials = (JSONArray) jsonObject.get("material");
for (Object material : Materials) {
// Get JSONObject from current Material Object
JSONObject currentMaterial = (JSONObject) material;
// Get Material's ID
long id = (long) currentMaterial.get("id");
// Create new Material Instance
Material newMaterial = new Material((int) id);
// Get material's Name & Apply
newMaterial.setName((String) currentMaterial.get("name"));
// Get material's isSolid & Apply
newMaterial.setIsSolid((boolean) currentMaterial.get("isSolid"));
// Get array of Material's Tiles
JSONArray tileIDs = (JSONArray) currentMaterial.get("tiles");
// Iterate over TileIDs & add them
for (Object tile : tileIDs) {
newMaterial.addTile(TILES.get(Integer.parseInt(tile + "")));
}
// Add the new Material to the Material Manager
addMaterial(newMaterial);
}
// All Materials have been parsed and added to the Material Manager
System.out.println(" Done");
}
/** Will create a JSON file from the Material ArrayList and write it into a file. Uses the default file. */
public void saveMaterials() {
// Calls overloaded method but with default file path as none was given
saveMaterials(this.filePath);
}
/** Will create a JSON file from the Material ArrayList and write it into a file. Asks for a specific file to use. */
public void saveMaterials(String filePath) {
// Create a new dummy JSONObject
JSONObject json = new JSONObject();
// Create JSONArray for all Materials
JSONArray material = new JSONArray();
// Insert Material Data into JSON
for (Material currentMaterial : MATERIALS ) {
// Create JSON Objects for each material type
JSONObject singleMaterial = new JSONObject();
singleMaterial.put("id", currentMaterial.id);
singleMaterial.put("name", currentMaterial.name);
singleMaterial.put("isSolid", currentMaterial.isSolid);
// Create a new JSON Array for Tiles
JSONArray tilesArray = new JSONArray();
for (Tile tile : currentMaterial.tiles)
tilesArray.add(tile.getID());
singleMaterial.put("tiles", tilesArray);
// Insert Array for Material's Values into Material's Object
// singleMaterial.put("", singleMaterialArray);
// Insert the current Material's Object into the Material's Array
material.add(singleMaterial);
json.put("material", material);
System.out.print('.');
}
// Write JSON Object to file
General.writeJSONtoFile(json, filePath);
// All Material Data has been saved to the JSON File
System.out.println(" Done");
}
/** Returns a single Material's Image by its ID */
public Tile getTileByID(int tileID) {
// TODO: sanity checks
return TILES.get(tileID);
}
/** Imports all the Material's Images from a single PNG file */
public void loadTiles(String filePath) {
// TODO: IMPLEMENT ME PLZ
TILES = new ArrayList<Tile>(300);
System.out.println("Loading Tile Source");
TileSource tileSource = new TileSource(filePath);
for (int id = 1; id<300; id++) {
Tile tile = new Tile(tileSource.getTile(id), id);
while (TILES.size() <= id) {
TILES.add(tile);
}
TILES.set(id, tile);
}
}
}