package ru.deadsoftware.cavedroid.game.mobs; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Vector2; import java.io.Serializable; /** * Mob class. */ public abstract class Mob implements Serializable { public static final int LEFT = 0; public static final int RIGHT = 1; private final float width; private final float height; private int dir; protected final Vector2 pos; protected Vector2 mov; private boolean dead; private boolean canJump, flyMode; protected int animDelta = 6; protected int anim; protected static int randomDir() { return MathUtils.random(1); } /** * * @param x in pixels * @param y in pixels * @param width in pixels * @param height in pixels * @param dir integer representing a direction where 0 is left and 1 is right. * You should use {@link #LEFT} and {@link #RIGHT} constants */ protected Mob(float x, float y, float width, float height, int dir) { pos = new Vector2(x, y); mov = new Vector2(0, 0); this.width = width; this.height = height; canJump = false; dead = false; this.dir = dir; } /** * * @return The X coordinate of a mob in blocks */ public int getMapX() { return (int) (pos.x + (getWidth() / 2)) / 16; } /** * * @return The Y coordinate of mob's upper edge in blocks */ public int getUpperMapY() { return (int) (pos.y / 16); } /** * * @return The Y coordinate if mob's vertical center in blocks */ public int getMiddleMapY() { return (int) (pos.y + (getHeight() / 2)) / 16; } /** * * @return The Y coordinate of mob's legs in blocks */ public int getLowerMapY() { return (int) (pos.y + getHeight()) / 16; } public float getWidth() { return width; } public float getHeight() { return height; } /** * * @return Integer representing a direction in which mob is looking, where 0 is left and 1 is right */ public int getDirection() { return dir; } public boolean looksLeft() { return getDirection() == LEFT; } public boolean looksRight() { return getDirection() == RIGHT; } /** * Switches direction in which mob is looking */ protected void switchDir() { dir = looksLeft() ? RIGHT : LEFT; } public boolean isDead() { return dead; } public int getAnim() { return anim; } /** * Set's mob's dead variable to true and nothing else. It doesn't delete the mob. */ public void kill() { dead = true; } /** * * @return A {@link Rectangle} with mob's coordinates and size */ public Rectangle getRect() { return new Rectangle(pos.x, pos.y, getWidth(), getHeight()); } public Vector2 getPos() { return pos; } public Vector2 getMov() { return mov; } public float getX() { return pos.x; } public float getY() { return pos.y; } public boolean canJump() { return canJump; } public void setCanJump(boolean canJump) { this.canJump = canJump; } public boolean isFlyMode() { return flyMode; } public void setFlyMode(boolean flyMode) { this.flyMode = flyMode; } public abstract void draw(SpriteBatch spriteBatch, float x, float y); public abstract void ai(); public abstract void changeDir(); /** * * @return 0 - if regular mob.
* 10 - if instance of {@link FallingSand}
11 - if instance of {@link FallingGravel} */ public abstract int getType(); //0 - mob, 10 - sand, 11 - gravel }