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
.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
;
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();
32 public static boolean isWater(int id
) {
33 return isWater(getBlock(id
));
37 public static boolean isWater(Block block
) {
38 return block
instanceof Block
.Water
;
42 public static boolean isLava(int id
) {
43 return isLava(getBlock(id
));
47 public static boolean isLava(Block block
) {
48 return block
instanceof Block
.Lava
;
52 public static boolean isSlab(int id
) {
53 return getBlock(id
) instanceof Block
.Slab
;
56 public static boolean fluidCanFlowThere(int thisId
, int thatId
) {
57 return thatId
== 0 || (!getBlock(thatId
).hasCollision() && !isFluid(thatId
)) ||
58 (isWater(thisId
) && isWater(thatId
) && thisId
< thatId
) ||
59 (isLava(thisId
) && isLava(thatId
) && thisId
< thatId
);
62 public static Block
getBlock(int id
) {
63 return blocks
.getValueAt(id
);
66 public static Item
getItem(int id
) {
67 return items
.getValueAt(id
);
70 public static Block
getBlock(String key
) {
71 return blocks
.getValueAt(blocksIds
.get(key
));
74 public static Item
getItem(String key
) {
75 return items
.getValueAt(itemsIds
.get(key
));
78 public static int getBlockId(String key
) {
79 return blocksIds
.get(key
);
82 public static int getItemId(String key
) {
83 return itemsIds
.get(key
);
86 public static String
getBlockKey(int id
) {
87 return blocks
.getKeyAt(id
);
90 public static String
getItemKey(int id
) {
91 return items
.getKeyAt(id
);
94 public static int getBlockIdByItemId(int id
) {
95 return getBlockId(items
.getKeyAt(id
));
98 public static int getItemIdByBlockId(int id
) {
99 return getItemId(blocks
.getKeyAt(id
));
102 public static int getBlocksSize() {
106 public static int getItemsSize() {
110 public static Sprite
getBlockTex(int id
) {
111 return getBlock(id
).getTexture();
114 public static Sprite
getItemTex(int id
) {
115 return items
.getValueAt(id
).getType().equals("block") ?
getBlockTex(id
) : getItem(id
).getTexture();
118 public static void load(AssetLoader assetLoader
) {
119 JsonValue json
= Assets
.jsonReader
.parse(assetLoader
.getAssetHandle("json/game_items.json"));
121 TreeSet
<Block
> blocksSet
= new TreeSet
<>(Comparator
.comparingInt(a
-> a
.getParams().getId()));
122 TreeSet
<Item
> itemsSet
= new TreeSet
<>(Comparator
.comparingInt(Item
::getId
));
126 for (JsonValue block
= json
.get("blocks").child(); block
!= null; block
= block
.next()) {
128 String key
= block
.name();
129 int left
= Assets
.getIntFromJson(block
, "left", 0);
130 int right
= Assets
.getIntFromJson(block
, "right", 0);
131 int top
= Assets
.getIntFromJson(block
, "top", 0);
132 int bottom
= Assets
.getIntFromJson(block
, "bottom", 0);
133 int clipX
= Assets
.getIntFromJson(block
, "sprite_left", 0);
134 int clipY
= Assets
.getIntFromJson(block
, "sprite_top", 0);
135 int clipWidth
= Assets
.getIntFromJson(block
, "sprite_right", 0);
136 int clipHeight
= Assets
.getIntFromJson(block
, "sprite_bottom", 0);
137 int hp
= Assets
.getIntFromJson(block
, "hp", -1);
138 boolean collision
= Assets
.getBooleanFromJson(block
, "collision", true);
139 boolean background
= Assets
.getBooleanFromJson(block
, "background", false);
140 boolean transparent
= Assets
.getBooleanFromJson(block
, "transparent", false);
141 boolean requiresBlock
= Assets
.getBooleanFromJson(block
, "block_required", false);
142 boolean fluid
= Assets
.getBooleanFromJson(block
, "fluid", false);
143 String drop
= Assets
.getStringFromJson(block
, "drop", key
);
144 String meta
= Assets
.getStringFromJson(block
, "meta", "");
145 String tex
= Assets
.getStringFromJson(block
, "texture", key
);
146 Texture texture
= tex
.equals("none") ?
null :
147 new Texture(assetLoader
.getAssetHandle("textures/blocks/" + tex
+ ".png"));
148 boolean animated
= Assets
.getBooleanFromJson(block
, "animated", false);
149 int frames
= Assets
.getIntFromJson(block
, "frames", 0);
150 int id
= Assets
.getIntFromJson(block
, "id", count
);
151 int dropCount
= Assets
.getIntFromJson(block
, "drop_count", 1);
152 String fullBlock
= Assets
.getStringFromJson(block
, "full_block", null);
153 blocksIds
.put(key
, id
);
159 BlockMargins collMargins
= new BlockMargins(left
, top
, right
, bottom
);
160 BlockMargins spriteMargins
= new BlockMargins(clipX
, clipY
, clipWidth
, clipHeight
);
161 BlockDropInfo dropInfo
= new BlockDropInfo(drop
, dropCount
);
162 BlockAnimationInfo animInfo
= null;
164 animInfo
= new BlockAnimationInfo(frames
);
167 CommonBlockParams params
= new CommonBlockParams(
182 Block newBlock
= switch (meta
) {
183 case "water" -> new Block
.Water(params
, 5);
184 case "lava" -> new Block
.Lava(params
, 5);
185 case "slab" -> new Block
.Slab(params
, fullBlock
);
186 default -> new Block
.Normal(params
);
189 newBlock
.initialize();
190 blocksSet
.add(newBlock
);
191 } catch (GdxRuntimeException e
) {
192 Gdx
.app
.error(TAG
, e
.getMessage());
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 float originX
= Assets
.getFloatFromJson(item
, "origin_x", 0f);
207 float originY
= Assets
.getFloatFromJson(item
, "origin_y", 1f);
208 originX
= MathUtils
.clamp(originX
, 0f, 1f);
209 originY
= MathUtils
.clamp(originY
, 0f, 1f);
210 SpriteOrigin origin
= new SpriteOrigin(originX
, originY
);
212 int id
= Assets
.getIntFromJson(item
, "id", count
);
214 String actionKey
= Assets
.getStringFromJson(item
, "action_key", null);
220 itemsIds
.put(key
, id
);
221 itemsSet
.add(new Item(id
, key
, name
, type
, sprite
, origin
, actionKey
));
222 } catch (GdxRuntimeException e
) {
223 Gdx
.app
.error(TAG
, e
.getMessage());
227 blocksSet
.forEach((block
-> blocks
.put(block
.getParams().getKey(), block
)));
228 itemsSet
.forEach((item
-> items
.put(item
.getKey(), item
)));