DEADSOFTWARE

Update version script
[cavedroid.git] / core / src / 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 javax.inject.Inject
16 @Reusable
17 class ItemMapper @Inject constructor(
18 private val assetLoader: AssetLoader,
19 ) {
21 fun map(key: String, dto: ItemDto, block: Block?, slabTopBlock: Block.Slab?, slabBottomBlock: Block.Slab?): Item {
22 val params = mapCommonParams(key, dto)
24 return when (dto.type) {
25 "normal" -> Normal(params, requireNotNull(loadSprite(dto)))
26 "bucket" -> Bucket(params, requireNotNull(loadSprite(dto)), requireNotNull(dto.actionKey))
27 "shovel" -> Shovel(params, requireNotNull(loadSprite(dto)), dto.mobDamageMultiplier, dto.blockDamageMultiplier, requireNotNull(dto.toolLevel))
28 "sword" -> Sword(params, requireNotNull(loadSprite(dto)), dto.mobDamageMultiplier, dto.blockDamageMultiplier, requireNotNull(dto.toolLevel))
29 "pickaxe" -> Pickaxe(params, requireNotNull(loadSprite(dto)), dto.mobDamageMultiplier, dto.blockDamageMultiplier, requireNotNull(dto.toolLevel))
30 "axe" -> Axe(params, requireNotNull(loadSprite(dto)), dto.mobDamageMultiplier, dto.blockDamageMultiplier, requireNotNull(dto.toolLevel))
31 "shears" -> Shears(params, requireNotNull(loadSprite(dto)), dto.mobDamageMultiplier, dto.blockDamageMultiplier, requireNotNull(dto.toolLevel))
32 "block" -> Block(params, requireNotNull(block))
33 "slab" -> Slab(params, requireNotNull(slabTopBlock), requireNotNull(slabBottomBlock))
34 "none" -> None(params)
35 else -> throw IllegalArgumentException("Unknown item type ${dto.type}")
36 }
37 }
39 private fun mapCommonParams(key: String, dto: ItemDto): CommonItemParams {
40 return CommonItemParams(
41 id = dto.id,
42 key = key,
43 name = dto.name,
44 inHandSpriteOrigin = SpriteOrigin(
45 x = dto.originX,
46 y = dto.origin_y,
47 ),
48 maxStack = dto.maxStack,
49 )
50 }
52 private fun loadSprite(dto: ItemDto): Sprite? {
53 if (dto.type == "none" || dto.type == "block" || dto.texture == GameItemsHolder.FALLBACK_ITEM_KEY) {
54 return null
55 }
57 val texture = Assets.resolveItemTexture(assetLoader, dto.texture)
58 return Sprite(texture)
59 .apply { flip(false, true) }
60 }
62 }