~brzeti/patat/dev

« back to all changes in this revision

Viewing changes to src/com/brackeen/javagamebook/tilegame/sprites/Creature.java

  • Committer: Arthur Skonecki
  • Date: 2008-12-02 12:59:20 UTC
  • Revision ID: admin@adb.cba.pl-20081202125920-hmvdzcrxgq1tl514
initial project's files submission

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package com.brackeen.javagamebook.tilegame.sprites;
 
2
 
 
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;
 
9
 
 
10
/**
 
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.
 
14
*/
 
15
public abstract class Creature extends Sprite {
 
16
 
 
17
        public static int bossCounter = 12;
 
18
    /**
 
19
        Amount of time to go from STATE_DYING to STATE_DEAD.
 
20
    */
 
21
    private static final int DIE_TIME = 1000;
 
22
 
 
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;
 
29
 
 
30
    private boolean isFlying = false;
 
31
    private boolean onBoard = false;
 
32
    private float onBoardSpeed;
 
33
 
 
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;
 
45
    private int state;
 
46
    private long stateTime;
 
47
 
 
48
    public Creature(Animation standingLeft,Animation standingRight,Animation jumpingLeft,Animation jumpingRight,Animation shootingLeft,Animation shootingRight , Animation left, Animation right,
 
49
            Animation deadLeft, Animation deadRight)
 
50
    {
 
51
        super(standingRight);
 
52
        this.standingLeft = standingLeft;
 
53
        this.standingRight = standingRight;
 
54
        this.jumpingLeft = jumpingRight;
 
55
        this.jumpingRight = jumpingRight;
 
56
        this.shootingLeft = shootingLeft;
 
57
        this.shootingRight = shootingRight;
 
58
        this.left = left;
 
59
        this.right = right;
 
60
        this.deadLeft = deadLeft;
 
61
        this.deadRight = deadRight;
 
62
        state = STATE_STANDING;
 
63
        bExtendedModel = true;
 
64
    }
 
65
  
 
66
    public Creature(Animation right) {
 
67
                super(right);
 
68
                this.left = right;
 
69
        this.right = right;
 
70
        this.deadLeft = right;
 
71
        this.deadRight = right;
 
72
        
 
73
                // TODO Auto-generated constructor stub
 
74
        }
 
75
    /**
 
76
    Creates a new Creature with the specified Animations.
 
77
*/
 
78
    public Creature(Animation left, Animation right,
 
79
        Animation deadLeft, Animation deadRight)
 
80
    {
 
81
        super(right);
 
82
        this.left = left;
 
83
        this.right = right;
 
84
        this.deadLeft = deadLeft;
 
85
        this.deadRight = deadRight;
 
86
        state = STATE_NORMAL;
 
87
    }
 
88
 
 
89
 
 
90
//    public Creature(Animation standing, Animation left[], Animation right[],
 
91
//            Animation deadLeft, Animation deadRight)
 
92
//        {
 
93
//            super(right);
 
94
//            this.left = left;
 
95
//            this.right = right;
 
96
//            this.deadLeft = deadLeft;
 
97
//            this.deadRight = deadRight;
 
98
//            state = STATE_NORMAL;
 
99
//        }
 
100
 
 
101
 
 
102
    public Object clone() {
 
103
        if(!bExtendedModel) {
 
104
                // use reflection to create the correct subclass
 
105
                Constructor constructor = getClass().getConstructors()[0];
 
106
                try {
 
107
                    return constructor.newInstance(new Object[] {
 
108
                        (Animation)left.clone(),
 
109
                        (Animation)right.clone(),
 
110
                        (Animation)deadLeft.clone(),
 
111
                        (Animation)deadRight.clone()
 
112
                    });
 
113
                }
 
114
                catch (Exception ex) {
 
115
                    // should never happen
 
116
                    ex.printStackTrace();
 
117
                    return null;
 
118
                }
 
119
        }
 
120
        else {
 
121
                // use reflection to create the correct subclass
 
122
                Constructor constructor = getClass().getConstructors()[0];
 
123
                try {
 
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()
 
135
                    });
 
136
                }
 
137
                catch (Exception ex) {
 
138
                    // should never happen
 
139
                    ex.printStackTrace();
 
140
                    return null;
 
141
                }
 
142
        }
 
143
    }
 
144
 
 
145
 
 
146
    /**
 
147
        Gets the maximum speed of this Creature.
 
148
    */
 
149
    public float getMaxSpeed() {
 
150
        return 0;
 
151
    }
 
