1fbc72a166ad536c2b9455e9d99f0fac28d6621b
1 package code
.kalter
.longflight
.game
;
3 import code
.kalter
.longflight
.ByteArrayInputStream
;
4 import code
.kalter
.longflight
.Color
;
5 import code
.kalter
.longflight
.Loader
;
6 import code
.kalter
.longflight
.LongFlight
;
7 import code
.kalter
.longflight
.game
.bullet
.Collisionable
;
8 import code
.kalter
.longflight
.game
.bullet
.Gun
;
9 import code
.kalter
.longflight
.game
.explosion
.Detonator
;
10 import code
.kalter
.longflight
.game
.ship
.Ship
;
11 import code
.kalter
.longflight
.game
.ship
.ShipGamer
;
12 import code
.kalter
.longflight
.rms
.RMS
;
13 import code
.kalter
.longflight
.screen
.Game
;
14 import java
.io
.IOException
;
15 import javax
.microedition
.lcdui
.Graphics
;
16 import javax
.microedition
.lcdui
.Image
;
17 import javax
.microedition
.rms
.RecordStoreException
;
20 * Игрок, и всё связанное с ним
24 public class Gamer
implements Collisionable
{
26 private final int screenW
;
27 private final int screenH
;
29 private final Image nozzle
;
31 private final ColorEffect colorEffect
;
32 private final Detonator detonator
;
33 private final ShipGamer ship
;
34 private final Gun gun
;
35 private boolean alive
;
41 private int positionX
;
42 private int positionY
;
44 private long lastFireTime
;
46 public Gamer(int screenW
, int screenH
, Gun gun
, Detonator detonator
)
48 this.screenW
= screenW
;
49 this.screenH
= screenH
;
51 this.detonator
= detonator
;
52 this.colorEffect
= new ColorEffect(Color
.WHITE
, 2, screenW
, screenH
);
53 ship
= ShipGamer
.getInstance();
54 Loader imageLoader
= Loader
.getInstance();
55 nozzle
= imageLoader
.getImage("/gfx/ship/nozzle.png");
58 // ставим игрока на своё место
59 public void setNull() {
61 lastFireTime
= System
.currentTimeMillis();
64 positionX
= (screenW
- Ship
.SIZE
) / 2;
65 positionY
= screenH
- 80;
67 RMS rms
= new RMS("longflight", true);
68 ByteArrayInputStream byteReader
= rms
.get(1);
69 type
= byteReader
.readByte();
71 } catch (RecordStoreException e
) {
72 LongFlight
.link
.catchException(e
);
74 life
= ship
.getLife(type
);
77 public void paint(Graphics graphics
) {
79 graphics
.drawImage(ship
.getImage(type
), positionX
, positionY
, 0);
82 graphics
.drawImage(nozzle
, positionX
+ 16, positionY
+ 36, 0);
84 colorEffect
.paint(graphics
);
87 public void update() {
89 overheat
-= overheat
> 0 ?
1 : 0;
92 positionX
+= speedX
/ ship
.getTime(type
);
93 positionY
+= speedY
/ ship
.getTime(type
);
95 // ограничение перемещения
99 } else if (positionX
> screenW
- Ship
.SIZE
) {
100 positionX
= screenW
- Ship
.SIZE
;
103 if (positionY
< 18) {
105 } else if (positionY
> screenH
- Ship
.SIZE
- 22) {
106 positionY
= screenH
- Ship
.SIZE
- 22;
109 // ускорение стремится к нулю
110 speedX
= speedX
< 0 ?
++speedX
: speedX
> 0 ?
--speedX
: speedX
;
111 speedY
= speedY
< 0 ?
++speedY
: speedY
> 0 ?
--speedY
: speedY
;
115 public boolean isCollisionOfBullet(int bx
, int by
) {
116 return alive
&& bx
> positionX
&& bx
< positionX
+ Ship
.SIZE
&& by
> positionY
&& by
< positionY
+ Ship
.SIZE
;
120 public void boomOfBullet() {
122 colorEffect
.activate();
126 detonator
.activate(positionX
, positionY
);
132 public int getType() {
136 public void moveLeft() {
137 speedX
-= ship
.getAcceleration(type
);
138 if (speedX
< (-ship
.getVelocityMax(type
))) {
139 speedX
= (-ship
.getVelocityMax(type
));
143 public void moveRight() {
144 speedX
+= ship
.getAcceleration(type
);
145 if (speedX
> ship
.getVelocityMax(type
)) {
146 speedX
= ship
.getVelocityMax(type
);
150 public void moveUp() {
151 speedY
-= ship
.getAcceleration(type
);
152 if (speedY
< (-ship
.getVelocityMax(type
))) {
153 speedY
= (-ship
.getVelocityMax(type
));
157 public void moveDown() {
158 speedY
+= ship
.getAcceleration(type
);
159 if (speedY
> ship
.getVelocityMax(type
)) {
160 speedY
= ship
.getVelocityMax(type
);
165 overheat
+= overheat
< 160 ? ship
.getSpeedOverheat(type
) : 0;
169 gun
.fire(Gun
.GAMER
, getX() + 4, getY());
170 gun
.fire(Gun
.GAMER
, getX() + 30, getY());
181 public int getSpeedX() {
185 public int getSpeedY() {
189 private boolean isGetFire() {
190 boolean result
= false;
191 if (System
.currentTimeMillis() - lastFireTime
> ship
.getDeltaFire(type
)) {
192 lastFireTime
= System
.currentTimeMillis();
195 return result
&& overheat
< 160;
198 public int getLife() {
202 public int getOverheat() {
203 int result
= overheat
/ 3;
204 return result
> 48 ?
48 : result
;