DEADSOFTWARE

Support reading item ids from json
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / objects / Item.kt
1 package ru.deadsoftware.cavedroid.game.objects
3 import com.badlogic.gdx.graphics.g2d.Sprite
4 import ru.deadsoftware.cavedroid.game.GameItems
5 import ru.deadsoftware.cavedroid.misc.utils.SpriteOrigin
7 data class Item(
8 val id: Int,
9 val key: String,
10 val name: String,
11 val type: String,
12 val sprite: Sprite?,
13 val defaultOrigin: SpriteOrigin,
14 ) {
16 init {
17 sprite?.flip(false, true)
18 }
20 fun requireSprite() = sprite ?: throw IllegalStateException("Sprite is null")
22 fun isBlock() = type == "block"
24 fun isTool() = type == "tool"
26 /**
27 * Returns block associated with this item. Null if this is not a block
28 */
29 fun toBlock(): Block? {
30 if (!isBlock()) {
31 return null
32 }
34 return GameItems.getBlock(GameItems.getBlockIdByItemId(id))
35 }
37 fun getItemOrBlockSprite(): Sprite {
38 return requireNotNull(sprite ?: toBlock()?.requireSprite()) { "wtf: sprite is null" }
39 }
41 fun isNone(): Boolean {
42 return id == 0;
43 }
45 @Deprecated("Was renamed to Sprite to comply with variable type.", ReplaceWith("requireSprite()"))
46 fun getTexture() = sprite
47 }