DEADSOFTWARE

Fix json load
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / GameItems.java
1 package ru.deadsoftware.cavedroid.game;
3 import com.badlogic.gdx.Gdx;
4 import com.badlogic.gdx.graphics.Texture;
5 import com.badlogic.gdx.graphics.g2d.Sprite;
6 import com.badlogic.gdx.utils.ArrayMap;
7 import com.badlogic.gdx.utils.JsonReader;
8 import com.badlogic.gdx.utils.JsonValue;
9 import ru.deadsoftware.cavedroid.game.objects.Block;
10 import ru.deadsoftware.cavedroid.game.objects.Item;
12 import java.util.HashMap;
14 public class GameItems {
16 private static HashMap<String, Integer> blocksIds = new HashMap<>();
17 private static HashMap<String, Integer> itemsIds = new HashMap<>();
19 private static ArrayMap<String, Block> blocks = new ArrayMap<>();
20 private static ArrayMap<String, Item> items = new ArrayMap<>();
22 public static boolean isFluid(int id) {
23 return getBlock(id).isFluid();
24 }
26 public static boolean isWater(int id) {
27 return getBlock(id).getMeta().equals("water");
28 }
30 public static boolean isLava(int id) {
31 return getBlock(id).getMeta().equals("lava");
32 }
34 public static boolean isSlab(int id) {
35 return getBlock(id).getMeta().equals("slab");
36 }
38 public static boolean fluidCanFlowThere(int thisId, int thatId) {
39 return thatId == 0 || (!getBlock(thatId).hasCollision() && !isFluid(thatId)) ||
40 (isWater(thisId) && isWater(thatId) && thisId < thatId) ||
41 (isLava(thisId) && isLava(thatId) && thisId < thatId);
42 }
44 public static Block getBlock(int id) {
45 return blocks.getValueAt(id);
46 }
48 public static Item getItem(int id) {
49 return items.getValueAt(id);
50 }
52 public static Block getBlock(String key) {
53 return blocks.getValueAt(blocksIds.get(key));
54 }
56 public static Item getItem(String key) {
57 return items.getValueAt(itemsIds.get(key));
58 }
60 public static int getBlockId(String key) {
61 return blocksIds.get(key);
62 }
64 public static int getItemId(String key) {
65 return itemsIds.get(key);
66 }
68 public static String getBlockKey(int id) {
69 return blocks.getKeyAt(id);
70 }
72 public static String getItemKey(int id) {
73 return items.getKeyAt(id);
74 }
76 public static int getBlockIdByItemId(int id) {
77 return getBlockId(items.getKeyAt(id));
78 }
80 public static int getBlocksSize() {
81 return blocks.size;
82 }
84 public static int getItemsSize() {
85 return items.size;
86 }
88 public static Sprite getBlockTex(int id) {
89 return getBlock(id).getTex();
90 }
92 public static Sprite getItemTex(int id) {
93 if (items.getValueAt(id).getType().equals("block")) return getBlockTex(id);
94 else return getItem(id).getTex();
95 }
97 public static void load() {
98 JsonValue json = new JsonReader().parse(Gdx.files.internal("game_items.json"));
99 JsonValue block = json.child.child;
100 JsonValue item = json.child.next.child;
101 while (block != null) {
102 String key = block.name;
103 int left = block.has("left") ? block.getInt("left") : 0;
104 int right = block.has("right") ? block.getInt("right") : 0;
105 int top = block.has("top") ? block.getInt("top") : 0;
106 int bottom = block.has("bottom") ? block.getInt("bottom") : 0;
107 int hp = block.has("hp") ? block.getInt("hp") : -1;
108 String drop = block.has("drop") ? block.getString("drop") : key;
109 boolean collision = !block.has("collision") || block.getBoolean("collision");
110 boolean background = block.has("background") && block.getBoolean("background");
111 boolean transparent = block.has("transparent") && block.getBoolean("transparent");
112 boolean blockRequired = block.has("block_required") && block.getBoolean("block_required");
113 boolean fluid = block.has("fluid") && block.getBoolean("fluid");
114 String meta = block.has("meta") ? block.getString("meta") : "";
115 String texture = block.has("texture") ? block.getString("texture") : key;
116 Sprite sprite = key.equals("none") ? null :
117 new Sprite(new Texture(Gdx.files.internal("textures/blocks/" + texture + ".png")));
118 Block newBlock = new Block(
119 left,
120 top,
121 right,
122 bottom,
123 hp,
124 drop,
125 collision,
126 background,
127 transparent,
128 blockRequired,
129 fluid,
130 meta,
131 sprite
132 );
134 blocksIds.put(key, blocks.size);
135 blocks.put(key, newBlock);
136 block = block.next();
138 while (item != null) {
139 String key = item.name;
140 String name = item.has("name") ? item.getString("name") : key;
141 String type = item.has("type") ? item.getString("type") : "item";
142 String texture = item.has("texture") ? item.getString("texture") : key;
143 Sprite sprite = type.equals("block") ? null :
144 new Sprite(new Texture(Gdx.files.internal("textures/items/" + texture + ".png")));
145 itemsIds.put(key, items.size);
146 items.put(key, new Item(name, type, sprite));
147 item = item.next();