From: fredboy Date: Wed, 17 Apr 2024 14:32:58 +0000 (+0700) Subject: Add item in player hand X-Git-Tag: alpha0.5.0~1 X-Git-Url: http://deadsoftware.ru/gitweb?a=commitdiff_plain;h=2b0cd1ba5dbf194bd5ed35964c0cb2ea16293be4;p=cavedroid.git Add item in player hand --- diff --git a/android/assets/json/game_items.json b/android/assets/json/game_items.json index 042f0c9..7afdfb1 100644 --- a/android/assets/json/game_items.json +++ b/android/assets/json/game_items.json @@ -458,11 +458,13 @@ }, "tallgrass": { "name": "Tall Grass", - "type": "block" + "type": "block", + "origin_x": 0.5 }, "deadbush": { "name": "Dead Bush", - "type": "block" + "type": "block", + "origin_x": 0.5 }, "bricks": { "name": "Bricks", @@ -470,19 +472,23 @@ }, "dandelion": { "name": "Dandelion", - "type": "block" + "type": "block", + "origin_x": 0.5 }, "rose": { "name": "Rose", - "type": "block" + "type": "block", + "origin_x": 0.5 }, "mushroom_brown": { "name": "Mushroom", - "type": "block" + "type": "block", + "origin_x": 0.5 }, "mushroom_red": { "name": "Mushroom", - "type": "block" + "type": "block", + "origin_x": 0.5 }, "wool_colored_white": { "name": "Wool", @@ -600,55 +606,71 @@ }, "wood_sword": { "name": "Wooden Sword", - "type": "tool" + "type": "tool", + "origin_x": 0.125 }, "stone_sword": { "name": "Stone Sword", - "type": "tool" + "type": "tool", + "origin_x": 0.125 }, "iron_sword": { "name": "Iron Sword", - "type": "tool" + "type": "tool", + "origin_x": 0.125 }, "diamond_sword": { "name": "Diamond Sword", - "type": "tool" + "type": "tool", + "origin_x": 0.125 }, "gold_sword": { "name": "Golden Sword", - "type": "tool" + "type": "tool", + "origin_x": 0.125 }, "wood_shovel": { "name": "Wooden Shovel", - "type": "tool" + "type": "tool", + "origin_x": 0.125 }, "stone_shovel": { "name": "Stone Shovel", - "type": "tool" + "type": "tool", + "origin_x": 0.125 }, "iron_shovel": { "name": "Iron Shovel", - "type": "tool" + "type": "tool", + "origin_x": 0.125 }, "diamond_shovel": { "name": "Diamond Shovel", - "type": "tool" + "type": "tool", + "origin_x": 0.125 }, "gold_shovel": { "name": "Golden Shovel", - "type": "tool" + "type": "tool", + "origin_x": 0.125 }, "bucket_empty": { "name": "Empty Bucket", - "type": "tool" + "type": "bucket", + "origin_x": 0.25, + "origin_y": 1 }, "bucket_water": { "name": "Water Bucket", - "type": "tool" + "type": "bucket", + "origin_x": 0.25, + "origin_y": 1 }, "bucket_lava": { "name": "Lava Bucket", - "type": "tool" + "type": "bucket", + "origin_x": 0.25, + "origin_y": 1 } } } \ No newline at end of file diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameItems.java b/core/src/ru/deadsoftware/cavedroid/game/GameItems.java index 7a619b3..56a2b58 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameItems.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameItems.java @@ -3,6 +3,7 @@ package ru.deadsoftware.cavedroid.game; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.Sprite; +import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.utils.ArrayMap; import com.badlogic.gdx.utils.GdxRuntimeException; import com.badlogic.gdx.utils.JsonValue; @@ -10,6 +11,7 @@ import ru.deadsoftware.cavedroid.game.objects.Block; import ru.deadsoftware.cavedroid.game.objects.Item; import ru.deadsoftware.cavedroid.misc.Assets; import ru.deadsoftware.cavedroid.misc.utils.AssetLoader; +import ru.deadsoftware.cavedroid.misc.utils.SpriteOrigin; import java.util.HashMap; @@ -131,8 +133,11 @@ public class GameItems { new Texture(assetLoader.getAssetHandle("textures/blocks/" + tex + ".png")); boolean animated = Assets.getBooleanFromJson(block, "animated", false); int frames = Assets.getIntFromJson(block, "frames", 0); + int id = blocks.size; + blocksIds.put(key, id); Block newBlock = new Block( + id, left, top, right, @@ -153,8 +158,6 @@ public class GameItems { clipWidth, clipHeight ); - - blocksIds.put(key, blocks.size); blocks.put(key, newBlock); } catch (GdxRuntimeException e) { Gdx.app.error(TAG, e.getMessage()); @@ -168,8 +171,16 @@ public class GameItems { String texture = Assets.getStringFromJson(item, "texture", key); Sprite sprite = type.equals("block") ? null : new Sprite(new Texture(assetLoader.getAssetHandle("textures/items/" + texture + ".png"))); - itemsIds.put(key, items.size); - items.put(key, new Item(name, type, sprite)); + + float originX = Assets.getFloatFromJson(item, "origin_x", 0f); + float originY = Assets.getFloatFromJson(item, "origin_y", 1f); + originX = MathUtils.clamp(originX, 0f, 1f); + originY = MathUtils.clamp(originY, 0f, 1f); + SpriteOrigin origin = new SpriteOrigin(originX, originY); + + int id = items.size; + itemsIds.put(key, id); + items.put(key, new Item(id, name, type, sprite, origin)); } catch (GdxRuntimeException e) { Gdx.app.error(TAG, e.getMessage()); } diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java index 2b58f7c..b7a975c 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java @@ -32,7 +32,7 @@ public abstract class Mob extends Rectangle implements Serializable { protected float mAnim; private Direction mDirection; - private boolean mDead; + protected boolean mDead; private boolean mCanJump; private boolean mFlyMode; diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java index 1f42039..93150b6 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java @@ -4,11 +4,16 @@ import com.badlogic.gdx.graphics.g2d.Sprite; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.Vector2; +import ru.deadsoftware.cavedroid.game.GameItems; import ru.deadsoftware.cavedroid.game.objects.Drop; +import ru.deadsoftware.cavedroid.game.objects.Item; import ru.deadsoftware.cavedroid.game.world.GameWorld; import ru.deadsoftware.cavedroid.misc.Assets; +import ru.deadsoftware.cavedroid.misc.utils.SpriteOrigin; import ru.deadsoftware.cavedroid.misc.utils.SpriteUtilsKt; +import javax.annotation.CheckForNull; + public class Player extends Mob { public final int[] inventory; @@ -29,6 +34,7 @@ public class Player extends Mob { this.x = pos.x; this.y = pos.y; mVelocity.setZero(); + mDead = false; } public void pickUpDrop(Drop drop) { @@ -70,6 +76,50 @@ public class Player extends Mob { public void changeDir() { } + private void drawItem(SpriteBatch spriteBatch, float x, float y) { + final int itemId = inventory[slot]; + final Item item = GameItems.getItem(itemId); + + @CheckForNull final Sprite sprite = item.isBlock() + ? item.toBlock().getTexture() + : item.getSprite(); + + if (sprite == null) { + return; + } + + if (!item.isTool()) { + sprite.setSize(Drop.DROP_SIZE, Drop.DROP_SIZE); + } + + final float handLength = Assets.playerSprite[0][2].getHeight(); + + final SpriteOrigin spriteOrigin = item.getDefaultOrigin(); + final int handMultiplier = 1 + -2 * dirMultiplier(); + final float xOffset = (-1 + dirMultiplier()) * sprite.getWidth() + 4 + handMultiplier * (sprite.getWidth() * spriteOrigin.getX()); + final float yOffset = item.isTool() ? -sprite.getHeight() / 2 : 0; + + float rotate = mAnim + 30; + + final float itemX = x + handLength * MathUtils.sin(handMultiplier * mAnim * MathUtils.degRad) + xOffset; + final float itemY = y + handLength * MathUtils.cos(handMultiplier * mAnim * MathUtils.degRad) + yOffset; + + if (looksLeft()) { + sprite.setFlip(true, sprite.isFlipY()); + SpriteUtilsKt.applyOrigin(sprite, spriteOrigin.getFlipped(true, false)); + } else { + sprite.setFlip(false, sprite.isFlipY()); + SpriteUtilsKt.applyOrigin(sprite, spriteOrigin); + } + + SpriteUtilsKt.draw(spriteBatch, sprite, itemX, itemY, -handMultiplier * rotate); + + // dont forget to reset + sprite.setFlip(false, sprite.isFlipY()); + sprite.setRotation(0); + sprite.setOriginCenter(); + } + @Override public void draw(SpriteBatch spriteBatch, float x, float y, float delta) { updateAnimation(delta); @@ -82,10 +132,20 @@ public class Player extends Mob { final Sprite frontHand = Assets.playerSprite[0][2]; SpriteUtilsKt.draw(spriteBatch, backHand, x + 2, y + 8, -mAnim); + + if (looksLeft()) { + drawItem(spriteBatch, x, y); + } + SpriteUtilsKt.draw(spriteBatch, backLeg, x + 2, y + 20, mAnim); SpriteUtilsKt.draw(spriteBatch, frontLeg, x + 2, y + 20, -mAnim); SpriteUtilsKt.draw(spriteBatch, head, x, y, headRotation); SpriteUtilsKt.draw(spriteBatch, body, x + 2, y + 8); + + if (looksRight()) { + drawItem(spriteBatch, x, y); + } + SpriteUtilsKt.draw(spriteBatch, frontHand, x + 2, y + 8, mAnim); } diff --git a/core/src/ru/deadsoftware/cavedroid/game/objects/Block.kt b/core/src/ru/deadsoftware/cavedroid/game/objects/Block.kt index d44f017..0b93809 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/objects/Block.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/objects/Block.kt @@ -33,6 +33,7 @@ private const val DEPRECATION_MESSAGE = * @param spriteBottom block's sprite bottom on texture */ data class Block( + val id: Int, val left: Int, val top: Int, val right: Int, diff --git a/core/src/ru/deadsoftware/cavedroid/game/objects/Item.kt b/core/src/ru/deadsoftware/cavedroid/game/objects/Item.kt index 59d231d..9eea976 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/objects/Item.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/objects/Item.kt @@ -1,11 +1,15 @@ package ru.deadsoftware.cavedroid.game.objects import com.badlogic.gdx.graphics.g2d.Sprite +import ru.deadsoftware.cavedroid.game.GameItems +import ru.deadsoftware.cavedroid.misc.utils.SpriteOrigin data class Item( + val id: Int, val name: String, val type: String, - val sprite: Sprite? + val sprite: Sprite?, + val defaultOrigin: SpriteOrigin, ) { init { @@ -16,7 +20,19 @@ data class Item( fun isBlock() = type == "block" + fun isTool() = type == "tool" + + /** + * Returns block associated with this item. Null if this is not a block + */ + fun toBlock(): Block? { + if (!isBlock()) { + return null + } + + return GameItems.getBlock(GameItems.getBlockIdByItemId(id)) + } + @Deprecated("Was renamed to Sprite to comply with variable type.", ReplaceWith("requireSprite()")) fun getTexture() = sprite - } \ No newline at end of file diff --git a/core/src/ru/deadsoftware/cavedroid/misc/Assets.java b/core/src/ru/deadsoftware/cavedroid/misc/Assets.java index 7055a73..c58f703 100644 --- a/core/src/ru/deadsoftware/cavedroid/misc/Assets.java +++ b/core/src/ru/deadsoftware/cavedroid/misc/Assets.java @@ -110,6 +110,10 @@ public class Assets { return json.has(name) ? json.getInt(name) : defaultValue; } + public static float getFloatFromJson(JsonValue json, String name, float defaultValue) { + return json.has(name) ? json.getFloat(name) : defaultValue; + } + public static String getStringFromJson(JsonValue json, String name, String defaultValue) { return json.has(name) ? json.getString(name) : defaultValue; } diff --git a/core/src/ru/deadsoftware/cavedroid/misc/utils/SpriteOrigin.kt b/core/src/ru/deadsoftware/cavedroid/misc/utils/SpriteOrigin.kt new file mode 100644 index 0000000..830dc9a --- /dev/null +++ b/core/src/ru/deadsoftware/cavedroid/misc/utils/SpriteOrigin.kt @@ -0,0 +1,25 @@ +package ru.deadsoftware.cavedroid.misc.utils + +/** + * An origin of a [com.badlogic.gdx.graphics.g2d.Sprite] + * + * x and y must be between 0 and 1 in percents from sprite size + */ +data class SpriteOrigin( + val x: Float, + val y: Float, +) { + + init { + assert(x in 0f..1f) + assert(y in 0f..1f) + } + + fun getFlipped(flipX: Boolean, flipY: Boolean): SpriteOrigin { + return SpriteOrigin( + x = if (flipX) 1 - x else x, + y = if (flipY) 1 - y else y, + ) + } + +} diff --git a/core/src/ru/deadsoftware/cavedroid/misc/utils/SpriteUtils.kt b/core/src/ru/deadsoftware/cavedroid/misc/utils/SpriteUtils.kt index d9f52d5..72165e7 100644 --- a/core/src/ru/deadsoftware/cavedroid/misc/utils/SpriteUtils.kt +++ b/core/src/ru/deadsoftware/cavedroid/misc/utils/SpriteUtils.kt @@ -12,3 +12,7 @@ fun SpriteBatch.draw(sprite: Sprite, x: Float, y: Float, rotation: Float = 0f) { sprite.setPosition(x, y) sprite.draw(this) } + +fun Sprite.applyOrigin(origin: SpriteOrigin) { + setOrigin(width * origin.x, height * origin.y) +} \ No newline at end of file