DEADSOFTWARE

5ae4f19a92e70b96ede2cfcecf01c40fac27d8e5
[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.math.MathUtils;
7 import com.badlogic.gdx.utils.ArrayMap;
8 import com.badlogic.gdx.utils.GdxRuntimeException;
9 import com.badlogic.gdx.utils.JsonValue;
10 import ru.deadsoftware.cavedroid.game.objects.Block;
11 import ru.deadsoftware.cavedroid.game.objects.Item;
12 import ru.deadsoftware.cavedroid.misc.Assets;
13 import ru.deadsoftware.cavedroid.misc.utils.AssetLoader;
14 import ru.deadsoftware.cavedroid.misc.utils.SpriteOrigin;
16 import java.util.HashMap;
18 public class GameItems {
20 private static final String TAG = "GameItems";
22 private static final HashMap<String, Integer> blocksIds = new HashMap<>();
23 private static final HashMap<String, Integer> itemsIds = new HashMap<>();
25 private static final ArrayMap<String, Block> blocks = new ArrayMap<>();
26 private static final ArrayMap<String, Item> items = new ArrayMap<>();
28 public static boolean isFluid(int id) {
29 return getBlock(id).isFluid();
30 }
32 public static boolean isWater(int id) {
33 return isWater(getBlock(id));
34 }
36 public static boolean isWater(Block block) {
37 return block.getMeta().equals("water");
38 }
40 public static boolean isLava(int id) {
41 return isLava(getBlock(id));
42 }
44 public static boolean isLava(Block block) {
45 return block.getMeta().equals("lava");
46 }
48 public static boolean isSlab(int id) {
49 return getBlock(id).getMeta().equals("slab");
50 }
52 public static boolean fluidCanFlowThere(int thisId, int thatId) {
53 return thatId == 0 || (!getBlock(thatId).hasCollision() && !isFluid(thatId)) ||
54 (isWater(thisId) && isWater(thatId) && thisId < thatId) ||
55 (isLava(thisId) && isLava(thatId) && thisId < thatId);
56 }
58 public static Block getBlock(int id) {
59 return blocks.getValueAt(id);
60 }
62 public static Item getItem(int id) {
63 return items.getValueAt(id);
64 }
66 public static Block getBlock(String key) {
67 return blocks.getValueAt(blocksIds.get(key));
68 }
70 public static Item getItem(String key) {
71 return items.getValueAt(itemsIds.get(key));
72 }
74 public static int getBlockId(String key) {
75 return blocksIds.get(key);
76 }
78 public static int getItemId(String key) {
79 return itemsIds.get(key);
80 }
82 public static String getBlockKey(int id) {
83 return blocks.getKeyAt(id);
84 }
86 public static String getItemKey(int id) {
87 return items.getKeyAt(id);
88 }
90 public static int getBlockIdByItemId(int id) {
91 return getBlockId(items.getKeyAt(id));
92 }
94 public static int getItemIdByBlockId(int id) {
95 return getItemId(blocks.getKeyAt(id));
96 }
98 public static int getBlocksSize() {
99 return blocks.size;
102 public static int getItemsSize() {
103 return items.size;
106 public static Sprite getBlockTex(int id) {
107 return getBlock(id).getTexture();
110 public static Sprite getItemTex(int id) {
111 return items.getValueAt(id).getType().equals("block") ? getBlockTex(id) : getItem(id).getTexture();
114 public static void load(AssetLoader assetLoader) {
115 JsonValue json = Assets.jsonReader.parse(assetLoader.getAssetHandle("json/game_items.json"));
116 for (JsonValue block = json.get("blocks").child(); block != null; block = block.next()) {
117 try {
118 String key = block.name();
119 int left = Assets.getIntFromJson(block, "left", 0);
120 int right = Assets.getIntFromJson(block, "right", 0);
121 int top = Assets.getIntFromJson(block, "top", 0);
122 int bottom = Assets.getIntFromJson(block, "bottom", 0);
123 int clipX = Assets.getIntFromJson(block, "sprite_left", 0);
124 int clipY = Assets.getIntFromJson(block, "sprite_top", 0);
125 int clipWidth = Assets.getIntFromJson(block, "sprite_right", 0);
126 int clipHeight = Assets.getIntFromJson(block, "sprite_bottom", 0);
127 int hp = Assets.getIntFromJson(block, "hp", -1);
128 boolean collision = Assets.getBooleanFromJson(block, "collision", true);
129 boolean background = Assets.getBooleanFromJson(block, "background", false);
130 boolean transparent = Assets.getBooleanFromJson(block, "transparent", false);
131 boolean requiresBlock = Assets.getBooleanFromJson(block, "block_required", false);
132 boolean fluid = Assets.getBooleanFromJson(block, "fluid", false);
133 String drop = Assets.getStringFromJson(block, "drop", key);
134 String meta = Assets.getStringFromJson(block, "meta", "");
135 String tex = Assets.getStringFromJson(block, "texture", key);
136 Texture texture = tex.equals("none") ? null :
137 new Texture(assetLoader.getAssetHandle("textures/blocks/" + tex + ".png"));
138 boolean animated = Assets.getBooleanFromJson(block, "animated", false);
139 int frames = Assets.getIntFromJson(block, "frames", 0);
140 int id = blocks.size;
141 blocksIds.put(key, id);
143 Block newBlock = new Block(
144 id,
145 left,
146 top,
147 right,
148 bottom,
149 hp,
150 drop,
151 collision,
152 background,
153 transparent,
154 requiresBlock,
155 fluid,
156 meta,
157 texture,
158 animated,
159 frames,
160 clipX,
161 clipY,
162 clipWidth,
163 clipHeight
164 );
165 blocks.put(key, newBlock);
166 } catch (GdxRuntimeException e) {
167 Gdx.app.error(TAG, e.getMessage());
170 for (JsonValue item = json.get("items").child(); item != null; item = item.next()) {
171 try {
172 String key = item.name();
173 String name = Assets.getStringFromJson(item, "name", key);
174 String type = Assets.getStringFromJson(item, "type", "item");
175 String texture = Assets.getStringFromJson(item, "texture", key);
176 Sprite sprite = type.equals("block") ? null :
177 new Sprite(new Texture(assetLoader.getAssetHandle("textures/items/" + texture + ".png")));
179 float originX = Assets.getFloatFromJson(item, "origin_x", 0f);
180 float originY = Assets.getFloatFromJson(item, "origin_y", 1f);
181 originX = MathUtils.clamp(originX, 0f, 1f);
182 originY = MathUtils.clamp(originY, 0f, 1f);
183 SpriteOrigin origin = new SpriteOrigin(originX, originY);
185 int id = items.size;
186 itemsIds.put(key, id);
187 items.put(key, new Item(id, name, type, sprite, origin));
188 } catch (GdxRuntimeException e) {
189 Gdx.app.error(TAG, e.getMessage());