package code.kalter.longflight.game; import code.kalter.longflight.ByteArrayInputStream; import code.kalter.longflight.Color; import code.kalter.longflight.Loader; import code.kalter.longflight.LongFlight; import code.kalter.longflight.game.bullet.Collisionable; import code.kalter.longflight.game.bullet.Gun; import code.kalter.longflight.game.explosion.Detonator; import code.kalter.longflight.game.ship.Ship; import code.kalter.longflight.game.ship.ShipGamer; import code.kalter.longflight.rms.RMS; import code.kalter.longflight.screen.Game; import java.io.IOException; import javax.microedition.lcdui.Graphics; import javax.microedition.lcdui.Image; import javax.microedition.rms.RecordStoreException; /** * Игрок, и всё связанное с ним * * @author KalterFive */ public class Gamer implements Collisionable { private final int screenW; private final int screenH; private final Image nozzle; private final ColorEffect colorEffect; private final Detonator detonator; private final ShipGamer ship; private final Gun gun; private boolean alive; private int overheat; private int type; private int life; private int speedX; private int speedY; private int positionX; private int positionY; private long lastFireTime; public Gamer(int screenW, int screenH, Gun gun, Detonator detonator) throws IOException { this.screenW = screenW; this.screenH = screenH; this.gun = gun; this.detonator = detonator; this.colorEffect = new ColorEffect(Color.WHITE, 2, screenW, screenH); ship = ShipGamer.getInstance(); Loader imageLoader = Loader.getInstance(); nozzle = imageLoader.getImage("/gfx/ship/nozzle.png"); } // ставим игрока на своё место public void setNull() { alive = true; lastFireTime = System.currentTimeMillis(); speedX = speedY = 0; overheat = 0; positionX = (screenW - Ship.SIZE) / 2; positionY = screenH - 80; try { RMS rms = new RMS("longflight", true); ByteArrayInputStream byteReader = rms.get(1); type = byteReader.readByte(); rms.close(); } catch (RecordStoreException e) { LongFlight.link.catchException(e); } life = ship.getLife(type); } public void paint(Graphics graphics) { if (alive) { graphics.drawImage(ship.getImage(type), positionX, positionY, 0); } if (speedY < 0) { graphics.drawImage(nozzle, positionX + 16, positionY + 36, 0); } colorEffect.paint(graphics); } public void update() { // охлаждение overheat -= overheat > 0 ? 1 : 0; // перемещение positionX += speedX / ship.getTime(type); positionY += speedY / ship.getTime(type); // ограничение перемещения // position x if (positionX < 0) { positionX = 0; } else if (positionX > screenW - Ship.SIZE) { positionX = screenW - Ship.SIZE; } // position y if (positionY < 18) { positionY = 18; } else if (positionY > screenH - Ship.SIZE - 22) { positionY = screenH - Ship.SIZE - 22; } // ускорение стремится к нулю speedX = speedX < 0 ? ++speedX : speedX > 0 ? --speedX : speedX; speedY = speedY < 0 ? ++speedY : speedY > 0 ? --speedY : speedY; } // override public boolean isCollisionOfBullet(int bx, int by) { return alive && bx > positionX && bx < positionX + Ship.SIZE && by > positionY && by < positionY + Ship.SIZE; } // override public void boomOfBullet() { if (alive) { colorEffect.activate(); if (--life < 0) { alive = false; Game.link.stop(); detonator.activate(positionX, positionY); } } } // override public int getType() { return Gun.GAMER; } public void moveLeft() { speedX -= ship.getAcceleration(type); if (speedX < (-ship.getVelocityMax(type))) { speedX = (-ship.getVelocityMax(type)); } } public void moveRight() { speedX += ship.getAcceleration(type); if (speedX > ship.getVelocityMax(type)) { speedX = ship.getVelocityMax(type); } } public void moveUp() { speedY -= ship.getAcceleration(type); if (speedY < (-ship.getVelocityMax(type))) { speedY = (-ship.getVelocityMax(type)); } } public void moveDown() { speedY += ship.getAcceleration(type); if (speedY > ship.getVelocityMax(type)) { speedY = ship.getVelocityMax(type); } } public void fire() { overheat += overheat < 160 ? ship.getSpeedOverheat(type) : 0; if (!isGetFire()) { return; } gun.fire(Gun.GAMER, getX() + 4, getY()); gun.fire(Gun.GAMER, getX() + 30, getY()); } public int getX() { return positionX; } public int getY() { return positionY; } public int getSpeedX() { return speedX; } public int getSpeedY() { return speedY; } private boolean isGetFire() { boolean result = false; if (System.currentTimeMillis() - lastFireTime > ship.getDeltaFire(type)) { lastFireTime = System.currentTimeMillis(); result = true; } return result && overheat < 160; } public int getLife() { return life * 4; } public int getOverheat() { int result = overheat / 3; return result > 48 ? 48 : result; } }