001.
package
com.synthdreams.GalacticBlast;
002.
003.
import
net.rim.device.api.system.Bitmap;
004.
import
net.rim.device.api.ui.Graphics;
005.
import
java.util.*;
006.
007.
008.
009.
class
OBJ
010.
{
011.
012.
public
static
int
STATE_NORMAL =
0
;
013.
public
static
int
STATE_HIT =
100
;
014.
public
static
int
STATE_DYING =
1000
;
015.
016.
int
_posX, _posY, _velX, _velY;
017.
int
_life, _value, _state;
018.
OBJ _parent;
019.
String _type;
020.
Bitmap _bitmap;
021.
022.
023.
OBJ(
int
passX,
int
passY)
024.
{
025.
_posX = passX;
026.
_posY = passY;
027.
_velX =
0
;
028.
_velY =
0
;
029.
_life =
1
;
030.
_value =
0
;
031.
_state = STATE_NORMAL;
032.
_type =
"generic"
;
033.
034.
}
035.
036.
037.
public
int
getX() {
return
_posX; }
038.
public
int
getY() {
return
_posY; }
039.
public
void
setX(
int
passX) { _posX = passX; }
040.
public
void
setY(
int
passY) { _posY = passY; }
041.
public
int
getVelX() {
return
_velX; }
042.
public
int
getVelY() {
return
_velY; }
043.
public
void
setVelX(
int
passX) { _velX = passX; }
044.
public
void
setVelY(
int
passY) { _velY = passY; }
045.
public
int
getLife() {
return
_life; }
046.
public
void
setLife(
int
passLife) { _life = passLife; }
047.
public
int
getValue() {
return
_value; }
048.
public
int
getState() {
return
_state; }
049.
public
void
setState(
int
passState) { _state = passState; }
050.
public
String getType() {
return
_type; }
051.
public
Bitmap getBitmap() {
return
_bitmap; }
052.
public
OBJ getParent() {
return
_parent; }
053.
054.
055.
public
void
process() { }
056.
public
void
think(Vector passObjects) { }
057.
public
void
damage() { }
058.
059.
060.
061.
public
void
fire(Vector passObjects, OBJ passParent,
int
passVelocity)
062.
{
063.
Photon tempPhoton;
064.
065.
066.
067.
if
(passVelocity >
0
)
068.
tempPhoton =
new
Photon(passParent.getX(),passParent.getY()+passParent.getBitmap().getHeight(),
0
, passVelocity, passParent);
069.
else
070.
tempPhoton =
new
Photon(passParent.getX(),passParent.getY(),
0
, passVelocity, passParent);
071.
072.
073.
tempPhoton.setX(tempPhoton.getX() + passParent.getBitmap().getWidth()/
2
);
074.
075.
076.
passObjects.addElement(tempPhoton);
077.
078.
079.
GamePlay.snd.playSound();
080.
081.
}
082.
083.
084.
085.
086.
087.
public
static
void
collisionDetect(Vector passObjects)
088.
{
089.
OBJ tempObject1, tempObject2;
090.
boolean
intersect, check;
091.
092.
093.
for
(
int
lcv =
0
; lcv < passObjects.size() ; lcv++)
094.
{
095.
096.
tempObject1 = (OBJ) passObjects.elementAt(lcv);
097.
098.
099.
for
(
int
lcv2 = lcv ; lcv2 < passObjects.size() ; lcv2++)
100.
{
101.
102.
tempObject2 = (OBJ) passObjects.elementAt(lcv2);
103.
104.
105.
106.
107.
108.
check =
false
;
109.
110.
111.
if
(tempObject1.getType() ==
"hero"
&& tempObject2.getType().startsWith(
"enemy"
))
112.
check =
true
;
113.
114.
115.
if
(tempObject1.getType() ==
"hero"
&& tempObject2.getType().startsWith(
"fire"
) && tempObject2.getParent().getType().startsWith(
"enemy"
))
116.
check =
true
;
117.
118.
119.
if
(tempObject1.getType().startsWith(
"enemy"
) && tempObject2.getType().startsWith(
"fire"
) && tempObject2.getParent().getType() ==
"hero"
)
120.
check =
true
;
121.
122.
123.
124.
125.
if
(check && tempObject1.getState() ==
0
&& tempObject2.getState() ==
0
)
126.
{
127.
128.
129.
intersect =
true
;
130.
131.
132.
if
(!(Math.abs((tempObject1.getX() + tempObject1.getBitmap().getWidth()/
2
) - (tempObject2.getX() + tempObject2.getBitmap().getWidth()/
2
)) <= tempObject1.getBitmap().getWidth() /
2
+ tempObject2.getBitmap().getWidth() /
2
))
133.
intersect =
false
;
134.
135.
136.
if
(!(Math.abs((tempObject1.getY() + tempObject1.getBitmap().getHeight()/
2
) - (tempObject2.getY() + tempObject2.getBitmap().getHeight()/
2
)) <= tempObject1.getBitmap().getHeight() /
2
+ tempObject2.getBitmap().getHeight() /
2
))
137.
intersect =
false
;
138.
139.
140.
if
(intersect)
141.
{
142.
tempObject1.damage();
143.
tempObject2.damage();
144.
}
145.
}
146.
}
147.
}
148.
}
149.
150.
151.
public
static
int
cleanObjects(Vector passObjects)
152.
{
153.
OBJ tempObject;
154.
boolean
delFlag;
155.
int
scoreAdd;
156.
157.
158.
scoreAdd =
0
;
159.
160.
161.
for
(
int
lcv =
0
; lcv < passObjects.size() ; lcv++)
162.
{
163.
164.
tempObject = (OBJ) passObjects.elementAt(lcv);
165.
166.
167.
delFlag =
false
;
168.
169.
170.
171.
if
(tempObject.getState() > STATE_DYING +
10
)
172.
{
173.
174.
175.
176.
if
(tempObject.getType() ==
"hero"
)
177.
{
178.
179.
if
(((Hero)tempObject).getLives() >
0
)
180.
{
181.
182.
183.
((Hero)tempObject).setLives(((Hero)tempObject).getLives()-
1
);
184.
tempObject.setLife(
5
);
185.
tempObject.setState(STATE_NORMAL);
186.
tempObject.setX(Graphics.getScreenWidth() /
2
);
187.
tempObject.setY(Graphics.getScreenHeight() -
50
);
188.
tempObject._bitmap = Bitmap.getBitmapResource(
"herogame.png"
);
189.
}
190.
else
191.
{
192.
193.
194.
delFlag =
true
;
195.
}
196.
}
197.
else
198.
{
199.
200.
201.
delFlag =
true
;
202.
scoreAdd += tempObject.getValue();
203.
}
204.
}
205.
206.
207.
if
(tempObject.getType() ==
"firephoton"
&& delFlag ==
false
)
208.
{
209.
210.
if
(tempObject.getX() + tempObject.getBitmap().getWidth() <
0
)
211.
delFlag =
true
;
212.
213.
214.
if
(tempObject.getX() > Graphics.getScreenWidth())
215.
delFlag =
true
;
216.
217.
218.
if
(tempObject.getY() + tempObject.getBitmap().getHeight() <
0
)
219.
delFlag =
true
;
220.
221.
222.
if
(tempObject.getY() > Graphics.getScreenHeight())
223.
delFlag =
true
;
224.
}
225.
226.
227.
228.
229.
230.
if
(tempObject.getType() ==
"enemydrone"
&& delFlag ==
false
)
231.
{
232.
233.
234.
235.
if
(tempObject.getX() + tempObject.getBitmap().getWidth() < -
100
)
236.
delFlag =
true
;
237.
238.
if
(tempObject.getX() > Graphics.getScreenWidth() +
100
)
239.
delFlag =
true
;
240.
241.
if
(tempObject.getY() + tempObject.getBitmap().getHeight() < -
100
)
242.
delFlag =
true
;
243.
244.
if
(tempObject.getY() > Graphics.getScreenHeight() +
100
)
245.
delFlag =
true
;
246.
}
247.
248.
249.
if
(delFlag)
250.
{
251.
252.
passObjects.removeElementAt(lcv);
253.
254.
255.
tempObject =
null
;
256.
257.
258.
259.
if
(lcv ==
0
)
260.
{
261.
return
(-
1
);
262.
}
263.
}
264.
}
265.
266.
267.
return
scoreAdd;
268.
}
269.
270.
271.
272.
273.
public
void
boundToScreen()
274.
{
275.
276.
277.
if
(_posX <
0
)
278.
{
279.
_posX =
0
;
280.
_velX =
0
;
281.
}
282.
283.
if
(_posY <
0
)
284.
{
285.
_posY =
0
;
286.
_velY =
0
;
287.
}
288.
289.
if
(_posX > Graphics.getScreenWidth() - _bitmap.getWidth())
290.
{
291.
_posX = Graphics.getScreenWidth() - _bitmap.getWidth();
292.
_velX =
0
;
293.
}
294.
295.
if
(_posY > Graphics.getScreenHeight() - _bitmap.getHeight())
296.
{
297.
_posY = Graphics.getScreenHeight() - _bitmap.getHeight();
298.
_velY =
0
;
299.
}
300.
301.
}
302.
303.
}
304.
305.
306.
class
Hero
extends
OBJ
307.
{
308.
int
_lives;
309.
310.
Hero(
int
passX,
int
passY)
311.
{
312.
super
(passX, passY);
313.
314.
315.
_bitmap = Bitmap.getBitmapResource(
"herogame.png"
);
316.
317.
318.
_velX =
0
;
319.
_velY =
0
;
320.
_life =
5
;
321.
_lives =
2
;
322.
_type =
"hero"
;
323.
}
324.
325.
326.
public
void
process()
327.
{
328.
329.
if
(_velX >
10
)
330.
_velX =
10
;
331.
332.
if
(_velX < -
10
)
333.
_velX = -
10
;
334.
335.
if
(_velY >
10
)
336.
_velY =
10
;
337.
338.
if
(_velY < -
10
)
339.
_velY = -
10
;
340.
341.
342.
_posX += _velX;
343.
_posY += _velY;
344.
345.
346.
if
(_life <
1
)
347.
{
348.
349.
350.
if
(_state == STATE_NORMAL)
351.
{
352.
353.
_bitmap = Bitmap.getBitmapResource(
"explosiongame.png"
);
354.
355.
356.
_state = STATE_DYING;
357.
358.
359.
GamePlay.snd.vibrate(
180
);
360.
}
361.
362.
}
363.
364.
365.
366.
if
(_state > STATE_NORMAL)
367.
{
368.
369.
370.
371.
372.
373.
_state++;
374.
375.
376.
if
((_state > STATE_HIT +
3
) && (_state < STATE_DYING))
377.
{
378.
_state = STATE_NORMAL;
379.
_bitmap = Bitmap.getBitmapResource(
"herogame.png"
);
380.
}
381.
}
382.
383.
384.
boundToScreen();
385.
}
386.
387.
388.
public
void
damage()
389.
{
390.
391.
_life--;
392.
393.
394.
if
(_life >
0
)
395.
{
396.
_bitmap = Bitmap.getBitmapResource(
"herogamehit.png"
);
397.
_state = STATE_HIT;
398.
}
399.
}
400.
401.
402.
public
void
fire(Vector passObjects)
403.
{
404.
405.
super
.fire(passObjects,
this
, -
20
);
406.
}
407.
408.
409.
public
int
getLives() {
return
_lives; }
410.
public
void
setLives(
int
passLives) { _lives = passLives; }
411.
412.
}
413.
414.
415.
class
Photon
extends
OBJ
416.
{
417.
418.
419.
420.
Photon(
int
passX,
int
passY,
int
passVelX,
int
passVelY, OBJ passParent)
421.
{
422.
super
(passX, passY);
423.
_bitmap = Bitmap.getBitmapResource(
"photon.png"
);
424.
_velX = passVelX;
425.
_velY = passVelY;
426.
_type =
"firephoton"
;
427.
_parent = passParent;
428.
}
429.
430.
431.
432.
public
void
process()
433.
{
434.
435.
436.
_posX += _velX;
437.
_posY += _velY;
438.
439.
}
440.
441.
442.
443.
444.
445.
446.
public
void
damage()
447.
{
448.
_life =
0
;
449.
_state = STATE_DYING+
11
;
450.
}
451.
452.
}
453.
454.
455.
class
EnemyDrone
extends
OBJ
456.
{
457.
int
_AIRoutine;
458.
459.
460.
461.
EnemyDrone(
int
passX,
int
passY)
462.
{
463.
super
(passX, passY);
464.
_bitmap = Bitmap.getBitmapResource(
"enemygame.png"
);
465.
_value =
50
;
466.
_type =
"enemydrone"
;
467.
468.
469.
if
(GamePlay.rndGenerator.nextInt() %
10
<
6
)
470.
_AIRoutine =
0
;
471.
else
472.
_AIRoutine =
1
;
473.
474.
}
475.
476.
477.
478.
479.
public
void
process()
480.
{
481.
_posX += _velX;
482.
_posY += _velY;
483.
484.
if
(_life <
1
)
485.
{
486.
if
(_state == STATE_NORMAL)
487.
{
488.
_bitmap = Bitmap.getBitmapResource(
"explosiongame.png"
);
489.
_state = STATE_DYING;
490.
GamePlay.snd.vibrate(
180
);
491.
}
492.
493.
_state++;
494.
}
495.
496.
}
497.
498.
499.
public
void
think(Vector passObjects)
500.
{
501.
502.
503.
if
(_life <
1
)
504.
return
;
505.
506.
507.
_velX =
0
;
508.
_velY =
0
;
509.
510.
511.
Hero tempHero = (Hero) passObjects.elementAt(
0
);
512.
513.
514.
if
(_AIRoutine ==
0
)
515.
{
516.
517.
if
(_posX + _bitmap.getWidth() /
2
< tempHero.getX() + tempHero.getBitmap().getWidth()/
2
)
518.
_velX =
5
;
519.
520.
521.
if
(_posX + _bitmap.getWidth() /
2
> tempHero.getX() + tempHero.getBitmap().getWidth()/
2
)
522.
_velX = -
5
;
523.
524.
525.
if
(_posY + _bitmap.getHeight() < tempHero.getY() -
40
)
526.
_velY =
5
;
527.
528.
529.
if
(_posY > tempHero.getY() + tempHero.getBitmap().getHeight())
530.
_velY = -
5
;
531.
532.
533.
_velX += GamePlay.rndGenerator.nextInt() %
4
-
2
;
534.
_velY += GamePlay.rndGenerator.nextInt() %
4
-
2
;
535.
536.
537.
if
(GamePlay.rndGenerator.nextInt() %
4
==
1
)
538.
{
539.
fire(passObjects);
540.
}
541.
}
542.
else
543.
{
544.
545.
if
(_posX + _bitmap.getWidth() /
2
< tempHero.getX() + tempHero.getBitmap().getWidth()/
2
)
546.
_velX =
5
;
547.
548.
if
(_posX + _bitmap.getWidth() /
2
> tempHero.getX() + tempHero.getBitmap().getWidth()/
2
)
549.
_velX = -
5
;
550.
551.
552.
_velY =
8
;
553.
}
554.
}
555.
556.
557.
public
void
damage()
558.
{
559.
_life--;
560.
}
561.
562.
563.
public
void
fire(Vector passObjects)
564.
{
565.
super
.fire(passObjects,
this
,
20
);
566.
}
567.
568.
}