DEADSOFTWARE

eeeeb115da03fad2112abe5b96682373251d083f
[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 String fullBlock = Assets.getStringFromJson(block, "full_block", null);
148 blocksIds.put(key, id);
150 if (count >= id) {
151 count++;
154 Block newBlock = new Block(
155 id,
156 key,
157 left,
158 top,
159 right,
160 bottom,
161 hp,
162 drop,
163 collision,
164 background,
165 transparent,
166 requiresBlock,
167 fluid,
168 meta,
169 texture,
170 animated,
171 frames,
172 clipX,
173 clipY,
174 clipWidth,
175 clipHeight,
176 fullBlock
177 );
178 blocksSet.add(newBlock);
179 } catch (GdxRuntimeException e) {
180 Gdx.app.error(TAG, e.getMessage());
184 count = 0;
185 for (JsonValue item = json.get("items").child(); item != null; item = item.next()) {
186 try {
187 String key = item.name();
188 String name = Assets.getStringFromJson(item, "name", key);
189 String type = Assets.getStringFromJson(item, "type", "item");
190 String texture = Assets.getStringFromJson(item, "texture", key);
191 Sprite sprite = type.equals("block") ? null :
192 new Sprite(new Texture(assetLoader.getAssetHandle("textures/items/" + texture + ".png")));
194 float originX = Assets.getFloatFromJson(item, "origin_x", 0f);
195 float originY = Assets.getFloatFromJson(item, "origin_y", 1f);
196 originX = MathUtils.clamp(originX, 0f, 1f);
197 originY = MathUtils.clamp(originY, 0f, 1f);
198 SpriteOrigin origin = new SpriteOrigin(originX, originY);
200 int id = Assets.getIntFromJson(item, "id", count);
202 String actionKey = Assets.getStringFromJson(item, "action_key", null);
204 if (count >= id) {
205 count++;
208 itemsIds.put(key, id);
209 itemsSet.add(new Item(id, key, name, type, sprite, origin, actionKey));
210 } catch (GdxRuntimeException e) {
211 Gdx.app.error(TAG, e.getMessage());
215 blocksSet.forEach((block -> blocks.put(block.getKey(), block)));
216 itemsSet.forEach((item -> items.put(item.getKey(), item)));