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
.io
.FileInputStream
;
20 public class GameItems
{
22 private static final String TAG
= "GameItems";
24 private static final HashMap
<String
, Integer
> blocksIds
= new HashMap
<>();
25 private static final HashMap
<String
, Integer
> itemsIds
= new HashMap
<>();
27 private static final ArrayMap
<String
, Block
> blocks
= new ArrayMap
<>();
28 private static final ArrayMap
<String
, Item
> items
= new ArrayMap
<>();
30 public static boolean isFluid(int id
) {
31 return getBlock(id
).isFluid();
34 public static boolean isWater(int id
) {
35 return isWater(getBlock(id
));
39 public static boolean isWater(Block block
) {
40 return block
instanceof Block
.Water
;
44 public static boolean isLava(int id
) {
45 return isLava(getBlock(id
));
49 public static boolean isLava(Block block
) {
50 return block
instanceof Block
.Lava
;
54 public static boolean isSlab(int id
) {
55 return getBlock(id
) instanceof Block
.Slab
;
58 public static Block
getBlock(int id
) {
59 return blocks
.getValueAt(id
);
62 public static Item
getItem(int id
) {
63 return items
.getValueAt(id
);
66 public static Block
getBlock(String key
) {
67 return blocks
.getValueAt(blocksIds
.get(key
));
70 public static Item
getItem(String key
) {
71 return items
.getValueAt(itemsIds
.get(key
));
74 public static int getBlockId(String key
) {
75 return blocksIds
.get(key
);
78 public static int getItemId(String key
) {
79 return itemsIds
.get(key
);
82 public static String
getBlockKey(int id
) {
83 return blocks
.getKeyAt(id
);
86 public static String
getItemKey(int id
) {
87 return items
.getKeyAt(id
);
90 public static int getBlockIdByItemId(int id
) {
91 return getBlockId(items
.getKeyAt(id
));
94 public static int getItemIdByBlockId(int id
) {
95 return getItemId(blocks
.getKeyAt(id
));
98 public static int getBlocksSize() {
102 public static int getItemsSize() {
106 public static Sprite
getBlockTex(int id
) {
107 return getBlock(id
).getTexture();
110 public static void load(AssetLoader assetLoader
) {
111 JsonValue json
= Assets
.jsonReader
.parse(assetLoader
.getAssetHandle("json/game_items.json"));
113 TreeSet
<Block
> blocksSet
= new TreeSet
<>(Comparator
.comparingInt(a
-> a
.getParams().getId()));
114 TreeSet
<Item
> itemsSet
= new TreeSet
<>(Comparator
.comparingInt(a
-> a
.getParams().getId()));
118 for (JsonValue block
= json
.get("blocks").child(); block
!= null; block
= block
.next()) {
120 String key
= block
.name();
121 int left
= Assets
.getIntFromJson(block
, "left", 0);
122 int right
= Assets
.getIntFromJson(block
, "right", 0);
123 int top
= Assets
.getIntFromJson(block
, "top", 0);
124 int bottom
= Assets
.getIntFromJson(block
, "bottom", 0);
125 int clipX
= Assets
.getIntFromJson(block
, "sprite_left", 0);
126 int clipY
= Assets
.getIntFromJson(block
, "sprite_top", 0);
127 int clipWidth
= Assets
.getIntFromJson(block
, "sprite_right", 0);
128 int clipHeight
= Assets
.getIntFromJson(block
, "sprite_bottom", 0);
129 int hp
= Assets
.getIntFromJson(block
, "hp", -1);
130 boolean collision
= Assets
.getBooleanFromJson(block
, "collision", true);
131 boolean background
= Assets
.getBooleanFromJson(block
, "background", false);
132 boolean transparent
= Assets
.getBooleanFromJson(block
, "transparent", false);
133 boolean requiresBlock
= Assets
.getBooleanFromJson(block
, "block_required", false);
134 boolean fluid
= Assets
.getBooleanFromJson(block
, "fluid", false);
135 String drop
= Assets
.getStringFromJson(block
, "drop", key
);
136 String meta
= Assets
.getStringFromJson(block
, "meta", "");
137 String tex
= Assets
.getStringFromJson(block
, "texture", key
);
138 Texture texture
= tex
.equals("none") ?
null :
139 new Texture(assetLoader
.getAssetHandle("textures/blocks/" + tex
+ ".png"));
140 boolean animated
= Assets
.getBooleanFromJson(block
, "animated", false);
141 int frames
= Assets
.getIntFromJson(block
, "frames", 0);
142 int id
= Assets
.getIntFromJson(block
, "id", count
);
143 int dropCount
= Assets
.getIntFromJson(block
, "drop_count", 1);
144 String fullBlock
= Assets
.getStringFromJson(block
, "full_block", null);
145 int state
= Assets
.getIntFromJson(block
, "state", 0);
146 blocksIds
.put(key
, id
);
152 BlockMargins collMargins
= new BlockMargins(left
, top
, right
, bottom
);
153 BlockMargins spriteMargins
= new BlockMargins(clipX
, clipY
, clipWidth
, clipHeight
);
154 BlockDropInfo dropInfo
= new BlockDropInfo(drop
, dropCount
);
155 BlockAnimationInfo animInfo
= null;
157 animInfo
= new BlockAnimationInfo(frames
);
160 CommonBlockParams params
= new CommonBlockParams(
175 Block newBlock
= switch (meta
) {
176 case "water" -> new Block
.Water(params
, state
);
177 case "lava" -> new Block
.Lava(params
, state
);
178 case "slab" -> new Block
.Slab(params
, fullBlock
);
179 default -> new Block
.Normal(params
);
182 newBlock
.initialize();
183 blocksSet
.add(newBlock
);
184 } catch (GdxRuntimeException e
) {
185 Gdx
.app
.error(TAG
, e
.getMessage());
189 blocksSet
.forEach((block
-> blocks
.put(block
.getParams().getKey(), block
)));
192 for (JsonValue item
= json
.get("items").child(); item
!= null; item
= item
.next()) {
194 String key
= item
.name();
195 String name
= Assets
.getStringFromJson(item
, "name", key
);
196 String type
= Assets
.getStringFromJson(item
, "type", "item");
197 String texture
= Assets
.getStringFromJson(item
, "texture", key
);
198 Sprite sprite
= type
.equals("block") ?
null :
199 new Sprite(new Texture(assetLoader
.getAssetHandle("textures/items/" + texture
+ ".png")));
201 if (sprite
!= null) {
202 sprite
.flip(false, true);
205 float originX
= Assets
.getFloatFromJson(item
, "origin_x", 0f);
206 float originY
= Assets
.getFloatFromJson(item
, "origin_y", 1f);
207 originX
= MathUtils
.clamp(originX
, 0f, 1f);
208 originY
= MathUtils
.clamp(originY
, 0f, 1f);
209 SpriteOrigin origin
= new SpriteOrigin(originX
, originY
);
211 int id
= Assets
.getIntFromJson(item
, "id", count
);
213 String actionKey
= Assets
.getStringFromJson(item
, "action_key", null);
215 float mobDamage
= Assets
.getFloatFromJson(item
, "mob_damage_multiplier", 1f);
216 float blockDamage
= Assets
.getFloatFromJson(item
, "block_damage_multiplier", 1f);
222 CommonItemParams params
= new CommonItemParams(id
, key
, name
, origin
);
224 Item newItem
= switch (type
) {
225 case "bucket" -> new Item
.Bucket(params
, sprite
, actionKey
);
226 case "shovel" -> new Item
.Shovel(params
, sprite
, mobDamage
, blockDamage
);
227 case "sword" -> new Item
.Sword(params
, sprite
, mobDamage
, blockDamage
);
228 case "block" -> new Item
.Placeable(params
, blocks
.get(key
));
229 default -> throw new RuntimeException("Unknown item type: " + type
);
232 itemsIds
.put(key
, id
);
233 itemsSet
.add(newItem
);
234 } catch (GdxRuntimeException e
) {
235 Gdx
.app
.error(TAG
, e
.getMessage());
239 itemsSet
.forEach((item
-> items
.put(item
.getParams().getKey(), item
)));