1
package com.brackeen.javagamebook.tilegame.sprites;
3
import java.lang.reflect.Constructor;
4
import com.brackeen.javagamebook.graphics.*;
5
import com.brackeen.javagamebook.state.MainGameState;
6
import com.brackeen.javagamebook.tilegame.ResourceManager;
7
import com.brackeen.javagamebook.tilegame.TileMapRenderer;
8
import com.brackeen.javagamebook.util.MessagesSimple;
11
A Creature is a Sprite that is affected by gravity and can
12
die. It has four Animations: moving left, moving right,
13
dying on the left, and dying on the right.
15
public abstract class Creature extends Sprite {
17
public static int bossCounter = 12;
19
Amount of time to go from STATE_DYING to STATE_DEAD.
21
private static final int DIE_TIME = 1000;
23
public static final int STATE_NORMAL = 0;
24
public static final int STATE_DYING = 1;
25
public static final int STATE_DEAD = 2;
26
public static final int STATE_STANDING = 3;
27
public static final int STATE_JUMPING = 4;
28
public static final int STATE_SHOOTING = 5;
30
private boolean isFlying = false;
31
private boolean onBoard = false;
32
private float onBoardSpeed;
34
private boolean bExtendedModel = false;
35
private Animation standingLeft;
36
private Animation standingRight;
37
private Animation jumpingLeft;
38
private Animation jumpingRight;
39
private Animation shootingLeft;
40
private Animation shootingRight;
41
private Animation left;
42
private Animation right;
43
private Animation deadLeft;
44
private Animation deadRight;
46
private long stateTime;
48
public Creature(Animation standingLeft,Animation standingRight,Animation jumpingLeft,Animation jumpingRight,Animation shootingLeft,Animation shootingRight , Animation left, Animation right,
49
Animation deadLeft, Animation deadRight)
52
this.standingLeft = standingLeft;
53
this.standingRight = standingRight;
54
this.jumpingLeft = jumpingRight;
55
this.jumpingRight = jumpingRight;
56
this.shootingLeft = shootingLeft;
57
this.shootingRight = shootingRight;
60
this.deadLeft = deadLeft;
61
this.deadRight = deadRight;
62
state = STATE_STANDING;
63
bExtendedModel = true;
66
public Creature(Animation right) {
70
this.deadLeft = right;
71
this.deadRight = right;
73
// TODO Auto-generated constructor stub
76
Creates a new Creature with the specified Animations.
78
public Creature(Animation left, Animation right,
79
Animation deadLeft, Animation deadRight)
84
this.deadLeft = deadLeft;
85
this.deadRight = deadRight;
90
// public Creature(Animation standing, Animation left[], Animation right[],
91
// Animation deadLeft, Animation deadRight)
95
// this.right = right;
96
// this.deadLeft = deadLeft;
97
// this.deadRight = deadRight;
98
// state = STATE_NORMAL;
102
public Object clone() {
103
if(!bExtendedModel) {
104
// use reflection to create the correct subclass
105
Constructor constructor = getClass().getConstructors()[0];
107
return constructor.newInstance(new Object[] {
108
(Animation)left.clone(),
109
(Animation)right.clone(),
110
(Animation)deadLeft.clone(),
111
(Animation)deadRight.clone()
114
catch (Exception ex) {
115
// should never happen
116
ex.printStackTrace();
121
// use reflection to create the correct subclass
122
Constructor constructor = getClass().getConstructors()[0];
124
return constructor.newInstance(new Object[] {
125
(Animation)standingLeft.clone(),
126
(Animation)standingRight.clone(),
127
(Animation)jumpingLeft.clone(),
128
(Animation)jumpingRight.clone(),
129
(Animation)shootingLeft.clone(),
130
(Animation)shootingRight.clone(),
131
(Animation)left.clone(),
132
(Animation)right.clone(),
133
(Animation)deadLeft.clone(),
134
(Animation)deadRight.clone()
137
catch (Exception ex) {
138
// should never happen
139
ex.printStackTrace();
147
Gets the maximum speed of this Creature.
149
public float getMaxSpeed() {
155
Wakes up the creature when the Creature first appears
156
on screen. Normally, the creature starts moving left.
158
public void wakeUp() {
159
if (getState() == STATE_NORMAL && getVelocityX() == 0) {
160
setVelocityX(-getMaxSpeed());
166
Gets the state of this Creature. The state is either
167
STATE_NORMAL, STATE_DYING, or STATE_DEAD.
169
public int getState() {
175
Sets the state of this Creature to STATE_NORMAL,
176
STATE_DYING, or STATE_DEAD.
178
public void setState(int state) {
179
if (this.state != state) {
182
if (state == STATE_DYING) {
185
if(this instanceof Boss) {
188
MainGameState.bNextMap = true;
191
else if(this instanceof Player) {
195
if (state == STATE_SHOOTING) {
204
Checks if this creature is alive.
206
public boolean isAlive() {
207
return (state == STATE_NORMAL|| state == STATE_JUMPING||state == STATE_SHOOTING||state == STATE_STANDING);
211
Checks if this creature is flying.
213
public boolean isFlying() {
216
public void setIsFlying(boolean bool) {
219
public boolean isOnBoard() {
223
public void setOnBoard(boolean bool) {
227
public void setOnBoardSpeed(float value) {
228
onBoardSpeed = value;
231
public float getOnBoardSpeed() {
236
Called before update() if the creature collided with a
239
public void collideHorizontal() {
240
setVelocityX(-getVelocityX());
245
Called before update() if the creature collided with a
248
public void collideVertical() {
254
Updates the animaton for this creature.
256
public void update(long elapsedTime) {
257
// select the correct Animation
258
Animation newAnim = anim;
259
if (getVelocityX() < 0) {
262
else if (getVelocityX() > 0) {
265
if (state == STATE_DYING && newAnim == left) {
268
else if (state == STATE_DYING && newAnim == right) {
275
// update the Animation
276
if (anim != newAnim) {
281
anim.update(elapsedTime);
284
// update to "dead" state
285
stateTime += elapsedTime;
286
if (state == STATE_DYING && stateTime >= DIE_TIME) {
287
setState(STATE_DEAD);