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
.utils
.ArrayMap
;
7 import com
.badlogic
.gdx
.utils
.GdxRuntimeException
;
8 import com
.badlogic
.gdx
.utils
.JsonValue
;
9 import ru
.deadsoftware
.cavedroid
.game
.objects
.Block
;
10 import ru
.deadsoftware
.cavedroid
.game
.objects
.Item
;
11 import ru
.deadsoftware
.cavedroid
.misc
.Assets
;
12 import ru
.deadsoftware
.cavedroid
.misc
.utils
.AssetLoader
;
14 import java
.util
.HashMap
;
16 public class GameItems
{
18 private static final String TAG
= "GameItems";
20 private static final HashMap
<String
, Integer
> blocksIds
= new HashMap
<>();
21 private static final HashMap
<String
, Integer
> itemsIds
= new HashMap
<>();
23 private static final ArrayMap
<String
, Block
> blocks
= new ArrayMap
<>();
24 private static final ArrayMap
<String
, Item
> items
= new ArrayMap
<>();
26 public static boolean isFluid(int id
) {
27 return getBlock(id
).isFluid();
30 public static boolean isWater(int id
) {
31 return isWater(getBlock(id
));
34 public static boolean isWater(Block block
) {
35 return block
.getMeta().equals("water");
38 public static boolean isLava(int id
) {
39 return isLava(getBlock(id
));
42 public static boolean isLava(Block block
) {
43 return block
.getMeta().equals("lava");
46 public static boolean isSlab(int id
) {
47 return getBlock(id
).getMeta().equals("slab");
50 public static boolean fluidCanFlowThere(int thisId
, int thatId
) {
51 return thatId
== 0 || (!getBlock(thatId
).hasCollision() && !isFluid(thatId
)) ||
52 (isWater(thisId
) && isWater(thatId
) && thisId
< thatId
) ||
53 (isLava(thisId
) && isLava(thatId
) && thisId
< thatId
);
56 public static Block
getBlock(int id
) {
57 return blocks
.getValueAt(id
);
60 public static Item
getItem(int id
) {
61 return items
.getValueAt(id
);
64 public static Block
getBlock(String key
) {
65 return blocks
.getValueAt(blocksIds
.get(key
));
68 public static Item
getItem(String key
) {
69 return items
.getValueAt(itemsIds
.get(key
));
72 public static int getBlockId(String key
) {
73 return blocksIds
.get(key
);
76 public static int getItemId(String key
) {
77 return itemsIds
.get(key
);
80 public static String
getBlockKey(int id
) {
81 return blocks
.getKeyAt(id
);
84 public static String
getItemKey(int id
) {
85 return items
.getKeyAt(id
);
88 public static int getBlockIdByItemId(int id
) {
89 return getBlockId(items
.getKeyAt(id
));
92 public static int getBlocksSize() {
96 public static int getItemsSize() {
100 public static Sprite
getBlockTex(int id
) {
101 return getBlock(id
).getTexture();
104 public static Sprite
getItemTex(int id
) {
105 return items
.getValueAt(id
).getType().equals("block") ?
getBlockTex(id
) : getItem(id
).getTexture();
108 public static void load(AssetLoader assetLoader
) {
109 JsonValue json
= Assets
.jsonReader
.parse(assetLoader
.getAssetHandle("json/game_items.json"));
110 for (JsonValue block
= json
.get("blocks").child(); block
!= null; block
= block
.next()) {
112 String key
= block
.name();
113 int left
= Assets
.getIntFromJson(block
, "left", 0);
114 int right
= Assets
.getIntFromJson(block
, "right", 0);
115 int top
= Assets
.getIntFromJson(block
, "top", 0);
116 int bottom
= Assets
.getIntFromJson(block
, "bottom", 0);
117 int clipX
= Assets
.getIntFromJson(block
, "sprite_left", 0);
118 int clipY
= Assets
.getIntFromJson(block
, "sprite_top", 0);
119 int clipWidth
= Assets
.getIntFromJson(block
, "sprite_right", 0);
120 int clipHeight
= Assets
.getIntFromJson(block
, "sprite_bottom", 0);
121 int hp
= Assets
.getIntFromJson(block
, "hp", -1);
122 boolean collision
= Assets
.getBooleanFromJson(block
, "collision", true);
123 boolean background
= Assets
.getBooleanFromJson(block
, "background", false);
124 boolean transparent
= Assets
.getBooleanFromJson(block
, "transparent", false);
125 boolean requiresBlock
= Assets
.getBooleanFromJson(block
, "block_required", false);
126 boolean fluid
= Assets
.getBooleanFromJson(block
, "fluid", false);
127 String drop
= Assets
.getStringFromJson(block
, "drop", key
);
128 String meta
= Assets
.getStringFromJson(block
, "meta", "");
129 String tex
= Assets
.getStringFromJson(block
, "texture", key
);
130 Texture texture
= tex
.equals("none") ?
null :
131 new Texture(assetLoader
.getAssetHandle("textures/blocks/" + tex
+ ".png"));
132 boolean animated
= Assets
.getBooleanFromJson(block
, "animated", false);
133 int frames
= Assets
.getIntFromJson(block
, "frames", 0);
135 Block newBlock
= new Block(
157 blocksIds
.put(key
, blocks
.size
);
158 blocks
.put(key
, newBlock
);
159 } catch (GdxRuntimeException e
) {
160 Gdx
.app
.error(TAG
, e
.getMessage());
163 for (JsonValue item
= json
.get("items").child(); item
!= null; item
= item
.next()) {
165 String key
= item
.name();
166 String name
= Assets
.getStringFromJson(item
, "name", key
);
167 String type
= Assets
.getStringFromJson(item
, "type", "item");
168 String texture
= Assets
.getStringFromJson(item
, "texture", key
);
169 Sprite sprite
= type
.equals("block") ?
null :
170 new Sprite(new Texture(assetLoader
.getAssetHandle("textures/items/" + texture
+ ".png")));
171 itemsIds
.put(key
, items
.size
);
172 items
.put(key
, new Item(name
, type
, sprite
));
173 } catch (GdxRuntimeException e
) {
174 Gdx
.app
.error(TAG
, e
.getMessage());