DEADSOFTWARE

079f4855d88465aebea7b60091b06e773d3ac328
[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.*;
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"));
117 TreeSet<Block> blocksSet = new TreeSet<>(Comparator.comparingInt(Block::getId));
118 TreeSet<Item> itemsSet = new TreeSet<>(Comparator.comparingInt(Item::getId));
121 int count = 0;
122 for (JsonValue block = json.get("blocks").child(); block != null; block = block.next()) {
123 try {
124 String key = block.name();
125 int left = Assets.getIntFromJson(block, "left", 0);
126 int right = Assets.getIntFromJson(block, "right", 0);
127 int top = Assets.getIntFromJson(block, "top", 0);
128 int bottom = Assets.getIntFromJson(block, "bottom", 0);
129 int clipX = Assets.getIntFromJson(block, "sprite_left", 0);
130 int clipY = Assets.getIntFromJson(block, "sprite_top", 0);
131 int clipWidth = Assets.getIntFromJson(block, "sprite_right", 0);
132 int clipHeight = Assets.getIntFromJson(block, "sprite_bottom", 0);
133 int hp = Assets.getIntFromJson(block, "hp", -1);
134 boolean collision = Assets.getBooleanFromJson(block, "collision", true);
135 boolean background = Assets.getBooleanFromJson(block, "background", false);
136 boolean transparent = Assets.getBooleanFromJson(block, "transparent", false);
137 boolean requiresBlock = Assets.getBooleanFromJson(block, "block_required", false);
138 boolean fluid = Assets.getBooleanFromJson(block, "fluid", false);
139 String drop = Assets.getStringFromJson(block, "drop", key);
140 String meta = Assets.getStringFromJson(block, "meta", "");
141 String tex = Assets.getStringFromJson(block, "texture", key);
142 Texture texture = tex.equals("none") ? null :
143 new Texture(assetLoader.getAssetHandle("textures/blocks/" + tex + ".png"));
144 boolean animated = Assets.getBooleanFromJson(block, "animated", false);
145 int frames = Assets.getIntFromJson(block, "frames", 0);
146 int id = Assets.getIntFromJson(block, "id", count);
147 blocksIds.put(key, id);
149 if (count >= id) {
150 count++;
153 Block newBlock = new Block(
154 id,
155 key,
156 left,
157 top,
158 right,
159 bottom,
160 hp,
161 drop,
162 collision,
163 background,
164 transparent,
165 requiresBlock,
166 fluid,
167 meta,
168 texture,
169 animated,
170 frames,
171 clipX,
172 clipY,
173 clipWidth,
174 clipHeight
175 );
176 blocksSet.add(newBlock);
177 } catch (GdxRuntimeException e) {
178 Gdx.app.error(TAG, e.getMessage());
182 count = 0;
183 for (JsonValue item = json.get("items").child(); item != null; item = item.next()) {
184 try {
185 String key = item.name();
186 String name = Assets.getStringFromJson(item, "name", key);
187 String type = Assets.getStringFromJson(item, "type", "item");
188 String texture = Assets.getStringFromJson(item, "texture", key);
189 Sprite sprite = type.equals("block") ? null :
190 new Sprite(new Texture(assetLoader.getAssetHandle("textures/items/" + texture + ".png")));
192 float originX = Assets.getFloatFromJson(item, "origin_x", 0f);
193 float originY = Assets.getFloatFromJson(item, "origin_y", 1f);
194 originX = MathUtils.clamp(originX, 0f, 1f);
195 originY = MathUtils.clamp(originY, 0f, 1f);
196 SpriteOrigin origin = new SpriteOrigin(originX, originY);
198 int id = Assets.getIntFromJson(item, "id", count);
200 String actionKey = Assets.getStringFromJson(item, "action_key", null);
202 if (count >= id) {
203 count++;
206 itemsIds.put(key, id);
207 itemsSet.add(new Item(id, key, name, type, sprite, origin, actionKey));
208 } catch (GdxRuntimeException e) {
209 Gdx.app.error(TAG, e.getMessage());
213 blocksSet.forEach((block -> blocks.put(block.getKey(), block)));
214 itemsSet.forEach((item -> items.put(item.getKey(), item)));