DEADSOFTWARE

Reimplement mobs
[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 /**
11 * Mob class.
12 */
13 public abstract class Mob implements Serializable {
15 public static final int LEFT = 0;
16 public static final int RIGHT = 1;
18 private final float width;
19 private final float height;
20 private int dir;
22 protected final Vector2 pos;
23 protected Vector2 mov;
25 private boolean dead;
27 private boolean canJump, flyMode;
28 protected int animDelta = 6;
29 protected int anim;
31 protected static int randomDir() {
32 return MathUtils.random(1);
33 }
35 /**
36 *
37 * @param x in pixels
38 * @param y in pixels
39 * @param width in pixels
40 * @param height in pixels
41 * @param dir integer representing a direction where 0 is left and 1 is right.
42 * You should use {@link #LEFT} and {@link #RIGHT} constants
43 */
44 protected Mob(float x, float y, float width, float height, int dir) {
45 pos = new Vector2(x, y);
46 mov = new Vector2(0, 0);
47 this.width = width;
48 this.height = height;
49 canJump = false;
50 dead = false;
51 this.dir = dir;
52 }
54 /**
55 *
56 * @return The X coordinate of a mob in blocks
57 */
58 public int getMapX() {
59 return (int) (pos.x + (getWidth() / 2)) / 16;
60 }
62 /**
63 *
64 * @return The Y coordinate of mob's upper edge in blocks
65 */
66 public int getUpperMapY() {
67 return (int) (pos.y / 16);
68 }
70 /**
71 *
72 * @return The Y coordinate if mob's vertical center in blocks
73 */
74 public int getMiddleMapY() {
75 return (int) (pos.y + (getHeight() / 2)) / 16;
76 }
78 /**
79 *
80 * @return The Y coordinate of mob's legs in blocks
81 */
82 public int getLowerMapY() {
83 return (int) (pos.y + getHeight()) / 16;
84 }
86 public float getWidth() {
87 return width;
88 }
90 public float getHeight() {
91 return height;
92 }
94 /**
95 *
96 * @return Integer representing a direction in which mob is looking, where 0 is left and 1 is right
97 */
98 public int getDirection() {
99 return dir;
102 public boolean looksLeft() {
103 return getDirection() == LEFT;
106 public boolean looksRight() {
107 return getDirection() == RIGHT;
110 /**
111 * Switches direction in which mob is looking
112 */
113 protected void switchDir() {
114 dir = looksLeft() ? RIGHT : LEFT;
117 public boolean isDead() {
118 return dead;
121 public int getAnim() {
122 return anim;
125 /**
126 * Set's mob's dead variable to true and nothing else. It doesn't delete the mob.
127 */
128 public void kill() {
129 dead = true;
132 /**
134 * @return A {@link Rectangle} with mob's coordinates and size
135 */
136 public Rectangle getRect() {
137 return new Rectangle(pos.x, pos.y, getWidth(), getHeight());
140 public Vector2 getPos() {
141 return pos;
144 public Vector2 getMov() {
145 return mov;
148 public float getX() {
149 return pos.x;
152 public float getY() {
153 return pos.y;
156 public boolean canJump() {
157 return canJump;
160 public void setCanJump(boolean canJump) {
161 this.canJump = canJump;
164 public boolean isFlyMode() {
165 return flyMode;
168 public void setFlyMode(boolean flyMode) {
169 this.flyMode = flyMode;
172 public abstract void draw(SpriteBatch spriteBatch, float x, float y);
174 public abstract void ai();
176 public abstract void changeDir();
178 /**
180 * @return 0 - if regular mob. <br>
181 * 10 - if instance of {@link FallingSand} <br> 11 - if instance of {@link FallingGravel}
182 */
183 public abstract int getType(); //0 - mob, 10 - sand, 11 - gravel