DEADSOFTWARE

Support different block texture sizes and animation
[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 clipX = Assets.getIntFromJson(block, "sprite_left", 0);
109 int clipY = Assets.getIntFromJson(block, "sprite_top", 0);
110 int clipWidth = Assets.getIntFromJson(block, "sprite_right", 0);
111 int clipHeight = Assets.getIntFromJson(block, "sprite_bottom", 0);
112 int hp = Assets.getIntFromJson(block, "hp", -1);
113 boolean collision = Assets.getBooleanFromJson(block, "collision", true);
114 boolean background = Assets.getBooleanFromJson(block, "background", false);
115 boolean transparent = Assets.getBooleanFromJson(block, "transparent", false);
116 boolean requiresBlock = Assets.getBooleanFromJson(block, "block_required", false);
117 boolean fluid = Assets.getBooleanFromJson(block, "fluid", false);
118 String drop = Assets.getStringFromJson(block, "drop", key);
119 String meta = Assets.getStringFromJson(block, "meta", "");
120 String tex = Assets.getStringFromJson(block, "texture", key);
121 Texture texture = tex.equals("none") ? null :
122 new Texture(Gdx.files.internal("textures/blocks/" + tex + ".png"));
123 boolean animated = Assets.getBooleanFromJson(block, "animated", false);
124 int frames = Assets.getIntFromJson(block, "frames", 0);
126 Block newBlock = new Block(
127 left,
128 top,
129 right,
130 bottom,
131 hp,
132 drop,
133 collision,
134 background,
135 transparent,
136 requiresBlock,
137 fluid,
138 meta,
139 texture,
140 animated,
141 frames,
142 clipX,
143 clipY,
144 clipWidth,
145 clipHeight
146 );
148 blocksIds.put(key, blocks.size);
149 blocks.put(key, newBlock);
150 } catch (GdxRuntimeException e) {
151 Gdx.app.error(TAG, e.getMessage());
154 for (JsonValue item = json.get("items").child(); item != null; item = item.next()) {
155 try {
156 String key = item.name();
157 String name = Assets.getStringFromJson(item, "name", key);
158 String type = Assets.getStringFromJson(item, "type", "item");
159 String texture = Assets.getStringFromJson(item, "texture", key);
160 Sprite sprite = type.equals("block") ? null :
161 new Sprite(new Texture(Gdx.files.internal("textures/items/" + texture + ".png")));
162 itemsIds.put(key, items.size);
163 items.put(key, new Item(name, type, sprite));
164 } catch (GdxRuntimeException e) {
165 Gdx.app.error(TAG, e.getMessage());