f19d247c4d9df9a972eb4d423cedbd3ea8c35ac1
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 private final int index
;
30 private final int basis
;
33 * Index for this direction (left = 0, right = 1)
35 public final int getIndex() {
40 * Basis for this direction (left = -1, right = 1)
42 public final int getBasis() {
46 Direction(int index
, int basis
) {
52 protected Vector2 mVelocity
;
54 protected int mAnimDelta
= ANIMATION_SPEED
;
55 protected float mAnim
;
57 private Direction mDirection
;
58 protected boolean mDead
;
59 private boolean mCanJump
;
60 private boolean mFlyMode
;
65 * @param width in pixels
66 * @param height in pixels
67 * @param mDirection Direction in which mob is looking
69 protected Mob(float x
, float y
, float width
, float height
, Direction mDirection
, Type type
) {
70 super(x
, y
, width
, height
);
71 mVelocity
= new Vector2(0, 0);
74 this.mDirection
= mDirection
;
78 protected static Direction
randomDir() {
79 return MathUtils
.randomBoolean(.5f) ? Direction
.LEFT
: Direction
.RIGHT
;
82 private boolean isAnimationIncreasing() {
83 return mAnim
> 0 && mAnimDelta
> 0 || mAnim
< 0 && mAnimDelta
< 0;
86 protected final void updateAnimation(float delta
) {
87 if (mVelocity
.x
!= 0f || Math
.abs(mAnim
) > mAnimDelta
* delta
) {
88 mAnim
+= mAnimDelta
* delta
;
95 mAnimDelta
= -ANIMATION_SPEED
;
96 } else if (mAnim
< -60f) {
98 mAnimDelta
= ANIMATION_SPEED
;
101 if (mVelocity
.x
== 0f && isAnimationIncreasing()) {
102 mAnimDelta
= -mAnimDelta
;
107 * @return The X coordinate of a mob in blocks
109 public final int getMapX() {
110 return (int) (x
+ (getWidth() / 2)) / 16;
114 * @return The Y coordinate of mob's upper edge in blocks
116 public final int getUpperMapY() {
117 return (int) (y
/ 16);
121 * @return The Y coordinate if mob's vertical center in blocks
123 public final int getMiddleMapY() {
124 return (int) (y
+ (getHeight() / 2)) / 16;
128 * @return The Y coordinate of mob's legs in blocks
130 public final int getLowerMapY() {
131 return (int) (y
+ getHeight()) / 16;
134 public final float getWidth() {
138 public final float getHeight() {
143 * @return Integer representing a direction in which mob is looking, where 0 is left and 1 is right
145 public final Direction
getDirection() {
149 public final boolean looksLeft() {
150 return mDirection
== Direction
.LEFT
;
153 public final boolean looksRight() {
154 return mDirection
== Direction
.RIGHT
;
158 * Switches direction in which mob is looking
160 protected final void switchDir() {
161 mDirection
= looksLeft() ? Direction
.RIGHT
: Direction
.LEFT
;
164 public final boolean isDead() {
168 public final float getAnim() {
173 * Set's mob's dead variable to true and nothing else. It doesn't delete the
175 public final void kill() {
179 public final void move(float delta
) {
180 x
+= mVelocity
.x
* delta
;
181 y
+= mVelocity
.y
* delta
;
184 public final Vector2
getVelocity() {
188 protected final void setVelocity(Vector2 velocity
) {
189 mVelocity
= velocity
;
192 public final boolean canJump() {
196 public final void setCanJump(boolean canJump
) {
197 this.mCanJump
= canJump
;
200 public final boolean isFlyMode() {
204 public final void setFlyMode(boolean flyMode
) {
205 this.mFlyMode
= flyMode
;
208 public final Type
getType() {
212 public final void checkWorldBounds(GameWorld gameWorld
) {
213 if (x
+ width
/ 2 < 0) {
214 x
+= gameWorld
.getWidthPx();
216 if (x
+ width
/ 2 > gameWorld
.getWidthPx()) {
217 x
-= gameWorld
.getWidthPx();
221 public abstract void draw(SpriteBatch spriteBatch
, float x
, float y
, float delta
);
223 public abstract void ai(GameWorld gameWorld
, float delta
);
225 public abstract void changeDir();
227 public abstract float getSpeed();
229 public abstract void jump();