DEADSOFTWARE

Move items to JSON
[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<String, Integer>();
17 private static HashMap<String, Integer> itemsIds = new HashMap<String, Integer>();
19 private static ArrayMap<String, Block> blocks = new ArrayMap<String, Block>();
20 private static ArrayMap<String, Item> items = new ArrayMap<String, Item>();
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 Block getBlock(int id) {
39 return blocks.getValueAt(id);
40 }
42 public static Item getItem(int id) {
43 return items.getValueAt(id);
44 }
46 public static Block getBlock(String key) {
47 return blocks.getValueAt(blocksIds.get(key));
48 }
50 public static Item getItem(String key) {
51 return items.getValueAt(itemsIds.get(key));
52 }
54 public static int getBlockId(String key) {
55 return blocksIds.get(key);
56 }
58 public static int getItemId(String key) {
59 return itemsIds.get(key);
60 }
62 public static String getBlockKey(int id) {
63 return blocks.getKeyAt(id);
64 }
66 public static String getItemKey(int id) {
67 return items.getKeyAt(id);
68 }
70 public static int getBlockIdByItemId(int id) {
71 return getBlockId(items.getKeyAt(id));
72 }
74 public static int getBlocksSize() {
75 return blocks.size;
76 }
78 public static int getItemsSize() {
79 return items.size;
80 }
82 public static Sprite getBlockTex(int id) {
83 return getBlock(id).getTex();
84 }
86 public static Sprite getItemTex(int id) {
87 if (items.getValueAt(id).getType().equals("block")) return getBlockTex(id);
88 else return getItem(id).getTex();
89 }
91 public static void load() {
92 JsonValue json = new JsonReader().parse(Gdx.files.internal("game_items.json"));
93 JsonValue block = json.child.child;
94 JsonValue item = json.child.next.child;
95 while (block != null) {
96 String key = block.name;
97 int left = (block.has("left") ? block.getInt("left") : 0);
98 int right = (block.has("right") ? block.getInt("right") : 0);
99 int top = (block.has("top") ? block.getInt("top") : 0);
100 int bottom = (block.has("bottom") ? block.getInt("bottom") : 0);
101 int hp = (block.has("hp") ? block.getInt("hp") : -1);
102 String drop = (block.has("drop") ? block.getString("drop") : key);
103 boolean collision = (!block.has("collision") || block.getBoolean("collision"));
104 boolean background = (block.has("background") && block.getBoolean("background"));
105 boolean transparent = !(!block.has("collision") || block.getBoolean("collision"));
106 boolean blockRequired = (block.has("block_required") && block.getBoolean("block_required"));
107 boolean fluid = (block.has("fluid") && block.getBoolean("fluid"));
108 String meta = (block.has("meta") ? block.getString("meta") : "");
109 String texture = (block.has("texture") ? block.getString("texture") : key);
110 blocksIds.put(key, blocks.size);
111 blocks.put(key, new Block(left, top, right, bottom, hp, drop, collision,
112 background, transparent, blockRequired, fluid, meta,
113 key.equals("none") ? null :
114 new Sprite(new Texture(Gdx.files.internal("textures/" + texture + ".png")))));
115 block = block.next();
117 while (item != null) {
118 String key = item.name;
119 String name = (item.has("name") ? item.getString("name") : key);
120 String type = (item.has("type") ? item.getString("type") : "item");
121 String texture = (item.has("texture") ? item.getString("texture") : key);
122 itemsIds.put(key, items.size);
123 items.put(key, new Item(name, type, type.equals("block") ? null :
124 new Sprite(new Texture(Gdx.files.internal("textures/" + texture + ".png")))));
125 item = item.next();