From: fredboy Date: Sun, 19 May 2024 14:21:30 +0000 (+0700) Subject: Add food X-Git-Url: http://deadsoftware.ru/gitweb?a=commitdiff_plain;h=90daad1796cd59f11f12589686d9afe882ddb9b1;p=cavedroid.git Add food --- diff --git a/android/assets/json/game_items.json b/android/assets/json/game_items.json index 245ebb0..349daf3 100644 --- a/android/assets/json/game_items.json +++ b/android/assets/json/game_items.json @@ -1421,6 +1421,19 @@ "tint": "#EDBFB4", "action_key": "use_spawn_egg_pig", "origin_x": 0.5 + }, + "porkchop_raw": { + "name": "Raw Porkchop", + "type": "food", + "texture": "porkchop_raw", + "heal": 3, + "smelt_product": "porkchop_cooked" + }, + "porkchop_cooked": { + "name": "Raw Porkchop", + "type": "food", + "texture": "porkchop_cooked", + "heal": 8 } } } diff --git a/android/assets/pp/textures/items/porkchop_cooked.png b/android/assets/pp/textures/items/porkchop_cooked.png new file mode 100644 index 0000000..dbd02c5 Binary files /dev/null and b/android/assets/pp/textures/items/porkchop_cooked.png differ diff --git a/android/assets/pp/textures/items/porkchop_raw.png b/android/assets/pp/textures/items/porkchop_raw.png new file mode 100644 index 0000000..6c83dd0 Binary files /dev/null and b/android/assets/pp/textures/items/porkchop_raw.png differ diff --git a/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java b/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java index cb5b37e..cece8e2 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java @@ -9,6 +9,7 @@ import ru.deadsoftware.cavedroid.game.mobs.Mob; import ru.deadsoftware.cavedroid.game.mobs.MobsController; import ru.deadsoftware.cavedroid.game.mobs.player.Player; import ru.deadsoftware.cavedroid.game.model.block.Block; +import ru.deadsoftware.cavedroid.game.model.item.InventoryItem; import ru.deadsoftware.cavedroid.game.objects.drop.Drop; import ru.deadsoftware.cavedroid.game.objects.drop.DropController; import ru.deadsoftware.cavedroid.game.world.GameWorld; @@ -357,6 +358,10 @@ public class GamePhysics { mob.ai(mGameWorld, mGameItemsHolder, mMobsController, delta); mobPhy(mob, delta); if (mob.isDead()) { + for (InventoryItem invItem : mob.getDrop(mGameItemsHolder)) { + mDropController.addDrop(mob.x, mob.y, invItem); + } + it.remove(); } } @@ -364,6 +369,10 @@ public class GamePhysics { playerPhy(player, delta); player.ai(mGameWorld, mGameItemsHolder, mMobsController, delta); if (player.isDead()) { + for (InventoryItem invItem : player.inventory.getItems()) { + mDropController.addDrop(player.x, player.y, invItem); + } + player.inventory.clear(); player.respawn(mGameWorld, mGameItemsHolder); } } diff --git a/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/UseItemMouseInputHandler.kt b/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/UseItemMouseInputHandler.kt index 541bba7..7405faf 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/UseItemMouseInputHandler.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/UseItemMouseInputHandler.kt @@ -3,6 +3,7 @@ package ru.deadsoftware.cavedroid.game.input.handler.mouse import ru.deadsoftware.cavedroid.misc.annotations.multibinding.BindMouseInputHandler import com.badlogic.gdx.Gdx import com.badlogic.gdx.utils.Timer +import ru.deadsoftware.cavedroid.game.GameItemsHolder import ru.deadsoftware.cavedroid.game.GameScope import ru.deadsoftware.cavedroid.game.GameUiWindow import ru.deadsoftware.cavedroid.game.actions.placeToBackgroundAction @@ -29,6 +30,7 @@ class UseItemMouseInputHandler @Inject constructor( private val useBlockActionMap: Map, private val gameWindowsManager: GameWindowsManager, private val gameWorld: GameWorld, + private val gameItemsHolder: GameItemsHolder, ) : IMouseInputHandler { private var buttonHoldTask: Timer.Task? = null @@ -104,6 +106,9 @@ class UseItemMouseInputHandler @Inject constructor( } else if (item is Item.Usable) { useItemActionMap[item.useActionKey]?.perform(item, player.cursorX, player.cursorY) ?: Gdx.app.error(TAG, "use item action ${item.useActionKey} not found"); + } else if (item is Item.Food && player.health < player.maxHealth) { + player.heal(item.heal) + player.decreaseCurrentItemCount(gameItemsHolder) } else { tryUseBlock() } diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java index 04fa1bb..e4ca679 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java @@ -8,10 +8,14 @@ import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.utils.Timer; import ru.deadsoftware.cavedroid.game.GameItemsHolder; +import ru.deadsoftware.cavedroid.game.model.item.InventoryItem; +import ru.deadsoftware.cavedroid.game.model.item.Item; import ru.deadsoftware.cavedroid.game.world.GameWorld; import javax.annotation.CheckForNull; import java.io.Serializable; +import java.util.Collections; +import java.util.List; /** * Mob class. @@ -209,7 +213,7 @@ public abstract class Mob extends Rectangle implements Serializable { /** * Set's mob's dead variable to true and nothing else. It doesn't delete the */ - public final void kill() { + public void kill() { mDead = true; } @@ -259,6 +263,10 @@ public abstract class Mob extends Rectangle implements Serializable { return mHealth; } + public final int getMaxHealth() { + return mMaxHealth; + } + public final void attachToController(MobsController controller) { controller.addMob(this); } @@ -323,6 +331,10 @@ public abstract class Mob extends Rectangle implements Serializable { return isTakingDamage() ? DAMAGE_TINT_COLOR : Color.WHITE; } + public List getDrop(GameItemsHolder gameItemsHolder) { + return Collections.emptyList(); + } + public abstract void draw(SpriteBatch spriteBatch, float x, float y, float delta); public abstract void ai(GameWorld gameWorld, GameItemsHolder gameItemsHolder, MobsController mobsController, float delta); diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/PeacefulMob.kt b/core/src/ru/deadsoftware/cavedroid/game/mobs/PeacefulMob.kt new file mode 100644 index 0000000..b0e5bc6 --- /dev/null +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/PeacefulMob.kt @@ -0,0 +1,20 @@ +package ru.deadsoftware.cavedroid.game.mobs + +import com.badlogic.gdx.math.MathUtils +import ru.deadsoftware.cavedroid.game.GameItemsHolder +import ru.deadsoftware.cavedroid.game.world.GameWorld + +abstract class PeacefulMob(x: Float, y: Float, width: Float, height: Float, direction: Direction, maxHealth: Int, ) + : Mob(x, y, width, height, direction, Type.MOB, maxHealth) { + + override fun ai(world: GameWorld, gameItemsHolder: GameItemsHolder, mobsController: MobsController, delta: Float) { + if (MathUtils.randomBoolean(delta)) { + if (velocity.x != 0f) { + velocity.x = 0f + } else { + changeDir() + } + } + } + +} \ No newline at end of file diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Pig.kt b/core/src/ru/deadsoftware/cavedroid/game/mobs/Pig.kt index 68c457e..b3d647d 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/Pig.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/Pig.kt @@ -1,10 +1,9 @@ package ru.deadsoftware.cavedroid.game.mobs import com.badlogic.gdx.graphics.g2d.SpriteBatch -import com.badlogic.gdx.math.MathUtils import com.badlogic.gdx.math.Vector2 import ru.deadsoftware.cavedroid.game.GameItemsHolder -import ru.deadsoftware.cavedroid.game.world.GameWorld +import ru.deadsoftware.cavedroid.game.model.item.InventoryItem import ru.deadsoftware.cavedroid.misc.utils.drawSprite import ru.deadsoftware.cavedroid.misc.utils.mobs.MobSprites.Pig.getBackgroundLeg import ru.deadsoftware.cavedroid.misc.utils.mobs.MobSprites.Pig.getBody @@ -13,7 +12,7 @@ import ru.deadsoftware.cavedroid.misc.utils.mobs.MobSprites.Pig.getLeftLegRelati import ru.deadsoftware.cavedroid.misc.utils.mobs.MobSprites.Pig.getLegsRelativeY import ru.deadsoftware.cavedroid.misc.utils.mobs.MobSprites.Pig.getRightLegRelativeX -class Pig(x: Float, y: Float) : Mob(x, y, WIDTH, HEIGHT, randomDir(), Type.MOB, MAX_HEALTH) { +class Pig(x: Float, y: Float) : PeacefulMob(x, y, WIDTH, HEIGHT, randomDir(), MAX_HEALTH) { override fun getSpeed(): Float { return SPEED @@ -38,14 +37,8 @@ class Pig(x: Float, y: Float) : Mob(x, y, WIDTH, HEIGHT, randomDir(), Type.MOB, } } - override fun ai(world: GameWorld, gameItemsHolder: GameItemsHolder, mobsController: MobsController, delta: Float) { - if (MathUtils.randomBoolean(delta)) { - if (velocity.x != 0f) { - velocity.x = 0f - } else { - changeDir() - } - } + override fun getDrop(gameItemsHolder: GameItemsHolder): List { + return listOf(gameItemsHolder.getItem("porkchop_raw").toInventoryItem()) } override fun draw(spriteBatch: SpriteBatch, x: Float, y: Float, delta: Float) { diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/player/Inventory.kt b/core/src/ru/deadsoftware/cavedroid/game/mobs/player/Inventory.kt index 3012d66..43f298c 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/player/Inventory.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/player/Inventory.kt @@ -133,4 +133,10 @@ class Inventory( fun decreaseCurrentItemAmount(count: Int = 1) { decreaseItemAmount(activeSlot, count) } + + fun clear() { + for (i in _items.indices) { + _items[i] = fallbackItem + } + } } \ No newline at end of file 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 373db46..a276b71 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/model/dto/ItemDto.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/model/dto/ItemDto.kt @@ -20,4 +20,5 @@ data class ItemDto( @SerialName("tint") val tint: String? = null, @SerialName("burning_time") val burningTime: Long? = null, @SerialName("smelt_product") val smeltProduct: String? = null, + @SerialName("heal") val heal: Int? = null, ) 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 bf2a55c..9b2c62d 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/model/item/Item.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/model/item/Item.kt @@ -50,6 +50,11 @@ sealed class Item { return this is Usable } + fun isFood(): Boolean { + contract { returns(true) implies (this@Item is Food) } + return this is Food + } + @JvmOverloads fun toInventoryItem(amount: Int = 1): InventoryItem { return InventoryItem(this, amount) @@ -137,4 +142,10 @@ sealed class Item { override val level: Int, ) : Tool() + data class Food( + override val params: CommonItemParams, + override val sprite: Sprite, + val heal: Int, + ) : Item() + } \ 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 a0949a0..a1a6f0c 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/model/mapper/ItemMapper.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/model/mapper/ItemMapper.kt @@ -32,6 +32,7 @@ class ItemMapper @Inject constructor( "shears" -> Shears(params, requireNotNull(loadSprite(dto)), dto.mobDamageMultiplier, dto.blockDamageMultiplier, requireNotNull(dto.toolLevel)) "block" -> Block(params, requireNotNull(block)) "slab" -> Slab(params, requireNotNull(slabTopBlock), requireNotNull(slabBottomBlock)) + "food" -> Food(params, requireNotNull(loadSprite(dto)), requireNotNull(dto.heal)) "none" -> None(params) else -> throw IllegalArgumentException("Unknown item type ${dto.type}") }