DEADSOFTWARE

Implement DI for menu and refactor #13
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / mobs / Mob.java
1 package ru.deadsoftware.cavedroid.game.mobs;
3 import com.badlogic.gdx.graphics.g2d.SpriteBatch;
4 import com.badlogic.gdx.math.MathUtils;
5 import com.badlogic.gdx.math.Rectangle;
6 import com.badlogic.gdx.math.Vector2;
7 import ru.deadsoftware.cavedroid.game.GameWorld;
9 import java.io.Serializable;
11 /**
12 * Mob class.
13 */
14 public abstract class Mob extends Rectangle implements Serializable {
16 public enum Type {
17 MOB,
18 SAND,
19 GRAVEL
20 }
22 public enum Direction {
23 LEFT,
24 RIGHT
25 }
27 protected Vector2 mMove;
28 protected Type mType;
29 protected int mAnimDelta = 6;
30 protected int mAnim;
32 private Direction mDirection;
33 private boolean mDead;
34 private boolean mCanJump;
35 private boolean mFlyMode;
37 /**
38 * @param x in pixels
39 * @param y in pixels
40 * @param width in pixels
41 * @param height in pixels
42 * @param mDirection Direction in which mob is looking
43 */
44 protected Mob(float x, float y, float width, float height, Direction mDirection, Type type) {
45 super(x, y, width, height);
46 mMove = new Vector2(0, 0);
47 mCanJump = false;
48 mDead = false;
49 this.mDirection = mDirection;
50 this.mType = type;
51 }
53 protected static Direction randomDir() {
54 return MathUtils.randomBoolean(.5f) ? Direction.LEFT : Direction.RIGHT;
55 }
57 /**
58 * @return The X coordinate of a mob in blocks
59 */
60 public final int getMapX() {
61 return (int) (x + (getWidth() / 2)) / 16;
62 }
64 /**
65 * @return The Y coordinate of mob's upper edge in blocks
66 */
67 public final int getUpperMapY() {
68 return (int) (y / 16);
69 }
71 /**
72 * @return The Y coordinate if mob's vertical center in blocks
73 */
74 public final int getMiddleMapY() {
75 return (int) (y + (getHeight() / 2)) / 16;
76 }
78 /**
79 * @return The Y coordinate of mob's legs in blocks
80 */
81 public final int getLowerMapY() {
82 return (int) (y + getHeight()) / 16;
83 }
85 public final float getWidth() {
86 return width;
87 }
89 public final float getHeight() {
90 return height;
91 }
93 /**
94 * @return Integer representing a direction in which mob is looking, where 0 is left and 1 is right
95 */
96 public final Direction getDirection() {
97 return mDirection;
98 }
100 public final boolean looksLeft() {
101 return mDirection == Direction.LEFT;
104 public final boolean looksRight() {
105 return mDirection == Direction.RIGHT;
108 /**
109 * Switches direction in which mob is looking
110 */
111 protected final void switchDir() {
112 mDirection = looksLeft() ? Direction.RIGHT : Direction.LEFT;
115 protected final int dirMultiplier() {
116 return looksLeft() ? 0 : 1;
119 public final boolean isDead() {
120 return mDead;
123 public final int getAnim() {
124 return mAnim;
127 /**
128 * Set's mob's dead variable to true and nothing else. It doesn't delete the
129 */
130 public final void kill() {
131 mDead = true;
134 public final void move() {
135 x += mMove.x;
136 y += mMove.y;
139 public final Vector2 getMove() {
140 return mMove;
143 public final boolean canJump() {
144 return mCanJump;
147 public final void setCanJump(boolean canJump) {
148 this.mCanJump = canJump;
151 public final boolean isFlyMode() {
152 return mFlyMode;
155 public final void setFlyMode(boolean flyMode) {
156 this.mFlyMode = flyMode;
159 public final Type getType() {
160 return mType;
163 public void checkWorldBounds(GameWorld gameWorld) {
164 if (x + width / 2 < 0) {
165 x += gameWorld.getWidthPx();
167 if (x + width / 2 > gameWorld.getWidthPx()) {
168 x -= gameWorld.getWidthPx();
172 public abstract void draw(SpriteBatch spriteBatch, float x, float y);
174 public abstract void ai(GameWorld gameWorld);
176 public abstract void changeDir();