DEADSOFTWARE

Implement DI for menu and refactor #13
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / mobs / Mob.java
index bcab5867bc4e8ee9186543eebd206e4733437b86..0f765925bc1679bed83830f29011a5f0bd88def8 100644 (file)
 package ru.deadsoftware.cavedroid.game.mobs;
 
 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 java.io.Serializable;
 
 /**
  * Mob class.
  */
-public abstract class Mob implements Serializable {
+public abstract class Mob extends Rectangle implements Serializable {
 
-    public static final int LEFT = 0;
-    public static final int RIGHT = 1;
-
-    private float width, height;
-    private int dir;
+    public enum Type {
+        MOB,
+        SAND,
+        GRAVEL
+    }
 
-    public boolean flyMode;
-    public Vector2 pos;
-    public Vector2 mov;
+    public enum Direction {
+        LEFT,
+        RIGHT
+    }
 
-    private boolean dead;
+    protected Vector2 mMove;
+    protected Type mType;
+    protected int mAnimDelta = 6;
+    protected int mAnim;
 
-    protected int animDelta = 6;
-    public boolean canJump;
-    int anim;
+    private Direction mDirection;
+    private boolean mDead;
+    private boolean mCanJump;
+    private boolean mFlyMode;
 
     /**
-     *
-     * @param x in pixels
-     * @param y in pixels
-     * @param width in pixels
-     * @param height in pixels
-     * @param dir integer representing a direction where 0 is left and 1 is right.
-     *            You should use {@link #LEFT} and {@link #RIGHT} constants
+     * @param x          in pixels
+     * @param y          in pixels
+     * @param width      in pixels
+     * @param height     in pixels
+     * @param mDirection Direction in which mob is looking
      */
-    public Mob(float x, float y, float width, float height, int dir) {
-        pos = new Vector2(x, y);
-        mov = new Vector2(0, 0);
-        this.width = width;
-        this.height = height;
-        canJump = false;
-        flyMode = false;
-        dead = false;
-        this.dir = dir;
+    protected Mob(float x, float y, float width, float height, Direction mDirection, Type type) {
+        super(x, y, width, height);
+        mMove = new Vector2(0, 0);
+        mCanJump = false;
+        mDead = false;
+        this.mDirection = mDirection;
+        this.mType = type;
+    }
+
+    protected static Direction randomDir() {
+        return MathUtils.randomBoolean(.5f) ? Direction.LEFT : Direction.RIGHT;
     }
 
     /**
-     *
      * @return The X coordinate of a mob in blocks
      */
-    public int getMapX() {
-        return (int) (pos.x + (getWidth() / 2)) / 16;
+    public final int getMapX() {
+        return (int) (x + (getWidth() / 2)) / 16;
     }
 
     /**
-     *
      * @return The Y coordinate of mob's upper edge in blocks
      */
-    public int getUpperMapY() {
-        return (int) (pos.y / 16);
+    public final int getUpperMapY() {
+        return (int) (y / 16);
     }
 
     /**
-     *
      * @return The Y coordinate if mob's vertical center in blocks
      */
-    public int getMiddleMapY() {
-        return (int) (pos.y + (getHeight() / 2)) / 16;
+    public final int getMiddleMapY() {
+        return (int) (y + (getHeight() / 2)) / 16;
     }
 
     /**
-     *
      * @return The Y coordinate of mob's legs in blocks
      */
-    public int getLowerMapY() {
-        return (int) (pos.y + getHeight()) / 16;
+    public final int getLowerMapY() {
+        return (int) (y + getHeight()) / 16;
     }
 
-    public float getWidth() {
+    public final float getWidth() {
         return width;
     }
 
-    public float getHeight() {
+    public final float getHeight() {
         return height;
     }
 
     /**
-     *
      * @return Integer representing a direction in which mob is looking, where 0 is left and 1 is right
      */
-    public int getDirection() {
-        return dir;
+    public final Direction getDirection() {
+        return mDirection;
     }
 
-    public boolean looksLeft() {
-        return getDirection() == LEFT;
+    public final boolean looksLeft() {
+        return mDirection == Direction.LEFT;
     }
 
-    public boolean looksRight() {
-        return getDirection() == RIGHT;
+    public final boolean looksRight() {
+        return mDirection == Direction.RIGHT;
     }
 
     /**
      * Switches direction in which mob is looking
      */
-    protected void switchDir() {
-        dir = looksLeft() ? RIGHT : LEFT;
+    protected final void switchDir() {
+        mDirection = looksLeft() ? Direction.RIGHT : Direction.LEFT;
     }
 
-    public boolean isDead() {
-        return dead;
+    protected final int dirMultiplier() {
+        return looksLeft() ? 0 : 1;
     }
 
-    /**
-     * Set's mob's dead variable to true and nothing else. It doesn't delete the mob.
-     */
-    public void kill() {
-        dead = true;
+    public final boolean isDead() {
+        return mDead;
+    }
+
+    public final int getAnim() {
+        return mAnim;
     }
 
     /**
-     *
-     * @return A {@link Rectangle} with mob's coordinates and size
+     * Set's mob's dead variable to true and nothing else. It doesn't delete the
      */
-    public Rectangle getRect() {
-        return new Rectangle(pos.x, pos.y, getWidth(), getHeight());
+    public final void kill() {
+        mDead = true;
     }
 
-    public abstract void ai();
+    public final void move() {
+        x += mMove.x;
+        y += mMove.y;
+    }
 
-    public abstract void changeDir();
+    public final Vector2 getMove() {
+        return mMove;
+    }
+
+    public final boolean canJump() {
+        return mCanJump;
+    }
+
+    public final void setCanJump(boolean canJump) {
+        this.mCanJump = canJump;
+    }
+
+    public final boolean isFlyMode() {
+        return mFlyMode;
+    }
+
+    public final void setFlyMode(boolean flyMode) {
+        this.mFlyMode = flyMode;
+    }
+
+    public final Type getType() {
+        return mType;
+    }
+
+    public void checkWorldBounds(GameWorld gameWorld) {
+        if (x + width / 2 < 0) {
+            x += gameWorld.getWidthPx();
+        }
+        if (x + width / 2 > gameWorld.getWidthPx()) {
+            x -= gameWorld.getWidthPx();
+        }
+    }
 
     public abstract void draw(SpriteBatch spriteBatch, float x, float y);
 
-    /**
-     *
-     * @return 0 - if regular mob. <br>
-     *     10 - if instance of {@link FallingSand} <br> 11 - if instance of {@link FallingGravel}
-     */
-    public abstract int getType(); //0 - mob, 10 - sand, 11 - gravel
+    public abstract void ai(GameWorld gameWorld);
+
+    public abstract void changeDir();
 }