},
"shade": {},
"gamelogo": {},
- "background": {}
+ "background": {},
+ "health":{
+ "heart_whole": {
+ "w": 9
+ },
+ "heart_half": {
+ "x": 9,
+ "w": 9
+ }
+ }
}
\ No newline at end of file
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());
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));
}
+ 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");
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()) {
* @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);
}
* @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);
}
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;
private boolean mCanJump;
private boolean mFlyMode;
+ private final int mMaxHealth;
+ private int mHealth;
+
/**
* @param x in pixels
* @param y in pixels
* @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() {
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;
}
}
+ 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);
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
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
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;
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;
this.y = pos.y;
mVelocity.setZero();
mDead = false;
+ heal(MAX_HEALTH);
}
public void pickUpDrop(Drop drop) {
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);
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;