package code.kalter.longflight.space; import code.kalter.longflight.Loader; import code.kalter.longflight.LongFlight; import java.io.IOException; import java.util.Random; import javax.microedition.lcdui.Graphics; import javax.microedition.lcdui.Image; /** * Генерация случайных планет * * @author KalterFive */ class Planet { private final int screenW; private final int screenH; private final int COUNT = 5; private final Random random; private final Image[] planetGraphics; // все планеты //======================= private long lastTime; // для засекания времени private long delay; // задержка для генерации планеты //======================= private int positionX; private int positionY; private int index; public Planet(int screenW, int screenH) throws IOException { this.screenW = screenW; this.screenH = screenH; random = new Random(); positionX = random.nextInt(screenW - 60); positionY = -100; index = random.nextInt(COUNT); planetGraphics = new Image[COUNT]; Loader iml = Loader.getInstance(); for (int i = 0; i < COUNT; i++) { planetGraphics[i] = iml.getImage("/gfx/space/planet/" + i + ".png"); } delay = random.nextInt(25000) + 10000; lastTime = System.currentTimeMillis(); } public void paint(Graphics graph) { long delta = System.currentTimeMillis() - lastTime; if (delta < delay) { return; } graph.drawImage(planetGraphics[index], positionX, positionY, 0); } public void upd() { long delta = System.currentTimeMillis() - lastTime; if (delta < delay) { return; } if (positionY++ > screenH) { newPlanet(); } } private void newPlanet() { for (int i = index; i == index; index = random.nextInt(COUNT)); delay = random.nextInt(25000) + 10000; lastTime = System.currentTimeMillis(); positionX = random.nextInt(screenW - 60); positionY = -planetGraphics[index].getHeight(); } }