DEADSOFTWARE

Initial commit.
[LongFlight.git] / src / code / kalter / longflight / game / Enemy.java
1 package code.kalter.longflight.game;
3 import code.kalter.longflight.game.bullet.Collisionable;
4 import code.kalter.longflight.game.bullet.Gun;
5 import code.kalter.longflight.game.explosion.Detonator;
6 import code.kalter.longflight.game.ship.Ship;
7 import code.kalter.longflight.game.ship.ShipEnemy;
8 import java.io.IOException;
9 import java.util.Random;
10 import java.util.Timer;
11 import java.util.TimerTask;
12 import javax.microedition.lcdui.Graphics;
14 /**
15 * Враги и всё связанное с ними
16 *
17 * @author KalterFive
18 */
19 public class Enemy implements Collisionable {
21 private static final int COUNT = 28;
23 // специальное обозначение для несуществующего врага
24 private static final int NULL = 666;
26 private final int screenW;
27 private final int screenH;
29 private final Gamer gamer;
30 private final Detonator detonator;
31 private final ShipEnemy ship;
32 private final Gun gun;
33 private final Random random;
34 private Timer createEnemy;
36 // enemy...
37 private final int[] x;
38 private final int[] y;
39 private final int[] type;
40 private final int[] life;
41 private final long[] lastFire;
43 private int score;
44 private int numKill;
45 private int boom; // индекс врага, в которого попала пуля
47 public Enemy(int screenW, int screenH, Gun gun,
48 Detonator detonator, Gamer gamer) throws IOException {
49 this.screenW = screenW;
50 this.screenH = screenH;
51 this.gun = gun;
52 this.detonator = detonator;
53 this.gamer = gamer;
54 ship = ShipEnemy.getInstance();
55 random = new Random();
56 x = new int[COUNT];
57 y = new int[COUNT];
58 type = new int[COUNT];
59 life = new int[COUNT];
60 lastFire = new long[COUNT];
61 }
63 // очистка всех врагов
64 public void setNull() {
65 for (int i = 0; i < COUNT; i++) {
66 type[i] = NULL;
67 }
68 numKill = score = 0;
69 }
71 public void update() {
72 for (int i = 0; i < COUNT; i++) {
73 if (type[i] == NULL) {
74 continue;
75 }
76 switch (type[i]) {
77 case Ship.LAX:
78 aiLax(i);
79 break;
81 case Ship.POWERFUL:
82 aiPowerful(i);
83 break;
85 case Ship.UNIVERSAL:
86 aiUniversal(i);
87 break;
89 case Ship.STRONG:
90 aiStrong(i);
91 break;
92 }
93 }
94 }
96 public void paint(Graphics graphics) {
97 for (int i = 0; i < COUNT; i++) {
98 if (type[i] == NULL) {
99 continue;
101 graphics.drawImage(ship.getImage(type[i]), x[i], y[i], 0);
105 // override
106 public boolean isCollisionOfBullet(int bx, int by) {
107 for (int i = 0; i < COUNT; i++) {
108 if (type[i] == NULL) {
109 continue;
111 if ((bx > x[i]) && (bx < x[i] + Ship.SIZE)
112 && (by > y[i]) && (by < y[i] + Ship.SIZE)) {
113 boom = i;
114 return true;
117 return false;
120 // override
121 public void boomOfBullet() {
122 if (--life[boom] <= 0) {
123 numKill++;
124 score += (((type[boom] + 1) * 2) - 1);
125 type[boom] = NULL;
126 detonator.activate(x[boom], y[boom]);
130 // override
131 public int getType() {
132 return Gun.ENEMY;
135 public void activateCreator() {
136 createEnemy = new Timer();
137 createEnemy.schedule(new TimerTask() {
138 // override
139 public void run() {
140 for (int i = 0; i < COUNT; i++) {
141 if (type[i] != NULL) {
142 continue;
144 int type = random.nextInt(numKill / 30 + 1);
145 Enemy.this.type[i] = type >= Ship.COUNT ? Ship.COUNT - 1 : type < 0 ? 0 : type;
146 life[i] = ship.getLife(Enemy.this.type[i]);
147 lastFire[i] = System.currentTimeMillis();
148 x[i] = random.nextInt(screenW - Ship.SIZE);
149 y[i] = -Ship.SIZE;
150 break;
153 }, 2500, 2500);
156 public void deactivateCreator() {
157 createEnemy.cancel();
160 public int getScore() {
161 return score;
164 private void aiLax(int i) {
165 y[i] += ship.getSpeed(type[i]);
166 if (y[i] > screenH) {
167 type[i] = NULL;
169 if (y[i] > gamer.getY()) {
170 return;
172 fire(i);
173 if (x[i] > gamer.getX()) {
174 x[i] -= ship.getSpeed(type[i]) / 2;
176 if (x[i] < gamer.getX()) {
177 x[i] += ship.getSpeed(type[i]) / 2;
181 private void aiPowerful(int i) {
182 y[i] += ship.getSpeed(type[i]);
183 if (y[i] > screenH) {
184 type[i] = NULL;
186 if (y[i] > gamer.getY()) {
187 return;
189 fire(i);
192 private void aiUniversal(int i) {
193 if (y[i] < screenH / 3) {
194 y[i] += ship.getSpeed(type[i]);
196 fire(i);
197 if (x[i] > gamer.getX()) {
198 x[i] -= ship.getSpeed(type[i]) / 2;
200 if (x[i] < gamer.getX()) {
201 x[i] += ship.getSpeed(type[i]) / 2;
205 private void aiStrong(int i) {
206 y[i] += ship.getSpeed(type[i]);
207 if (y[i] > screenH) {
208 type[i] = NULL;
210 if (y[i] > gamer.getY()) {
211 return;
213 if (x[i] > gamer.getX()) {
214 x[i] -= ship.getSpeed(type[i]);
216 if (x[i] < gamer.getX()) {
217 x[i] += ship.getSpeed(type[i]);
219 final int deltaX = Math.abs(x[i] - gamer.getX());
220 if (deltaX < Ship.SIZE * 2) {
221 fire(i);
225 private void fire(int i) {
226 if (System.currentTimeMillis() - lastFire[i] > ship.getDeltaFire(type[i])) {
227 gun.fire(Gun.ENEMY, x[i] + 4, y[i] + 38);
228 gun.fire(Gun.ENEMY, x[i] + 30, y[i] + 38);
229 lastFire[i] = System.currentTimeMillis();