DEADSOFTWARE

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