DEADSOFTWARE

Update version script
[cavedroid.git] / core / src / main / kotlin / ru / deadsoftware / cavedroid / game / model / mapper / ItemMapper.kt
1 package ru.deadsoftware.cavedroid.game.model.mapper
3 import com.badlogic.gdx.graphics.g2d.Sprite
4 import dagger.Reusable
5 import ru.deadsoftware.cavedroid.game.GameItemsHolder
6 import ru.deadsoftware.cavedroid.game.model.block.Block
7 import ru.deadsoftware.cavedroid.game.model.dto.ItemDto
8 import ru.deadsoftware.cavedroid.game.model.item.CommonItemParams
9 import ru.deadsoftware.cavedroid.game.model.item.Item
10 import ru.deadsoftware.cavedroid.game.model.item.Item.*
11 import ru.deadsoftware.cavedroid.misc.Assets
12 import ru.deadsoftware.cavedroid.misc.utils.AssetLoader
13 import ru.deadsoftware.cavedroid.misc.utils.SpriteOrigin
14 import ru.deadsoftware.cavedroid.misc.utils.colorFromHexString
15 import javax.inject.Inject
17 @Reusable
18 class ItemMapper @Inject constructor(
19 private val assetLoader: AssetLoader,
20 ) {
22 fun map(key: String, dto: ItemDto, block: Block?, slabTopBlock: Block.Slab?, slabBottomBlock: Block.Slab?): Item {
23 val params = mapCommonParams(key, dto)
25 return when (dto.type) {
26 "normal" -> Normal(params, requireNotNull(loadSprite(dto)))
27 "usable" -> Usable(params, requireNotNull(loadSprite(dto)), requireNotNull(dto.actionKey))
28 "shovel" -> Shovel(params, requireNotNull(loadSprite(dto)), dto.mobDamageMultiplier, dto.blockDamageMultiplier, requireNotNull(dto.toolLevel))
29 "sword" -> Sword(params, requireNotNull(loadSprite(dto)), dto.mobDamageMultiplier, dto.blockDamageMultiplier, requireNotNull(dto.toolLevel))
30 "pickaxe" -> Pickaxe(params, requireNotNull(loadSprite(dto)), dto.mobDamageMultiplier, dto.blockDamageMultiplier, requireNotNull(dto.toolLevel))
31 "axe" -> Axe(params, requireNotNull(loadSprite(dto)), dto.mobDamageMultiplier, dto.blockDamageMultiplier, requireNotNull(dto.toolLevel))
32 "shears" -> Shears(params, requireNotNull(loadSprite(dto)), dto.mobDamageMultiplier, dto.blockDamageMultiplier, requireNotNull(dto.toolLevel))
33 "block" -> Block(params, requireNotNull(block))
34 "slab" -> Slab(params, requireNotNull(slabTopBlock), requireNotNull(slabBottomBlock))
35 "food" -> Food(params, requireNotNull(loadSprite(dto)), requireNotNull(dto.heal))
36 "none" -> None(params)
37 else -> throw IllegalArgumentException("Unknown item type ${dto.type}")
38 }
39 }
41 private fun mapCommonParams(key: String, dto: ItemDto): CommonItemParams {
42 return CommonItemParams(
43 key = key,
44 name = dto.name,
45 inHandSpriteOrigin = SpriteOrigin(
46 x = dto.originX,
47 y = dto.origin_y,
48 ),
49 maxStack = dto.maxStack,
50 burningTimeMs = dto.burningTime,
51 smeltProductKey = dto.smeltProduct,
52 )
53 }
55 private fun loadSprite(dto: ItemDto): Sprite? {
56 if (dto.type == "none" || dto.type == "block" || dto.texture == GameItemsHolder.FALLBACK_ITEM_KEY) {
57 return null
58 }
60 val texture = Assets.resolveItemTexture(assetLoader, dto.texture)
61 return Sprite(texture)
62 .apply {
63 flip(false, true)
64 dto.tint?.let {
65 color = colorFromHexString(it)
66 }
67 }
68 }
70 }