DEADSOFTWARE

Add food
authorfredboy <fredboy@protonmail.com>
Sun, 19 May 2024 14:21:30 +0000 (21:21 +0700)
committerfredboy <fredboy@protonmail.com>
Sun, 19 May 2024 14:21:30 +0000 (21:21 +0700)
12 files changed:
android/assets/json/game_items.json
android/assets/pp/textures/items/porkchop_cooked.png [new file with mode: 0644]
android/assets/pp/textures/items/porkchop_raw.png [new file with mode: 0644]
core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java
core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/UseItemMouseInputHandler.kt
core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java
core/src/ru/deadsoftware/cavedroid/game/mobs/PeacefulMob.kt [new file with mode: 0644]
core/src/ru/deadsoftware/cavedroid/game/mobs/Pig.kt
core/src/ru/deadsoftware/cavedroid/game/mobs/player/Inventory.kt
core/src/ru/deadsoftware/cavedroid/game/model/dto/ItemDto.kt
core/src/ru/deadsoftware/cavedroid/game/model/item/Item.kt
core/src/ru/deadsoftware/cavedroid/game/model/mapper/ItemMapper.kt

index 245ebb04c8b169875bbab55d0ff5c7e9e4a44ee8..349daf3f752dfb4a5caeb70f9a8c2c6dbd6e1589 100644 (file)
       "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 (file)
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 (file)
index 0000000..6c83dd0
Binary files /dev/null and b/android/assets/pp/textures/items/porkchop_raw.png differ
index cb5b37e35a5d5a0c157c225cfac938e4154dbdfc..cece8e2c625ebe9e8823f9a5b347f063e5485e4b 100644 (file)
@@ -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);
         }
     }
index 541bba71243e63ababb76f794f5960e1b7a29b09..7405faf0c5eae97f812199b8ec46599506f822ef 100644 (file)
@@ -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<String, @JvmSuppressWildcards IUseBlockAction>,
     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()
         }
index 04fa1bbe91f8b88ca6879094dbf343f5dbbef068..e4ca679cb43c5a68430d521889459c1958e6c439 100644 (file)
@@ -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<InventoryItem> 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 (file)
index 0000000..b0e5bc6
--- /dev/null
@@ -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
index 68c457ee05887ee81521ac93bffe0740e8e84688..b3d647d328aae2fd0a65f89f66b92ff3c5b0e832 100644 (file)
@@ -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<InventoryItem> {
+        return listOf(gameItemsHolder.getItem("porkchop_raw").toInventoryItem())
     }
 
     override fun draw(spriteBatch: SpriteBatch, x: Float, y: Float, delta: Float) {
index 3012d6675a3346bed978a5183b342120d31ab0b9..43f298c876e9702fb5e675e5b429602b22fbf13d 100644 (file)
@@ -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
index 373db466baf14f54fb86b352be00acbb8353f3e6..a276b7100decd2174e87cd88bb0f9231f7030b9e 100644 (file)
@@ -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,
 )
index bf2a55cd6a9d164870c6d4ebb8339310a0bece86..9b2c62de4794111b4edab8bb332af7a0a99f5809 100644 (file)
@@ -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
index a0949a05d2c082c427cb6ccec26524c6a87a047c..a1a6f0c68d0cb2357eebc5262fa2acb9c1b6f777 100644 (file)
@@ -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}")
         }