DEADSOFTWARE

Initial commit.
[LongFlight.git] / src / code / kalter / longflight / game / Gamer.java
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;
19 /**
20 * Игрок, и всё связанное с ним
21 *
22 * @author KalterFive
23 */
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;
36 private int overheat;
37 private int type;
38 private int life;
39 private int speedX;
40 private int speedY;
41 private int positionX;
42 private int positionY;
44 private long lastFireTime;
46 public Gamer(int screenW, int screenH, Gun gun, Detonator detonator)
47 throws IOException {
48 this.screenW = screenW;
49 this.screenH = screenH;
50 this.gun = gun;
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");
56 }
58 // ставим игрока на своё место
59 public void setNull() {
60 alive = true;
61 lastFireTime = System.currentTimeMillis();
62 speedX = speedY = 0;
63 overheat = 0;
64 positionX = (screenW - Ship.SIZE) / 2;
65 positionY = screenH - 80;
66 try {
67 RMS rms = new RMS("longflight", true);
68 ByteArrayInputStream byteReader = rms.get(1);
69 type = byteReader.readByte();
70 rms.close();
71 } catch (RecordStoreException e) {
72 LongFlight.link.catchException(e);
73 }
74 life = ship.getLife(type);
75 }
77 public void paint(Graphics graphics) {
78 if (alive) {
79 graphics.drawImage(ship.getImage(type), positionX, positionY, 0);
80 }
81 if (speedY < 0) {
82 graphics.drawImage(nozzle, positionX + 16, positionY + 36, 0);
83 }
84 colorEffect.paint(graphics);
85 }
87 public void update() {
88 // охлаждение
89 overheat -= overheat > 0 ? 1 : 0;
91 // перемещение
92 positionX += speedX / ship.getTime(type);
93 positionY += speedY / ship.getTime(type);
95 // ограничение перемещения
96 // position x
97 if (positionX < 0) {
98 positionX = 0;
99 } else if (positionX > screenW - Ship.SIZE) {
100 positionX = screenW - Ship.SIZE;
102 // position y
103 if (positionY < 18) {
104 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;
114 // override
115 public boolean isCollisionOfBullet(int bx, int by) {
116 return alive && bx > positionX && bx < positionX + Ship.SIZE && by > positionY && by < positionY + Ship.SIZE;
119 // override
120 public void boomOfBullet() {
121 if (alive) {
122 colorEffect.activate();
123 if (--life < 0) {
124 alive = false;
125 Game.link.stop();
126 detonator.activate(positionX, positionY);
131 // override
132 public int getType() {
133 return Gun.GAMER;
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);
164 public void fire() {
165 overheat += overheat < 160 ? ship.getSpeedOverheat(type) : 0;
166 if (!isGetFire()) {
167 return;
169 gun.fire(Gun.GAMER, getX() + 4, getY());
170 gun.fire(Gun.GAMER, getX() + 30, getY());
173 public int getX() {
174 return positionX;
177 public int getY() {
178 return positionY;
181 public int getSpeedX() {
182 return speedX;
185 public int getSpeedY() {
186 return speedY;
189 private boolean isGetFire() {
190 boolean result = false;
191 if (System.currentTimeMillis() - lastFireTime > ship.getDeltaFire(type)) {
192 lastFireTime = System.currentTimeMillis();
193 result = true;
195 return result && overheat < 160;
198 public int getLife() {
199 return life * 4;
202 public int getOverheat() {
203 int result = overheat / 3;
204 return result > 48 ? 48 : result;