DEADSOFTWARE

32c63cc26514d674c777f6f5a777104c68508fe4
[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.Rectangle;
5 import com.badlogic.gdx.math.Vector2;
7 import java.io.Serializable;
9 /**
10 * Mob class.
11 */
12 public abstract class Mob implements Serializable {
14 public static final int LEFT = 0;
15 public static final int RIGHT = 1;
17 private final float width;
18 private final float height;
19 private int dir;
21 public boolean flyMode;
22 public final Vector2 pos;
23 public Vector2 mov;
25 private boolean dead;
27 protected int animDelta = 6;
28 public boolean canJump;
29 int anim;
31 /**
32 *
33 * @param x in pixels
34 * @param y in pixels
35 * @param width in pixels
36 * @param height in pixels
37 * @param dir integer representing a direction where 0 is left and 1 is right.
38 * You should use {@link #LEFT} and {@link #RIGHT} constants
39 */
40 protected Mob(float x, float y, float width, float height, int dir) {
41 pos = new Vector2(x, y);
42 mov = new Vector2(0, 0);
43 this.width = width;
44 this.height = height;
45 canJump = false;
46 flyMode = false;
47 dead = false;
48 this.dir = dir;
49 }
51 /**
52 *
53 * @return The X coordinate of a mob in blocks
54 */
55 public int getMapX() {
56 return (int) (pos.x + (getWidth() / 2)) / 16;
57 }
59 /**
60 *
61 * @return The Y coordinate of mob's upper edge in blocks
62 */
63 public int getUpperMapY() {
64 return (int) (pos.y / 16);
65 }
67 /**
68 *
69 * @return The Y coordinate if mob's vertical center in blocks
70 */
71 public int getMiddleMapY() {
72 return (int) (pos.y + (getHeight() / 2)) / 16;
73 }
75 /**
76 *
77 * @return The Y coordinate of mob's legs in blocks
78 */
79 public int getLowerMapY() {
80 return (int) (pos.y + getHeight()) / 16;
81 }
83 public float getWidth() {
84 return width;
85 }
87 public float getHeight() {
88 return height;
89 }
91 /**
92 *
93 * @return Integer representing a direction in which mob is looking, where 0 is left and 1 is right
94 */
95 public int getDirection() {
96 return dir;
97 }
99 public boolean looksLeft() {
100 return getDirection() == LEFT;
103 public boolean looksRight() {
104 return getDirection() == RIGHT;
107 /**
108 * Switches direction in which mob is looking
109 */
110 protected void switchDir() {
111 dir = looksLeft() ? RIGHT : LEFT;
114 public boolean isDead() {
115 return dead;
118 /**
119 * Set's mob's dead variable to true and nothing else. It doesn't delete the mob.
120 */
121 public void kill() {
122 dead = true;
125 /**
127 * @return A {@link Rectangle} with mob's coordinates and size
128 */
129 public Rectangle getRect() {
130 return new Rectangle(pos.x, pos.y, getWidth(), getHeight());
133 public abstract void ai();
135 public abstract void changeDir();
137 public abstract void draw(SpriteBatch spriteBatch, float x, float y);
139 /**
141 * @return 0 - if regular mob. <br>
142 * 10 - if instance of {@link FallingSand} <br> 11 - if instance of {@link FallingGravel}
143 */
144 public abstract int getType(); //0 - mob, 10 - sand, 11 - gravel