DEADSOFTWARE

Refactor physics
[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 private Direction dir;
21 /**
22 * @param x in pixels
23 * @param y in pixels
24 * @param width in pixels
25 * @param height in pixels
26 * @param dir Direction in which mob is looking
27 */
28 protected Mob(float x, float y, float width, float height, Direction dir, Type type) {
29 super(x, y, width, height);
30 move = new Vector2(0, 0);
31 canJump = false;
32 dead = false;
33 this.dir = dir;
34 this.type = type;
35 }
37 private boolean dead;
39 private boolean canJump, flyMode;
40 protected int animDelta = 6;
41 protected int anim;
43 protected static Direction randomDir() {
44 return MathUtils.randomBoolean(.5f) ? Direction.LEFT : Direction.RIGHT;
45 }
47 /**
48 * @return The X coordinate of a mob in blocks
49 */
50 public final int getMapX() {
51 return (int) (x + (getWidth() / 2)) / 16;
52 }
54 /**
55 * @return The Y coordinate of mob's upper edge in blocks
56 */
57 public final int getUpperMapY() {
58 return (int) (y / 16);
59 }
61 /**
62 *
63 * @return The Y coordinate if mob's vertical center in blocks
64 */
65 public final int getMiddleMapY() {
66 return (int) (y + (getHeight() / 2)) / 16;
67 }
69 /**
70 *
71 * @return The Y coordinate of mob's legs in blocks
72 */
73 public final int getLowerMapY() {
74 return (int) (y + getHeight()) / 16;
75 }
77 public final float getWidth() {
78 return width;
79 }
81 public final float getHeight() {
82 return height;
83 }
85 /**
86 * @return Integer representing a direction in which mob is looking, where 0 is left and 1 is right
87 */
88 public final Direction getDirection() {
89 return dir;
90 }
92 public final boolean looksLeft() {
93 return dir == Direction.LEFT;
94 }
96 public final boolean looksRight() {
97 return dir == Direction.RIGHT;
98 }
100 /**
101 * Switches direction in which mob is looking
102 */
103 protected final void switchDir() {
104 dir = looksLeft() ? Direction.RIGHT : Direction.LEFT;
107 protected final int dirMultiplier() {
108 return looksLeft() ? 0 : 1;
111 public final boolean isDead() {
112 return dead;
115 public final int getAnim() {
116 return anim;
119 /**
120 * Set's mob's dead variable to true and nothing else. It doesn't delete the
121 */
122 public final void kill() {
123 dead = true;
126 public final void move() {
127 x += move.x;
128 y += move.y;
131 public final Vector2 getMove() {
132 return move;
135 public final boolean canJump() {
136 return canJump;
139 public final void setCanJump(boolean canJump) {
140 this.canJump = canJump;
143 public final boolean isFlyMode() {
144 return flyMode;
147 public final void setFlyMode(boolean flyMode) {
148 this.flyMode = flyMode;
151 public final Type getType() {
152 return type;
155 public void checkWorldBounds() {
156 if (x + width / 2 < 0) {
157 x += GP.world.getWidthPx();
159 if (x + width / 2 > GP.world.getWidthPx()) {
160 x -= GP.world.getWidthPx();
164 public enum Type {
165 MOB,
166 SAND,
167 GRAVEL
170 public enum Direction {
171 LEFT,
172 RIGHT
175 public abstract void draw(SpriteBatch spriteBatch, float x, float y);
177 public abstract void ai();
179 public abstract void changeDir();