DEADSOFTWARE

64fba5add7c89326fb65c70a63df094cc894eb85
[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.GdxRuntimeException;
8 import com.badlogic.gdx.utils.JsonValue;
9 import ru.deadsoftware.cavedroid.game.objects.Block;
10 import ru.deadsoftware.cavedroid.game.objects.Item;
11 import ru.deadsoftware.cavedroid.misc.Assets;
13 import java.util.HashMap;
15 public class GameItems {
17 private static final String TAG = "GameItems";
19 private static final HashMap<String, Integer> blocksIds = new HashMap<>();
20 private static final HashMap<String, Integer> itemsIds = new HashMap<>();
22 private static final ArrayMap<String, Block> blocks = new ArrayMap<>();
23 private static final ArrayMap<String, Item> items = new ArrayMap<>();
25 public static boolean isFluid(int id) {
26 return getBlock(id).isFluid();
27 }
29 public static boolean isWater(int id) {
30 return getBlock(id).getMeta().equals("water");
31 }
33 public static boolean isLava(int id) {
34 return getBlock(id).getMeta().equals("lava");
35 }
37 public static boolean isSlab(int id) {
38 return getBlock(id).getMeta().equals("slab");
39 }
41 public static boolean fluidCanFlowThere(int thisId, int thatId) {
42 return thatId == 0 || (!getBlock(thatId).hasCollision() && !isFluid(thatId)) ||
43 (isWater(thisId) && isWater(thatId) && thisId < thatId) ||
44 (isLava(thisId) && isLava(thatId) && thisId < thatId);
45 }
47 public static Block getBlock(int id) {
48 return blocks.getValueAt(id);
49 }
51 public static Item getItem(int id) {
52 return items.getValueAt(id);
53 }
55 public static Block getBlock(String key) {
56 return blocks.getValueAt(blocksIds.get(key));
57 }
59 public static Item getItem(String key) {
60 return items.getValueAt(itemsIds.get(key));
61 }
63 public static int getBlockId(String key) {
64 return blocksIds.get(key);
65 }
67 public static int getItemId(String key) {
68 return itemsIds.get(key);
69 }
71 public static String getBlockKey(int id) {
72 return blocks.getKeyAt(id);
73 }
75 public static String getItemKey(int id) {
76 return items.getKeyAt(id);
77 }
79 public static int getBlockIdByItemId(int id) {
80 return getBlockId(items.getKeyAt(id));
81 }
83 public static int getBlocksSize() {
84 return blocks.size;
85 }
87 public static int getItemsSize() {
88 return items.size;
89 }
91 public static Sprite getBlockTex(int id) {
92 return getBlock(id).getTexture();
93 }
95 public static Sprite getItemTex(int id) {
96 return items.getValueAt(id).getType().equals("block") ? getBlockTex(id) : getItem(id).getTexture();
97 }
99 public static void load() {
100 JsonValue json = Assets.jsonReader.parse(Gdx.files.internal("json/game_items.json"));
101 for (JsonValue block = json.get("blocks").child(); block != null; block = block.next()) {
102 try {
103 String key = block.name();
104 int left = Assets.getIntFromJson(block, "left", 0);
105 int right = Assets.getIntFromJson(block, "right", 0);
106 int top = Assets.getIntFromJson(block, "top", 0);
107 int bottom = Assets.getIntFromJson(block, "bottom", 0);
108 int hp = Assets.getIntFromJson(block, "hp", -1);
109 boolean coll = Assets.getBooleanFromJson(block, "collision", true);
110 boolean bg = Assets.getBooleanFromJson(block, "background", false);
111 boolean tp = Assets.getBooleanFromJson(block, "transparent", false);
112 boolean br = Assets.getBooleanFromJson(block, "block_required", false);
113 boolean fluid = Assets.getBooleanFromJson(block, "fluid", false);
114 String drop = Assets.getStringFromJson(block, "drop", key);
115 String meta = Assets.getStringFromJson(block, "meta", "");
116 String tex = Assets.getStringFromJson(block, "texture", key);
117 Sprite sprite = tex.equals("none") ? null :
118 new Sprite(new Texture(Gdx.files.internal("textures/blocks/" + tex + ".png")));
120 Block newBlock = new Block(left, top, right, bottom, hp, drop, coll, bg, tp, br, fluid, meta, sprite);
121 blocksIds.put(key, blocks.size);
122 blocks.put(key, newBlock);
123 } catch (GdxRuntimeException e) {
124 Gdx.app.error(TAG, e.getMessage());
127 for (JsonValue item = json.get("items").child(); item != null; item = item.next()) {
128 try {
129 String key = item.name();
130 String name = Assets.getStringFromJson(item, "name", key);
131 String type = Assets.getStringFromJson(item, "type", "item");
132 String texture = Assets.getStringFromJson(item, "texture", key);
133 Sprite sprite = type.equals("block") ? null :
134 new Sprite(new Texture(Gdx.files.internal("textures/items/" + texture + ".png")));
135 itemsIds.put(key, items.size);
136 items.put(key, new Item(name, type, sprite));
137 } catch (GdxRuntimeException e) {
138 Gdx.app.error(TAG, e.getMessage());