diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java
index 723a9298acc5d066dd5ada8b3dc01a99ad1a5bcf..b7a975caf3b4217032573afdfc200760515f78f3 100644 (file)
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
+import ru.deadsoftware.cavedroid.game.world.GameWorld;
import java.io.Serializable;
-import static ru.deadsoftware.cavedroid.GameScreen.GP;
-
/**
* Mob class.
*/
public abstract class Mob extends Rectangle implements Serializable {
- protected Vector2 move;
- protected Type type;
- private Direction dir;
+ protected static int ANIMATION_SPEED = 360;
+
+ public enum Type {
+ MOB,
+ SAND,
+ GRAVEL
+ }
+
+ public enum Direction {
+ LEFT,
+ RIGHT
+ }
+
+ protected Vector2 mVelocity;
+ protected Type mType;
+ protected int mAnimDelta = ANIMATION_SPEED;
+ protected float mAnim;
+
+ private Direction mDirection;
+ protected boolean mDead;
+ private boolean mCanJump;
+ private boolean mFlyMode;
/**
- * @param x in pixels
- * @param y in pixels
- * @param width in pixels
- * @param height in pixels
- * @param dir Direction in which mob is looking
+ * @param x in pixels
+ * @param y in pixels
+ * @param width in pixels
+ * @param height in pixels
+ * @param mDirection Direction in which mob is looking
*/
- protected Mob(float x, float y, float width, float height, Direction dir, Type type) {
+ protected Mob(float x, float y, float width, float height, Direction mDirection, Type type) {
super(x, y, width, height);
- move = new Vector2(0, 0);
- canJump = false;
- dead = false;
- this.dir = dir;
- this.type = type;
+ mVelocity = new Vector2(0, 0);
+ mCanJump = false;
+ mDead = false;
+ this.mDirection = mDirection;
+ this.mType = type;
}
- private boolean dead;
-
- private boolean canJump, flyMode;
- protected int animDelta = 6;
- protected int anim;
-
protected static Direction randomDir() {
return MathUtils.randomBoolean(.5f) ? Direction.LEFT : Direction.RIGHT;
}
+ private boolean isAnimationIncreasing() {
+ return mAnim > 0 && mAnimDelta > 0 || mAnim < 0 && mAnimDelta < 0;
+ }
+
+ protected final void updateAnimation(float delta) {
+ if (mVelocity.x != 0f || Math.abs(mAnim) > mAnimDelta * delta) {
+ mAnim += mAnimDelta * delta;
+ } else {
+ mAnim = 0;
+ }
+
+ if (mAnim > 60f) {
+ mAnim = 60f;
+ mAnimDelta = -ANIMATION_SPEED;
+ } else if (mAnim < -60f) {
+ mAnim = -60f;
+ mAnimDelta = ANIMATION_SPEED;
+ }
+
+ if (mVelocity.x == 0f && isAnimationIncreasing()) {
+ mAnimDelta = -mAnimDelta;
+ }
+ }
+
/**
* @return The X coordinate of a mob in blocks
*/
}
/**
- *
* @return The Y coordinate if mob's vertical center in blocks
*/
public final int getMiddleMapY() {
}
/**
- *
* @return The Y coordinate of mob's legs in blocks
*/
public final int getLowerMapY() {
* @return Integer representing a direction in which mob is looking, where 0 is left and 1 is right
*/
public final Direction getDirection() {
- return dir;
+ return mDirection;
}
public final boolean looksLeft() {
- return dir == Direction.LEFT;
+ return mDirection == Direction.LEFT;
}
public final boolean looksRight() {
- return dir == Direction.RIGHT;
+ return mDirection == Direction.RIGHT;
}
/**
* Switches direction in which mob is looking
*/
protected final void switchDir() {
- dir = looksLeft() ? Direction.RIGHT : Direction.LEFT;
+ mDirection = looksLeft() ? Direction.RIGHT : Direction.LEFT;
}
protected final int dirMultiplier() {
}
public final boolean isDead() {
- return dead;
+ return mDead;
}
- public final int getAnim() {
- return anim;
+ public final float getAnim() {
+ return mAnim;
}
/**
* Set's mob's dead variable to true and nothing else. It doesn't delete the
*/
public final void kill() {
- dead = true;
+ mDead = true;
}
- public final void move() {
- x += move.x;
- y += move.y;
+ public final void move(float delta) {
+ x += mVelocity.x * delta;
+ y += mVelocity.y * delta;
}
- public final Vector2 getMove() {
- return move;
+ public final Vector2 getVelocity() {
+ return mVelocity;
}
public final boolean canJump() {
- return canJump;
+ return mCanJump;
}
public final void setCanJump(boolean canJump) {
- this.canJump = canJump;
+ this.mCanJump = canJump;
}
public final boolean isFlyMode() {
- return flyMode;
+ return mFlyMode;
}
public final void setFlyMode(boolean flyMode) {
- this.flyMode = flyMode;
+ this.mFlyMode = flyMode;
}
public final Type getType() {
- return type;
+ return mType;
}
- public void checkWorldBounds() {
+ public void checkWorldBounds(GameWorld gameWorld) {
if (x + width / 2 < 0) {
- x += GP.world.getWidthPx();
+ x += gameWorld.getWidthPx();
}
- if (x + width / 2 > GP.world.getWidthPx()) {
- x -= GP.world.getWidthPx();
+ if (x + width / 2 > gameWorld.getWidthPx()) {
+ x -= gameWorld.getWidthPx();
}
}
- public enum Type {
- MOB,
- SAND,
- GRAVEL
- }
-
- public enum Direction {
- LEFT,
- RIGHT
- }
-
- public abstract void draw(SpriteBatch spriteBatch, float x, float y);
+ public abstract void draw(SpriteBatch spriteBatch, float x, float y, float delta);
- public abstract void ai();
+ public abstract void ai(GameWorld gameWorld, float delta);
public abstract void changeDir();
}