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 boolean fluidCanFlowThere(int thisId
, int thatId
) {
59 return thatId
== 0 || (!getBlock(thatId
).hasCollision() && !isFluid(thatId
)) ||
60 (isWater(thisId
) && isWater(thatId
) && thisId
< thatId
) ||
61 (isLava(thisId
) && isLava(thatId
) && thisId
< thatId
);
64 public static Block
getBlock(int id
) {
65 return blocks
.getValueAt(id
);
68 public static Item
getItem(int id
) {
69 return items
.getValueAt(id
);
72 public static Block
getBlock(String key
) {
73 return blocks
.getValueAt(blocksIds
.get(key
));
76 public static Item
getItem(String key
) {
77 return items
.getValueAt(itemsIds
.get(key
));
80 public static int getBlockId(String key
) {
81 return blocksIds
.get(key
);
84 public static int getItemId(String key
) {
85 return itemsIds
.get(key
);
88 public static String
getBlockKey(int id
) {
89 return blocks
.getKeyAt(id
);
92 public static String
getItemKey(int id
) {
93 return items
.getKeyAt(id
);
96 public static int getBlockIdByItemId(int id
) {
97 return getBlockId(items
.getKeyAt(id
));
100 public static int getItemIdByBlockId(int id
) {
101 return getItemId(blocks
.getKeyAt(id
));
104 public static int getBlocksSize() {
108 public static int getItemsSize() {
112 public static Sprite
getBlockTex(int id
) {
113 return getBlock(id
).getTexture();
116 public static void load(AssetLoader assetLoader
) {
117 JsonValue json
= Assets
.jsonReader
.parse(assetLoader
.getAssetHandle("json/game_items.json"));
119 TreeSet
<Block
> blocksSet
= new TreeSet
<>(Comparator
.comparingInt(a
-> a
.getParams().getId()));
120 TreeSet
<Item
> itemsSet
= new TreeSet
<>(Comparator
.comparingInt(a
-> a
.getParams().getId()));
124 for (JsonValue block
= json
.get("blocks").child(); block
!= null; block
= block
.next()) {
126 String key
= block
.name();
127 int left
= Assets
.getIntFromJson(block
, "left", 0);
128 int right
= Assets
.getIntFromJson(block
, "right", 0);
129 int top
= Assets
.getIntFromJson(block
, "top", 0);
130 int bottom
= Assets
.getIntFromJson(block
, "bottom", 0);
131 int clipX
= Assets
.getIntFromJson(block
, "sprite_left", 0);
132 int clipY
= Assets
.getIntFromJson(block
, "sprite_top", 0);
133 int clipWidth
= Assets
.getIntFromJson(block
, "sprite_right", 0);
134 int clipHeight
= Assets
.getIntFromJson(block
, "sprite_bottom", 0);
135 int hp
= Assets
.getIntFromJson(block
, "hp", -1);
136 boolean collision
= Assets
.getBooleanFromJson(block
, "collision", true);
137 boolean background
= Assets
.getBooleanFromJson(block
, "background", false);
138 boolean transparent
= Assets
.getBooleanFromJson(block
, "transparent", false);
139 boolean requiresBlock
= Assets
.getBooleanFromJson(block
, "block_required", false);
140 boolean fluid
= Assets
.getBooleanFromJson(block
, "fluid", false);
141 String drop
= Assets
.getStringFromJson(block
, "drop", key
);
142 String meta
= Assets
.getStringFromJson(block
, "meta", "");
143 String tex
= Assets
.getStringFromJson(block
, "texture", key
);
144 Texture texture
= tex
.equals("none") ?
null :
145 new Texture(assetLoader
.getAssetHandle("textures/blocks/" + tex
+ ".png"));
146 boolean animated
= Assets
.getBooleanFromJson(block
, "animated", false);
147 int frames
= Assets
.getIntFromJson(block
, "frames", 0);
148 int id
= Assets
.getIntFromJson(block
, "id", count
);
149 int dropCount
= Assets
.getIntFromJson(block
, "drop_count", 1);
150 String fullBlock
= Assets
.getStringFromJson(block
, "full_block", null);
151 blocksIds
.put(key
, id
);
157 BlockMargins collMargins
= new BlockMargins(left
, top
, right
, bottom
);
158 BlockMargins spriteMargins
= new BlockMargins(clipX
, clipY
, clipWidth
, clipHeight
);
159 BlockDropInfo dropInfo
= new BlockDropInfo(drop
, dropCount
);
160 BlockAnimationInfo animInfo
= null;
162 animInfo
= new BlockAnimationInfo(frames
);
165 CommonBlockParams params
= new CommonBlockParams(
180 Block newBlock
= switch (meta
) {
181 case "water" -> new Block
.Water(params
);
182 case "lava" -> new Block
.Lava(params
);
183 case "slab" -> new Block
.Slab(params
, fullBlock
);
184 default -> new Block
.Normal(params
);
187 newBlock
.initialize();
188 blocksSet
.add(newBlock
);
189 } catch (GdxRuntimeException e
) {
190 Gdx
.app
.error(TAG
, e
.getMessage());
194 blocksSet
.forEach((block
-> blocks
.put(block
.getParams().getKey(), block
)));
197 for (JsonValue item
= json
.get("items").child(); item
!= null; item
= item
.next()) {
199 String key
= item
.name();
200 String name
= Assets
.getStringFromJson(item
, "name", key
);
201 String type
= Assets
.getStringFromJson(item
, "type", "item");
202 String texture
= Assets
.getStringFromJson(item
, "texture", key
);
203 Sprite sprite
= type
.equals("block") ?
null :
204 new Sprite(new Texture(assetLoader
.getAssetHandle("textures/items/" + texture
+ ".png")));
206 if (sprite
!= null) {
207 sprite
.flip(false, true);
210 float originX
= Assets
.getFloatFromJson(item
, "origin_x", 0f);
211 float originY
= Assets
.getFloatFromJson(item
, "origin_y", 1f);
212 originX
= MathUtils
.clamp(originX
, 0f, 1f);
213 originY
= MathUtils
.clamp(originY
, 0f, 1f);
214 SpriteOrigin origin
= new SpriteOrigin(originX
, originY
);
216 int id
= Assets
.getIntFromJson(item
, "id", count
);
218 String actionKey
= Assets
.getStringFromJson(item
, "action_key", null);
220 float mobDamage
= Assets
.getFloatFromJson(item
, "mob_damage_multiplier", 1f);
221 float blockDamage
= Assets
.getFloatFromJson(item
, "block_damage_multiplier", 1f);
227 CommonItemParams params
= new CommonItemParams(id
, key
, name
, origin
);
229 Item newItem
= switch (type
) {
230 case "bucket" -> new Item
.Bucket(params
, sprite
, actionKey
);
231 case "shovel" -> new Item
.Shovel(params
, sprite
, mobDamage
, blockDamage
);
232 case "sword" -> new Item
.Sword(params
, sprite
, mobDamage
, blockDamage
);
233 case "block" -> new Item
.Placeable(params
, blocks
.get(key
));
234 default -> throw new RuntimeException("Unknown item type: " + type
);
237 itemsIds
.put(key
, id
);
238 itemsSet
.add(newItem
);
239 } catch (GdxRuntimeException e
) {
240 Gdx
.app
.error(TAG
, e
.getMessage());
244 itemsSet
.forEach((item
-> items
.put(item
.getParams().getKey(), item
)));