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
.JsonValue
;
8 import ru
.deadsoftware
.cavedroid
.game
.objects
.Block
;
9 import ru
.deadsoftware
.cavedroid
.game
.objects
.Item
;
10 import ru
.deadsoftware
.cavedroid
.misc
.Assets
;
12 import java
.util
.HashMap
;
14 public class GameItems
{
16 private static final HashMap
<String
, Integer
> blocksIds
= new HashMap
<>();
17 private static final HashMap
<String
, Integer
> itemsIds
= new HashMap
<>();
19 private static final ArrayMap
<String
, Block
> blocks
= new ArrayMap
<>();
20 private static final ArrayMap
<String
, Item
> items
= new ArrayMap
<>();
22 public static boolean isFluid(int id
) {
23 return getBlock(id
).isFluid();
26 public static boolean isWater(int id
) {
27 return getBlock(id
).getMeta().equals("water");
30 public static boolean isLava(int id
) {
31 return getBlock(id
).getMeta().equals("lava");
34 public static boolean isSlab(int id
) {
35 return getBlock(id
).getMeta().equals("slab");
38 public static boolean fluidCanFlowThere(int thisId
, int thatId
) {
39 return thatId
== 0 || (!getBlock(thatId
).hasCollision() && !isFluid(thatId
)) ||
40 (isWater(thisId
) && isWater(thatId
) && thisId
< thatId
) ||
41 (isLava(thisId
) && isLava(thatId
) && thisId
< thatId
);
44 public static Block
getBlock(int id
) {
45 return blocks
.getValueAt(id
);
48 public static Item
getItem(int id
) {
49 return items
.getValueAt(id
);
52 public static Block
getBlock(String key
) {
53 return blocks
.getValueAt(blocksIds
.get(key
));
56 public static Item
getItem(String key
) {
57 return items
.getValueAt(itemsIds
.get(key
));
60 public static int getBlockId(String key
) {
61 return blocksIds
.get(key
);
64 public static int getItemId(String key
) {
65 return itemsIds
.get(key
);
68 public static String
getBlockKey(int id
) {
69 return blocks
.getKeyAt(id
);
72 public static String
getItemKey(int id
) {
73 return items
.getKeyAt(id
);
76 public static int getBlockIdByItemId(int id
) {
77 return getBlockId(items
.getKeyAt(id
));
80 public static int getBlocksSize() {
84 public static int getItemsSize() {
88 public static Sprite
getBlockTex(int id
) {
89 return getBlock(id
).getTex();
92 public static Sprite
getItemTex(int id
) {
93 if (items
.getValueAt(id
).getType().equals("block")) {
94 return getBlockTex(id
);
96 return getItem(id
).getTex();
100 public static void load() {
101 JsonValue json
= Assets
.jsonReader
.parse(Gdx
.files
.internal("json/game_items.json"));
102 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 hp
= Assets
.getIntFromJson(block
, "hp", -1);
109 boolean coll
= Assets
.getBooleanFromJson(block
, "collision", true);
110 boolean bg
= Assets
.getBooleanFromJson(block
, "background", false);
111 boolean tp
= Assets
.getBooleanFromJson(block
, "transparent", false);
112 boolean br
= Assets
.getBooleanFromJson(block
, "block_required", false);
113 boolean fluid
= Assets
.getBooleanFromJson(block
, "fluid", false);
114 String drop
= Assets
.getStringFromJson(block
, "drop", key
);
115 String meta
= Assets
.getStringFromJson(block
, "meta", "");
116 String tex
= Assets
.getStringFromJson(block
, "texture", key
);
117 Sprite sprite
= tex
.equals("none") ?
null :
118 new Sprite(new Texture(Gdx
.files
.internal("textures/blocks/" + tex
+ ".png")));
119 Block newBlock
= new Block(left
, top
, right
, bottom
, hp
, drop
, coll
, bg
, tp
, br
, fluid
, meta
, sprite
);
120 blocksIds
.put(key
, blocks
.size
);
121 blocks
.put(key
, newBlock
);
123 for (JsonValue item
= json
.get("items").child(); item
!= null; item
= item
.next()) {
124 String key
= item
.name();
125 String name
= Assets
.getStringFromJson(item
, "name", key
);
126 String type
= Assets
.getStringFromJson(item
, "type", "item");
127 String texture
= Assets
.getStringFromJson(item
, "texture", key
);
128 Sprite sprite
= type
.equals("block") ?
null :
129 new Sprite(new Texture(Gdx
.files
.internal("textures/items/" + texture
+ ".png")));
130 itemsIds
.put(key
, items
.size
);
131 items
.put(key
, new Item(name
, type
, sprite
));