From ed1189467d0d1b57df7ca1335f2134c08acae5ec Mon Sep 17 00:00:00 2001 From: fredboy Date: Fri, 19 Apr 2024 19:12:27 +0700 Subject: [PATCH] Add player health --- android/assets/health.png | Bin 0 -> 259 bytes android/assets/json/texture_regions.json | 11 +++- .../cavedroid/game/GamePhysics.java | 13 ++--- .../cavedroid/game/GameRenderer.java | 27 +++++++++- .../cavedroid/game/mobs/FallingGravel.java | 2 +- .../cavedroid/game/mobs/FallingSand.java | 2 +- .../deadsoftware/cavedroid/game/mobs/Mob.java | 48 +++++++++++++++++- .../deadsoftware/cavedroid/game/mobs/Pig.kt | 3 +- .../cavedroid/game/mobs/Player.java | 20 +++++++- .../deadsoftware/cavedroid/misc/Assets.java | 1 - 10 files changed, 111 insertions(+), 16 deletions(-) create mode 100644 android/assets/health.png diff --git a/android/assets/health.png b/android/assets/health.png new file mode 100644 index 0000000000000000000000000000000000000000..93314e6e623a263d9543f036bf1fb3036ce37e38 GIT binary patch literal 259 zcmeAS@N?(olHy`uVBq!ia0vp^LO{&P!3HGl@7M4HDVAa<&kznEsNqQI0P=Tvx;Tbt zOk6wBkgv%>!1caF&(et*>5aT{2b3$>{0>T52(dTJ(Ak^DdseOEWYi0xa!0NDt;YY~ zo=;)>r^0yR(ve+mapsejc3xFt5ScnF#mzBt){V<)wkD^lyH}sC?iN$rHf;juVWsak z^2-+&KE5jSx5DVSg>Icy!@FOWw}V+0^fcbSYCOg8u~ovJ&QH4p?|#)tZaaTUqU8Ew zR}C%I;4>N=_riD7lnJ(37^FB=&bF85Kis+BdD)B$^^E^Av){D#ukQr 0) { + mob.damage(dmg); + } } mob.y = MathUtils.round(mob.getY()); @@ -218,14 +223,6 @@ public class GamePhysics { mob.getVelocity().y = 0; - - - //todo fall damage - // h = (v^2) / 2g - // dmg = max(0, (h - 48) / 32) - half of blocks fallen starting from 3 blocks height - // int dmg = ((int)Math.max(0f, (((mob.getVelocity().y * mob.getVelocity().y) / (2 * gravity.y)) - 48f) / 16f)); - // if (dmg > 0) System.out.println("Damage: " + dmg); - } else { mob.y += 1; mob.setCanJump(checkColl(mob)); diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java b/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java index c18ee53..b555f6b 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java @@ -228,6 +228,27 @@ public class GameRenderer extends Renderer { } + private void drawHealth(float x, float y) { + Player player = mMobsController.getPlayer(); + + if (player.gameMode == 1) { + return; + } + + TextureRegion wholeHeart = textureRegions.get("heart_whole"); + TextureRegion halfHeart = textureRegions.get("heart_half"); + + int wholeHearts = player.getHealth() / 2; + + for (int i = 0; i < wholeHearts; i++) { + spriter.draw(wholeHeart, x + i * wholeHeart.getRegionWidth(), y); + } + + if (player.getHealth() % 2 == 1) { + spriter.draw(halfHeart, x + wholeHearts * wholeHeart.getRegionWidth(), y); + } + } + private void drawGUI() { TextureRegion cursor = textureRegions.get("cursor"); TextureRegion hotbar = textureRegions.get("hotbar"); @@ -238,7 +259,11 @@ public class GameRenderer extends Renderer { mGameInput.getControlMode() == ControlMode.CURSOR || mMainConfig.isTouch()) { spriter.draw(cursor, mGameInput.getCurX() * 16 - getCamX(), mGameInput.getCurY() * 16 - getCamY()); } - spriter.draw(hotbar, getWidth() / 2 - (float) hotbar.getRegionWidth() / 2, 0); + + float hotbarX = getWidth() / 2 - (float) hotbar.getRegionWidth() / 2; + spriter.draw(hotbar, hotbarX, 0); + drawHealth(hotbarX, hotbar.getRegionHeight()); + for (int i = 0; i < 9; i++) { if (mMobsController.getPlayer().inventory[i] > 0) { if (GameItems.getItem(mMobsController.getPlayer().inventory[i]).isBlock()) { diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingGravel.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingGravel.java index ede6ba2..81fdb93 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingGravel.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingGravel.java @@ -18,7 +18,7 @@ public class FallingGravel extends Mob { * @param y Y in pixels */ public FallingGravel(float x, float y) { - super(x, y, 16, 16, Direction.LEFT, Type.GRAVEL); + super(x, y, 16, 16, Direction.LEFT, Type.GRAVEL, Integer.MAX_VALUE); mVelocity = new Vector2(0, 1); } diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingSand.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingSand.java index c59c3fc..7186b3a 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingSand.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingSand.java @@ -19,7 +19,7 @@ public class FallingSand extends Mob { * @param y Y in pixels */ public FallingSand(float x, float y) { - super(x, y, 16, 16, Direction.LEFT, Type.SAND); + super(x, y, 16, 16, Direction.LEFT, Type.SAND, Integer.MAX_VALUE); mVelocity = new Vector2(0, 1); } diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java index f19d247..592f78d 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java @@ -1,5 +1,6 @@ package ru.deadsoftware.cavedroid.game.mobs; +import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.Rectangle; @@ -59,6 +60,9 @@ public abstract class Mob extends Rectangle implements Serializable { private boolean mCanJump; private boolean mFlyMode; + private final int mMaxHealth; + private int mHealth; + /** * @param x in pixels * @param y in pixels @@ -66,13 +70,15 @@ public abstract class Mob extends Rectangle implements Serializable { * @param height in pixels * @param mDirection Direction in which mob is looking */ - protected Mob(float x, float y, float width, float height, Direction mDirection, Type type) { + protected Mob(float x, float y, float width, float height, Direction mDirection, Type type, int maxHealth) { super(x, y, width, height); mVelocity = new Vector2(0, 0); mCanJump = false; mDead = false; this.mDirection = mDirection; this.mType = type; + this.mMaxHealth = maxHealth; + this.mHealth = mMaxHealth; } protected static Direction randomDir() { @@ -83,6 +89,14 @@ public abstract class Mob extends Rectangle implements Serializable { return mAnim > 0 && mAnimDelta > 0 || mAnim < 0 && mAnimDelta < 0; } + private void checkHealth() { + mHealth = MathUtils.clamp(mHealth, 0, mMaxHealth); + + if (mHealth <= 0) { + kill(); + } + } + protected final void updateAnimation(float delta) { if (mVelocity.x != 0f || Math.abs(mAnim) > mAnimDelta * delta) { mAnim += mAnimDelta * delta; @@ -218,6 +232,38 @@ public abstract class Mob extends Rectangle implements Serializable { } } + public final int getHealth() { + return mHealth; + } + + public void damage(int damage) { + if (damage < 0) { + Gdx.app.error(this.getClass().getSimpleName(), "Damage cant be negative!"); + return; + } + + if (mHealth <= Integer.MIN_VALUE + damage) { + mHealth = Integer.MIN_VALUE + damage; + } + + mHealth -= damage; + checkHealth(); + } + + public void heal(int heal) { + if (heal < 0) { + Gdx.app.error(this.getClass().getSimpleName(), "Heal cant be negative!"); + return; + } + + if (mHealth >= Integer.MAX_VALUE - heal) { + mHealth = Integer.MAX_VALUE - heal; + } + + mHealth += heal; + checkHealth(); + } + public abstract void draw(SpriteBatch spriteBatch, float x, float y, float delta); public abstract void ai(GameWorld gameWorld, float delta); diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Pig.kt b/core/src/ru/deadsoftware/cavedroid/game/mobs/Pig.kt index a929546..4f9d5fc 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/Pig.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/Pig.kt @@ -12,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) { +class Pig(x: Float, y: Float) : Mob(x, y, WIDTH, HEIGHT, randomDir(), Type.MOB, MAX_HEALTH) { override fun getSpeed(): Float { return SPEED @@ -57,5 +57,6 @@ class Pig(x: Float, y: Float) : Mob(x, y, WIDTH, HEIGHT, randomDir(), Type.MOB) private const val HEIGHT = 18f private const val SPEED = 69.072f private const val JUMP_VELOCITY = -133.332f + private const val MAX_HEALTH = 10; } } \ No newline at end of file diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java index 8fce116..ebdd9c0 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java @@ -18,6 +18,7 @@ public class Player extends Mob { private static final float SPEED = 69.072f; private static final float JUMP_VELOCITY = -133.332f; + private static final int MAX_HEALTH = 20; private boolean hitting = false; private float hitAnim = 0f; @@ -30,7 +31,7 @@ public class Player extends Mob { public float headRotation = 0f; public Player() { - super(0, 0, 4, 30, randomDir(), Type.MOB); + super(0, 0, 4, 30, randomDir(), Type.MOB, MAX_HEALTH); this.gameMode = 1; inventory = new int[9]; swim = false; @@ -42,6 +43,7 @@ public class Player extends Mob { this.y = pos.y; mVelocity.setZero(); mDead = false; + heal(MAX_HEALTH); } public void pickUpDrop(Drop drop) { @@ -93,6 +95,22 @@ public class Player extends Mob { public void changeDir() { } + @Override + public void damage(int damage) { + if (gameMode == 1) { + return; + } + super.damage(damage); + } + + @Override + public void heal(int heal) { + if (gameMode == 1) { + return; + } + super.heal(heal); + } + private void drawItem(SpriteBatch spriteBatch, float x, float y, float anim) { final int itemId = inventory[slot]; final Item item = GameItems.getItem(itemId); diff --git a/core/src/ru/deadsoftware/cavedroid/misc/Assets.java b/core/src/ru/deadsoftware/cavedroid/misc/Assets.java index c58f703..05bd47f 100644 --- a/core/src/ru/deadsoftware/cavedroid/misc/Assets.java +++ b/core/src/ru/deadsoftware/cavedroid/misc/Assets.java @@ -1,6 +1,5 @@ package ru.deadsoftware.cavedroid.misc; -import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.GlyphLayout; -- 2.29.2