DEADSOFTWARE

b7a975caf3b4217032573afdfc200760515f78f3
[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 {
25 LEFT,
26 RIGHT
27 }
29 protected Vector2 mVelocity;
30 protected Type mType;
31 protected int mAnimDelta = ANIMATION_SPEED;
32 protected float mAnim;
34 private Direction mDirection;
35 protected boolean mDead;
36 private boolean mCanJump;
37 private boolean mFlyMode;
39 /**
40 * @param x in pixels
41 * @param y in pixels
42 * @param width in pixels
43 * @param height in pixels
44 * @param mDirection Direction in which mob is looking
45 */
46 protected Mob(float x, float y, float width, float height, Direction mDirection, Type type) {
47 super(x, y, width, height);
48 mVelocity = new Vector2(0, 0);
49 mCanJump = false;
50 mDead = false;
51 this.mDirection = mDirection;
52 this.mType = type;
53 }
55 protected static Direction randomDir() {
56 return MathUtils.randomBoolean(.5f) ? Direction.LEFT : Direction.RIGHT;
57 }
59 private boolean isAnimationIncreasing() {
60 return mAnim > 0 && mAnimDelta > 0 || mAnim < 0 && mAnimDelta < 0;
61 }
63 protected final void updateAnimation(float delta) {
64 if (mVelocity.x != 0f || Math.abs(mAnim) > mAnimDelta * delta) {
65 mAnim += mAnimDelta * delta;
66 } else {
67 mAnim = 0;
68 }
70 if (mAnim > 60f) {
71 mAnim = 60f;
72 mAnimDelta = -ANIMATION_SPEED;
73 } else if (mAnim < -60f) {
74 mAnim = -60f;
75 mAnimDelta = ANIMATION_SPEED;
76 }
78 if (mVelocity.x == 0f && isAnimationIncreasing()) {
79 mAnimDelta = -mAnimDelta;
80 }
81 }
83 /**
84 * @return The X coordinate of a mob in blocks
85 */
86 public final int getMapX() {
87 return (int) (x + (getWidth() / 2)) / 16;
88 }
90 /**
91 * @return The Y coordinate of mob's upper edge in blocks
92 */
93 public final int getUpperMapY() {
94 return (int) (y / 16);
95 }
97 /**
98 * @return The Y coordinate if mob's vertical center in blocks
99 */
100 public final int getMiddleMapY() {
101 return (int) (y + (getHeight() / 2)) / 16;
104 /**
105 * @return The Y coordinate of mob's legs in blocks
106 */
107 public final int getLowerMapY() {
108 return (int) (y + getHeight()) / 16;
111 public final float getWidth() {
112 return width;
115 public final float getHeight() {
116 return height;
119 /**
120 * @return Integer representing a direction in which mob is looking, where 0 is left and 1 is right
121 */
122 public final Direction getDirection() {
123 return mDirection;
126 public final boolean looksLeft() {
127 return mDirection == Direction.LEFT;
130 public final boolean looksRight() {
131 return mDirection == Direction.RIGHT;
134 /**
135 * Switches direction in which mob is looking
136 */
137 protected final void switchDir() {
138 mDirection = looksLeft() ? Direction.RIGHT : Direction.LEFT;
141 protected final int dirMultiplier() {
142 return looksLeft() ? 0 : 1;
145 public final boolean isDead() {
146 return mDead;
149 public final float getAnim() {
150 return mAnim;
153 /**
154 * Set's mob's dead variable to true and nothing else. It doesn't delete the
155 */
156 public final void kill() {
157 mDead = true;
160 public final void move(float delta) {
161 x += mVelocity.x * delta;
162 y += mVelocity.y * delta;
165 public final Vector2 getVelocity() {
166 return mVelocity;
169 public final boolean canJump() {
170 return mCanJump;
173 public final void setCanJump(boolean canJump) {
174 this.mCanJump = canJump;
177 public final boolean isFlyMode() {
178 return mFlyMode;
181 public final void setFlyMode(boolean flyMode) {
182 this.mFlyMode = flyMode;
185 public final Type getType() {
186 return mType;
189 public void checkWorldBounds(GameWorld gameWorld) {
190 if (x + width / 2 < 0) {
191 x += gameWorld.getWidthPx();
193 if (x + width / 2 > gameWorld.getWidthPx()) {
194 x -= gameWorld.getWidthPx();
198 public abstract void draw(SpriteBatch spriteBatch, float x, float y, float delta);
200 public abstract void ai(GameWorld gameWorld, float delta);
202 public abstract void changeDir();