package code.kalter.longflight.game; import code.kalter.longflight.Loader; import code.kalter.longflight.game.bullet.Collisionable; import code.kalter.longflight.game.bullet.Gun; import java.io.IOException; import javax.microedition.lcdui.Graphics; import javax.microedition.lcdui.Image; /** * Пули! * * @author KalterFive */ public class Bullet implements Gun { private final int SIZE = 128; private final int screenH; private final Image[] gfx; private final int[] positionX; private final int[] positionY; private final int[] type; private Collisionable[] object; public Bullet(int screenH) throws IOException { this.screenH = screenH; positionX = new int[SIZE]; positionY = new int[SIZE]; type = new int[SIZE]; Loader imageLoader = Loader.getInstance(); gfx = new Image[]{ imageLoader.getImage("/gfx/bullet/0.png"), imageLoader.getImage("/gfx/bullet/1.png") }; } // магия - все пули исчезают public void setNull() { for (int i = 0; i < SIZE; i++) { type[i] = NULL; } } public void setCollisionable(Collisionable[] object) { this.object = object; } public void update() { for (int i = 0; i < SIZE; i++) { if (type[i] == NULL) { continue; } collision(i); positionY[i] += (type[i] * 2 - 1) * 5; if ((positionY[i] < -10) || (positionY[i] > screenH)) { type[i] = NULL; } } } public void paint(Graphics graphics) { for (int i = 0; i < SIZE; i++) { if (type[i] == NULL) { continue; } graphics.drawImage(gfx[type[i]], positionX[i], positionY[i], 0); } } // override public void fire(int type, int positionX, int positionY) { for (int i = 0; i < SIZE; i++) { if (this.type[i] != NULL) { continue; } this.type[i] = type; this.positionX[i] = positionX; this.positionY[i] = positionY; break; } } private void collision(int i) { for (int j = 0; j < object.length; j++) { if (type[i] == object[j].getType()) { continue; } if (object[j].isCollisionOfBullet(positionX[i], positionY[i])) { object[j].boomOfBullet(); type[i] = NULL; break; } } } }