From 36f78fdd1d4f8b9a1c77f8fbedc05e5675a5c288 Mon Sep 17 00:00:00 2001 From: fredboy Date: Mon, 22 Apr 2024 16:25:41 +0700 Subject: [PATCH] Fix launch on desktop --- build.gradle | 1 + .../cavedroid/game/mobs/Player.java | 9 ++++++--- .../cavedroid/game/model/item/Item.kt | 6 +++++- .../game/model/mapper/BlockMapper.kt | 7 +++++-- .../cavedroid/game/model/mapper/ItemMapper.kt | 8 ++++++-- .../deadsoftware/cavedroid/misc/Assets.java | 20 +++++++++++++++++++ desktop/build.gradle | 5 ++++- 7 files changed, 47 insertions(+), 9 deletions(-) diff --git a/build.gradle b/build.gradle index 1f0b0e1..375d55d 100644 --- a/build.gradle +++ b/build.gradle @@ -42,6 +42,7 @@ project(":desktop") { dependencies { implementation project(":core") + implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:$kotlinSerializationVersion" api "com.badlogicgames.gdx:gdx-backend-lwjgl3:$gdxVersion" api "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop" } diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java index 7f517df..46cb8cd 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java @@ -175,8 +175,9 @@ public class Player extends Mob { } final Sprite sprite = item.getSprite(); + final boolean smallSprite = !item.isTool() || item.isShears(); - if (!item.isTool()) { + if (smallSprite) { sprite.setSize(Drop.DROP_SIZE, Drop.DROP_SIZE); } @@ -185,7 +186,7 @@ public class Player extends Mob { final SpriteOrigin spriteOrigin = item.getParams().getInHandSpriteOrigin(); final int handMultiplier = -getDirection().getBasis(); final float xOffset = (-1 + getDirection().getIndex()) * sprite.getWidth() + 4 + handMultiplier * (sprite.getWidth() * spriteOrigin.getX()); - final float yOffset = item.isTool() ? -sprite.getHeight() / 2 : 0; + final float yOffset = !smallSprite ? -sprite.getHeight() / 2 : 0; float rotate = anim + 30; @@ -210,7 +211,9 @@ public class Player extends Mob { sprite.setFlip(false, sprite.isFlipY()); sprite.setRotation(0); sprite.setOriginCenter(); - sprite.rotate90(looksRight()); + if (item.isTool()) { + sprite.rotate90(looksRight()); + } } public void startHitting() { diff --git a/core/src/ru/deadsoftware/cavedroid/game/model/item/Item.kt b/core/src/ru/deadsoftware/cavedroid/game/model/item/Item.kt index 2b6b5fc..7eeb940 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/model/item/Item.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/model/item/Item.kt @@ -35,12 +35,16 @@ sealed class Item { return this is Slab } - fun isTool(): Boolean { contract { returns(true) implies (this@Item is Tool) } return this is Tool } + fun isShears(): Boolean { + contract { returns(true) implies (this@Item is Shears) } + return this is Shears + } + fun isUsable(): Boolean { contract { returns(true) implies (this@Item is Placeable) } return this is Placeable diff --git a/core/src/ru/deadsoftware/cavedroid/game/model/mapper/BlockMapper.kt b/core/src/ru/deadsoftware/cavedroid/game/model/mapper/BlockMapper.kt index 667d92b..8373e02 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/model/mapper/BlockMapper.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/model/mapper/BlockMapper.kt @@ -8,10 +8,13 @@ import ru.deadsoftware.cavedroid.game.model.block.Block.* import ru.deadsoftware.cavedroid.game.model.dto.BlockDto import ru.deadsoftware.cavedroid.game.model.item.Item import ru.deadsoftware.cavedroid.misc.Assets +import ru.deadsoftware.cavedroid.misc.utils.AssetLoader import javax.inject.Inject @Reusable -class BlockMapper @Inject constructor() { +class BlockMapper @Inject constructor( + private val assetLoader: AssetLoader, +) { fun map(key: String, dto: BlockDto): Block { val commonBlockParams = mapCommonParams(key, dto) @@ -95,7 +98,7 @@ class BlockMapper @Inject constructor() { return null } - return Assets.blockTextures[textureName] + return Assets.resolveBlockTexture(assetLoader, textureName) } } \ No newline at end of file diff --git a/core/src/ru/deadsoftware/cavedroid/game/model/mapper/ItemMapper.kt b/core/src/ru/deadsoftware/cavedroid/game/model/mapper/ItemMapper.kt index 6bcad04..1e6b528 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/model/mapper/ItemMapper.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/model/mapper/ItemMapper.kt @@ -9,11 +9,14 @@ import ru.deadsoftware.cavedroid.game.model.item.CommonItemParams import ru.deadsoftware.cavedroid.game.model.item.Item import ru.deadsoftware.cavedroid.game.model.item.Item.* import ru.deadsoftware.cavedroid.misc.Assets +import ru.deadsoftware.cavedroid.misc.utils.AssetLoader import ru.deadsoftware.cavedroid.misc.utils.SpriteOrigin import javax.inject.Inject @Reusable -class ItemMapper @Inject constructor() { +class ItemMapper @Inject constructor( + private val assetLoader: AssetLoader, +) { fun map(key: String, dto: ItemDto, block: Block?, slabTopBlock: Block.Slab?, slabBottomBlock: Block.Slab?): Item { val params = mapCommonParams(key, dto) @@ -49,7 +52,8 @@ class ItemMapper @Inject constructor() { return null } - return Sprite(Assets.itemTextures[dto.texture]) + val texture = Assets.resolveItemTexture(assetLoader, dto.texture) + return Sprite(texture) .apply { flip(false, true) } } diff --git a/core/src/ru/deadsoftware/cavedroid/misc/Assets.java b/core/src/ru/deadsoftware/cavedroid/misc/Assets.java index abec0a0..6a4305f 100644 --- a/core/src/ru/deadsoftware/cavedroid/misc/Assets.java +++ b/core/src/ru/deadsoftware/cavedroid/misc/Assets.java @@ -12,6 +12,7 @@ import com.badlogic.gdx.utils.JsonValue; import ru.deadsoftware.cavedroid.game.objects.TouchButton; import ru.deadsoftware.cavedroid.misc.utils.AssetLoader; +import java.io.File; import java.util.HashMap; import java.util.LinkedList; import java.util.List; @@ -112,6 +113,25 @@ public class Assets { } } + private static Texture resolveTexture(AssetLoader assetLoader, String textureName, String lookUpPath, Map cache) { + if (cache.containsKey(textureName)) { + return cache.get(textureName); + } + + final Texture texture = loadTexture(assetLoader.getAssetHandle(lookUpPath + File.separator + textureName + ".png")); + cache.put(textureName, texture); + + return texture; + } + + public static Texture resolveItemTexture(AssetLoader assetLoader, String textureName) { + return resolveTexture(assetLoader, textureName, "textures/items", itemTextures); + } + + public static Texture resolveBlockTexture(AssetLoader assetLoader, String textureName) { + return resolveTexture(assetLoader, textureName, "textures/blocks", blockTextures); + } + private static void loadAllPngsFromDirInto(FileHandle dir, Map loadInto) { for (FileHandle handle : dir.list((d, name) -> name.endsWith(".png"))) { loadInto.put(handle.nameWithoutExtension(), loadTexture(handle)); diff --git a/desktop/build.gradle b/desktop/build.gradle index 3436d70..70a63ba 100644 --- a/desktop/build.gradle +++ b/desktop/build.gradle @@ -1,4 +1,7 @@ -apply plugin: "kotlin" +plugins { + id 'kotlin' + id 'org.jetbrains.kotlin.plugin.serialization' version "$kotlinVersion" +} sourceCompatibility = 17 sourceSets.main.java.srcDirs = [ "src/" ] -- 2.29.2