diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java
index f19d247c4d9df9a972eb4d423cedbd3ea8c35ac1..feb5ceb08a0165f58c00538678dbcc982e4a6812 100644 (file)
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 final void attachToController(MobsController controller) {
+ controller.addMob(this);
+ }
+
+ 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);