DEADSOFTWARE

bcab5867bc4e8ee9186543eebd206e4733437b86
[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 float width, height;
18 private int dir;
20 public boolean flyMode;
21 public Vector2 pos;
22 public Vector2 mov;
24 private boolean dead;
26 protected int animDelta = 6;
27 public boolean canJump;
28 int anim;
30 /**
31 *
32 * @param x in pixels
33 * @param y in pixels
34 * @param width in pixels
35 * @param height in pixels
36 * @param dir integer representing a direction where 0 is left and 1 is right.
37 * You should use {@link #LEFT} and {@link #RIGHT} constants
38 */
39 public Mob(float x, float y, float width, float height, int dir) {
40 pos = new Vector2(x, y);
41 mov = new Vector2(0, 0);
42 this.width = width;
43 this.height = height;
44 canJump = false;
45 flyMode = false;
46 dead = false;
47 this.dir = dir;
48 }
50 /**
51 *
52 * @return The X coordinate of a mob in blocks
53 */
54 public int getMapX() {
55 return (int) (pos.x + (getWidth() / 2)) / 16;
56 }
58 /**
59 *
60 * @return The Y coordinate of mob's upper edge in blocks
61 */
62 public int getUpperMapY() {
63 return (int) (pos.y / 16);
64 }
66 /**
67 *
68 * @return The Y coordinate if mob's vertical center in blocks
69 */
70 public int getMiddleMapY() {
71 return (int) (pos.y + (getHeight() / 2)) / 16;
72 }
74 /**
75 *
76 * @return The Y coordinate of mob's legs in blocks
77 */
78 public int getLowerMapY() {
79 return (int) (pos.y + getHeight()) / 16;
80 }
82 public float getWidth() {
83 return width;
84 }
86 public float getHeight() {
87 return height;
88 }
90 /**
91 *
92 * @return Integer representing a direction in which mob is looking, where 0 is left and 1 is right
93 */
94 public int getDirection() {
95 return dir;
96 }
98 public boolean looksLeft() {
99 return getDirection() == LEFT;
102 public boolean looksRight() {
103 return getDirection() == RIGHT;
106 /**
107 * Switches direction in which mob is looking
108 */
109 protected void switchDir() {
110 dir = looksLeft() ? RIGHT : LEFT;
113 public boolean isDead() {
114 return dead;
117 /**
118 * Set's mob's dead variable to true and nothing else. It doesn't delete the mob.
119 */
120 public void kill() {
121 dead = true;
124 /**
126 * @return A {@link Rectangle} with mob's coordinates and size
127 */
128 public Rectangle getRect() {
129 return new Rectangle(pos.x, pos.y, getWidth(), getHeight());
132 public abstract void ai();
134 public abstract void changeDir();
136 public abstract void draw(SpriteBatch spriteBatch, float x, float y);
138 /**
140 * @return 0 - if regular mob. <br>
141 * 10 - if instance of {@link FallingSand} <br> 11 - if instance of {@link FallingGravel}
142 */
143 public abstract int getType(); //0 - mob, 10 - sand, 11 - gravel