DEADSOFTWARE

Initial commit.
[LongFlight.git] / src / code / kalter / longflight / game / Bullet.java
1 package code.kalter.longflight.game;
3 import code.kalter.longflight.Loader;
4 import code.kalter.longflight.game.bullet.Collisionable;
5 import code.kalter.longflight.game.bullet.Gun;
6 import java.io.IOException;
7 import javax.microedition.lcdui.Graphics;
8 import javax.microedition.lcdui.Image;
10 /**
11 * Пули!
12 *
13 * @author KalterFive
14 */
15 public class Bullet implements Gun {
17 private final int SIZE = 128;
18 private final int screenH;
20 private final Image[] gfx;
22 private final int[] positionX;
23 private final int[] positionY;
24 private final int[] type;
26 private Collisionable[] object;
28 public Bullet(int screenH) throws IOException {
29 this.screenH = screenH;
30 positionX = new int[SIZE];
31 positionY = new int[SIZE];
32 type = new int[SIZE];
33 Loader imageLoader = Loader.getInstance();
34 gfx = new Image[]{
35 imageLoader.getImage("/gfx/bullet/0.png"),
36 imageLoader.getImage("/gfx/bullet/1.png")
37 };
38 }
40 // магия - все пули исчезают
41 public void setNull() {
42 for (int i = 0; i < SIZE; i++) {
43 type[i] = NULL;
44 }
45 }
47 public void setCollisionable(Collisionable[] object) {
48 this.object = object;
49 }
51 public void update() {
52 for (int i = 0; i < SIZE; i++) {
53 if (type[i] == NULL) {
54 continue;
55 }
56 collision(i);
57 positionY[i] += (type[i] * 2 - 1) * 5;
58 if ((positionY[i] < -10) || (positionY[i] > screenH)) {
59 type[i] = NULL;
60 }
61 }
62 }
64 public void paint(Graphics graphics) {
65 for (int i = 0; i < SIZE; i++) {
66 if (type[i] == NULL) {
67 continue;
68 }
69 graphics.drawImage(gfx[type[i]], positionX[i], positionY[i], 0);
70 }
71 }
73 // override
74 public void fire(int type, int positionX, int positionY) {
75 for (int i = 0; i < SIZE; i++) {
76 if (this.type[i] != NULL) {
77 continue;
78 }
79 this.type[i] = type;
80 this.positionX[i] = positionX;
81 this.positionY[i] = positionY;
82 break;
83 }
84 }
86 private void collision(int i) {
87 for (int j = 0; j < object.length; j++) {
88 if (type[i] == object[j].getType()) {
89 continue;
90 }
91 if (object[j].isCollisionOfBullet(positionX[i], positionY[i])) {
92 object[j].boomOfBullet();
93 type[i] = NULL;
94 break;
95 }
96 }
97 }
98 }