DEADSOFTWARE

Update version
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / objects / Item.kt
index 59d231d5b6205e3c1ad7a35280121ea4848f819b..a4c41b0424d62342a42ff00838f2bada8e594623 100644 (file)
@@ -1,11 +1,17 @@
 package ru.deadsoftware.cavedroid.game.objects
 
 import com.badlogic.gdx.graphics.g2d.Sprite
+import ru.deadsoftware.cavedroid.game.GameItems
+import ru.deadsoftware.cavedroid.misc.utils.SpriteOrigin
 
 data class Item(
-        val name: String,
-        val type: String,
-        val sprite: Sprite?
+    val id: Int,
+    val key: String,
+    val name: String,
+    val type: String,
+    val sprite: Sprite?,
+    val defaultOrigin: SpriteOrigin,
+    val actionKey: String?,
 ) {
 
     init {
@@ -16,7 +22,27 @@ data class Item(
 
     fun isBlock() = type == "block"
 
+    fun isTool() = type == "tool"
+
+    /**
+     * Returns block associated with this item. Null if this is not a block
+     */
+    fun toBlock(): Block? {
+        if (!isBlock()) {
+            return null
+        }
+
+        return GameItems.getBlock(GameItems.getBlockIdByItemId(id))
+    }
+
+    fun getItemOrBlockSprite(): Sprite {
+        return requireNotNull(sprite ?: toBlock()?.requireSprite()) { "wtf: sprite is null" }
+    }
+
+    fun isNone(): Boolean {
+        return id == 0;
+    }
+
     @Deprecated("Was renamed to Sprite to comply with variable type.", ReplaceWith("requireSprite()"))
     fun getTexture() = sprite
-
 }
\ No newline at end of file