X-Git-Url: https://deadsoftware.ru/gitweb?a=blobdiff_plain;f=core%2Fsrc%2Fru%2Fdeadsoftware%2Fcavedroid%2Fgame%2Fmobs%2FMob.java;h=62ddc50d4df7a6e00217a3a1f161cee8e745cb44;hb=1e285247085ba04351feb486a0be6aa577f43093;hp=f19d247c4d9df9a972eb4d423cedbd3ea8c35ac1;hpb=462f97f8da742fe35f516fec00ca9a581d688e7a;p=cavedroid.git diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java index f19d247..62ddc50 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java @@ -1,11 +1,16 @@ package ru.deadsoftware.cavedroid.game.mobs; +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Vector2; +import com.badlogic.gdx.utils.Timer; +import ru.deadsoftware.cavedroid.game.GameItemsHolder; import ru.deadsoftware.cavedroid.game.world.GameWorld; +import javax.annotation.CheckForNull; import java.io.Serializable; /** @@ -13,6 +18,11 @@ import java.io.Serializable; */ public abstract class Mob extends Rectangle implements Serializable { + private static final float DAMAGE_TINT_TIMEOUT_S = 0.5f; + private static final Color DAMAGE_TINT_COLOR = new Color(0xff8080ff); + + private static final float HIT_RANGE = 8f; + protected static int ANIMATION_SPEED = 360; public enum Type { @@ -49,6 +59,14 @@ public abstract class Mob extends Rectangle implements Serializable { } } + private class ResetTakeDamageTask extends Timer.Task { + + @Override + public void run() { + mTakingDamage = false; + } + } + protected Vector2 mVelocity; protected Type mType; protected int mAnimDelta = ANIMATION_SPEED; @@ -59,6 +77,12 @@ public abstract class Mob extends Rectangle implements Serializable { private boolean mCanJump; private boolean mFlyMode; + private final int mMaxHealth; + private int mHealth; + + private transient boolean mTakingDamage = false; + @CheckForNull private transient ResetTakeDamageTask mResetTakeDamageTask = null; + /** * @param x in pixels * @param y in pixels @@ -66,13 +90,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,18 +109,30 @@ 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; + final float velocityMultiplier = (Math.abs(getVelocity().x) / getSpeed()); + final float animMultiplier = (velocityMultiplier == 0f ? 1f : velocityMultiplier) * delta; + final float maxAnim = 60f * (velocityMultiplier == 0f ? 1f : velocityMultiplier); + + if (mVelocity.x != 0f || Math.abs(mAnim) > mAnimDelta * animMultiplier) { + mAnim += mAnimDelta * animMultiplier; } else { mAnim = 0; } - if (mAnim > 60f) { - mAnim = 60f; + if (mAnim > maxAnim) { + mAnim = maxAnim; mAnimDelta = -ANIMATION_SPEED; - } else if (mAnim < -60f) { - mAnim = -60f; + } else if (mAnim < -maxAnim) { + mAnim = -maxAnim; mAnimDelta = ANIMATION_SPEED; } @@ -218,9 +256,77 @@ public abstract class Mob extends Rectangle implements Serializable { } } + public final int getHealth() { + return mHealth; + } + + public final void attachToController(MobsController controller) { + controller.addMob(this); + } + + public void damage(int damage) { + if (damage == 0) { + return; + } + + 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(); + + setTakingDamage(true); + } + + 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 Rectangle getHitBox() { + return new Rectangle(x - HIT_RANGE, y - HIT_RANGE, width + HIT_RANGE, height + HIT_RANGE); + } + + public boolean isTakingDamage() { + return mTakingDamage; + } + + public void setTakingDamage(boolean takingDamage) { + mTakingDamage = takingDamage; + + if (takingDamage) { + if (mResetTakeDamageTask != null && mResetTakeDamageTask.isScheduled()) { + mResetTakeDamageTask.cancel(); + } else if (mResetTakeDamageTask == null) { + mResetTakeDamageTask = new ResetTakeDamageTask(); + } + + Timer.schedule(mResetTakeDamageTask, DAMAGE_TINT_TIMEOUT_S); + } + } + + protected Color getTintColor() { + return isTakingDamage() ? DAMAGE_TINT_COLOR : Color.WHITE; + } + public abstract void draw(SpriteBatch spriteBatch, float x, float y, float delta); - public abstract void ai(GameWorld gameWorld, float delta); + public abstract void ai(GameWorld gameWorld, GameItemsHolder gameItemsHolder, MobsController mobsController, float delta); public abstract void changeDir();