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
;
7 import ru
.deadsoftware
.cavedroid
.game
.world
.GameWorld
;
9 import java
.io
.Serializable
;
14 public abstract class Mob
extends Rectangle
implements Serializable
{
16 protected static int ANIMATION_SPEED
= 360;
24 public enum Direction
{
29 protected Vector2 mVelocity
;
31 protected int mAnimDelta
= ANIMATION_SPEED
;
32 protected float mAnim
;
34 private Direction mDirection
;
35 private boolean mDead
;
36 private boolean mCanJump
;
37 private boolean mFlyMode
;
42 * @param width in pixels
43 * @param height in pixels
44 * @param mDirection Direction in which mob is looking
46 protected Mob(float x
, float y
, float width
, float height
, Direction mDirection
, Type type
) {
47 super(x
, y
, width
, height
);
48 mVelocity
= new Vector2(0, 0);
51 this.mDirection
= mDirection
;
55 protected static Direction
randomDir() {
56 return MathUtils
.randomBoolean(.5f) ? Direction
.LEFT
: Direction
.RIGHT
;
59 private boolean isAnimationIncreasing() {
60 return mAnim
> 0 && mAnimDelta
> 0 || mAnim
< 0 && mAnimDelta
< 0;
63 protected final void updateAnimation(float delta
) {
64 if (mVelocity
.x
!= 0f || Math
.abs(mAnim
) > mAnimDelta
* delta
) {
65 mAnim
+= mAnimDelta
* delta
;
72 mAnimDelta
= -ANIMATION_SPEED
;
73 } else if (mAnim
< -60f) {
75 mAnimDelta
= ANIMATION_SPEED
;
78 if (mVelocity
.x
== 0f && isAnimationIncreasing()) {
79 mAnimDelta
= -mAnimDelta
;
84 * @return The X coordinate of a mob in blocks
86 public final int getMapX() {
87 return (int) (x
+ (getWidth() / 2)) / 16;
91 * @return The Y coordinate of mob's upper edge in blocks
93 public final int getUpperMapY() {
94 return (int) (y
/ 16);
98 * @return The Y coordinate if mob's vertical center in blocks
100 public final int getMiddleMapY() {
101 return (int) (y
+ (getHeight() / 2)) / 16;
105 * @return The Y coordinate of mob's legs in blocks
107 public final int getLowerMapY() {
108 return (int) (y
+ getHeight()) / 16;
111 public final float getWidth() {
115 public final float getHeight() {
120 * @return Integer representing a direction in which mob is looking, where 0 is left and 1 is right
122 public final Direction
getDirection() {
126 public final boolean looksLeft() {
127 return mDirection
== Direction
.LEFT
;
130 public final boolean looksRight() {
131 return mDirection
== Direction
.RIGHT
;
135 * Switches direction in which mob is looking
137 protected final void switchDir() {
138 mDirection
= looksLeft() ? Direction
.RIGHT
: Direction
.LEFT
;
141 protected final int dirMultiplier() {
142 return looksLeft() ?
0 : 1;
145 public final boolean isDead() {
149 public final float getAnim() {
154 * Set's mob's dead variable to true and nothing else. It doesn't delete the
156 public final void kill() {
160 public final void move(float delta
) {
161 x
+= mVelocity
.x
* delta
;
162 y
+= mVelocity
.y
* delta
;
165 public final Vector2
getVelocity() {
169 public final boolean canJump() {
173 public final void setCanJump(boolean canJump
) {
174 this.mCanJump
= canJump
;
177 public final boolean isFlyMode() {
181 public final void setFlyMode(boolean flyMode
) {
182 this.mFlyMode
= flyMode
;
185 public final Type
getType() {
189 public void checkWorldBounds(GameWorld gameWorld
) {
190 if (x
+ width
/ 2 < 0) {
191 x
+= gameWorld
.getWidthPx();
193 if (x
+ width
/ 2 > gameWorld
.getWidthPx()) {
194 x
-= gameWorld
.getWidthPx();
198 public abstract void draw(SpriteBatch spriteBatch
, float x
, float y
, float delta
);
200 public abstract void ai(GameWorld gameWorld
, float delta
);
202 public abstract void changeDir();