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
;
15 public abstract class Mob
extends Rectangle
implements Serializable
{
17 protected Vector2 move
;
19 protected int animDelta
= 6;
21 private Direction dir
;
23 private boolean canJump
, flyMode
;
28 * @param width in pixels
29 * @param height in pixels
30 * @param dir Direction in which mob is looking
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);
41 protected static Direction
randomDir() {
42 return MathUtils
.randomBoolean(.5f) ? Direction
.LEFT
: Direction
.RIGHT
;
46 * @return The X coordinate of a mob in blocks
48 public final int getMapX() {
49 return (int) (x
+ (getWidth() / 2)) / 16;
53 * @return The Y coordinate of mob's upper edge in blocks
55 public final int getUpperMapY() {
56 return (int) (y
/ 16);
60 * @return The Y coordinate if mob's vertical center in blocks
62 public final int getMiddleMapY() {
63 return (int) (y
+ (getHeight() / 2)) / 16;
67 * @return The Y coordinate of mob's legs in blocks
69 public final int getLowerMapY() {
70 return (int) (y
+ getHeight()) / 16;
73 public final float getWidth() {
77 public final float getHeight() {
82 * @return Integer representing a direction in which mob is looking, where 0 is left and 1 is right
84 public final Direction
getDirection() {
88 public final boolean looksLeft() {
89 return dir
== Direction
.LEFT
;
92 public final boolean looksRight() {
93 return dir
== Direction
.RIGHT
;
97 * Switches direction in which mob is looking
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() {
111 public final int getAnim() {
116 * Set's mob's dead variable to true and nothing else. It doesn't delete the
118 public final void kill() {
122 public final void move() {
127 public final Vector2
getMove() {
131 public final boolean canJump() {
135 public final void setCanJump(boolean canJump
) {
136 this.canJump
= canJump
;
139 public final boolean isFlyMode() {
143 public final void setFlyMode(boolean flyMode
) {
144 this.flyMode
= flyMode
;
147 public final Type
getType() {
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();
172 public enum Direction
{