1 package ru
.deadsoftware
.cavedroid
.game
.mobs
;
3 import com
.badlogic
.gdx
.Gdx
;
4 import com
.badlogic
.gdx
.graphics
.Color
;
5 import com
.badlogic
.gdx
.graphics
.g2d
.SpriteBatch
;
6 import com
.badlogic
.gdx
.math
.MathUtils
;
7 import com
.badlogic
.gdx
.math
.Rectangle
;
8 import com
.badlogic
.gdx
.math
.Vector2
;
9 import com
.badlogic
.gdx
.utils
.Timer
;
10 import ru
.deadsoftware
.cavedroid
.game
.GameItemsHolder
;
11 import ru
.deadsoftware
.cavedroid
.game
.world
.GameWorld
;
13 import javax
.annotation
.CheckForNull
;
14 import java
.io
.Serializable
;
19 public abstract class Mob
extends Rectangle
implements Serializable
{
21 private static final float DAMAGE_TINT_TIMEOUT_S
= 0.5f;
22 private static final Color DAMAGE_TINT_COLOR
= new Color(0xff8080ff);
24 private static final float HIT_RANGE
= 8f;
26 protected static int ANIMATION_SPEED
= 360;
33 public enum Direction
{
38 private final int index
;
39 private final int basis
;
42 * Index for this direction (left = 0, right = 1)
44 public final int getIndex() {
49 * Basis for this direction (left = -1, right = 1)
51 public final int getBasis() {
55 Direction(int index
, int basis
) {
61 private class ResetTakeDamageTask
extends Timer
.Task
{
65 mTakingDamage
= false;
69 protected Vector2 mVelocity
;
71 protected int mAnimDelta
= ANIMATION_SPEED
;
72 protected float mAnim
;
74 private Direction mDirection
;
75 protected boolean mDead
;
76 private boolean mCanJump
;
77 private boolean mFlyMode
;
79 private final int mMaxHealth
;
82 private transient boolean mTakingDamage
= false;
83 @CheckForNull private transient ResetTakeDamageTask mResetTakeDamageTask
= null;
88 * @param width in pixels
89 * @param height in pixels
90 * @param mDirection Direction in which mob is looking
92 protected Mob(float x
, float y
, float width
, float height
, Direction mDirection
, Type type
, int maxHealth
) {
93 super(x
, y
, width
, height
);
94 mVelocity
= new Vector2(0, 0);
97 this.mDirection
= mDirection
;
99 this.mMaxHealth
= maxHealth
;
100 this.mHealth
= mMaxHealth
;
103 protected static Direction
randomDir() {
104 return MathUtils
.randomBoolean(.5f) ? Direction
.LEFT
: Direction
.RIGHT
;
107 private boolean isAnimationIncreasing() {
108 return mAnim
> 0 && mAnimDelta
> 0 || mAnim
< 0 && mAnimDelta
< 0;
111 private void checkHealth() {
112 mHealth
= MathUtils
.clamp(mHealth
, 0, mMaxHealth
);
119 protected final void updateAnimation(float delta
) {
120 final float velocityMultiplier
= (Math
.abs(getVelocity().x
) / getSpeed());
121 final float animMultiplier
= (velocityMultiplier
== 0f ?
1f : velocityMultiplier
) * delta
;
122 final float maxAnim
= 60f * (velocityMultiplier
== 0f ?
1f : velocityMultiplier
);
124 if (mVelocity
.x
!= 0f || Math
.abs(mAnim
) > mAnimDelta
* animMultiplier
) {
125 mAnim
+= mAnimDelta
* animMultiplier
;
130 if (mAnim
> maxAnim
) {
132 mAnimDelta
= -ANIMATION_SPEED
;
133 } else if (mAnim
< -maxAnim
) {
135 mAnimDelta
= ANIMATION_SPEED
;
138 if (mVelocity
.x
== 0f && isAnimationIncreasing()) {
139 mAnimDelta
= -mAnimDelta
;
144 * @return The X coordinate of a mob in blocks
146 public final int getMapX() {
147 return (int) (x
+ (getWidth() / 2)) / 16;
151 * @return The Y coordinate of mob's upper edge in blocks
153 public final int getUpperMapY() {
154 return (int) (y
/ 16);
158 * @return The Y coordinate if mob's vertical center in blocks
160 public final int getMiddleMapY() {
161 return (int) (y
+ (getHeight() / 2)) / 16;
165 * @return The Y coordinate of mob's legs in blocks
167 public final int getLowerMapY() {
168 return (int) (y
+ getHeight()) / 16;
171 public final float getWidth() {
175 public final float getHeight() {
180 * @return Integer representing a direction in which mob is looking, where 0 is left and 1 is right
182 public final Direction
getDirection() {
186 public final boolean looksLeft() {
187 return mDirection
== Direction
.LEFT
;
190 public final boolean looksRight() {
191 return mDirection
== Direction
.RIGHT
;
195 * Switches direction in which mob is looking
197 protected final void switchDir() {
198 mDirection
= looksLeft() ? Direction
.RIGHT
: Direction
.LEFT
;
201 public final boolean isDead() {
205 public final float getAnim() {
210 * Set's mob's dead variable to true and nothing else. It doesn't delete the
212 public final void kill() {
216 public final void move(float delta
) {
217 x
+= mVelocity
.x
* delta
;
218 y
+= mVelocity
.y
* delta
;
221 public final Vector2
getVelocity() {
225 protected final void setVelocity(Vector2 velocity
) {
226 mVelocity
= velocity
;
229 public final boolean canJump() {
233 public final void setCanJump(boolean canJump
) {
234 this.mCanJump
= canJump
;
237 public final boolean isFlyMode() {
241 public final void setFlyMode(boolean flyMode
) {
242 this.mFlyMode
= flyMode
;
245 public final Type
getType() {
249 public final void checkWorldBounds(GameWorld gameWorld
) {
250 if (x
+ width
/ 2 < 0) {
251 x
+= gameWorld
.getWidthPx();
253 if (x
+ width
/ 2 > gameWorld
.getWidthPx()) {
254 x
-= gameWorld
.getWidthPx();
258 public final int getHealth() {
262 public final void attachToController(MobsController controller
) {
263 controller
.addMob(this);
266 public void damage(int damage
) {
272 Gdx
.app
.error(this.getClass().getSimpleName(), "Damage cant be negative!");
276 if (mHealth
<= Integer
.MIN_VALUE
+ damage
) {
277 mHealth
= Integer
.MIN_VALUE
+ damage
;
283 setTakingDamage(true);
286 public void heal(int heal
) {
288 Gdx
.app
.error(this.getClass().getSimpleName(), "Heal cant be negative!");
292 if (mHealth
>= Integer
.MAX_VALUE
- heal
) {
293 mHealth
= Integer
.MAX_VALUE
- heal
;
300 public Rectangle
getHitBox() {
301 return new Rectangle(x
- HIT_RANGE
, y
- HIT_RANGE
, width
+ HIT_RANGE
, height
+ HIT_RANGE
);
304 public boolean isTakingDamage() {
305 return mTakingDamage
;
308 public void setTakingDamage(boolean takingDamage
) {
309 mTakingDamage
= takingDamage
;
312 if (mResetTakeDamageTask
!= null && mResetTakeDamageTask
.isScheduled()) {
313 mResetTakeDamageTask
.cancel();
314 } else if (mResetTakeDamageTask
== null) {
315 mResetTakeDamageTask
= new ResetTakeDamageTask();
318 Timer
.schedule(mResetTakeDamageTask
, DAMAGE_TINT_TIMEOUT_S
);
322 protected Color
getTintColor() {
323 return isTakingDamage() ? DAMAGE_TINT_COLOR
: Color
.WHITE
;
326 public abstract void draw(SpriteBatch spriteBatch
, float x
, float y
, float delta
);
328 public abstract void ai(GameWorld gameWorld
, GameItemsHolder gameItemsHolder
, MobsController mobsController
, float delta
);
330 public abstract void changeDir();
332 public abstract float getSpeed();
334 public abstract void jump();