DEADSOFTWARE

Fix codestyle
[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;
8 import java.io.Serializable;
10 import static ru.deadsoftware.cavedroid.GameScreen.GP;
12 /**
13 * Mob class.
14 */
15 public abstract class Mob extends Rectangle implements Serializable {
17 protected Vector2 move;
18 protected Type type;
19 protected int animDelta = 6;
20 protected int anim;
21 private Direction dir;
22 private boolean dead;
23 private boolean canJump, flyMode;
24 /**
25 * @param x in pixels
26 * @param y in pixels
27 * @param width in pixels
28 * @param height in pixels
29 * @param dir Direction in which mob is looking
30 */
31 protected Mob(float x, float y, float width, float height, Direction dir, Type type) {
32 super(x, y, width, height);
33 move = new Vector2(0, 0);
34 canJump = false;
35 dead = false;
36 this.dir = dir;
37 this.type = type;
38 }
40 protected static Direction randomDir() {
41 return MathUtils.randomBoolean(.5f) ? Direction.LEFT : Direction.RIGHT;
42 }
44 /**
45 * @return The X coordinate of a mob in blocks
46 */
47 public final int getMapX() {
48 return (int) (x + (getWidth() / 2)) / 16;
49 }
51 /**
52 * @return The Y coordinate of mob's upper edge in blocks
53 */
54 public final int getUpperMapY() {
55 return (int) (y / 16);
56 }
58 /**
59 * @return The Y coordinate if mob's vertical center in blocks
60 */
61 public final int getMiddleMapY() {
62 return (int) (y + (getHeight() / 2)) / 16;
63 }
65 /**
66 * @return The Y coordinate of mob's legs in blocks
67 */
68 public final int getLowerMapY() {
69 return (int) (y + getHeight()) / 16;
70 }
72 public final float getWidth() {
73 return width;
74 }
76 public final float getHeight() {
77 return height;
78 }
80 /**
81 * @return Integer representing a direction in which mob is looking, where 0 is left and 1 is right
82 */
83 public final Direction getDirection() {
84 return dir;
85 }
87 public final boolean looksLeft() {
88 return dir == Direction.LEFT;
89 }
91 public final boolean looksRight() {
92 return dir == Direction.RIGHT;
93 }
95 /**
96 * Switches direction in which mob is looking
97 */
98 protected final void switchDir() {
99 dir = looksLeft() ? Direction.RIGHT : Direction.LEFT;
102 protected final int dirMultiplier() {
103 return looksLeft() ? 0 : 1;
106 public final boolean isDead() {
107 return dead;
110 public final int getAnim() {
111 return anim;
114 /**
115 * Set's mob's dead variable to true and nothing else. It doesn't delete the
116 */
117 public final void kill() {
118 dead = true;
121 public final void move() {
122 x += move.x;
123 y += move.y;
126 public final Vector2 getMove() {
127 return move;
130 public final boolean canJump() {
131 return canJump;
134 public final void setCanJump(boolean canJump) {
135 this.canJump = canJump;
138 public final boolean isFlyMode() {
139 return flyMode;
142 public final void setFlyMode(boolean flyMode) {
143 this.flyMode = flyMode;
146 public final Type getType() {
147 return type;
150 public void checkWorldBounds() {
151 if (x + width / 2 < 0) {
152 x += GP.world.getWidthPx();
154 if (x + width / 2 > GP.world.getWidthPx()) {
155 x -= GP.world.getWidthPx();
159 public abstract void draw(SpriteBatch spriteBatch, float x, float y);
161 public abstract void ai();
163 public abstract void changeDir();
165 public enum Type {
166 MOB,
167 SAND,
168 GRAVEL
171 public enum Direction {
172 LEFT,
173 RIGHT