DEADSOFTWARE

Add environment damage
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / mobs / Mob.java
index 91dfae55e90823ce7fdc91aa6f919835edc6e58c..415ef30376503a491a3eefd9eb8c127d53089b77 100644 (file)
@@ -1,10 +1,12 @@
 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;
 import com.badlogic.gdx.math.Vector2;
-import ru.deadsoftware.cavedroid.game.GameWorld;
+import ru.deadsoftware.cavedroid.game.GameItemsHolder;
+import ru.deadsoftware.cavedroid.game.world.GameWorld;
 
 import java.io.Serializable;
 
@@ -13,6 +15,8 @@ import java.io.Serializable;
  */
 public abstract class Mob extends Rectangle implements Serializable {
 
+    protected static int ANIMATION_SPEED = 360;
+
     public enum Type {
         MOB,
         SAND,
@@ -20,40 +24,100 @@ public abstract class Mob extends Rectangle implements Serializable {
     }
 
     public enum Direction {
-        LEFT,
-        RIGHT
+
+        LEFT(0, -1),
+        RIGHT(1, 1);
+
+        private final int index;
+        private final int basis;
+
+        /**
+         * Index for this direction (left = 0, right = 1)
+         */
+        public final int getIndex() {
+            return index;
+        }
+
+        /**
+         * Basis for this direction (left = -1, right = 1)
+         */
+        public final int getBasis() {
+            return basis;
+        }
+
+        Direction(int index, int basis) {
+            this.index = index;
+            this.basis = basis;
+        }
     }
 
-    protected Vector2 mMove;
+    protected Vector2 mVelocity;
     protected Type mType;
-    protected int mAnimDelta = 6;
-    protected int mAnim;
+    protected int mAnimDelta = ANIMATION_SPEED;
+    protected float mAnim;
 
     private Direction mDirection;
-    private boolean mDead;
+    protected boolean mDead;
     private boolean mCanJump;
     private boolean mFlyMode;
 
+    private final int mMaxHealth;
+    private int mHealth;
+
     /**
-     * @param x      in pixels
-     * @param y      in pixels
-     * @param width  in pixels
-     * @param height in pixels
-     * @param mDirection    Direction in which mob is looking
+     * @param x          in pixels
+     * @param y          in pixels
+     * @param width      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);
-        mMove = new Vector2(0, 0);
+        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 MathUtils.randomBoolean(.5f) ? Direction.LEFT : Direction.RIGHT;
     }
 
+    private boolean isAnimationIncreasing() {
+        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;
+        } else {
+            mAnim = 0;
+        }
+
+        if (mAnim > 60f) {
+            mAnim = 60f;
+            mAnimDelta = -ANIMATION_SPEED;
+        } else if (mAnim < -60f) {
+            mAnim = -60f;
+            mAnimDelta = ANIMATION_SPEED;
+        }
+
+        if (mVelocity.x == 0f && isAnimationIncreasing()) {
+            mAnimDelta = -mAnimDelta;
+        }
+    }
+
     /**
      * @return The X coordinate of a mob in blocks
      */
@@ -112,15 +176,11 @@ public abstract class Mob extends Rectangle implements Serializable {
         mDirection = looksLeft() ? Direction.RIGHT : Direction.LEFT;
     }
 
-    protected final int dirMultiplier() {
-        return looksLeft() ? 0 : 1;
-    }
-
     public final boolean isDead() {
         return mDead;
     }
 
-    public final int getAnim() {
+    public final float getAnim() {
         return mAnim;
     }
 
@@ -131,13 +191,17 @@ public abstract class Mob extends Rectangle implements Serializable {
         mDead = true;
     }
 
-    public final void move() {
-        x += mMove.x;
-        y += mMove.y;
+    public final void move(float delta) {
+        x += mVelocity.x * delta;
+        y += mVelocity.y * delta;
+    }
+
+    public final Vector2 getVelocity() {
+        return mVelocity;
     }
 
-    public final Vector2 getMove() {
-        return mMove;
+    protected final void setVelocity(Vector2 velocity) {
+        mVelocity = velocity;
     }
 
     public final boolean canJump() {
@@ -160,7 +224,7 @@ public abstract class Mob extends Rectangle implements Serializable {
         return mType;
     }
 
-    public void checkWorldBounds(GameWorld gameWorld) {
+    public final void checkWorldBounds(GameWorld gameWorld) {
         if (x + width / 2 < 0) {
             x += gameWorld.getWidthPx();
         }
@@ -169,9 +233,53 @@ public abstract class Mob extends Rectangle implements Serializable {
         }
     }
 
-    public abstract void draw(SpriteBatch spriteBatch, float x, float y);
+    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;
+        }
 
-    public abstract void ai(GameWorld gameWorld);
+        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, GameItemsHolder gameItemsHolder, float delta);
 
     public abstract void changeDir();
+
+    public abstract float getSpeed();
+
+    public abstract void jump();
 }