DEADSOFTWARE

Make separate textures for mob limbs #8
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / mobs / Mob.java
1 package ru.deadsoftware.cavedroid.game.mobs;
2
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
8 import java.io.Serializable;
9
10 import static ru.deadsoftware.cavedroid.GameScreen.GP;
11
12 /**
13 * Mob class.
14 */
15 public abstract class Mob extends Rectangle implements Serializable {
16
17 protected Vector2 move;
18 protected Type type;
19 protected int animDelta = 6;
20 protected int anim;
21 private Direction dir;
22 private boolean dead;
23 private boolean canJump, flyMode;
24
25 /**
26 * @param x in pixels
27 * @param y in pixels
28 * @param width in pixels
29 * @param height in pixels
30 * @param dir Direction in which mob is looking
31 */
32 protected Mob(float x, float y, float width, float height, Direction dir, Type type) {
33 super(x, y, width, height);
34 move = new Vector2(0, 0);
35 canJump = false;
36 dead = false;
37 this.dir = dir;
38 this.type = type;
39 }
40
41 protected static Direction randomDir() {
42 return MathUtils.randomBoolean(.5f) ? Direction.LEFT : Direction.RIGHT;
43 }
44
45 /**
46 * @return The X coordinate of a mob in blocks
47 */
48 public final int getMapX() {
49 return (int) (x + (getWidth() / 2)) / 16;
50 }
51
52 /**
53 * @return The Y coordinate of mob's upper edge in blocks
54 */
55 public final int getUpperMapY() {
56 return (int) (y / 16);
57 }
58
59 /**
60 * @return The Y coordinate if mob's vertical center in blocks
61 */
62 public final int getMiddleMapY() {
63 return (int) (y + (getHeight() / 2)) / 16;
64 }
65
66 /**
67 * @return The Y coordinate of mob's legs in blocks
68 */
69 public final int getLowerMapY() {
70 return (int) (y + getHeight()) / 16;
71 }
72
73 public final float getWidth() {
74 return width;
75 }
76
77 public final float getHeight() {
78 return height;
79 }
80
81 /**
82 * @return Integer representing a direction in which mob is looking, where 0 is left and 1 is right
83 */
84 public final Direction getDirection() {
85 return dir;
86 }
87
88 public final boolean looksLeft() {
89 return dir == Direction.LEFT;
90 }
91
92 public final boolean looksRight() {
93 return dir == Direction.RIGHT;
94 }
95
96 /**
97 * Switches direction in which mob is looking
98 */
99 protected final void switchDir() {
100 dir = looksLeft() ? Direction.RIGHT : Direction.LEFT;
101 }
102
103 protected final int dirMultiplier() {
104 return looksLeft() ? 0 : 1;
105 }
106
107 public final boolean isDead() {
108 return dead;
109 }
110
111 public final int getAnim() {
112 return anim;
113 }
114
115 /**
116 * Set's mob's dead variable to true and nothing else. It doesn't delete the
117 */
118 public final void kill() {
119 dead = true;
120 }
121
122 public final void move() {
123 x += move.x;
124 y += move.y;
125 }
126
127 public final Vector2 getMove() {
128 return move;
129 }
130
131 public final boolean canJump() {
132 return canJump;
133 }
134
135 public final void setCanJump(boolean canJump) {
136 this.canJump = canJump;
137 }
138
139 public final boolean isFlyMode() {
140 return flyMode;
141 }
142
143 public final void setFlyMode(boolean flyMode) {
144 this.flyMode = flyMode;
145 }
146
147 public final Type getType() {
148 return type;
149 }
150
151 public void checkWorldBounds() {
152 if (x + width / 2 < 0) {
153 x += GP.world.getWidthPx();
154 }
155 if (x + width / 2 > GP.world.getWidthPx()) {
156 x -= GP.world.getWidthPx();
157 }
158 }
159
160 public abstract void draw(SpriteBatch spriteBatch, float x, float y);
161
162 public abstract void ai();
163
164 public abstract void changeDir();
165
166 public enum Type {
167 MOB,
168 SAND,
169 GRAVEL
170 }
171
172 public enum Direction {
173 LEFT,
174 RIGHT
175 }
176 }