DEADSOFTWARE

Closes #22: Rewrite Item class as Kotlin data class
authorfred-boy <fredboy@protonmail.com>
Mon, 15 Jun 2020 20:52:44 +0000 (03:52 +0700)
committerfredboy <fredboy@protonmail.com>
Tue, 16 Jun 2020 11:57:48 +0000 (18:57 +0700)
core/src/ru/deadsoftware/cavedroid/game/objects/Item.java [deleted file]
core/src/ru/deadsoftware/cavedroid/game/objects/Item.kt [new file with mode: 0644]

diff --git a/core/src/ru/deadsoftware/cavedroid/game/objects/Item.java b/core/src/ru/deadsoftware/cavedroid/game/objects/Item.java
deleted file mode 100644 (file)
index 8e664ac..0000000
+++ /dev/null
@@ -1,40 +0,0 @@
-package ru.deadsoftware.cavedroid.game.objects;
-
-import com.badlogic.gdx.graphics.g2d.Sprite;
-
-import javax.annotation.CheckForNull;
-
-public class Item {
-
-    private final String name;
-    private final String type;
-    @CheckForNull
-    private final Sprite tex;
-
-    public Item(String name, String type, @CheckForNull Sprite tex) {
-        this.name = name;
-        this.type = type;
-        this.tex = tex;
-        if (this.tex != null) {
-            this.tex.flip(false, true);
-        }
-    }
-
-    public Sprite getTexture() {
-        assert tex != null;
-        return tex;
-    }
-
-    public String getType() {
-        return type;
-    }
-
-    public boolean isBlock() {
-        return type.equals("block");
-    }
-
-    public String getName() {
-        return name;
-    }
-
-}
\ No newline at end of file
diff --git a/core/src/ru/deadsoftware/cavedroid/game/objects/Item.kt b/core/src/ru/deadsoftware/cavedroid/game/objects/Item.kt
new file mode 100644 (file)
index 0000000..59d231d
--- /dev/null
@@ -0,0 +1,22 @@
+package ru.deadsoftware.cavedroid.game.objects
+
+import com.badlogic.gdx.graphics.g2d.Sprite
+
+data class Item(
+        val name: String,
+        val type: String,
+        val sprite: Sprite?
+) {
+
+    init {
+        sprite?.flip(false, true)
+    }
+
+    fun requireSprite() = sprite ?: throw IllegalStateException("Sprite is null")
+
+    fun isBlock() = type == "block"
+
+    @Deprecated("Was renamed to Sprite to comply with variable type.", ReplaceWith("requireSprite()"))
+    fun getTexture() = sprite
+
+}
\ No newline at end of file