DEADSOFTWARE

7cf525263a52dec4c252f87d6e91129b45ada223
[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.model.block.*;
11 import ru.deadsoftware.cavedroid.game.model.item.CommonItemParams;
12 import ru.deadsoftware.cavedroid.game.model.item.Item;
13 import ru.deadsoftware.cavedroid.misc.Assets;
14 import ru.deadsoftware.cavedroid.misc.utils.AssetLoader;
15 import ru.deadsoftware.cavedroid.misc.utils.SpriteOrigin;
17 import java.util.*;
19 public class GameItems {
21 private static final String TAG = "GameItems";
23 private static final HashMap<String, Integer> blocksIds = new HashMap<>();
24 private static final HashMap<String, Integer> itemsIds = new HashMap<>();
26 private static final ArrayMap<String, Block> blocks = new ArrayMap<>();
27 private static final ArrayMap<String, Item> items = new ArrayMap<>();
29 public static boolean isFluid(int id) {
30 return getBlock(id).isFluid();
31 }
33 public static boolean isWater(int id) {
34 return isWater(getBlock(id));
35 }
37 @Deprecated
38 public static boolean isWater(Block block) {
39 return block instanceof Block.Water;
40 }
42 @Deprecated
43 public static boolean isLava(int id) {
44 return isLava(getBlock(id));
45 }
47 @Deprecated
48 public static boolean isLava(Block block) {
49 return block instanceof Block.Lava;
50 }
52 @Deprecated
53 public static boolean isSlab(int id) {
54 return getBlock(id) instanceof Block.Slab;
55 }
57 public static boolean fluidCanFlowThere(int thisId, int thatId) {
58 return thatId == 0 || (!getBlock(thatId).hasCollision() && !isFluid(thatId)) ||
59 (isWater(thisId) && isWater(thatId) && thisId < thatId) ||
60 (isLava(thisId) && isLava(thatId) && thisId < thatId);
61 }
63 public static Block getBlock(int id) {
64 return blocks.getValueAt(id);
65 }
67 public static Item getItem(int id) {
68 return items.getValueAt(id);
69 }
71 public static Block getBlock(String key) {
72 return blocks.getValueAt(blocksIds.get(key));
73 }
75 public static Item getItem(String key) {
76 return items.getValueAt(itemsIds.get(key));
77 }
79 public static int getBlockId(String key) {
80 return blocksIds.get(key);
81 }
83 public static int getItemId(String key) {
84 return itemsIds.get(key);
85 }
87 public static String getBlockKey(int id) {
88 return blocks.getKeyAt(id);
89 }
91 public static String getItemKey(int id) {
92 return items.getKeyAt(id);
93 }
95 public static int getBlockIdByItemId(int id) {
96 return getBlockId(items.getKeyAt(id));
97 }
99 public static int getItemIdByBlockId(int id) {
100 return getItemId(blocks.getKeyAt(id));
103 public static int getBlocksSize() {
104 return blocks.size;
107 public static int getItemsSize() {
108 return items.size;
111 public static Sprite getBlockTex(int id) {
112 return getBlock(id).getTexture();
115 public static void load(AssetLoader assetLoader) {
116 JsonValue json = Assets.jsonReader.parse(assetLoader.getAssetHandle("json/game_items.json"));
118 TreeSet<Block> blocksSet = new TreeSet<>(Comparator.comparingInt(a -> a.getParams().getId()));
119 TreeSet<Item> itemsSet = new TreeSet<>(Comparator.comparingInt(a -> a.getParams().getId()));
122 int count = 0;
123 for (JsonValue block = json.get("blocks").child(); block != null; block = block.next()) {
124 try {
125 String key = block.name();
126 int left = Assets.getIntFromJson(block, "left", 0);
127 int right = Assets.getIntFromJson(block, "right", 0);
128 int top = Assets.getIntFromJson(block, "top", 0);
129 int bottom = Assets.getIntFromJson(block, "bottom", 0);
130 int clipX = Assets.getIntFromJson(block, "sprite_left", 0);
131 int clipY = Assets.getIntFromJson(block, "sprite_top", 0);
132 int clipWidth = Assets.getIntFromJson(block, "sprite_right", 0);
133 int clipHeight = Assets.getIntFromJson(block, "sprite_bottom", 0);
134 int hp = Assets.getIntFromJson(block, "hp", -1);
135 boolean collision = Assets.getBooleanFromJson(block, "collision", true);
136 boolean background = Assets.getBooleanFromJson(block, "background", false);
137 boolean transparent = Assets.getBooleanFromJson(block, "transparent", false);
138 boolean requiresBlock = Assets.getBooleanFromJson(block, "block_required", false);
139 boolean fluid = Assets.getBooleanFromJson(block, "fluid", false);
140 String drop = Assets.getStringFromJson(block, "drop", key);
141 String meta = Assets.getStringFromJson(block, "meta", "");
142 String tex = Assets.getStringFromJson(block, "texture", key);
143 Texture texture = tex.equals("none") ? null :
144 new Texture(assetLoader.getAssetHandle("textures/blocks/" + tex + ".png"));
145 boolean animated = Assets.getBooleanFromJson(block, "animated", false);
146 int frames = Assets.getIntFromJson(block, "frames", 0);
147 int id = Assets.getIntFromJson(block, "id", count);
148 int dropCount = Assets.getIntFromJson(block, "drop_count", 1);
149 String fullBlock = Assets.getStringFromJson(block, "full_block", null);
150 blocksIds.put(key, id);
152 if (count >= id) {
153 count++;
156 BlockMargins collMargins = new BlockMargins(left, top, right, bottom);
157 BlockMargins spriteMargins = new BlockMargins(clipX, clipY, clipWidth, clipHeight);
158 BlockDropInfo dropInfo = new BlockDropInfo(drop, dropCount);
159 BlockAnimationInfo animInfo = null;
160 if (animated) {
161 animInfo = new BlockAnimationInfo(frames);
164 CommonBlockParams params = new CommonBlockParams(
165 id,
166 key,
167 collMargins,
168 hp,
169 dropInfo,
170 collision,
171 background,
172 transparent,
173 requiresBlock,
174 animInfo,
175 texture,
176 spriteMargins
177 );
179 Block newBlock = switch (meta) {
180 case "water" -> new Block.Water(params, 5);
181 case "lava" -> new Block.Lava(params, 5);
182 case "slab" -> new Block.Slab(params, fullBlock);
183 default -> new Block.Normal(params);
184 };
186 newBlock.initialize();
187 blocksSet.add(newBlock);
188 } catch (GdxRuntimeException e) {
189 Gdx.app.error(TAG, e.getMessage());
193 blocksSet.forEach((block -> blocks.put(block.getParams().getKey(), block)));
195 count = 0;
196 for (JsonValue item = json.get("items").child(); item != null; item = item.next()) {
197 try {
198 String key = item.name();
199 String name = Assets.getStringFromJson(item, "name", key);
200 String type = Assets.getStringFromJson(item, "type", "item");
201 String texture = Assets.getStringFromJson(item, "texture", key);
202 Sprite sprite = type.equals("block") ? null :
203 new Sprite(new Texture(assetLoader.getAssetHandle("textures/items/" + texture + ".png")));
205 if (sprite != null) {
206 sprite.flip(false, true);
209 float originX = Assets.getFloatFromJson(item, "origin_x", 0f);
210 float originY = Assets.getFloatFromJson(item, "origin_y", 1f);
211 originX = MathUtils.clamp(originX, 0f, 1f);
212 originY = MathUtils.clamp(originY, 0f, 1f);
213 SpriteOrigin origin = new SpriteOrigin(originX, originY);
215 int id = Assets.getIntFromJson(item, "id", count);
217 String actionKey = Assets.getStringFromJson(item, "action_key", null);
219 float mobDamage = Assets.getFloatFromJson(item, "mob_damage_multiplier", 1f);
220 float blockDamage = Assets.getFloatFromJson(item, "block_damage_multiplier", 1f);
222 if (count >= id) {
223 count++;
226 CommonItemParams params = new CommonItemParams(id, key, name, origin);
228 Item newItem = switch (type) {
229 case "bucket" -> new Item.Bucket(params, sprite, actionKey);
230 case "shovel" -> new Item.Shovel(params, sprite, mobDamage, blockDamage);
231 case "sword" -> new Item.Sword(params, sprite, mobDamage, blockDamage);
232 case "block" -> new Item.Placeable(params, blocks.get(key));
233 default -> throw new RuntimeException("Unknown item type: " + type);
234 };
236 itemsIds.put(key, id);
237 itemsSet.add(newItem);
238 } catch (GdxRuntimeException e) {
239 Gdx.app.error(TAG, e.getMessage());
243 itemsSet.forEach((item -> items.put(item.getParams().getKey(), item)));