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
;
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();
33 public static boolean isWater(int id
) {
34 return isWater(getBlock(id
));
38 public static boolean isWater(Block block
) {
39 return block
instanceof Block
.Water
;
43 public static boolean isLava(int id
) {
44 return isLava(getBlock(id
));
48 public static boolean isLava(Block block
) {
49 return block
instanceof Block
.Lava
;
53 public static boolean isSlab(int id
) {
54 return getBlock(id
) instanceof Block
.Slab
;
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
);
63 public static Block
getBlock(int id
) {
64 return blocks
.getValueAt(id
);
67 public static Item
getItem(int id
) {
68 return items
.getValueAt(id
);
71 public static Block
getBlock(String key
) {
72 return blocks
.getValueAt(blocksIds
.get(key
));
75 public static Item
getItem(String key
) {
76 return items
.getValueAt(itemsIds
.get(key
));
79 public static int getBlockId(String key
) {
80 return blocksIds
.get(key
);
83 public static int getItemId(String key
) {
84 return itemsIds
.get(key
);
87 public static String
getBlockKey(int id
) {
88 return blocks
.getKeyAt(id
);
91 public static String
getItemKey(int id
) {
92 return items
.getKeyAt(id
);
95 public static int getBlockIdByItemId(int id
) {
96 return getBlockId(items
.getKeyAt(id
));
99 public static int getItemIdByBlockId(int id
) {
100 return getItemId(blocks
.getKeyAt(id
));
103 public static int getBlocksSize() {
107 public static int getItemsSize() {
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()));
123 for (JsonValue block
= json
.get("blocks").child(); block
!= null; block
= block
.next()) {
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
);
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;
161 animInfo
= new BlockAnimationInfo(frames
);
164 CommonBlockParams params
= new CommonBlockParams(
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
);
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
)));
196 for (JsonValue item
= json
.get("items").child(); item
!= null; item
= item
.next()) {
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);
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
);
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
)));