From 0f7f8555c9c892f95e291b7324316acc9e7b1b43 Mon Sep 17 00:00:00 2001 From: fred-boy Date: Mon, 15 Jun 2020 20:45:32 +0700 Subject: [PATCH] Rewrite Block class as Kotlin data class --- .../cavedroid/game/GameItems.java | 66 ++++++----- .../cavedroid/game/objects/Block.java | 112 ------------------ .../cavedroid/game/objects/Block.kt | 76 ++++++++++++ 3 files changed, 115 insertions(+), 139 deletions(-) delete mode 100644 core/src/ru/deadsoftware/cavedroid/game/objects/Block.java create mode 100644 core/src/ru/deadsoftware/cavedroid/game/objects/Block.kt diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameItems.java b/core/src/ru/deadsoftware/cavedroid/game/GameItems.java index f210baa..64fba5a 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameItems.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameItems.java @@ -4,6 +4,7 @@ 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.GdxRuntimeException; import com.badlogic.gdx.utils.JsonValue; import ru.deadsoftware.cavedroid.game.objects.Block; import ru.deadsoftware.cavedroid.game.objects.Item; @@ -13,6 +14,8 @@ import java.util.HashMap; public class GameItems { + private static final String TAG = "GameItems"; + private static final HashMap blocksIds = new HashMap<>(); private static final HashMap itemsIds = new HashMap<>(); @@ -96,35 +99,44 @@ public class GameItems { public static void load() { JsonValue json = Assets.jsonReader.parse(Gdx.files.internal("json/game_items.json")); for (JsonValue block = json.get("blocks").child(); block != null; block = block.next()) { - 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 hp = Assets.getIntFromJson(block, "hp", -1); - boolean coll = Assets.getBooleanFromJson(block, "collision", true); - boolean bg = Assets.getBooleanFromJson(block, "background", false); - boolean tp = Assets.getBooleanFromJson(block, "transparent", false); - boolean br = 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); - Sprite sprite = tex.equals("none") ? null : - new Sprite(new Texture(Gdx.files.internal("textures/blocks/" + tex + ".png"))); - Block newBlock = new Block(left, top, right, bottom, hp, drop, coll, bg, tp, br, fluid, meta, sprite); - blocksIds.put(key, blocks.size); - blocks.put(key, newBlock); + 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 hp = Assets.getIntFromJson(block, "hp", -1); + boolean coll = Assets.getBooleanFromJson(block, "collision", true); + boolean bg = Assets.getBooleanFromJson(block, "background", false); + boolean tp = Assets.getBooleanFromJson(block, "transparent", false); + boolean br = 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); + Sprite sprite = tex.equals("none") ? null : + new Sprite(new Texture(Gdx.files.internal("textures/blocks/" + tex + ".png"))); + + Block newBlock = new Block(left, top, right, bottom, hp, drop, coll, bg, tp, br, fluid, meta, sprite); + blocksIds.put(key, blocks.size); + blocks.put(key, newBlock); + } catch (GdxRuntimeException e) { + Gdx.app.error(TAG, e.getMessage()); + } } for (JsonValue item = json.get("items").child(); item != null; item = item.next()) { - 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)); + 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()); + } } } diff --git a/core/src/ru/deadsoftware/cavedroid/game/objects/Block.java b/core/src/ru/deadsoftware/cavedroid/game/objects/Block.java deleted file mode 100644 index 0800b5b..0000000 --- a/core/src/ru/deadsoftware/cavedroid/game/objects/Block.java +++ /dev/null @@ -1,112 +0,0 @@ -package ru.deadsoftware.cavedroid.game.objects; - -import com.badlogic.gdx.graphics.g2d.Sprite; -import com.badlogic.gdx.math.Rectangle; - -import javax.annotation.CheckForNull; - -public class Block { - - private final int x; - private final int y; - private final int w; - private final int h; - private final int hp; - private final String drop; - private final String meta; - @CheckForNull - private final Sprite tex; - - private final boolean coll; - private final boolean bg; - private final boolean tp; - private final boolean rb; - private final boolean fluid; - - /** - * @param left margin from left edge - * @param top margin from top edge - * @param right margin from right edge - * @param bottom margin from bottom edge - * @param hp hit points - * @param drop id of an item the block will drop when destroyed - * @param coll true if block has collision - * @param bg true if block should be drawn behind player - * @param tp true if block is transparent and renderer should draw a block behind it - * @param rb true if block should break when there is no block with collision under it - * @param fluid true if fluid - * @param meta extra info for storing - * @param tex block's texture - */ - public Block(int left, int top, int right, int bottom, int hp, String drop, boolean coll, boolean bg, boolean tp, - boolean rb, boolean fluid, String meta, @CheckForNull Sprite tex) { - this.x = left; - this.y = top; - this.w = 16 - right - left; - this.h = 16 - top - bottom; - this.hp = hp; - this.drop = drop; - this.coll = coll; - this.bg = bg; - this.tp = tp; - this.rb = rb; - this.fluid = fluid; - this.meta = meta; - this.tex = tex; - if (this.tex != null) { - this.tex.flip(false, true); - } - } - - public boolean hasCollision() { - return coll; - } - - public boolean isBackground() { - return bg; - } - - public boolean isTransparent() { - return tp; - } - - public boolean requiresBlock() { - return rb; - } - - public int getHp() { - return hp; - } - - public String getDrop() { - return drop; - } - - public boolean hasDrop() { - return !drop.equals("none"); - } - - public Sprite getTexture() { - assert tex != null; - return tex; - } - - public Rectangle getRectangle(int x, int y) { - x *= 16; - y *= 16; - return new Rectangle(x + this.x, y + this.y, w, h); - } - - public boolean isFluid() { - return fluid; - } - - public String getMeta() { - return meta; - } - - public boolean toJump() { - return (y < 8 && coll); - } - -} \ No newline at end of file diff --git a/core/src/ru/deadsoftware/cavedroid/game/objects/Block.kt b/core/src/ru/deadsoftware/cavedroid/game/objects/Block.kt new file mode 100644 index 0000000..e8ed1e7 --- /dev/null +++ b/core/src/ru/deadsoftware/cavedroid/game/objects/Block.kt @@ -0,0 +1,76 @@ +@file:Suppress("DeprecatedCallableAddReplaceWith") + +package ru.deadsoftware.cavedroid.game.objects + +import com.badlogic.gdx.graphics.g2d.Sprite +import com.badlogic.gdx.math.Rectangle + +private const val DEPRECATION_MESSAGE = + "Deprecated since moved to Kotlin. Use generated getter or kotlin property access." + +/** + * @param left margin from left edge + * @param top margin from top edge + * @param right margin from right edge + * @param bottom margin from bottom edge + * @param hp hit points + * @param drop id of an item the block will drop when destroyed + * @param collision true if block has collision + * @param background true if block should be drawn behind player + * @param transparent true if block is transparent and renderer should draw a block behind it + * @param requiresBlock true if block should break when there is no block with collision under it + * @param fluid true if fluid + * @param meta extra info for storing + * @param sprite block's texture + */ +data class Block( + val left: Int, + val top: Int, + val right: Int, + val bottom: Int, + val hp: Int, + val drop: String, + val collision: Boolean, + val background: Boolean, + val transparent: Boolean, + val requiresBlock: Boolean, + val fluid: Boolean, + val meta: String, + val sprite: Sprite? +) { + + init { + sprite?.flip(false, true) + } + + val width = 16 - right - left + val height = 16 - top - bottom + + fun getRectangle(x: Int, y: Int) = + Rectangle(x * 16f + left, y * 16f + this.top, width.toFloat(), height.toFloat()) + + fun requireSprite() = sprite ?: throw IllegalStateException("Sprite is null") + + fun hasDrop() = drop != "none" + + fun toJump() = top < 8 && collision + + @Deprecated(DEPRECATION_MESSAGE) + fun hasCollision() = collision + + @Deprecated(DEPRECATION_MESSAGE) + fun isBackground() = background + + @Deprecated(DEPRECATION_MESSAGE) + fun isTransparent() = transparent + + @Deprecated(DEPRECATION_MESSAGE) + fun isFluid() = fluid + + @Deprecated(DEPRECATION_MESSAGE) + fun requiresBlock() = requiresBlock + + @Deprecated("Was renamed to Sprite to comply with variable type.", ReplaceWith("getSprite()")) + fun getTexture() = sprite + +} \ No newline at end of file -- 2.29.2