DEADSOFTWARE

MainComponent in kotlin
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / model / item / InventoryItem.kt
index acc0d2931efd026c4cfa3dcb9cba8b6e77322ded..f420120b2844e7bd2d77ae4d01513837bac80c74 100644 (file)
@@ -4,17 +4,19 @@ import com.badlogic.gdx.graphics.Color
 import com.badlogic.gdx.graphics.g2d.SpriteBatch
 import com.badlogic.gdx.graphics.glutils.ShapeRenderer
 import ru.deadsoftware.cavedroid.game.GameItemsHolder
-import ru.deadsoftware.cavedroid.game.mobs.player.Inventory
+import ru.deadsoftware.cavedroid.game.model.dto.SaveDataDto
 import ru.deadsoftware.cavedroid.misc.Assets
+import ru.deadsoftware.cavedroid.misc.Saveable
 import ru.deadsoftware.cavedroid.misc.utils.drawSprite
 import ru.deadsoftware.cavedroid.misc.utils.drawString
 import ru.deadsoftware.cavedroid.misc.utils.px
-import java.io.Serializable
+import kotlin.contracts.ExperimentalContracts
+import kotlin.contracts.contract
 
 class InventoryItem @JvmOverloads constructor(
     val itemKey: String,
     _amount: Int = 1,
-) : Serializable {
+) :  Saveable {
 
     var amount = _amount
         set(value) {
@@ -25,20 +27,27 @@ class InventoryItem @JvmOverloads constructor(
             }
         }
 
-    @Transient
-    lateinit var item: Item
-        private set
+    private var _item: Item? = null
+
+    var item: Item
+        get() {
+            requireNotNull(_item) { "_item is null" }
+            return _item.takeIf { amount > 0 } ?: throw IllegalArgumentException("Accessing item with zero amount")
+        }
+        private set (value) {
+            _item = value
+        }
 
     @JvmOverloads
-    constructor(_item: Item, amount: Int = 1) : this(_item.params.key, amount) {
-        item = _item
+    constructor(item: Item, amount: Int = 1) : this(item.params.key, amount) {
+        _item = item
     }
 
     fun init(gameItemsHolder: GameItemsHolder) {
-        if (this::item.isInitialized) {
+        if (_item != null) {
             return
         }
-        item = gameItemsHolder.getItem(itemKey)
+        _item = gameItemsHolder.getItem(itemKey)
     }
 
     @JvmOverloads
@@ -91,7 +100,9 @@ class InventoryItem @JvmOverloads constructor(
         }
 
         val sprite = item.sprite
-        spriteBatch.drawSprite(sprite, x, y)
+        val placeableMarginTop = (item as? Item.Placeable)?.block?.params?.spriteMargins?.top ?: 0
+        val placeableMarginLeft = (item as? Item.Placeable)?.block?.params?.spriteMargins?.left ?: 0
+        spriteBatch.drawSprite(sprite, x + placeableMarginLeft, y + placeableMarginTop)
 
         if (amount < 2) {
             return
@@ -120,7 +131,35 @@ class InventoryItem @JvmOverloads constructor(
         }
     }
 
+    override fun getSaveData(): SaveDataDto.InventoryItemSaveData {
+        return SaveDataDto.InventoryItemSaveData(
+            version = SAVE_DATA_VERSION,
+            itemKey = itemKey,
+            amount = amount,
+        )
+    }
+
     companion object {
-        fun InventoryItem?.isNoneOrNull() = this?.item == null || this.item.isNone()
+        private const val SAVE_DATA_VERSION = 1
+
+        @OptIn(ExperimentalContracts::class)
+        fun InventoryItem?.isNoneOrNull(): Boolean {
+            contract { returns(false) implies(this@isNoneOrNull != null) }
+            return this?.item == null || this.item.isNone()
+        }
+
+
+        fun fromSaveData(
+            saveData: SaveDataDto.InventoryItemSaveData,
+            gameItemsHolder: GameItemsHolder? = null
+        ): InventoryItem {
+            saveData.verifyVersion(SAVE_DATA_VERSION)
+
+            val inventoryItem = InventoryItem(saveData.itemKey, saveData.amount)
+            gameItemsHolder?.let(inventoryItem::init)
+
+            return inventoryItem
+        }
     }
+
 }