Commit 62b20791 authored by Lockhead's avatar Lockhead

Create material system, which will later be used for crafting

parent a2371e76
......@@ -11,16 +11,14 @@ import nl.lockhead.oddfolk.game.world.items.impl.Category;
import nl.lockhead.oddfolk.game.world.items.impl.Item;
import nl.lockhead.oddfolk.game.world.tiles.Tile;
import java.awt.*;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import static nl.lockhead.oddfolk.game.utilities.Tools.getColorFrom;
public final class Importer {
public static final int VERSION = 1;
public static final int VERSION = 3;
private static Importer instance;
private final LPFLogger logger;
......@@ -75,7 +73,7 @@ public final class Importer {
b.setGenerateTint(e.getAsInt());
break;
case "structures":
b.getStructures().addAll(List.of(importStructure(e)));
b.getStructures().addAll(Arrays.asList(importStructure(e)));
break;
case "name":
b.setName(e.getAsString());
......@@ -85,11 +83,22 @@ public final class Importer {
break;
}
}
//TODO rewrite json file with correct version
writeJson(file, jObject);
return b;
}
private void writeJson(File file, JsonElement jsonElement) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
FileManager.writeToFile(file, gson.toJson(jsonElement), true);
}
public AreaStructure[] importStructure(JsonElement element) {
if (!element.isJsonArray()) {
JsonArray a = new JsonArray();
a.add(element);
element = a;
//lol
}
List<AreaStructure> structureList = new ArrayList<>();
try {
for (JsonElement json : element.getAsJsonArray()) {
......@@ -169,10 +178,16 @@ public final class Importer {
List<WorldObject> objectList = new ArrayList<>();
String name = file.getName();
JsonElement element = parser.parse(FileManager.readFile(file));
boolean v = hasCorrectVersion(name, element);
boolean v = false;
if (!element.isJsonArray()) {
JsonArray a = new JsonArray();
a.add(element);
element = a;
//lol
}
for (JsonElement json : element.getAsJsonArray()) {
try {
v = hasCorrectVersion(name, element);
String s;
WorldObject wo;
switch (s = json.getAsJsonObject().get("type").getAsString().toLowerCase()) {
......@@ -195,46 +210,55 @@ public final class Importer {
e.printStackTrace();
}
}
//TODO rewrite json file with correct version
if (!v)
writeJson(file, element);
return objectList.toArray(new WorldObject[0]);
}
private Tile getTile(JsonObject json) { //TODO make sure this is functional
private Tile getTile(JsonObject json) {
Tile t = new Tile();
for (Map.Entry<String, JsonElement> entry : json.entrySet()) {
JsonElement e = entry.getValue();
switch (entry.getKey()) {
case "id":
t.setId(e.getAsString());
json.addProperty("id", t.getId());
break;
case "name":
t.setName(e.getAsString());
json.addProperty("name", t.getName());
break;
case "description":
t.setDescription(e.getAsString());
json.addProperty("description", t.getDescription());
break;
case "color":
case "colour":
t.setColor(getColorFrom(e.getAsString()));
break;
case "volume":
t.setVolume(e.getAsFloat());
json.addProperty("volume", t.getVolume());
break;
case "weight":
t.setWeight(e.getAsFloat());
json.addProperty("weight", t.getWeight());
break;
case "hardness":
t.setHardness(e.getAsFloat());
json.addProperty("hardness", t.getHardness());
break;
case "symbol":
t.setSymbol(e.getAsCharacter());
json.addProperty("symbol", t.getSymbol());
break;
case "category":
t.setCategory(Category.fromString(e.getAsString()));
json.addProperty("category", t.getCategory().toString());
break;
case "flags":
JsonArray array = e.getAsJsonArray();
array.forEach(a -> t.getFlags().add(Flag.from(a.getAsString())));
json.add("flags", new Gson().toJsonTree(t.getFlags()));
break;
}
}
......@@ -247,35 +271,48 @@ public final class Importer {
switch (entry.getKey()) {
case "id":
i.setId(e.getAsString());
json.addProperty("id", i.getId());
break;
case "name":
i.setName(e.getAsString());
json.addProperty("name", i.getName());
break;
case "description":
i.setDescription(e.getAsString());
json.addProperty("description", i.getDescription());
break;
case "color":
case "colour":
i.setColor(getColorFrom(e.getAsString()));
break;
case "volume":
i.setVolume(e.getAsFloat());
json.addProperty("volume", i.getVolume());
break;
case "weight":
i.setWeight(e.getAsFloat());
json.addProperty("weight", i.getWeight());
break;
case "hardness":
i.setHardness(e.getAsFloat());
json.addProperty("hardness", i.getHardness());
break;
case "symbol":
i.setSymbol(e.getAsCharacter());
json.addProperty("symbol", i.getSymbol());
break;
case "category":
i.setCategory(Category.fromString(e.getAsString()));
json.addProperty("category", i.getCategory().toString());
break;
case "materials":
e.getAsJsonArray().forEach(s -> i.getMaterials().add(s.getAsString()
.toLowerCase()
.replaceAll("\\W", "")));
json.add("materials", new Gson().toJsonTree(i.getMaterials()));
break;
case "flags":
JsonArray array = e.getAsJsonArray();
array.forEach(a -> i.getFlags().add(Flag.from(a.getAsString())));
e.getAsJsonArray().forEach(a -> i.getFlags().add(Flag.from(a.getAsString())));
json.add("flags", new Gson().toJsonTree(i.getFlags()));
break;
default:
i.getMeta().put(entry.getKey(), e.getAsString());
......@@ -284,30 +321,23 @@ public final class Importer {
return i;
}
private static Color getColorFrom(String s) {
Color c;
if (s.matches("\\d+,\\d+,\\d+")) {
String[] ss = s.split(",");
c = new Color(Integer.parseInt(ss[0]), Integer.parseInt(ss[1]), Integer.parseInt(ss[2]));
}
else c = Color.getColor(s);
return c;
}
private boolean hasCorrectVersion(String name, JsonElement json) {
try {
int version = json.getAsJsonObject().get("v").getAsInt();
if (version < VERSION) {
json.getAsJsonObject().addProperty("v", VERSION);
logger.warning(String.format("%s: outdated format, version %s is older than importer version %s " +
"-- importing might go wrong.",
name, version, VERSION));
json.getAsJsonObject().addProperty("v", VERSION);
logger.warning("version has been applied -- json file will be reformatted");
return false;
} else if (version > VERSION) {
logger.warning(String.format("%s: outdated importer, version %s is newer than importer version %s " +
"-- importing might go wrong.",
name, version, VERSION));
logger.warning("version has been applied -- json file will be reformatted");
return false;
}
return true;
......@@ -315,6 +345,8 @@ public final class Importer {
logger.warning(String.format("%s: no version found, version %s will be added automatically " +
"-- importing might go wrong",
name, VERSION));
json.getAsJsonObject().addProperty("v", VERSION);
logger.warning("version has been applied -- json file will be reformatted");
return false;
}
}
......
......@@ -19,8 +19,9 @@ import java.util.stream.Collectors;
public class Item extends WorldObject {
// 1 = pristine, 0 = ruined
protected Map<String, Object> meta = new LinkedHashMap<>();
protected Map<String, Object> meta;
private transient Container container;
protected List<String> materials;
@Override
public String getDetailedDesc() {
......@@ -34,8 +35,11 @@ public class Item extends WorldObject {
volume = 1;
hardness = 0.5f;
condition = 1;
materials = new ArrayList<>();
meta = new LinkedHashMap<>();
}
public Item() {
this(GameScreen.getWorld());
}
......@@ -133,6 +137,10 @@ public class Item extends WorldObject {
// }
}
public List<String> getMaterials() {
return materials;
}
public Container getContainer() {
return container;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment