DEADSOFTWARE

Add item in player hand
[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 getBlocksSize() {
95 return blocks.size;
96 }
98 public static int getItemsSize() {
99 return items.size;
102 public static Sprite getBlockTex(int id) {
103 return getBlock(id).getTexture();
106 public static Sprite getItemTex(int id) {
107 return items.getValueAt(id).getType().equals("block") ? getBlockTex(id) : getItem(id).getTexture();
110 public static void load(AssetLoader assetLoader) {
111 JsonValue json = Assets.jsonReader.parse(assetLoader.getAssetHandle("json/game_items.json"));
112 for (JsonValue block = json.get("blocks").child(); block != null; block = block.next()) {
113 try {
114 String key = block.name();
115 int left = Assets.getIntFromJson(block, "left", 0);
116 int right = Assets.getIntFromJson(block, "right", 0);
117 int top = Assets.getIntFromJson(block, "top", 0);
118 int bottom = Assets.getIntFromJson(block, "bottom", 0);
119 int clipX = Assets.getIntFromJson(block, "sprite_left", 0);
120 int clipY = Assets.getIntFromJson(block, "sprite_top", 0);
121 int clipWidth = Assets.getIntFromJson(block, "sprite_right", 0);
122 int clipHeight = Assets.getIntFromJson(block, "sprite_bottom", 0);
123 int hp = Assets.getIntFromJson(block, "hp", -1);
124 boolean collision = Assets.getBooleanFromJson(block, "collision", true);
125 boolean background = Assets.getBooleanFromJson(block, "background", false);
126 boolean transparent = Assets.getBooleanFromJson(block, "transparent", false);
127 boolean requiresBlock = Assets.getBooleanFromJson(block, "block_required", false);
128 boolean fluid = Assets.getBooleanFromJson(block, "fluid", false);
129 String drop = Assets.getStringFromJson(block, "drop", key);
130 String meta = Assets.getStringFromJson(block, "meta", "");
131 String tex = Assets.getStringFromJson(block, "texture", key);
132 Texture texture = tex.equals("none") ? null :
133 new Texture(assetLoader.getAssetHandle("textures/blocks/" + tex + ".png"));
134 boolean animated = Assets.getBooleanFromJson(block, "animated", false);
135 int frames = Assets.getIntFromJson(block, "frames", 0);
136 int id = blocks.size;
137 blocksIds.put(key, id);
139 Block newBlock = new Block(
140 id,
141 left,
142 top,
143 right,
144 bottom,
145 hp,
146 drop,
147 collision,
148 background,
149 transparent,
150 requiresBlock,
151 fluid,
152 meta,
153 texture,
154 animated,
155 frames,
156 clipX,
157 clipY,
158 clipWidth,
159 clipHeight
160 );
161 blocks.put(key, newBlock);
162 } catch (GdxRuntimeException e) {
163 Gdx.app.error(TAG, e.getMessage());
166 for (JsonValue item = json.get("items").child(); item != null; item = item.next()) {
167 try {
168 String key = item.name();
169 String name = Assets.getStringFromJson(item, "name", key);
170 String type = Assets.getStringFromJson(item, "type", "item");
171 String texture = Assets.getStringFromJson(item, "texture", key);
172 Sprite sprite = type.equals("block") ? null :
173 new Sprite(new Texture(assetLoader.getAssetHandle("textures/items/" + texture + ".png")));
175 float originX = Assets.getFloatFromJson(item, "origin_x", 0f);
176 float originY = Assets.getFloatFromJson(item, "origin_y", 1f);
177 originX = MathUtils.clamp(originX, 0f, 1f);
178 originY = MathUtils.clamp(originY, 0f, 1f);
179 SpriteOrigin origin = new SpriteOrigin(originX, originY);
181 int id = items.size;
182 itemsIds.put(key, id);
183 items.put(key, new Item(id, name, type, sprite, origin));
184 } catch (GdxRuntimeException e) {
185 Gdx.app.error(TAG, e.getMessage());