X-Git-Url: http://deadsoftware.ru/gitweb?a=blobdiff_plain;f=core%2Fsrc%2Fru%2Fdeadsoftware%2Fcavedroid%2Fgame%2Fmobs%2FMob.java;h=5f73a6b4720af3ad3f185a81d6ec47fb65ac06de;hb=59d48c1b28c570755327a8fb0827fa57e7fd3914;hp=3b7edae21ed1748870f7b0d663407a11f33e4cd6;hpb=0a855ca3c1d0c84de41a928cc99fd8544a933015;p=cavedroid.git diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java index 3b7edae..5f73a6b 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java @@ -1,81 +1,175 @@ 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; -public abstract class Mob implements Serializable { +import static ru.deadsoftware.cavedroid.GameScreen.GP; - public boolean flyMode; - private float width, height; - private int dir; - - public Vector2 pos; - public Vector2 mov; +/** + * Mob class. + */ +public abstract class Mob extends Rectangle implements Serializable { + protected Vector2 move; + protected Type type; + protected int animDelta = 6; + protected int anim; + private Direction dir; private boolean dead; - - public boolean canJump; - protected int anim, animDelta = 6; - - protected Mob(float x, float y, float width, float height, int dir, boolean player) { - pos = new Vector2(x, y); - mov = new Vector2(0, 0); - this.width = width; - this.height = height; + private boolean canJump, flyMode; + /** + * @param x in pixels + * @param y in pixels + * @param width in pixels + * @param height in pixels + * @param dir Direction in which mob is looking + */ + protected Mob(float x, float y, float width, float height, Direction dir, Type type) { + super(x, y, width, height); + move = new Vector2(0, 0); canJump = false; - flyMode = false; dead = false; this.dir = dir; + this.type = type; + } + + protected static Direction randomDir() { + return MathUtils.randomBoolean(.5f) ? Direction.LEFT : Direction.RIGHT; } - protected Mob(float x, float y, float width, float height, int dir) { - this(x, y, width, height, dir, false); + /** + * @return The X coordinate of a mob in blocks + */ + public final int getMapX() { + return (int) (x + (getWidth() / 2)) / 16; } - public int getMapX() { - return (int) (pos.x + (getWidth() / 2)) / 16; + /** + * @return The Y coordinate of mob's upper edge in blocks + */ + public final int getUpperMapY() { + return (int) (y / 16); } - public int getMapY() { - return (int) (pos.y + (getHeight() / 2)) / 16; + /** + * @return The Y coordinate if mob's vertical center in blocks + */ + public final int getMiddleMapY() { + return (int) (y + (getHeight() / 2)) / 16; } - public float getWidth() { + /** + * @return The Y coordinate of mob's legs in blocks + */ + public final int getLowerMapY() { + return (int) (y + getHeight()) / 16; + } + + public final float getWidth() { return width; } - public float getHeight() { + public final float getHeight() { return height; } - public int getDir() { + /** + * @return Integer representing a direction in which mob is looking, where 0 is left and 1 is right + */ + public final Direction getDirection() { return dir; } - protected void switchDir() { - dir = -dir + 1; + public final boolean looksLeft() { + return dir == Direction.LEFT; + } + + public final boolean looksRight() { + return dir == Direction.RIGHT; } - public boolean isDead() { + /** + * Switches direction in which mob is looking + */ + protected final void switchDir() { + dir = looksLeft() ? Direction.RIGHT : Direction.LEFT; + } + + protected final int dirMultiplier() { + return looksLeft() ? 0 : 1; + } + + public final boolean isDead() { return dead; } - public void kill() { + public final int getAnim() { + return anim; + } + + /** + * Set's mob's dead variable to true and nothing else. It doesn't delete the + */ + public final void kill() { dead = true; } - public Rectangle getRect() { - return new Rectangle(pos.x, pos.y, getWidth(), getHeight()); + public final void move() { + x += move.x; + y += move.y; + } + + public final Vector2 getMove() { + return move; } + public final boolean canJump() { + return canJump; + } + + public final void setCanJump(boolean canJump) { + this.canJump = canJump; + } + + public final boolean isFlyMode() { + return flyMode; + } + + public final void setFlyMode(boolean flyMode) { + this.flyMode = flyMode; + } + + public final Type getType() { + return type; + } + + public void checkWorldBounds() { + if (x + width / 2 < 0) { + x += GP.world.getWidthPx(); + } + if (x + width / 2 > GP.world.getWidthPx()) { + x -= GP.world.getWidthPx(); + } + } + + public abstract void draw(SpriteBatch spriteBatch, float x, float y); + public abstract void ai(); public abstract void changeDir(); - public abstract void draw(SpriteBatch spriteBatch, float x, float y); + public enum Type { + MOB, + SAND, + GRAVEL + } - public abstract int getType(); //0 - mob, 10 - sand, 11 - gravel + public enum Direction { + LEFT, + RIGHT + } }