3e879c1fe249b84589f2462e197a0230c5cf781b
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
;
15 * Враги и всё связанное с ними
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
;
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
;
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
;
52 this.detonator
= detonator
;
54 ship
= ShipEnemy
.getInstance();
55 random
= new Random();
58 type
= new int[COUNT
];
59 life
= new int[COUNT
];
60 lastFire
= new long[COUNT
];
63 // очистка всех врагов
64 public void setNull() {
65 for (int i
= 0; i
< COUNT
; i
++) {
71 public void update() {
72 for (int i
= 0; i
< COUNT
; i
++) {
73 if (type
[i
] == NULL
) {
96 public void paint(Graphics graphics
) {
97 for (int i
= 0; i
< COUNT
; i
++) {
98 if (type
[i
] == NULL
) {
101 graphics
.drawImage(ship
.getImage(type
[i
]), x
[i
], y
[i
], 0);
106 public boolean isCollisionOfBullet(int bx
, int by
) {
107 for (int i
= 0; i
< COUNT
; i
++) {
108 if (type
[i
] == NULL
) {
111 if ((bx
> x
[i
]) && (bx
< x
[i
] + Ship
.SIZE
)
112 && (by
> y
[i
]) && (by
< y
[i
] + Ship
.SIZE
)) {
121 public void boomOfBullet() {
122 if (--life
[boom
] <= 0) {
124 score
+= (((type
[boom
] + 1) * 2) - 1);
126 detonator
.activate(x
[boom
], y
[boom
]);
131 public int getType() {
135 public void activateCreator() {
136 createEnemy
= new Timer();
137 createEnemy
.schedule(new TimerTask() {
140 for (int i
= 0; i
< COUNT
; i
++) {
141 if (type
[i
] != NULL
) {
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
);
156 public void deactivateCreator() {
157 createEnemy
.cancel();
160 public int getScore() {
164 private void aiLax(int i
) {
165 y
[i
] += ship
.getSpeed(type
[i
]);
166 if (y
[i
] > screenH
) {
169 if (y
[i
] > gamer
.getY()) {
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
) {
186 if (y
[i
] > gamer
.getY()) {
192 private void aiUniversal(int i
) {
193 if (y
[i
] < screenH
/ 3) {
194 y
[i
] += ship
.getSpeed(type
[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
) {
210 if (y
[i
] > gamer
.getY()) {
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) {
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();