From 409a22b3fe7c43b7f686f33cc2f01b6277edec78 Mon Sep 17 00:00:00 2001 From: fredboy Date: Mon, 22 Apr 2024 18:22:29 +0700 Subject: [PATCH] Add items count and tools duration --- android/assets/json/game_items.json | 63 ++++++++++++------- .../cavedroid/game/GameInput.java | 3 +- .../PlaceBlockItemToBackgroundAction.kt | 8 ++- .../PlaceBlockItemToForegroundAction.kt | 8 ++- .../actions/placeblock/PlaceSlabAction.kt | 15 ++++- .../cavedroid/game/mobs/FallingGravel.java | 2 +- .../cavedroid/game/mobs/FallingSand.java | 2 +- .../cavedroid/game/mobs/Player.java | 26 +++++++- .../cavedroid/game/model/dto/ItemDto.kt | 1 + .../game/model/item/CommonItemParams.kt | 1 + .../game/model/item/InventoryItem.kt | 48 +++++++++++++- .../cavedroid/game/model/mapper/ItemMapper.kt | 3 +- .../cavedroid/game/render/DebugRenderer.kt | 5 +- .../cavedroid/game/render/HudRenderer.kt | 24 +++---- .../render/windows/CreativeWindowRenderer.kt | 19 +++--- .../cavedroid/game/world/GameWorld.java | 43 +++++++++++-- .../cavedroid/misc/utils/RenderingUtils.kt | 12 +++- 17 files changed, 221 insertions(+), 62 deletions(-) diff --git a/android/assets/json/game_items.json b/android/assets/json/game_items.json index 4763629..0e40786 100644 --- a/android/assets/json/game_items.json +++ b/android/assets/json/game_items.json @@ -952,63 +952,72 @@ "type": "sword", "texture": "wood_sword", "origin_x": 0.125, - "tool_level": 1 + "tool_level": 1, + "max_stack": 60 }, "stone_sword": { "name": "Stone Sword", "type": "sword", "texture": "stone_sword", "origin_x": 0.125, - "tool_level": 2 + "tool_level": 2, + "max_stack": 132 }, "iron_sword": { "name": "Iron Sword", "type": "sword", "texture": "iron_sword", "origin_x": 0.125, - "tool_level": 3 + "tool_level": 3, + "max_stack": 251 }, "diamond_sword": { "name": "Diamond Sword", "type": "sword", "texture": "diamond_sword", "origin_x": 0.125, - "tool_level": 4 + "tool_level": 4, + "max_stack": 1562 }, "gold_sword": { "name": "Golden Sword", "type": "sword", "texture": "gold_sword", "origin_x": 0.125, - "tool_level": 1 + "tool_level": 1, + "max_stack": 33 }, "wood_shovel": { "name": "Wooden Shovel", "type": "shovel", "texture": "wood_shovel", "origin_x": 0.125, - "tool_level" : 1 + "tool_level" : 1, + "max_stack": 59 }, "stone_shovel": { "name": "Stone Shovel", "type": "shovel", "texture": "stone_shovel", "origin_x": 0.125, - "tool_level" : 2 + "tool_level" : 2, + "max_stack": 131 }, "iron_shovel": { "name": "Iron Shovel", "type": "shovel", "texture": "iron_shovel", "origin_x": 0.125, - "tool_level" : 3 + "tool_level" : 3, + "max_stack": 250 }, "diamond_shovel": { "name": "Diamond Shovel", "type": "shovel", "texture": "diamond_shovel", "origin_x": 0.125, - "tool_level" : 4 + "tool_level" : 4, + "max_stack": 1561 }, "gold_shovel": { "name": "Golden Shovel", @@ -1016,35 +1025,40 @@ "texture": "gold_shovel", "origin_x": 0.125, "tool_level" : 1, - "block_damage_multiplier": 6 + "block_damage_multiplier": 6, + "max_stack": 32 }, "wood_pickaxe": { "name": "Wooden Pickaxe", "type": "pickaxe", "texture": "wood_pickaxe", "origin_x": 0.125, - "tool_level" : 1 + "tool_level" : 1, + "max_stack": 59 }, "stone_pickaxe": { "name": "Stone Pickaxe", "type": "pickaxe", "texture": "stone_pickaxe", "origin_x": 0.125, - "tool_level" : 2 + "tool_level" : 2, + "max_stack": 131 }, "iron_pickaxe": { "name": "Iron Pickaxe", "type": "pickaxe", "texture": "iron_pickaxe", "origin_x": 0.125, - "tool_level" : 3 + "tool_level" : 3, + "max_stack": 251 }, "diamond_pickaxe": { "name": "Diamond Pickaxe", "type": "pickaxe", "texture": "diamond_pickaxe", "origin_x": 0.125, - "tool_level" : 4 + "tool_level" : 4, + "max_stack": 1562 }, "gold_pickaxe": { "name": "Golden Pickaxe", @@ -1052,35 +1066,40 @@ "texture": "gold_pickaxe", "origin_x": 0.125, "tool_level" : 1, - "block_damage_multiplier": 6 + "block_damage_multiplier": 6, + "max_stack": 33 }, "wood_axe": { "name": "Wooden Axe", "type": "axe", "texture": "wood_axe", "origin_x": 0.125, - "tool_level" : 1 + "tool_level" : 1, + "max_stack": 60 }, "stone_axe": { "name": "Stone Axe", "type": "axe", "texture": "stone_axe", "origin_x": 0.125, - "tool_level" : 2 + "tool_level" : 2, + "max_stack": 131 }, "iron_axe": { "name": "Iron Axe", "type": "axe", "texture": "iron_axe", "origin_x": 0.125, - "tool_level" : 3 + "tool_level" : 3, + "max_stack": 250 }, "diamond_axe": { "name": "Diamond Axe", "type": "axe", "texture": "diamond_axe", "origin_x": 0.125, - "tool_level" : 4 + "tool_level" : 4, + "max_stack": 1561 }, "gold_axe": { "name": "Golden Axe", @@ -1088,14 +1107,16 @@ "texture": "gold_axe", "origin_x": 0.125, "tool_level" : 1, - "block_damage_multiplier": 6 + "block_damage_multiplier": 6, + "max_stack": 32 }, "shears": { "name": "Shears", "type": "shears", "texture": "shears", "tool_level" : 1, - "origin_x": 0.125 + "origin_x": 0.125, + "max_stack": 238 }, "bucket_empty": { "name": "Empty Bucket", diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameInput.java b/core/src/ru/deadsoftware/cavedroid/game/GameInput.java index e1257d5..a45e704 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameInput.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameInput.java @@ -407,7 +407,8 @@ public class GameInput { } System.arraycopy(mPlayer.inventory, 0, mPlayer.inventory, 1, 8); - mPlayer.inventory[0] = mGameItemsHolder.getItemFromCreativeInventory(itemPos).toInventoryItem(); + final Item newItem = mGameItemsHolder.getItemFromCreativeInventory(itemPos); + mPlayer.inventory[0] = newItem.toInventoryItem(newItem.getParams().getMaxStack()); } else if (mMainConfig.checkGameUiWindow(GameUiWindow.CREATIVE_INVENTORY)) { mMainConfig.setGameUiWindow(GameUiWindow.NONE); } else if (screenY < hotbar.getRegionHeight() && diff --git a/core/src/ru/deadsoftware/cavedroid/game/actions/placeblock/PlaceBlockItemToBackgroundAction.kt b/core/src/ru/deadsoftware/cavedroid/game/actions/placeblock/PlaceBlockItemToBackgroundAction.kt index bdebfcb..fdfb61a 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/actions/placeblock/PlaceBlockItemToBackgroundAction.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/actions/placeblock/PlaceBlockItemToBackgroundAction.kt @@ -1,6 +1,8 @@ package ru.deadsoftware.cavedroid.game.actions.placeblock +import ru.deadsoftware.cavedroid.game.GameItemsHolder import ru.deadsoftware.cavedroid.game.GameScope +import ru.deadsoftware.cavedroid.game.mobs.MobsController import ru.deadsoftware.cavedroid.game.model.item.Item import ru.deadsoftware.cavedroid.game.world.GameWorld import javax.inject.Inject @@ -8,10 +10,14 @@ import javax.inject.Inject @GameScope class PlaceBlockItemToBackgroundAction @Inject constructor( private val gameWorld: GameWorld, + private val gameItemsHolder: GameItemsHolder, + private val mobsController: MobsController, ) : IPlaceBlockAction { override fun place(placeable: Item.Placeable, x: Int, y: Int) { - gameWorld.placeToBackground(x, y, placeable.block) + if (gameWorld.placeToBackground(x, y, placeable.block)) { + mobsController.player.decreaseCurrentItemCount(gameItemsHolder) + } } companion object { diff --git a/core/src/ru/deadsoftware/cavedroid/game/actions/placeblock/PlaceBlockItemToForegroundAction.kt b/core/src/ru/deadsoftware/cavedroid/game/actions/placeblock/PlaceBlockItemToForegroundAction.kt index d409a80..22aafdd 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/actions/placeblock/PlaceBlockItemToForegroundAction.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/actions/placeblock/PlaceBlockItemToForegroundAction.kt @@ -1,6 +1,8 @@ package ru.deadsoftware.cavedroid.game.actions.placeblock +import ru.deadsoftware.cavedroid.game.GameItemsHolder import ru.deadsoftware.cavedroid.game.GameScope +import ru.deadsoftware.cavedroid.game.mobs.MobsController import ru.deadsoftware.cavedroid.game.model.item.Item import ru.deadsoftware.cavedroid.game.world.GameWorld import javax.inject.Inject @@ -9,13 +11,17 @@ import javax.inject.Inject class PlaceBlockItemToForegroundAction @Inject constructor( private val gameWorld: GameWorld, private val placeSlabAction: PlaceSlabAction, + private val gameItemsHolder: GameItemsHolder, + private val mobsController: MobsController, ) : IPlaceBlockAction { override fun place(placeable: Item.Placeable, x: Int, y: Int) { if (placeable.isSlab()) { placeSlabAction.place(placeable, x, y) } else { - gameWorld.placeToForeground(x, y, placeable.block) + if (gameWorld.placeToForeground(x, y, placeable.block)) { + mobsController.player.decreaseCurrentItemCount(gameItemsHolder) + } } } diff --git a/core/src/ru/deadsoftware/cavedroid/game/actions/placeblock/PlaceSlabAction.kt b/core/src/ru/deadsoftware/cavedroid/game/actions/placeblock/PlaceSlabAction.kt index 079d225..6ac94c9 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/actions/placeblock/PlaceSlabAction.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/actions/placeblock/PlaceSlabAction.kt @@ -7,7 +7,6 @@ import ru.deadsoftware.cavedroid.game.mobs.MobsController import ru.deadsoftware.cavedroid.game.model.item.Item import ru.deadsoftware.cavedroid.game.world.GameWorld import javax.inject.Inject -import kotlin.random.Random @GameScope class PlaceSlabAction @Inject constructor( @@ -22,8 +21,18 @@ class PlaceSlabAction @Inject constructor( return } - val slabPart = if (Random.nextBoolean()) placeable.topPartBlock else placeable.bottomPartBlock - gameWorld.placeToForeground(x, y, slabPart) + val slabPart = if ((gameWorld.hasForeAt(x, y - 1) + || gameWorld.getForeMap(x - 1, y) == placeable.topPartBlock + || gameWorld.getForeMap(x + 1, y) == placeable.topPartBlock) + && !gameWorld.hasForeAt(x, y + 1)) { + placeable.topPartBlock + } else { + placeable.bottomPartBlock + } + + if (gameWorld.placeToForeground(x, y, slabPart)) { + mobsController.player.decreaseCurrentItemCount(gameItemsHolder) + } } companion object { diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingGravel.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingGravel.java index 4743fcf..641ffe5 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingGravel.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingGravel.java @@ -42,7 +42,7 @@ public class FallingGravel extends Mob { @Override public void ai(GameWorld gameWorld, GameItemsHolder gameItemsHolder, float delta) { if (mVelocity.isZero()) { - gameWorld.setForeMap(getMapX(), getMiddleMapY(), gameItemsHolder.getBlock("gravel")); + gameWorld.setForeMap(getMapX(), getUpperMapY(), gameItemsHolder.getBlock("gravel")); kill(); } } diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingSand.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingSand.java index bce918e..f41da96 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingSand.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingSand.java @@ -43,7 +43,7 @@ public class FallingSand extends Mob { @Override public void ai(GameWorld gameWorld, GameItemsHolder gameItemsHolder, float delta) { if (mVelocity.isZero()) { - gameWorld.setForeMap(getMapX(), getMiddleMapY(), gameItemsHolder.getBlock("sand")); + gameWorld.setForeMap(getMapX(), getUpperMapY(), gameItemsHolder.getBlock("sand")); kill(); } } diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java index 46cb8cd..6e0dfe3 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java @@ -65,9 +65,33 @@ public class Player extends Mob { heal(MAX_HEALTH); } + public void decreaseCurrentItemCount(GameItemsHolder gameItemsHolder) { + if (gameMode == 1) { + return; + } + getCurrentItem().setAmount(getCurrentItem().getAmount() - 1); + if (getCurrentItem().getAmount() <= 0) { + setCurrentInventorySlotItem(gameItemsHolder.getFallbackItem()); + } + } + + public InventoryItem getCurrentItem() { + return inventory[slot]; + } + public void pickUpDrop(Drop drop) { + for (InventoryItem invItem : inventory) { + if (!invItem.getItem().isTool() + && invItem.getItem() == drop.getItem() + && invItem.getAmount() < invItem.getItem().getParams().getMaxStack()) { + invItem.setAmount(invItem.getAmount() + 1); + drop.setPickedUp(true); + return; + } + } + for (int i = 0; i < inventory.length; i++) { - if (inventory(i) == null || inventory(i).getParams().getKey().equals(GameItemsHolder.FALLBACK_ITEM_KEY) || inventory(i) == drop.getItem()) { + if (inventory(i) == null || inventory(i).getParams().getKey().equals(GameItemsHolder.FALLBACK_ITEM_KEY)) { inventory[i] = drop.getItem().toInventoryItem(); drop.setPickedUp(true); break; diff --git a/core/src/ru/deadsoftware/cavedroid/game/model/dto/ItemDto.kt b/core/src/ru/deadsoftware/cavedroid/game/model/dto/ItemDto.kt index 0fa2075..3573fa9 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/model/dto/ItemDto.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/model/dto/ItemDto.kt @@ -17,4 +17,5 @@ data class ItemDto( @SerialName("top_slab_block") val topSlabBlock: String? = null, @SerialName("bottom_slab_block") val bottomSlabBlock: String? = null, @SerialName("tool_level") val toolLevel: Int? = null, + @SerialName("max_stack") val maxStack: Int = 64, ) diff --git a/core/src/ru/deadsoftware/cavedroid/game/model/item/CommonItemParams.kt b/core/src/ru/deadsoftware/cavedroid/game/model/item/CommonItemParams.kt index 06da72f..291cfb8 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/model/item/CommonItemParams.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/model/item/CommonItemParams.kt @@ -7,4 +7,5 @@ data class CommonItemParams( val key: String, val name: String, val inHandSpriteOrigin: SpriteOrigin, + val maxStack: Int, ) \ No newline at end of file diff --git a/core/src/ru/deadsoftware/cavedroid/game/model/item/InventoryItem.kt b/core/src/ru/deadsoftware/cavedroid/game/model/item/InventoryItem.kt index 1e1f43e..49cc59d 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/model/item/InventoryItem.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/model/item/InventoryItem.kt @@ -1,11 +1,17 @@ package ru.deadsoftware.cavedroid.game.model.item +import com.badlogic.gdx.graphics.Color +import com.badlogic.gdx.graphics.g2d.SpriteBatch +import com.badlogic.gdx.graphics.glutils.ShapeRenderer import ru.deadsoftware.cavedroid.game.GameItemsHolder +import ru.deadsoftware.cavedroid.misc.Assets +import ru.deadsoftware.cavedroid.misc.utils.drawString +import ru.deadsoftware.cavedroid.misc.utils.px import java.io.Serializable class InventoryItem @JvmOverloads constructor( val itemKey: String, - val amount: Int = 1, + var amount: Int = 1, ) : Serializable { @Transient @@ -24,4 +30,44 @@ class InventoryItem @JvmOverloads constructor( item = gameItemsHolder.getItem(itemKey) } + private fun drawAmountText(spriteBatch: SpriteBatch, text: String, x: Float, y: Float) { + spriteBatch.drawString(text, x + 1, y + 1, Color.BLACK) + spriteBatch.drawString(text, x, y, Color.WHITE) + } + + fun draw(spriteBatch: SpriteBatch, shapeRenderer: ShapeRenderer, x: Float, y: Float) { + if (item.isNone()) { + return + } + + val sprite = item.sprite + spriteBatch.draw(sprite, x, y) + + if (amount < 2) { + return + } + + if (item.isTool()) { + spriteBatch.end() + shapeRenderer.begin(ShapeRenderer.ShapeType.Filled) + shapeRenderer.color = Color.GREEN + shapeRenderer.rect( + /* x = */ x, + /* y = */ y + 1.px - 2, + /* width = */ 1.px * (amount.toFloat() / item.params.maxStack.toFloat()), + /* height = */ 2f + ) + shapeRenderer.end() + spriteBatch.begin() + } else { + val amountString = amount.toString() + drawAmountText( + spriteBatch = spriteBatch, + text = amountString, + x = x + 1.px - Assets.getStringWidth(amountString), + y = y + 1.px - Assets.getStringHeight(amountString) + ) + } + } + } 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 1e6b528..4d6d551 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/model/mapper/ItemMapper.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/model/mapper/ItemMapper.kt @@ -43,7 +43,8 @@ class ItemMapper @Inject constructor( inHandSpriteOrigin = SpriteOrigin( x = dto.originX, y = dto.origin_y, - ) + ), + maxStack = dto.maxStack, ) } diff --git a/core/src/ru/deadsoftware/cavedroid/game/render/DebugRenderer.kt b/core/src/ru/deadsoftware/cavedroid/game/render/DebugRenderer.kt index c8be755..03f6253 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/render/DebugRenderer.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/render/DebugRenderer.kt @@ -12,6 +12,7 @@ import ru.deadsoftware.cavedroid.game.model.block.Block import ru.deadsoftware.cavedroid.game.world.GameWorld import ru.deadsoftware.cavedroid.misc.Assets import ru.deadsoftware.cavedroid.misc.utils.bl +import ru.deadsoftware.cavedroid.misc.utils.drawString import ru.deadsoftware.cavedroid.misc.utils.forEachBlockInArea import ru.deadsoftware.cavedroid.misc.utils.px import javax.inject.Inject @@ -26,10 +27,6 @@ class DebugRenderer @Inject constructor( override val renderLayer get() = RENDER_LAYER - private fun SpriteBatch.drawString(str: String, x: Float, y: Float) { - Assets.minecraftFont.draw(this, str, x, y) - } - private fun getMinimapColor(x: Int, y: Int): Color? { val foregroundBlock = gameWorld.getForeMap(x, y) diff --git a/core/src/ru/deadsoftware/cavedroid/game/render/HudRenderer.kt b/core/src/ru/deadsoftware/cavedroid/game/render/HudRenderer.kt index cea623a..7bbb876 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/render/HudRenderer.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/render/HudRenderer.kt @@ -59,19 +59,19 @@ class HudRenderer @Inject constructor( } } - private fun drawHotbarItems(spriteBatch: SpriteBatch, hotbarX: Float) { - mobsController.player.inventory.asSequence() - .map(InventoryItem::item) + private fun drawHotbarItems(spriteBatch: SpriteBatch, shapeRenderer: ShapeRenderer, hotbarX: Float) { + mobsController.player.inventory .forEachIndexed { index, item -> - if (item.isNone()) { + if (item.item.isNone()) { return@forEachIndexed } - spriteBatch.draw( - /* region = */ item.sprite, - /* x = */ hotbarX + HotbarConfig.horizontalMargin - + index * (HotbarConfig.itemSeparatorWidth + HotbarConfig.itemSlotSpace), - /* y = */ HotbarConfig.verticalMargin, + item.draw( + spriteBatch = spriteBatch, + shapeRenderer = shapeRenderer, + x = hotbarX + HotbarConfig.horizontalMargin + + index * (HotbarConfig.itemSeparatorWidth + HotbarConfig.itemSlotSpace), + y = HotbarConfig.verticalMargin, ) } } @@ -85,19 +85,19 @@ class HudRenderer @Inject constructor( ) } - private fun drawHotbar(spriteBatch: SpriteBatch, viewport: Rectangle) { + private fun drawHotbar(spriteBatch: SpriteBatch, shapeRenderer: ShapeRenderer, viewport: Rectangle) { val hotbar = hotbarTexture val hotbarX = viewport.width / 2 - hotbar.regionWidth / 2 spriteBatch.draw(hotbar, hotbarX, 0f) drawHealth(spriteBatch, hotbarX, hotbarTexture.regionHeight.toFloat()) - drawHotbarItems(spriteBatch, hotbarX) drawHotbarSelector(spriteBatch, hotbarX) + drawHotbarItems(spriteBatch, shapeRenderer, hotbarX) } override fun draw(spriteBatch: SpriteBatch, shapeRenderer: ShapeRenderer, viewport: Rectangle, delta: Float) { drawCursor(spriteBatch, viewport) - drawHotbar(spriteBatch, viewport) + drawHotbar(spriteBatch, shapeRenderer, viewport) } companion object { diff --git a/core/src/ru/deadsoftware/cavedroid/game/render/windows/CreativeWindowRenderer.kt b/core/src/ru/deadsoftware/cavedroid/game/render/windows/CreativeWindowRenderer.kt index 3480e27..d9423ff 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/render/windows/CreativeWindowRenderer.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/render/windows/CreativeWindowRenderer.kt @@ -32,7 +32,7 @@ class CreativeWindowRenderer @Inject constructor( val startIndex = gameInput.creativeScroll * CreativeWindowConfig.itemsInRow val endIndex = startIndex + CreativeWindowConfig.itemsOnPage - for (i in startIndex ..< endIndex) { + for (i in startIndex.. - if (item.isNone()) { + if (item.item.isNone()) { return@forEachIndexed } val itemX = inventoryX + index * CreativeWindowConfig.itemsGridColWidth - spriteBatch.draw(item.sprite, itemX, inventoryY) + item.draw(spriteBatch, shapeRenderer, itemX, inventoryY) } } - private fun drawCreative(spriteBatch: SpriteBatch, viewport: Rectangle) { + private fun drawCreative(spriteBatch: SpriteBatch, shapeRenderer: ShapeRenderer, viewport: Rectangle) { val creativeWindow = creativeWindowTexture val windowX = viewport.width / 2 - creativeWindow.regionWidth / 2 @@ -88,13 +92,14 @@ class CreativeWindowRenderer @Inject constructor( drawPlayerInventory( spriteBatch = spriteBatch, + shapeRenderer = shapeRenderer, inventoryX = windowX + CreativeWindowConfig.itemsGridMarginLeft, inventoryY = windowY + creativeWindow.regionHeight - CreativeWindowConfig.playerInventoryOffsetFromBottom ) } override fun draw(spriteBatch: SpriteBatch, shapeRenderer: ShapeRenderer, viewport: Rectangle, delta: Float) { - drawCreative(spriteBatch, viewport) + drawCreative(spriteBatch, shapeRenderer, viewport) } companion object { diff --git a/core/src/ru/deadsoftware/cavedroid/game/world/GameWorld.java b/core/src/ru/deadsoftware/cavedroid/game/world/GameWorld.java index 32fef76..f8a5cc9 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/world/GameWorld.java +++ b/core/src/ru/deadsoftware/cavedroid/game/world/GameWorld.java @@ -5,6 +5,8 @@ import ru.deadsoftware.cavedroid.game.GameItemsHolder; import ru.deadsoftware.cavedroid.game.GameScope; import ru.deadsoftware.cavedroid.game.mobs.MobsController; import ru.deadsoftware.cavedroid.game.model.block.Block; +import ru.deadsoftware.cavedroid.game.model.item.InventoryItem; +import ru.deadsoftware.cavedroid.game.model.item.Item; import ru.deadsoftware.cavedroid.game.model.world.generator.WorldGeneratorConfig; import ru.deadsoftware.cavedroid.game.objects.DropController; import ru.deadsoftware.cavedroid.misc.utils.MeasureUnitsUtilsKt; @@ -151,34 +153,63 @@ public class GameWorld { setMap(x, y, 1, block); } - public void placeToForeground(int x, int y, Block value) { + public boolean placeToForeground(int x, int y, Block value) { if (!hasForeAt(x, y) || value == mGameItemsHolder.getFallbackBlock() || !getForeMap(x, y).hasCollision()) { setForeMap(x, y, value); + return true; } else if (value instanceof Block.Slab && isSameSlab(value, getForeMap(x, y))) { setForeMap(x, y, mGameItemsHolder.getBlock(((Block.Slab) value).getFullBlockKey())); + return true; } + return false; } - public void placeToBackground(int x, int y, Block value) { + public boolean placeToBackground(int x, int y, Block value) { if (value == mGameItemsHolder.getFallbackBlock() || (getBackMap(x, y) == mGameItemsHolder.getFallbackBlock() && value.hasCollision()) && (!value.isTransparent() || value == mGameItemsHolder.getBlock("glass"))) { setBackMap(x, y, value); + return true; } + return false; + } + + private void playerDurateTool() { + final InventoryItem playerCurrentItem = mMobsController.getPlayer().getCurrentItem(); + if (mMobsController.getPlayer().getCurrentItem().getItem().isTool()) { + mMobsController.getPlayer().decreaseCurrentItemCount(mGameItemsHolder); + } + } + + private boolean shouldDrop(Block block) { + final Item item = mMobsController.getPlayer().getCurrentItem().getItem(); + int toolLevel = item.isTool() ? ((Item.Tool)item).getLevel() : 0; + if (item.isTool() && block.getParams().getToolType() != item.getClass()) { + toolLevel = 0; + } + return toolLevel >= block.getParams().getToolLevel(); } public void destroyForeMap(int x, int y) { Block block = getForeMap(x, y); - if (block.hasDrop()) { - mDropController.addDrop(transformX(x) * 16 + 4, y * 16 + 4, mGameItemsHolder.getItem(block.getDrop())); + if (block.hasDrop() && shouldDrop(block)) { + for (int i = 0; i < block.getParams().getDropInfo().getCount(); i++) { + mDropController.addDrop(transformX(x) * 16 + 4, y * 16 + 4, mGameItemsHolder.getItem(block.getDrop())); + } } + playerDurateTool(); placeToForeground(x, y, mGameItemsHolder.getFallbackBlock()); } + + public void destroyBackMap(int x, int y) { Block block = getBackMap(x, y); - if (block.hasDrop()) { - mDropController.addDrop(transformX(x) * 16 + 4, y * 16 + 4, mGameItemsHolder.getItem(block.getDrop())); + if (block.hasDrop() && shouldDrop(block)) { + for (int i = 0; i < block.getParams().getDropInfo().getCount(); i++) { + mDropController.addDrop(transformX(x) * 16 + 4, y * 16 + 4, mGameItemsHolder.getItem(block.getDrop())); + } } + playerDurateTool(); placeToBackground(x, y, mGameItemsHolder.getFallbackBlock()); } } \ No newline at end of file diff --git a/core/src/ru/deadsoftware/cavedroid/misc/utils/RenderingUtils.kt b/core/src/ru/deadsoftware/cavedroid/misc/utils/RenderingUtils.kt index 28c3827..5849f03 100644 --- a/core/src/ru/deadsoftware/cavedroid/misc/utils/RenderingUtils.kt +++ b/core/src/ru/deadsoftware/cavedroid/misc/utils/RenderingUtils.kt @@ -1,6 +1,10 @@ package ru.deadsoftware.cavedroid.misc.utils +import com.badlogic.gdx.graphics.Color +import com.badlogic.gdx.graphics.g2d.GlyphLayout +import com.badlogic.gdx.graphics.g2d.SpriteBatch import com.badlogic.gdx.math.Rectangle +import ru.deadsoftware.cavedroid.misc.Assets private fun Rectangle.shifted(shift: Float) = Rectangle(x + shift, y, width, height) @@ -39,4 +43,10 @@ fun forEachBlockInArea( func(x, y) } } -} \ No newline at end of file +} + +@JvmOverloads +fun SpriteBatch.drawString(str: String, x: Float, y: Float, color: Color = Color.WHITE): GlyphLayout { + Assets.minecraftFont.color = color + return Assets.minecraftFont.draw(this, str, x, y) +} -- 2.29.2