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 getBlock(id
).getMeta().equals("water");
33 public static boolean isLava(int id
) {
34 return getBlock(id
).getMeta().equals("lava");
37 public static boolean isSlab(int id
) {
38 return getBlock(id
).getMeta().equals("slab");
41 public static boolean fluidCanFlowThere(int thisId
, int thatId
) {
42 return thatId
== 0 || (!getBlock(thatId
).hasCollision() && !isFluid(thatId
)) ||
43 (isWater(thisId
) && isWater(thatId
) && thisId
< thatId
) ||
44 (isLava(thisId
) && isLava(thatId
) && thisId
< thatId
);
47 public static Block
getBlock(int id
) {
48 return blocks
.getValueAt(id
);
51 public static Item
getItem(int id
) {
52 return items
.getValueAt(id
);
55 public static Block
getBlock(String key
) {
56 return blocks
.getValueAt(blocksIds
.get(key
));
59 public static Item
getItem(String key
) {
60 return items
.getValueAt(itemsIds
.get(key
));
63 public static int getBlockId(String key
) {
64 return blocksIds
.get(key
);
67 public static int getItemId(String key
) {
68 return itemsIds
.get(key
);
71 public static String
getBlockKey(int id
) {
72 return blocks
.getKeyAt(id
);
75 public static String
getItemKey(int id
) {
76 return items
.getKeyAt(id
);
79 public static int getBlockIdByItemId(int id
) {
80 return getBlockId(items
.getKeyAt(id
));
83 public static int getBlocksSize() {
87 public static int getItemsSize() {
91 public static Sprite
getBlockTex(int id
) {
92 return getBlock(id
).getTexture();
95 public static Sprite
getItemTex(int id
) {
96 return items
.getValueAt(id
).getType().equals("block") ?
getBlockTex(id
) : getItem(id
).getTexture();
99 public static void load() {
100 JsonValue json
= Assets
.jsonReader
.parse(Gdx
.files
.internal("json/game_items.json"));
101 for (JsonValue block
= json
.get("blocks").child(); block
!= null; block
= block
.next()) {
103 String key
= block
.name();
104 int left
= Assets
.getIntFromJson(block
, "left", 0);
105 int right
= Assets
.getIntFromJson(block
, "right", 0);
106 int top
= Assets
.getIntFromJson(block
, "top", 0);
107 int bottom
= Assets
.getIntFromJson(block
, "bottom", 0);
108 int clipX
= Assets
.getIntFromJson(block
, "sprite_left", 0);
109 int clipY
= Assets
.getIntFromJson(block
, "sprite_top", 0);
110 int clipWidth
= Assets
.getIntFromJson(block
, "sprite_right", 0);
111 int clipHeight
= Assets
.getIntFromJson(block
, "sprite_bottom", 0);
112 int hp
= Assets
.getIntFromJson(block
, "hp", -1);
113 boolean collision
= Assets
.getBooleanFromJson(block
, "collision", true);
114 boolean background
= Assets
.getBooleanFromJson(block
, "background", false);
115 boolean transparent
= Assets
.getBooleanFromJson(block
, "transparent", false);
116 boolean requiresBlock
= Assets
.getBooleanFromJson(block
, "block_required", false);
117 boolean fluid
= Assets
.getBooleanFromJson(block
, "fluid", false);
118 String drop
= Assets
.getStringFromJson(block
, "drop", key
);
119 String meta
= Assets
.getStringFromJson(block
, "meta", "");
120 String tex
= Assets
.getStringFromJson(block
, "texture", key
);
121 Texture texture
= tex
.equals("none") ?
null :
122 new Texture(Gdx
.files
.internal("textures/blocks/" + tex
+ ".png"));
123 boolean animated
= Assets
.getBooleanFromJson(block
, "animated", false);
124 int frames
= Assets
.getIntFromJson(block
, "frames", 0);
126 Block newBlock
= new Block(
148 blocksIds
.put(key
, blocks
.size
);
149 blocks
.put(key
, newBlock
);
150 } catch (GdxRuntimeException e
) {
151 Gdx
.app
.error(TAG
, e
.getMessage());
154 for (JsonValue item
= json
.get("items").child(); item
!= null; item
= item
.next()) {
156 String key
= item
.name();
157 String name
= Assets
.getStringFromJson(item
, "name", key
);
158 String type
= Assets
.getStringFromJson(item
, "type", "item");
159 String texture
= Assets
.getStringFromJson(item
, "texture", key
);
160 Sprite sprite
= type
.equals("block") ?
null :
161 new Sprite(new Texture(Gdx
.files
.internal("textures/items/" + texture
+ ".png")));
162 itemsIds
.put(key
, items
.size
);
163 items
.put(key
, new Item(name
, type
, sprite
));
164 } catch (GdxRuntimeException e
) {
165 Gdx
.app
.error(TAG
, e
.getMessage());