DEADSOFTWARE

Support different block texture sizes and animation
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / GameItems.java
index c3e020791570a456bf906f41165d8a76cb654171..85f4b9c765401ba0c2cf851d0ece119043b4d19c 100644 (file)
@@ -4,20 +4,23 @@ import com.badlogic.gdx.Gdx;
 import com.badlogic.gdx.graphics.Texture;
 import com.badlogic.gdx.graphics.g2d.Sprite;
 import com.badlogic.gdx.utils.ArrayMap;
-import com.badlogic.gdx.utils.JsonReader;
+import com.badlogic.gdx.utils.GdxRuntimeException;
 import com.badlogic.gdx.utils.JsonValue;
 import ru.deadsoftware.cavedroid.game.objects.Block;
 import ru.deadsoftware.cavedroid.game.objects.Item;
+import ru.deadsoftware.cavedroid.misc.Assets;
 
 import java.util.HashMap;
 
 public class GameItems {
 
-    private static HashMap<String, Integer> blocksIds = new HashMap<>();
-    private static HashMap<String, Integer> itemsIds = new HashMap<>();
+    private static final String TAG = "GameItems";
 
-    private static ArrayMap<String, Block> blocks = new ArrayMap<>();
-    private static ArrayMap<String, Item> items = new ArrayMap<>();
+    private static final HashMap<String, Integer> blocksIds = new HashMap<>();
+    private static final HashMap<String, Integer> itemsIds = new HashMap<>();
+
+    private static final ArrayMap<String, Block> blocks = new ArrayMap<>();
+    private static final ArrayMap<String, Item> items = new ArrayMap<>();
 
     public static boolean isFluid(int id) {
         return getBlock(id).isFluid();
@@ -37,8 +40,8 @@ public class GameItems {
 
     public static boolean fluidCanFlowThere(int thisId, int thatId) {
         return thatId == 0 || (!getBlock(thatId).hasCollision() && !isFluid(thatId)) ||
-                (isWater(thisId) && isWater(thatId) && thatId >= thisId) ||
-                (isLava(thisId) && isLava(thatId) && thatId >= thisId);
+                (isWater(thisId) && isWater(thatId) && thisId < thatId) ||
+                (isLava(thisId) && isLava(thatId) && thisId < thatId);
     }
 
     public static Block getBlock(int id) {
@@ -86,49 +89,81 @@ public class GameItems {
     }
 
     public static Sprite getBlockTex(int id) {
-        return getBlock(id).getTex();
+        return getBlock(id).getTexture();
     }
 
     public static Sprite getItemTex(int id) {
-        if (items.getValueAt(id).getType().equals("block")) return getBlockTex(id);
-        else return getItem(id).getTex();
+        return items.getValueAt(id).getType().equals("block") ? getBlockTex(id) : getItem(id).getTexture();
     }
 
     public static void load() {
-        JsonValue json = new JsonReader().parse(Gdx.files.internal("game_items.json"));
-        JsonValue block = json.child.child;
-        JsonValue item = json.child.next.child;
-        while (block != null) {
-            String key = block.name;
-            int left = (block.has("left") ? block.getInt("left") : 0);
-            int right = (block.has("right") ? block.getInt("right") : 0);
-            int top = (block.has("top") ? block.getInt("top") : 0);
-            int bottom = (block.has("bottom") ? block.getInt("bottom") : 0);
-            int hp = (block.has("hp") ? block.getInt("hp") : -1);
-            String drop = (block.has("drop") ? block.getString("drop") : key);
-            boolean collision = (!block.has("collision") || block.getBoolean("collision"));
-            boolean background = (block.has("background") && block.getBoolean("background"));
-            boolean transparent = !(!block.has("collision") || block.getBoolean("collision"));
-            boolean blockRequired = (block.has("block_required") && block.getBoolean("block_required"));
-            boolean fluid = (block.has("fluid") && block.getBoolean("fluid"));
-            String meta = (block.has("meta") ? block.getString("meta") : "");
-            String texture = (block.has("texture") ? block.getString("texture") : key);
-            blocksIds.put(key, blocks.size);
-            blocks.put(key, new Block(left, top, right, bottom, hp, drop, collision,
-                    background, transparent, blockRequired, fluid, meta,
-                    key.equals("none") ? null :
-                            new Sprite(new Texture(Gdx.files.internal("textures/" + texture + ".png")))));
-            block = block.next();
+        JsonValue json = Assets.jsonReader.parse(Gdx.files.internal("json/game_items.json"));
+        for (JsonValue block = json.get("blocks").child(); block != null; block = block.next()) {
+            try {
+                String key = block.name();
+                int left = Assets.getIntFromJson(block, "left", 0);
+                int right = Assets.getIntFromJson(block, "right", 0);
+                int top = Assets.getIntFromJson(block, "top", 0);
+                int bottom = Assets.getIntFromJson(block, "bottom", 0);
+                int clipX = Assets.getIntFromJson(block, "sprite_left", 0);
+                int clipY = Assets.getIntFromJson(block, "sprite_top", 0);
+                int clipWidth = Assets.getIntFromJson(block, "sprite_right", 0);
+                int clipHeight = Assets.getIntFromJson(block, "sprite_bottom", 0);
+                int hp = Assets.getIntFromJson(block, "hp", -1);
+                boolean collision = Assets.getBooleanFromJson(block, "collision", true);
+                boolean background = Assets.getBooleanFromJson(block, "background", false);
+                boolean transparent = Assets.getBooleanFromJson(block, "transparent", false);
+                boolean requiresBlock = Assets.getBooleanFromJson(block, "block_required", false);
+                boolean fluid = Assets.getBooleanFromJson(block, "fluid", false);
+                String drop = Assets.getStringFromJson(block, "drop", key);
+                String meta = Assets.getStringFromJson(block, "meta", "");
+                String tex = Assets.getStringFromJson(block, "texture", key);
+                Texture texture = tex.equals("none") ? null :
+                        new Texture(Gdx.files.internal("textures/blocks/" + tex + ".png"));
+                boolean animated = Assets.getBooleanFromJson(block, "animated", false);
+                int frames = Assets.getIntFromJson(block, "frames", 0);
+
+                Block newBlock = new Block(
+                        left,
+                        top,
+                        right,
+                        bottom,
+                        hp,
+                        drop,
+                        collision,
+                        background,
+                        transparent,
+                        requiresBlock,
+                        fluid,
+                        meta,
+                        texture,
+                        animated,
+                        frames,
+                        clipX,
+                        clipY,
+                        clipWidth,
+                        clipHeight
+                );
+
+                blocksIds.put(key, blocks.size);
+                blocks.put(key, newBlock);
+            } catch (GdxRuntimeException e) {
+                Gdx.app.error(TAG, e.getMessage());
+            }
         }
-        while (item != null) {
-            String key = item.name;
-            String name = (item.has("name") ? item.getString("name") : key);
-            String type = (item.has("type") ? item.getString("type") : "item");
-            String texture = (item.has("texture") ? item.getString("texture") : key);
-            itemsIds.put(key, items.size);
-            items.put(key, new Item(name, type, type.equals("block") ? null :
-                    new Sprite(new Texture(Gdx.files.internal("textures/" + texture + ".png")))));
-            item = item.next();
+        for (JsonValue item = json.get("items").child(); item != null; item = item.next()) {
+            try {
+                String key = item.name();
+                String name = Assets.getStringFromJson(item, "name", key);
+                String type = Assets.getStringFromJson(item, "type", "item");
+                String texture = Assets.getStringFromJson(item, "texture", key);
+                Sprite sprite = type.equals("block") ? null :
+                        new Sprite(new Texture(Gdx.files.internal("textures/items/" + texture + ".png")));
+                itemsIds.put(key, items.size);
+                items.put(key, new Item(name, type, sprite));
+            } catch (GdxRuntimeException e) {
+                Gdx.app.error(TAG, e.getMessage());
+            }
         }
     }