DEADSOFTWARE

Fix mob physics
[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 isWater(getBlock(id));
31 }
33 public static boolean isWater(Block block) {
34 return block.getMeta().equals("water");
35 }
37 public static boolean isLava(int id) {
38 return isLava(getBlock(id));
39 }
41 public static boolean isLava(Block block) {
42 return block.getMeta().equals("lava");
43 }
45 public static boolean isSlab(int id) {
46 return getBlock(id).getMeta().equals("slab");
47 }
49 public static boolean fluidCanFlowThere(int thisId, int thatId) {
50 return thatId == 0 || (!getBlock(thatId).hasCollision() && !isFluid(thatId)) ||
51 (isWater(thisId) && isWater(thatId) && thisId < thatId) ||
52 (isLava(thisId) && isLava(thatId) && thisId < thatId);
53 }
55 public static Block getBlock(int id) {
56 return blocks.getValueAt(id);
57 }
59 public static Item getItem(int id) {
60 return items.getValueAt(id);
61 }
63 public static Block getBlock(String key) {
64 return blocks.getValueAt(blocksIds.get(key));
65 }
67 public static Item getItem(String key) {
68 return items.getValueAt(itemsIds.get(key));
69 }
71 public static int getBlockId(String key) {
72 return blocksIds.get(key);
73 }
75 public static int getItemId(String key) {
76 return itemsIds.get(key);
77 }
79 public static String getBlockKey(int id) {
80 return blocks.getKeyAt(id);
81 }
83 public static String getItemKey(int id) {
84 return items.getKeyAt(id);
85 }
87 public static int getBlockIdByItemId(int id) {
88 return getBlockId(items.getKeyAt(id));
89 }
91 public static int getBlocksSize() {
92 return blocks.size;
93 }
95 public static int getItemsSize() {
96 return items.size;
97 }
99 public static Sprite getBlockTex(int id) {
100 return getBlock(id).getTexture();
103 public static Sprite getItemTex(int id) {
104 return items.getValueAt(id).getType().equals("block") ? getBlockTex(id) : getItem(id).getTexture();
107 public static void load() {
108 JsonValue json = Assets.jsonReader.parse(Gdx.files.internal("json/game_items.json"));
109 for (JsonValue block = json.get("blocks").child(); block != null; block = block.next()) {
110 try {
111 String key = block.name();
112 int left = Assets.getIntFromJson(block, "left", 0);
113 int right = Assets.getIntFromJson(block, "right", 0);
114 int top = Assets.getIntFromJson(block, "top", 0);
115 int bottom = Assets.getIntFromJson(block, "bottom", 0);
116 int clipX = Assets.getIntFromJson(block, "sprite_left", 0);
117 int clipY = Assets.getIntFromJson(block, "sprite_top", 0);
118 int clipWidth = Assets.getIntFromJson(block, "sprite_right", 0);
119 int clipHeight = Assets.getIntFromJson(block, "sprite_bottom", 0);
120 int hp = Assets.getIntFromJson(block, "hp", -1);
121 boolean collision = Assets.getBooleanFromJson(block, "collision", true);
122 boolean background = Assets.getBooleanFromJson(block, "background", false);
123 boolean transparent = Assets.getBooleanFromJson(block, "transparent", false);
124 boolean requiresBlock = Assets.getBooleanFromJson(block, "block_required", false);
125 boolean fluid = Assets.getBooleanFromJson(block, "fluid", false);
126 String drop = Assets.getStringFromJson(block, "drop", key);
127 String meta = Assets.getStringFromJson(block, "meta", "");
128 String tex = Assets.getStringFromJson(block, "texture", key);
129 Texture texture = tex.equals("none") ? null :
130 new Texture(Gdx.files.internal("textures/blocks/" + tex + ".png"));
131 boolean animated = Assets.getBooleanFromJson(block, "animated", false);
132 int frames = Assets.getIntFromJson(block, "frames", 0);
134 Block newBlock = new Block(
135 left,
136 top,
137 right,
138 bottom,
139 hp,
140 drop,
141 collision,
142 background,
143 transparent,
144 requiresBlock,
145 fluid,
146 meta,
147 texture,
148 animated,
149 frames,
150 clipX,
151 clipY,
152 clipWidth,
153 clipHeight
154 );
156 blocksIds.put(key, blocks.size);
157 blocks.put(key, newBlock);
158 } catch (GdxRuntimeException e) {
159 Gdx.app.error(TAG, e.getMessage());
162 for (JsonValue item = json.get("items").child(); item != null; item = item.next()) {
163 try {
164 String key = item.name();
165 String name = Assets.getStringFromJson(item, "name", key);
166 String type = Assets.getStringFromJson(item, "type", "item");
167 String texture = Assets.getStringFromJson(item, "texture", key);
168 Sprite sprite = type.equals("block") ? null :
169 new Sprite(new Texture(Gdx.files.internal("textures/items/" + texture + ".png")));
170 itemsIds.put(key, items.size);
171 items.put(key, new Item(name, type, sprite));
172 } catch (GdxRuntimeException e) {
173 Gdx.app.error(TAG, e.getMessage());