DEADSOFTWARE

ad79cbd824e01f6b0308f3d04e190f9952f16f29
[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.SpriteOrigin
13 import javax.inject.Inject
15 @Reusable
16 class ItemMapper @Inject constructor() {
18 fun map(key: String, dto: ItemDto, block: Block?): Item {
19 val params = mapCommonParams(key, dto)
21 return when (dto.type) {
22 "bucket" -> Bucket(params, requireNotNull(loadSprite(dto)), requireNotNull(dto.actionKey))
23 "shovel" -> Shovel(params, requireNotNull(loadSprite(dto)), dto.mobDamageMultiplier, dto.blockDamageMultiplier)
24 "sword" -> Sword(params, requireNotNull(loadSprite(dto)), dto.mobDamageMultiplier, dto.blockDamageMultiplier)
25 "block" -> Placeable(params, requireNotNull(block))
26 "none" -> None(params)
27 else -> throw IllegalArgumentException("Unknown item type ${dto.type}")
28 }
29 }
31 private fun mapCommonParams(key: String, dto: ItemDto): CommonItemParams {
32 return CommonItemParams(
33 id = dto.id,
34 key = key,
35 name = dto.name,
36 inHandSpriteOrigin = SpriteOrigin(
37 x = dto.originX,
38 y = dto.origin_y,
39 )
40 )
41 }
43 private fun loadSprite(dto: ItemDto): Sprite? {
44 if (dto.type == "none" || dto.type == "block" || dto.texture == GameItemsHolder.FALLBACK_ITEM_KEY) {
45 return null
46 }
48 return Sprite(Assets.itemTextures[dto.texture])
49 .apply { flip(false, true) }
50 }
52 }