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 static boolean isFluid(int id
) {
23 return getBlock(id
).isFluid();
26 static boolean isWater(int id
) {
27 return getBlock(id
).getMeta().equals("water");
30 static boolean isLava(int id
) {
31 return getBlock(id
).getMeta().equals("lava");
34 static boolean isSlab(int id
) {
35 return getBlock(id
).getMeta().equals("slab");
38 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 static Block
getBlock(int id
) {
45 return blocks
.getValueAt(id
);
48 static Item
getItem(int id
) {
49 return items
.getValueAt(id
);
52 static Block
getBlock(String key
) {
53 return blocks
.getValueAt(blocksIds
.get(key
));
56 static Item
getItem(String key
) {
57 return items
.getValueAt(itemsIds
.get(key
));
60 static int getBlockId(String key
) {
61 return blocksIds
.get(key
);
64 static int getItemId(String key
) {
65 return itemsIds
.get(key
);
68 static String
getBlockKey(int id
) {
69 return blocks
.getKeyAt(id
);
72 static String
getItemKey(int id
) {
73 return items
.getKeyAt(id
);
76 static int getBlockIdByItemId(int id
) {
77 return getBlockId(items
.getKeyAt(id
));
80 static int getBlocksSize() {
84 static int getItemsSize() {
88 static Sprite
getBlockTex(int id
) {
89 return getBlock(id
).getTex();
92 static Sprite
getItemTex(int id
) {
93 if (items
.getValueAt(id
).getType().equals("block")) return getBlockTex(id
);
94 else return getItem(id
).getTex();
97 public static void load() {
98 JsonValue json
= Assets
.jsonReader
.parse(Gdx
.files
.internal("json/game_items.json"));
99 for (JsonValue block
= json
.get("blocks").child(); block
!= null; block
= block
.next()) {
100 String key
= block
.name();
101 int left
= block
.has("left") ? block
.getInt("left") : 0;
102 int right
= block
.has("right") ? block
.getInt("right") : 0;
103 int top
= block
.has("top") ? block
.getInt("top") : 0;
104 int bottom
= block
.has("bottom") ? block
.getInt("bottom") : 0;
105 int hp
= block
.has("hp") ? block
.getInt("hp") : -1;
106 String drop
= block
.has("drop") ? block
.getString("drop") : key
;
107 boolean collision
= !block
.has("collision") || block
.getBoolean("collision");
108 boolean background
= block
.has("background") && block
.getBoolean("background");
109 boolean transparent
= block
.has("transparent") && block
.getBoolean("transparent");
110 boolean blockRequired
= block
.has("block_required") && block
.getBoolean("block_required");
111 boolean fluid
= block
.has("fluid") && block
.getBoolean("fluid");
112 String meta
= block
.has("meta") ? block
.getString("meta") : "";
113 String texture
= block
.has("texture") ? block
.getString("texture") : key
;
114 Sprite sprite
= key
.equals("none") ?
null :
115 new Sprite(new Texture(Gdx
.files
.internal("textures/blocks/" + texture
+ ".png")));
116 Block newBlock
= new Block(
132 blocksIds
.put(key
, blocks
.size
);
133 blocks
.put(key
, newBlock
);
135 for (JsonValue item
= json
.get("items").child(); item
!= null; item
= item
.next()) {
136 String key
= item
.name();
137 String name
= item
.has("name") ? item
.getString("name") : key
;
138 String type
= item
.has("type") ? item
.getString("type") : "item";
139 String texture
= item
.has("texture") ? item
.getString("texture") : key
;
140 Sprite sprite
= type
.equals("block") ?
null :
141 new Sprite(new Texture(Gdx
.files
.internal("textures/items/" + texture
+ ".png")));
142 itemsIds
.put(key
, items
.size
);
143 items
.put(key
, new Item(name
, type
, sprite
));