DEADSOFTWARE

Some mobs refactor
[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.world.GameWorld;
9 import java.io.Serializable;
11 /**
12 * Mob class.
13 */
14 public abstract class Mob extends Rectangle implements Serializable {
16 protected static int ANIMATION_SPEED = 360;
18 public enum Type {
19 MOB,
20 SAND,
21 GRAVEL
22 }
24 public enum Direction {
26 LEFT(0, -1),
27 RIGHT(1, 1);
29 private final int index;
30 private final int basis;
32 /**
33 * Index for this direction (left = 0, right = 1)
34 */
35 public final int getIndex() {
36 return index;
37 }
39 /**
40 * Basis for this direction (left = -1, right = 1)
41 */
42 public final int getBasis() {
43 return basis;
44 }
46 Direction(int index, int basis) {
47 this.index = index;
48 this.basis = basis;
49 }
50 }
52 protected Vector2 mVelocity;
53 protected Type mType;
54 protected int mAnimDelta = ANIMATION_SPEED;
55 protected float mAnim;
57 private Direction mDirection;
58 protected boolean mDead;
59 private boolean mCanJump;
60 private boolean mFlyMode;
62 /**
63 * @param x in pixels
64 * @param y in pixels
65 * @param width in pixels
66 * @param height in pixels
67 * @param mDirection Direction in which mob is looking
68 */
69 protected Mob(float x, float y, float width, float height, Direction mDirection, Type type) {
70 super(x, y, width, height);
71 mVelocity = new Vector2(0, 0);
72 mCanJump = false;
73 mDead = false;
74 this.mDirection = mDirection;
75 this.mType = type;
76 }
78 protected static Direction randomDir() {
79 return MathUtils.randomBoolean(.5f) ? Direction.LEFT : Direction.RIGHT;
80 }
82 private boolean isAnimationIncreasing() {
83 return mAnim > 0 && mAnimDelta > 0 || mAnim < 0 && mAnimDelta < 0;
84 }
86 protected final void updateAnimation(float delta) {
87 if (mVelocity.x != 0f || Math.abs(mAnim) > mAnimDelta * delta) {
88 mAnim += mAnimDelta * delta;
89 } else {
90 mAnim = 0;
91 }
93 if (mAnim > 60f) {
94 mAnim = 60f;
95 mAnimDelta = -ANIMATION_SPEED;
96 } else if (mAnim < -60f) {
97 mAnim = -60f;
98 mAnimDelta = ANIMATION_SPEED;
99 }
101 if (mVelocity.x == 0f && isAnimationIncreasing()) {
102 mAnimDelta = -mAnimDelta;
106 /**
107 * @return The X coordinate of a mob in blocks
108 */
109 public final int getMapX() {
110 return (int) (x + (getWidth() / 2)) / 16;
113 /**
114 * @return The Y coordinate of mob's upper edge in blocks
115 */
116 public final int getUpperMapY() {
117 return (int) (y / 16);
120 /**
121 * @return The Y coordinate if mob's vertical center in blocks
122 */
123 public final int getMiddleMapY() {
124 return (int) (y + (getHeight() / 2)) / 16;
127 /**
128 * @return The Y coordinate of mob's legs in blocks
129 */
130 public final int getLowerMapY() {
131 return (int) (y + getHeight()) / 16;
134 public final float getWidth() {
135 return width;
138 public final float getHeight() {
139 return height;
142 /**
143 * @return Integer representing a direction in which mob is looking, where 0 is left and 1 is right
144 */
145 public final Direction getDirection() {
146 return mDirection;
149 public final boolean looksLeft() {
150 return mDirection == Direction.LEFT;
153 public final boolean looksRight() {
154 return mDirection == Direction.RIGHT;
157 /**
158 * Switches direction in which mob is looking
159 */
160 protected final void switchDir() {
161 mDirection = looksLeft() ? Direction.RIGHT : Direction.LEFT;
164 public final boolean isDead() {
165 return mDead;
168 public final float getAnim() {
169 return mAnim;
172 /**
173 * Set's mob's dead variable to true and nothing else. It doesn't delete the
174 */
175 public final void kill() {
176 mDead = true;
179 public final void move(float delta) {
180 x += mVelocity.x * delta;
181 y += mVelocity.y * delta;
184 public final Vector2 getVelocity() {
185 return mVelocity;
188 protected final void setVelocity(Vector2 velocity) {
189 mVelocity = velocity;
192 public final boolean canJump() {
193 return mCanJump;
196 public final void setCanJump(boolean canJump) {
197 this.mCanJump = canJump;
200 public final boolean isFlyMode() {
201 return mFlyMode;
204 public final void setFlyMode(boolean flyMode) {
205 this.mFlyMode = flyMode;
208 public final Type getType() {
209 return mType;
212 public final void checkWorldBounds(GameWorld gameWorld) {
213 if (x + width / 2 < 0) {
214 x += gameWorld.getWidthPx();
216 if (x + width / 2 > gameWorld.getWidthPx()) {
217 x -= gameWorld.getWidthPx();
221 public abstract void draw(SpriteBatch spriteBatch, float x, float y, float delta);
223 public abstract void ai(GameWorld gameWorld, float delta);
225 public abstract void changeDir();
227 public abstract float getSpeed();
229 public abstract void jump();