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