DEADSOFTWARE

Add support for external assets
[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;
12 import ru.deadsoftware.cavedroid.misc.utils.AssetLoader;
14 import java.util.HashMap;
16 public class GameItems {
18 private static final String TAG = "GameItems";
20 private static final HashMap<String, Integer> blocksIds = new HashMap<>();
21 private static final HashMap<String, Integer> itemsIds = new HashMap<>();
23 private static final ArrayMap<String, Block> blocks = new ArrayMap<>();
24 private static final ArrayMap<String, Item> items = new ArrayMap<>();
26 public static boolean isFluid(int id) {
27 return getBlock(id).isFluid();
28 }
30 public static boolean isWater(int id) {
31 return isWater(getBlock(id));
32 }
34 public static boolean isWater(Block block) {
35 return block.getMeta().equals("water");
36 }
38 public static boolean isLava(int id) {
39 return isLava(getBlock(id));
40 }
42 public static boolean isLava(Block block) {
43 return block.getMeta().equals("lava");
44 }
46 public static boolean isSlab(int id) {
47 return getBlock(id).getMeta().equals("slab");
48 }
50 public static boolean fluidCanFlowThere(int thisId, int thatId) {
51 return thatId == 0 || (!getBlock(thatId).hasCollision() && !isFluid(thatId)) ||
52 (isWater(thisId) && isWater(thatId) && thisId < thatId) ||
53 (isLava(thisId) && isLava(thatId) && thisId < thatId);
54 }
56 public static Block getBlock(int id) {
57 return blocks.getValueAt(id);
58 }
60 public static Item getItem(int id) {
61 return items.getValueAt(id);
62 }
64 public static Block getBlock(String key) {
65 return blocks.getValueAt(blocksIds.get(key));
66 }
68 public static Item getItem(String key) {
69 return items.getValueAt(itemsIds.get(key));
70 }
72 public static int getBlockId(String key) {
73 return blocksIds.get(key);
74 }
76 public static int getItemId(String key) {
77 return itemsIds.get(key);
78 }
80 public static String getBlockKey(int id) {
81 return blocks.getKeyAt(id);
82 }
84 public static String getItemKey(int id) {
85 return items.getKeyAt(id);
86 }
88 public static int getBlockIdByItemId(int id) {
89 return getBlockId(items.getKeyAt(id));
90 }
92 public static int getBlocksSize() {
93 return blocks.size;
94 }
96 public static int getItemsSize() {
97 return items.size;
98 }
100 public static Sprite getBlockTex(int id) {
101 return getBlock(id).getTexture();
104 public static Sprite getItemTex(int id) {
105 return items.getValueAt(id).getType().equals("block") ? getBlockTex(id) : getItem(id).getTexture();
108 public static void load(AssetLoader assetLoader) {
109 JsonValue json = Assets.jsonReader.parse(assetLoader.getAssetHandle("json/game_items.json"));
110 for (JsonValue block = json.get("blocks").child(); block != null; block = block.next()) {
111 try {
112 String key = block.name();
113 int left = Assets.getIntFromJson(block, "left", 0);
114 int right = Assets.getIntFromJson(block, "right", 0);
115 int top = Assets.getIntFromJson(block, "top", 0);
116 int bottom = Assets.getIntFromJson(block, "bottom", 0);
117 int clipX = Assets.getIntFromJson(block, "sprite_left", 0);
118 int clipY = Assets.getIntFromJson(block, "sprite_top", 0);
119 int clipWidth = Assets.getIntFromJson(block, "sprite_right", 0);
120 int clipHeight = Assets.getIntFromJson(block, "sprite_bottom", 0);
121 int hp = Assets.getIntFromJson(block, "hp", -1);
122 boolean collision = Assets.getBooleanFromJson(block, "collision", true);
123 boolean background = Assets.getBooleanFromJson(block, "background", false);
124 boolean transparent = Assets.getBooleanFromJson(block, "transparent", false);
125 boolean requiresBlock = Assets.getBooleanFromJson(block, "block_required", false);
126 boolean fluid = Assets.getBooleanFromJson(block, "fluid", false);
127 String drop = Assets.getStringFromJson(block, "drop", key);
128 String meta = Assets.getStringFromJson(block, "meta", "");
129 String tex = Assets.getStringFromJson(block, "texture", key);
130 Texture texture = tex.equals("none") ? null :
131 new Texture(assetLoader.getAssetHandle("textures/blocks/" + tex + ".png"));
132 boolean animated = Assets.getBooleanFromJson(block, "animated", false);
133 int frames = Assets.getIntFromJson(block, "frames", 0);
135 Block newBlock = new Block(
136 left,
137 top,
138 right,
139 bottom,
140 hp,
141 drop,
142 collision,
143 background,
144 transparent,
145 requiresBlock,
146 fluid,
147 meta,
148 texture,
149 animated,
150 frames,
151 clipX,
152 clipY,
153 clipWidth,
154 clipHeight
155 );
157 blocksIds.put(key, blocks.size);
158 blocks.put(key, newBlock);
159 } catch (GdxRuntimeException e) {
160 Gdx.app.error(TAG, e.getMessage());
163 for (JsonValue item = json.get("items").child(); item != null; item = item.next()) {
164 try {
165 String key = item.name();
166 String name = Assets.getStringFromJson(item, "name", key);
167 String type = Assets.getStringFromJson(item, "type", "item");
168 String texture = Assets.getStringFromJson(item, "texture", key);
169 Sprite sprite = type.equals("block") ? null :
170 new Sprite(new Texture(assetLoader.getAssetHandle("textures/items/" + texture + ".png")));
171 itemsIds.put(key, items.size);
172 items.put(key, new Item(name, type, sprite));
173 } catch (GdxRuntimeException e) {
174 Gdx.app.error(TAG, e.getMessage());