DEADSOFTWARE

Dispose textures on app close
[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 "none" -> None(commonBlockParams)
23 else -> Normal(commonBlockParams)
24 }
25 }
27 private fun mapCommonParams(key: String, dto: BlockDto): CommonBlockParams {
28 return CommonBlockParams(
29 id = dto.id,
30 key = key,
31 collisionMargins = BlockMargins(
32 left = dto.left,
33 top = dto.top,
34 right = dto.right,
35 bottom = dto.bottom
36 ),
37 hitPoints = dto.hp,
38 dropInfo = mapBlockDropInfo(dto),
39 hasCollision = dto.collision,
40 isBackground = dto.background,
41 isTransparent = dto.transparent,
42 requiresBlock = dto.blockRequired,
43 animationInfo = mapBlockAnimationInfo(dto),
44 texture = getTexture(dto.texture),
45 spriteMargins = BlockMargins(
46 left = dto.spriteLeft,
47 top = dto.spriteTop,
48 right = dto.spriteRight,
49 bottom = dto.spriteBottom,
50 )
51 )
52 }
54 private fun mapBlockDropInfo(dto: BlockDto): BlockDropInfo? {
55 val drop = dto.drop
56 val dropCount = dto.dropCount
58 if (drop == GameItemsHolder.FALLBACK_ITEM_KEY || dropCount == 0) {
59 return null
60 }
62 return BlockDropInfo(
63 itemKey = drop,
64 count = dropCount,
65 )
66 }
68 private fun mapBlockAnimationInfo(dto: BlockDto): BlockAnimationInfo? {
69 if (!dto.animated) {
70 return null
71 }
73 return BlockAnimationInfo(
74 framesCount = dto.frames,
75 )
76 }
78 private fun getTexture(textureName: String): Texture? {
79 if (textureName == GameItemsHolder.FALLBACK_BLOCK_KEY) {
80 return null
81 }
83 return Assets.blockTextures[textureName]
84 }
86 }