feb5ceb08a0165f58c00538678dbcc982e4a6812
1 package ru
.deadsoftware
.cavedroid
.game
.mobs
;
3 import com
.badlogic
.gdx
.Gdx
;
4 import com
.badlogic
.gdx
.graphics
.g2d
.SpriteBatch
;
5 import com
.badlogic
.gdx
.math
.MathUtils
;
6 import com
.badlogic
.gdx
.math
.Rectangle
;
7 import com
.badlogic
.gdx
.math
.Vector2
;
8 import ru
.deadsoftware
.cavedroid
.game
.world
.GameWorld
;
10 import java
.io
.Serializable
;
15 public abstract class Mob
extends Rectangle
implements Serializable
{
17 protected static int ANIMATION_SPEED
= 360;
25 public enum Direction
{
30 private final int index
;
31 private final int basis
;
34 * Index for this direction (left = 0, right = 1)
36 public final int getIndex() {
41 * Basis for this direction (left = -1, right = 1)
43 public final int getBasis() {
47 Direction(int index
, int basis
) {
53 protected Vector2 mVelocity
;
55 protected int mAnimDelta
= ANIMATION_SPEED
;
56 protected float mAnim
;
58 private Direction mDirection
;
59 protected boolean mDead
;
60 private boolean mCanJump
;
61 private boolean mFlyMode
;
63 private final int mMaxHealth
;
69 * @param width in pixels
70 * @param height in pixels
71 * @param mDirection Direction in which mob is looking
73 protected Mob(float x
, float y
, float width
, float height
, Direction mDirection
, Type type
, int maxHealth
) {
74 super(x
, y
, width
, height
);
75 mVelocity
= new Vector2(0, 0);
78 this.mDirection
= mDirection
;
80 this.mMaxHealth
= maxHealth
;
81 this.mHealth
= mMaxHealth
;
84 protected static Direction
randomDir() {
85 return MathUtils
.randomBoolean(.5f) ? Direction
.LEFT
: Direction
.RIGHT
;
88 private boolean isAnimationIncreasing() {
89 return mAnim
> 0 && mAnimDelta
> 0 || mAnim
< 0 && mAnimDelta
< 0;
92 private void checkHealth() {
93 mHealth
= MathUtils
.clamp(mHealth
, 0, mMaxHealth
);
100 protected final void updateAnimation(float delta
) {
101 if (mVelocity
.x
!= 0f || Math
.abs(mAnim
) > mAnimDelta
* delta
) {
102 mAnim
+= mAnimDelta
* delta
;
109 mAnimDelta
= -ANIMATION_SPEED
;
110 } else if (mAnim
< -60f) {
112 mAnimDelta
= ANIMATION_SPEED
;
115 if (mVelocity
.x
== 0f && isAnimationIncreasing()) {
116 mAnimDelta
= -mAnimDelta
;
121 * @return The X coordinate of a mob in blocks
123 public final int getMapX() {
124 return (int) (x
+ (getWidth() / 2)) / 16;
128 * @return The Y coordinate of mob's upper edge in blocks
130 public final int getUpperMapY() {
131 return (int) (y
/ 16);
135 * @return The Y coordinate if mob's vertical center in blocks
137 public final int getMiddleMapY() {
138 return (int) (y
+ (getHeight() / 2)) / 16;
142 * @return The Y coordinate of mob's legs in blocks
144 public final int getLowerMapY() {
145 return (int) (y
+ getHeight()) / 16;
148 public final float getWidth() {
152 public final float getHeight() {
157 * @return Integer representing a direction in which mob is looking, where 0 is left and 1 is right
159 public final Direction
getDirection() {
163 public final boolean looksLeft() {
164 return mDirection
== Direction
.LEFT
;
167 public final boolean looksRight() {
168 return mDirection
== Direction
.RIGHT
;
172 * Switches direction in which mob is looking
174 protected final void switchDir() {
175 mDirection
= looksLeft() ? Direction
.RIGHT
: Direction
.LEFT
;
178 public final boolean isDead() {
182 public final float getAnim() {
187 * Set's mob's dead variable to true and nothing else. It doesn't delete the
189 public final void kill() {
193 public final void move(float delta
) {
194 x
+= mVelocity
.x
* delta
;
195 y
+= mVelocity
.y
* delta
;
198 public final Vector2
getVelocity() {
202 protected final void setVelocity(Vector2 velocity
) {
203 mVelocity
= velocity
;
206 public final boolean canJump() {
210 public final void setCanJump(boolean canJump
) {
211 this.mCanJump
= canJump
;
214 public final boolean isFlyMode() {
218 public final void setFlyMode(boolean flyMode
) {
219 this.mFlyMode
= flyMode
;
222 public final Type
getType() {
226 public final void checkWorldBounds(GameWorld gameWorld
) {
227 if (x
+ width
/ 2 < 0) {
228 x
+= gameWorld
.getWidthPx();
230 if (x
+ width
/ 2 > gameWorld
.getWidthPx()) {
231 x
-= gameWorld
.getWidthPx();
235 public final int getHealth() {
239 public final void attachToController(MobsController controller
) {
240 controller
.addMob(this);
243 public void damage(int damage
) {
245 Gdx
.app
.error(this.getClass().getSimpleName(), "Damage cant be negative!");
249 if (mHealth
<= Integer
.MIN_VALUE
+ damage
) {
250 mHealth
= Integer
.MIN_VALUE
+ damage
;
257 public void heal(int heal
) {
259 Gdx
.app
.error(this.getClass().getSimpleName(), "Heal cant be negative!");
263 if (mHealth
>= Integer
.MAX_VALUE
- heal
) {
264 mHealth
= Integer
.MAX_VALUE
- heal
;
271 public abstract void draw(SpriteBatch spriteBatch
, float x
, float y
, float delta
);
273 public abstract void ai(GameWorld gameWorld
, float delta
);
275 public abstract void changeDir();
277 public abstract float getSpeed();
279 public abstract void jump();