152
 
 
153
 
 
154
    /**
 
155
        Wakes up the creature when the Creature first appears
 
156
        on screen. Normally, the creature starts moving left.
 
157
    */
 
158
    public void wakeUp() {
 
159
        if (getState() == STATE_NORMAL && getVelocityX() == 0) {
 
160
            setVelocityX(-getMaxSpeed());
 
161
        }
 
162
    }
 
163
 
 
164
 
 
165
    /**
 
166
        Gets the state of this Creature. The state is either
 
167
        STATE_NORMAL, STATE_DYING, or STATE_DEAD.
 
168
    */
 
169
    public int getState() {
 
170
        return state;
 
171
    }
 
172
 
 
173
 
 
174
    /**
 
175
        Sets the state of this Creature to STATE_NORMAL,
 
176
        STATE_DYING, or STATE_DEAD.
 
177
    */
 
178
    public void setState(int state) {
 
179
        if (this.state != state) {
 
180
            this.state = state;
 
181
            stateTime = 0;
 
182
            if (state == STATE_DYING) {
 
183
                setVelocityX(0);
 
184
                setVelocityY(0);
 
185
                if(this instanceof Boss) {
 
186
                        --bossCounter;
 
187
                        if(bossCounter<=0) {
 
188
                                MainGameState.bNextMap = true;  
 
189
                        }
 
190
                }
 
191
                else if(this instanceof Player) {
 
192
                        bossCounter = 10;
 
193
                }
 
194
            }
 
195
            if (state == STATE_SHOOTING) {
 
196
                setVelocityX(0);
 
197
                setVelocityY(0);
 
198
            }
 
199
        }
 
200
    }
 
201
 
 
202
 
 
203
    /**
 
204
        Checks if this creature is alive.
 
205
    */
 
206
    public boolean isAlive() {
 
207
        return (state == STATE_NORMAL|| state == STATE_JUMPING||state == STATE_SHOOTING||state == STATE_STANDING);
 
208
    }
 
209
 
 
210
    /**
 
211
        Checks if this creature is flying.
 
212
    */
 
213
    public boolean isFlying() {
 
214
        return isFlying;
 
215
    }
 
216
    public void setIsFlying(boolean bool) {
 
217
        isFlying = bool;
 
218
    }
 
219
    public boolean isOnBoard() {
 
220
        return onBoard;
 
221
    }
 
222
 
 
223
    public void setOnBoard(boolean bool) {
 
224
        onBoard = bool;
 
225
    }
 
226
 
 
227
    public void setOnBoardSpeed(float value) {
 
228
                onBoardSpeed = value;
 
229
        }
 
230
 
 
231
    public float getOnBoardSpeed() {
 
232
                return onBoardSpeed;
 
233
        }
 
234
 
 
235
    /**
 
236
        Called before update() if the creature collided with a
 
237
        tile horizontally.
 
238
    */
 
239
    public void collideHorizontal() {
 
240
        setVelocityX(-getVelocityX());
 
241
    }
 
242
 
 
243
 
 
244
    /**
 
245
        Called before update() if the creature collided with a
 
246
        tile vertically.
 
247
    */
 
248
    public void collideVertical() {
 
249
        setVelocityY(0);
 
250
    }
 
251
 
 
252
 
 
253
    /**
 
254
        Updates the animaton for this creature.
 
255
    */
 
256
    public void update(long elapsedTime) {
 
257
        // select the correct Animation
 
258
        Animation newAnim = anim;
 
259
        if (getVelocityX() < 0) {
 
260
            newAnim = left;
 
261
        }
 
262
        else if (getVelocityX() > 0) {
 
263
            newAnim = right;
 
264
        }
 
265
        if (state == STATE_DYING && newAnim == left) {
 
266
            newAnim = deadLeft;
 
267
        }
 
268
        else if (state == STATE_DYING && newAnim == right) {
 
269
            newAnim = deadRight;
 
270
        }
 
271
        if(bExtendedModel) {
 
272
                //if(sta)
 
273
        }
 
274
 
 
275
        // update the Animation
 
276
        if (anim != newAnim) {
 
277
            anim = newAnim;
 
278
            anim.start();
 
279
        }
 
280
        else {
 
281
            anim.update(elapsedTime);
 
282
        }
 
283
 
 
284
        // update to "dead" state
 
285
        stateTime += elapsedTime;
 
286
        if (state == STATE_DYING && stateTime >= DIE_TIME) {
 
287
            setState(STATE_DEAD);
 
288
        }
 
289
    }
 
290
 
 
291
}