From: fredboy Date: Fri, 19 Apr 2024 12:12:27 +0000 (+0700) Subject: Add player health X-Git-Tag: alpha0.5.2~9 X-Git-Url: http://deadsoftware.ru/gitweb?p=cavedroid.git;a=commitdiff_plain;h=ed1189467d0d1b57df7ca1335f2134c08acae5ec Add player health --- diff --git a/android/assets/health.png b/android/assets/health.png new file mode 100644 index 0000000..93314e6 Binary files /dev/null and b/android/assets/health.png differ diff --git a/android/assets/json/texture_regions.json b/android/assets/json/texture_regions.json index e1f3e48..9254986 100644 --- a/android/assets/json/texture_regions.json +++ b/android/assets/json/texture_regions.json @@ -134,5 +134,14 @@ }, "shade": {}, "gamelogo": {}, - "background": {} + "background": {}, + "health":{ + "heart_whole": { + "w": 9 + }, + "heart_half": { + "x": 9, + "w": 9 + } + } } \ No newline at end of file diff --git a/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java b/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java index 70afcbf..4127b6d 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java @@ -208,6 +208,11 @@ public class GamePhysics { if (d == -1) { mob.setCanJump(true); mob.setFlyMode(false); + + int dmg = ((int)Math.max(0f, (((mob.getVelocity().y * mob.getVelocity().y) / (2 * gravity.y)) - 48f) / 16f)); + if (dmg > 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;