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
;
13 import java
.util
.HashMap
;
15 public class GameItems
{
17 private static final String TAG
= "GameItems";
19 private static final HashMap
<String
, Integer
> blocksIds
= new HashMap
<>();
20 private static final HashMap
<String
, Integer
> itemsIds
= new HashMap
<>();
22 private static final ArrayMap
<String
, Block
> blocks
= new ArrayMap
<>();
23 private static final ArrayMap
<String
, Item
> items
= new ArrayMap
<>();
25 public static boolean isFluid(int id
) {
26 return getBlock(id
).isFluid();
29 public static boolean isWater(int id
) {
30 return isWater(getBlock(id
));
33 public static boolean isWater(Block block
) {
34 return block
.getMeta().equals("water");
37 public static boolean isLava(int id
) {
38 return isLava(getBlock(id
));
41 public static boolean isLava(Block block
) {
42 return block
.getMeta().equals("lava");
45 public static boolean isSlab(int id
) {
46 return getBlock(id
).getMeta().equals("slab");
49 public static boolean fluidCanFlowThere(int thisId
, int thatId
) {
50 return thatId
== 0 || (!getBlock(thatId
).hasCollision() && !isFluid(thatId
)) ||
51 (isWater(thisId
) && isWater(thatId
) && thisId
< thatId
) ||
52 (isLava(thisId
) && isLava(thatId
) && thisId
< thatId
);
55 public static Block
getBlock(int id
) {
56 return blocks
.getValueAt(id
);
59 public static Item
getItem(int id
) {
60 return items
.getValueAt(id
);
63 public static Block
getBlock(String key
) {
64 return blocks
.getValueAt(blocksIds
.get(key
));
67 public static Item
getItem(String key
) {
68 return items
.getValueAt(itemsIds
.get(key
));
71 public static int getBlockId(String key
) {
72 return blocksIds
.get(key
);
75 public static int getItemId(String key
) {
76 return itemsIds
.get(key
);
79 public static String
getBlockKey(int id
) {
80 return blocks
.getKeyAt(id
);
83 public static String
getItemKey(int id
) {
84 return items
.getKeyAt(id
);
87 public static int getBlockIdByItemId(int id
) {
88 return getBlockId(items
.getKeyAt(id
));
91 public static int getBlocksSize() {
95 public static int getItemsSize() {
99 public static Sprite
getBlockTex(int id
) {
100 return getBlock(id
).getTexture();
103 public static Sprite
getItemTex(int id
) {
104 return items
.getValueAt(id
).getType().equals("block") ?
getBlockTex(id
) : getItem(id
).getTexture();
107 public static void load() {
108 JsonValue json
= Assets
.jsonReader
.parse(Gdx
.files
.internal("json/game_items.json"));
109 for (JsonValue block
= json
.get("blocks").child(); block
!= null; block
= block
.next()) {
111 String key
= block
.name();
112 int left
= Assets
.getIntFromJson(block
, "left", 0);
113 int right
= Assets
.getIntFromJson(block
, "right", 0);
114 int top
= Assets
.getIntFromJson(block
, "top", 0);
115 int bottom
= Assets
.getIntFromJson(block
, "bottom", 0);
116 int clipX
= Assets
.getIntFromJson(block
, "sprite_left", 0);
117 int clipY
= Assets
.getIntFromJson(block
, "sprite_top", 0);
118 int clipWidth
= Assets
.getIntFromJson(block
, "sprite_right", 0);
119 int clipHeight
= Assets
.getIntFromJson(block
, "sprite_bottom", 0);
120 int hp
= Assets
.getIntFromJson(block
, "hp", -1);
121 boolean collision
= Assets
.getBooleanFromJson(block
, "collision", true);
122 boolean background
= Assets
.getBooleanFromJson(block
, "background", false);
123 boolean transparent
= Assets
.getBooleanFromJson(block
, "transparent", false);
124 boolean requiresBlock
= Assets
.getBooleanFromJson(block
, "block_required", false);
125 boolean fluid
= Assets
.getBooleanFromJson(block
, "fluid", false);
126 String drop
= Assets
.getStringFromJson(block
, "drop", key
);
127 String meta
= Assets
.getStringFromJson(block
, "meta", "");
128 String tex
= Assets
.getStringFromJson(block
, "texture", key
);
129 Texture texture
= tex
.equals("none") ?
null :
130 new Texture(Gdx
.files
.internal("textures/blocks/" + tex
+ ".png"));
131 boolean animated
= Assets
.getBooleanFromJson(block
, "animated", false);
132 int frames
= Assets
.getIntFromJson(block
, "frames", 0);
134 Block newBlock
= new Block(
156 blocksIds
.put(key
, blocks
.size
);
157 blocks
.put(key
, newBlock
);
158 } catch (GdxRuntimeException e
) {
159 Gdx
.app
.error(TAG
, e
.getMessage());
162 for (JsonValue item
= json
.get("items").child(); item
!= null; item
= item
.next()) {
164 String key
= item
.name();
165 String name
= Assets
.getStringFromJson(item
, "name", key
);
166 String type
= Assets
.getStringFromJson(item
, "type", "item");
167 String texture
= Assets
.getStringFromJson(item
, "texture", key
);
168 Sprite sprite
= type
.equals("block") ?
null :
169 new Sprite(new Texture(Gdx
.files
.internal("textures/items/" + texture
+ ".png")));
170 itemsIds
.put(key
, items
.size
);
171 items
.put(key
, new Item(name
, type
, sprite
));
172 } catch (GdxRuntimeException e
) {
173 Gdx
.app
.error(TAG
, e
.getMessage());