DEADSOFTWARE

Delete old GameItems
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / model / mapper / BlockMapper.kt
1 package ru.deadsoftware.cavedroid.game.model.mapper
3 import com.badlogic.gdx.graphics.Texture
4 import dagger.Reusable
5 import ru.deadsoftware.cavedroid.game.GameItemsHolder
6 import ru.deadsoftware.cavedroid.game.model.block.*
7 import ru.deadsoftware.cavedroid.game.model.block.Block.*
8 import ru.deadsoftware.cavedroid.game.model.dto.BlockDto
9 import ru.deadsoftware.cavedroid.misc.Assets
10 import javax.inject.Inject
12 @Reusable
13 class BlockMapper @Inject constructor() {
15 fun map(key: String, dto: BlockDto): Block {
16 val commonBlockParams = mapCommonParams(key, dto)
18 return when (dto.meta) {
19 "water" -> Water(commonBlockParams, requireNotNull(dto.state))
20 "lava" -> Lava(commonBlockParams, requireNotNull(dto.state))
21 "slab" -> Slab(commonBlockParams, requireNotNull(dto.fullBlock))
22 else -> Normal(commonBlockParams)
23 }
24 }
26 private fun mapCommonParams(key: String, dto: BlockDto): CommonBlockParams {
27 return CommonBlockParams(
28 id = dto.id,
29 key = key,
30 collisionMargins = BlockMargins(
31 left = dto.left,
32 top = dto.top,
33 right = dto.right,
34 bottom = dto.bottom
35 ),
36 hitPoints = dto.hp,
37 dropInfo = mapBlockDropInfo(dto),
38 hasCollision = dto.collision,
39 isBackground = dto.background,
40 isTransparent = dto.transparent,
41 requiresBlock = dto.blockRequired,
42 animationInfo = mapBlockAnimationInfo(dto),
43 texture = loadTexture(dto.texture),
44 spriteMargins = BlockMargins(
45 left = dto.spriteLeft,
46 top = dto.spriteTop,
47 right = dto.spriteRight,
48 bottom = dto.spriteBottom,
49 )
50 )
51 }
53 private fun mapBlockDropInfo(dto: BlockDto): BlockDropInfo? {
54 val drop = dto.drop
55 val dropCount = dto.dropCount
57 if (drop == GameItemsHolder.FALLBACK_ITEM_KEY || dropCount == 0) {
58 return null
59 }
61 return BlockDropInfo(
62 itemKey = drop,
63 count = dropCount,
64 )
65 }
67 private fun mapBlockAnimationInfo(dto: BlockDto): BlockAnimationInfo? {
68 if (!dto.animated) {
69 return null
70 }
72 return BlockAnimationInfo(
73 framesCount = dto.frames,
74 )
75 }
77 private fun loadTexture(textureName: String): Texture? {
78 if (textureName == GameItemsHolder.FALLBACK_BLOCK_KEY) {
79 return null
80 }
82 return Assets.blockTextures[textureName]
83 }
85 }