DEADSOFTWARE

4ad04e8538affb3b49e3bbf2eb0f80a796ce4510
[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 private 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 /**
60 * @return The X coordinate of a mob in blocks
61 */
62 public final int getMapX() {
63 return (int) (x + (getWidth() / 2)) / 16;
64 }
66 /**
67 * @return The Y coordinate of mob's upper edge in blocks
68 */
69 public final int getUpperMapY() {
70 return (int) (y / 16);
71 }
73 /**
74 * @return The Y coordinate if mob's vertical center in blocks
75 */
76 public final int getMiddleMapY() {
77 return (int) (y + (getHeight() / 2)) / 16;
78 }
80 /**
81 * @return The Y coordinate of mob's legs in blocks
82 */
83 public final int getLowerMapY() {
84 return (int) (y + getHeight()) / 16;
85 }
87 public final float getWidth() {
88 return width;
89 }
91 public final float getHeight() {
92 return height;
93 }
95 /**
96 * @return Integer representing a direction in which mob is looking, where 0 is left and 1 is right
97 */
98 public final Direction getDirection() {
99 return mDirection;
102 public final boolean looksLeft() {
103 return mDirection == Direction.LEFT;
106 public final boolean looksRight() {
107 return mDirection == Direction.RIGHT;
110 /**
111 * Switches direction in which mob is looking
112 */
113 protected final void switchDir() {
114 mDirection = looksLeft() ? Direction.RIGHT : Direction.LEFT;
117 protected final int dirMultiplier() {
118 return looksLeft() ? 0 : 1;
121 public final boolean isDead() {
122 return mDead;
125 public final float getAnim() {
126 return mAnim;
129 /**
130 * Set's mob's dead variable to true and nothing else. It doesn't delete the
131 */
132 public final void kill() {
133 mDead = true;
136 public final void move(float delta) {
137 x += mVelocity.x * delta;
138 y += mVelocity.y * delta;
141 public final Vector2 getVelocity() {
142 return mVelocity;
145 public final boolean canJump() {
146 return mCanJump;
149 public final void setCanJump(boolean canJump) {
150 this.mCanJump = canJump;
153 public final boolean isFlyMode() {
154 return mFlyMode;
157 public final void setFlyMode(boolean flyMode) {
158 this.mFlyMode = flyMode;
161 public final Type getType() {
162 return mType;
165 public void checkWorldBounds(GameWorld gameWorld) {
166 if (x + width / 2 < 0) {
167 x += gameWorld.getWidthPx();
169 if (x + width / 2 > gameWorld.getWidthPx()) {
170 x -= gameWorld.getWidthPx();
174 public abstract void draw(SpriteBatch spriteBatch, float x, float y, float delta);
176 public abstract void ai(GameWorld gameWorld, float delta);
178 public abstract void changeDir();