summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 55eff4a)
raw | patch | inline | side by side (parent: 55eff4a)
author | fredboy <fredboy@protonmail.com> | |
Wed, 17 Apr 2024 14:32:58 +0000 (21:32 +0700) | ||
committer | fredboy <fredboy@protonmail.com> | |
Wed, 17 Apr 2024 14:36:11 +0000 (21:36 +0700) |
index 042f0c9bbea1a77cde66481caaf6ec8fde516cc2..7afdfb1599413c593437c547a02f6866cead1cb3 100644 (file)
},
"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",
},
"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",
},
"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 7a619b30748da834cdd003dcf4a7da2362b94979..56a2b5860eb6df91ef1691207e37e80d631491d7 100644 (file)
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;
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;
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,
clipWidth,
clipHeight
);
-
- blocksIds.put(key, blocks.size);
blocks.put(key, newBlock);
} catch (GdxRuntimeException e) {
Gdx.app.error(TAG, e.getMessage());
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 2b58f7c626817f0fd9789f453b751d7463aa8b59..b7a975caf3b4217032573afdfc200760515f78f3 100644 (file)
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 1f42039bfec7bd2969ec002bdacc4c9ee8ae4386..93150b6a5d3f4870187251071d4dfabf2bdeff2c 100644 (file)
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;
this.x = pos.x;
this.y = pos.y;
mVelocity.setZero();
+ mDead = false;
}
public void pickUpDrop(Drop drop) {
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);
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 d44f017e0647016afb767fba663815f63a699063..0b93809af232d0dce08cdf90fad13cd7cf23f090 100644 (file)
* @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 59d231d5b6205e3c1ad7a35280121ea4848f819b..9eea9767e3967ed2e9783e65d20c84207bcecd24 100644 (file)
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 {
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 7055a73cfaa3b7a9b3ed1269f0ba123d980b23d3..c58f7032bbb213931a93567280b0113a1ba5c34d 100644 (file)
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
--- /dev/null
@@ -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 d9f52d522c090c8026362521318ee8807e1b6dc0..72165e7cb196e4bf03dcb39fe8c7efb548aef05f 100644 (file)
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