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
.model
.item
.InventoryItem
;
12 import ru
.deadsoftware
.cavedroid
.game
.model
.item
.Item
;
13 import ru
.deadsoftware
.cavedroid
.game
.world
.GameWorld
;
15 import javax
.annotation
.CheckForNull
;
16 import java
.io
.Serializable
;
17 import java
.util
.Collections
;
18 import java
.util
.List
;
23 public abstract class Mob
extends Rectangle
implements Serializable
{
25 private static final float DAMAGE_TINT_TIMEOUT_S
= 0.5f;
26 private static final Color DAMAGE_TINT_COLOR
= new Color(0xff8080ff);
28 private static final float HIT_RANGE
= 8f;
30 protected static int ANIMATION_SPEED
= 360;
37 public enum Direction
{
42 private final int index
;
43 private final int basis
;
46 * Index for this direction (left = 0, right = 1)
48 public final int getIndex() {
53 * Basis for this direction (left = -1, right = 1)
55 public final int getBasis() {
59 Direction(int index
, int basis
) {
65 private class ResetTakeDamageTask
extends Timer
.Task
{
69 mTakingDamage
= false;
73 protected Vector2 mVelocity
;
75 protected int mAnimDelta
= ANIMATION_SPEED
;
76 protected float mAnim
;
78 private Direction mDirection
;
79 protected boolean mDead
;
80 private boolean mCanJump
;
81 private boolean mFlyMode
;
83 private final int mMaxHealth
;
86 private transient boolean mTakingDamage
= false;
87 @CheckForNull private transient ResetTakeDamageTask mResetTakeDamageTask
= null;
92 * @param width in pixels
93 * @param height in pixels
94 * @param mDirection Direction in which mob is looking
96 protected Mob(float x
, float y
, float width
, float height
, Direction mDirection
, Type type
, int maxHealth
) {
97 super(x
, y
, width
, height
);
98 mVelocity
= new Vector2(0, 0);
101 this.mDirection
= mDirection
;
103 this.mMaxHealth
= maxHealth
;
104 this.mHealth
= mMaxHealth
;
107 protected static Direction
randomDir() {
108 return MathUtils
.randomBoolean(.5f) ? Direction
.LEFT
: Direction
.RIGHT
;
111 private boolean isAnimationIncreasing() {
112 return mAnim
> 0 && mAnimDelta
> 0 || mAnim
< 0 && mAnimDelta
< 0;
115 private void checkHealth() {
116 mHealth
= MathUtils
.clamp(mHealth
, 0, mMaxHealth
);
123 protected final void updateAnimation(float delta
) {
124 final float velocityMultiplier
= (Math
.abs(getVelocity().x
) / getSpeed());
125 final float animMultiplier
= (velocityMultiplier
== 0f ?
1f : velocityMultiplier
) * delta
;
126 final float maxAnim
= 60f * (velocityMultiplier
== 0f ?
1f : velocityMultiplier
);
128 if (mVelocity
.x
!= 0f || Math
.abs(mAnim
) > mAnimDelta
* animMultiplier
) {
129 mAnim
+= mAnimDelta
* animMultiplier
;
134 if (mAnim
> maxAnim
) {
136 mAnimDelta
= -ANIMATION_SPEED
;
137 } else if (mAnim
< -maxAnim
) {
139 mAnimDelta
= ANIMATION_SPEED
;
142 if (mVelocity
.x
== 0f && isAnimationIncreasing()) {
143 mAnimDelta
= -mAnimDelta
;
148 * @return The X coordinate of a mob in blocks
150 public final int getMapX() {
151 return (int) (x
+ (getWidth() / 2)) / 16;
155 * @return The Y coordinate of mob's upper edge in blocks
157 public final int getUpperMapY() {
158 return (int) (y
/ 16);
162 * @return The Y coordinate if mob's vertical center in blocks
164 public final int getMiddleMapY() {
165 return (int) (y
+ (getHeight() / 2)) / 16;
169 * @return The Y coordinate of mob's legs in blocks
171 public final int getLowerMapY() {
172 return (int) (y
+ getHeight()) / 16;
175 public final float getWidth() {
179 public final float getHeight() {
184 * @return Integer representing a direction in which mob is looking, where 0 is left and 1 is right
186 public final Direction
getDirection() {
190 public final boolean looksLeft() {
191 return mDirection
== Direction
.LEFT
;
194 public final boolean looksRight() {
195 return mDirection
== Direction
.RIGHT
;
199 * Switches direction in which mob is looking
201 protected final void switchDir() {
202 mDirection
= looksLeft() ? Direction
.RIGHT
: Direction
.LEFT
;
205 public final boolean isDead() {
209 public final float getAnim() {
214 * Set's mob's dead variable to true and nothing else. It doesn't delete the
220 public final void move(float delta
) {
221 x
+= mVelocity
.x
* delta
;
222 y
+= mVelocity
.y
* delta
;
225 public final Vector2
getVelocity() {
229 protected final void setVelocity(Vector2 velocity
) {
230 mVelocity
= velocity
;
233 public final boolean canJump() {
237 public final void setCanJump(boolean canJump
) {
238 this.mCanJump
= canJump
;
241 public final boolean isFlyMode() {
245 public final void setFlyMode(boolean flyMode
) {
246 this.mFlyMode
= flyMode
;
249 public final Type
getType() {
253 public final void checkWorldBounds(GameWorld gameWorld
) {
254 if (x
+ width
/ 2 < 0) {
255 x
+= gameWorld
.getWidthPx();
257 if (x
+ width
/ 2 > gameWorld
.getWidthPx()) {
258 x
-= gameWorld
.getWidthPx();
262 public final int getHealth() {
266 public final int getMaxHealth() {
270 public final void attachToController(MobsController controller
) {
271 controller
.addMob(this);
274 public void damage(int damage
) {
280 Gdx
.app
.error(this.getClass().getSimpleName(), "Damage cant be negative!");
284 if (mHealth
<= Integer
.MIN_VALUE
+ damage
) {
285 mHealth
= Integer
.MIN_VALUE
+ damage
;
291 setTakingDamage(true);
294 public void heal(int heal
) {
296 Gdx
.app
.error(this.getClass().getSimpleName(), "Heal cant be negative!");
300 if (mHealth
>= Integer
.MAX_VALUE
- heal
) {
301 mHealth
= Integer
.MAX_VALUE
- heal
;
308 public Rectangle
getHitBox() {
309 return new Rectangle(x
- HIT_RANGE
, y
- HIT_RANGE
, width
+ HIT_RANGE
, height
+ HIT_RANGE
);
312 public boolean isTakingDamage() {
313 return mTakingDamage
;
316 public void setTakingDamage(boolean takingDamage
) {
317 mTakingDamage
= takingDamage
;
320 if (mResetTakeDamageTask
!= null && mResetTakeDamageTask
.isScheduled()) {
321 mResetTakeDamageTask
.cancel();
322 } else if (mResetTakeDamageTask
== null) {
323 mResetTakeDamageTask
= new ResetTakeDamageTask();
326 Timer
.schedule(mResetTakeDamageTask
, DAMAGE_TINT_TIMEOUT_S
);
330 protected Color
getTintColor() {
331 return isTakingDamage() ? DAMAGE_TINT_COLOR
: Color
.WHITE
;
334 public List
<InventoryItem
> getDrop(GameItemsHolder gameItemsHolder
) {
335 return Collections
.emptyList();
338 public abstract void draw(SpriteBatch spriteBatch
, float x
, float y
, float delta
);
340 public abstract void ai(GameWorld gameWorld
, GameItemsHolder gameItemsHolder
, MobsController mobsController
, float delta
);
342 public abstract void changeDir();
344 public abstract float getSpeed();
346 public abstract void jump();