diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameItems.java b/core/src/ru/deadsoftware/cavedroid/game/GameItems.java
index 56a2b5860eb6df91ef1691207e37e80d631491d7..a5d6c23f716681c78acf0d34f923d6fedbbaf3b0 100644 (file)
import ru.deadsoftware.cavedroid.misc.utils.AssetLoader;
import ru.deadsoftware.cavedroid.misc.utils.SpriteOrigin;
-import java.util.HashMap;
+import java.util.*;
public class GameItems {
return getBlockId(items.getKeyAt(id));
}
+ public static int getItemIdByBlockId(int id) {
+ return getItemId(blocks.getKeyAt(id));
+ }
+
public static int getBlocksSize() {
return blocks.size;
}
public static void load(AssetLoader assetLoader) {
JsonValue json = Assets.jsonReader.parse(assetLoader.getAssetHandle("json/game_items.json"));
+
+ TreeSet<Block> blocksSet = new TreeSet<>(Comparator.comparingInt(Block::getId));
+ TreeSet<Item> itemsSet = new TreeSet<>(Comparator.comparingInt(Item::getId));
+
+
+ int count = 0;
for (JsonValue block = json.get("blocks").child(); block != null; block = block.next()) {
try {
String key = block.name();
new Texture(assetLoader.getAssetHandle("textures/blocks/" + tex + ".png"));
boolean animated = Assets.getBooleanFromJson(block, "animated", false);
int frames = Assets.getIntFromJson(block, "frames", 0);
- int id = blocks.size;
+ int id = Assets.getIntFromJson(block, "id", count);
blocksIds.put(key, id);
+ if (count >= id) {
+ count++;
+ }
+
Block newBlock = new Block(
id,
+ key,
left,
top,
right,
clipWidth,
clipHeight
);
- blocks.put(key, newBlock);
+ blocksSet.add(newBlock);
} catch (GdxRuntimeException e) {
Gdx.app.error(TAG, e.getMessage());
}
}
+
+ count = 0;
for (JsonValue item = json.get("items").child(); item != null; item = item.next()) {
try {
String key = item.name();
originY = MathUtils.clamp(originY, 0f, 1f);
SpriteOrigin origin = new SpriteOrigin(originX, originY);
- int id = items.size;
+ int id = Assets.getIntFromJson(item, "id", count);
+
+ if (count >= id) {
+ count++;
+ }
+
itemsIds.put(key, id);
- items.put(key, new Item(id, name, type, sprite, origin));
+ itemsSet.add(new Item(id, key, name, type, sprite, origin));
} catch (GdxRuntimeException e) {
Gdx.app.error(TAG, e.getMessage());
}
}
+
+ blocksSet.forEach((block -> blocks.put(block.getKey(), block)));
+ itemsSet.forEach((item -> items.put(item.getKey(), item)));
}
}
\ No newline at end of file