DEADSOFTWARE

License project under GNU GPL 3.
[LongFlight.git] / src / code / kalter / longflight / space / Planet.java
1 package code.kalter.longflight.space;
3 import code.kalter.longflight.Loader;
4 import code.kalter.longflight.LongFlight;
5 import java.io.IOException;
6 import java.util.Random;
7 import javax.microedition.lcdui.Graphics;
8 import javax.microedition.lcdui.Image;
10 /**
11 * Генерация случайных планет
12 *
13 * @author KalterFive
14 */
15 class Planet {
17 private final int screenW;
18 private final int screenH;
19 private final int COUNT = 5;
21 private final Random random;
22 private final Image[] planetGraphics; // все планеты
23 //=======================
24 private long lastTime; // для засекания времени
25 private long delay; // задержка для генерации планеты
26 //=======================
27 private int positionX;
28 private int positionY;
29 private int index;
31 public Planet(int screenW, int screenH) throws IOException {
32 this.screenW = screenW;
33 this.screenH = screenH;
34 random = new Random();
35 positionX = random.nextInt(screenW - 60);
36 positionY = -100;
37 index = random.nextInt(COUNT);
38 planetGraphics = new Image[COUNT];
39 Loader iml = Loader.getInstance();
40 for (int i = 0; i < COUNT; i++) {
41 planetGraphics[i] = iml.getImage("/gfx/space/planet/" + i + ".png");
42 }
43 delay = random.nextInt(25000) + 10000;
44 lastTime = System.currentTimeMillis();
45 }
47 public void paint(Graphics graph) {
48 long delta = System.currentTimeMillis() - lastTime;
49 if (delta < delay) {
50 return;
51 }
52 graph.drawImage(planetGraphics[index], positionX, positionY, 0);
53 }
55 public void upd() {
56 long delta = System.currentTimeMillis() - lastTime;
57 if (delta < delay) {
58 return;
59 }
60 if (positionY++ > screenH) {
61 newPlanet();
62 }
63 }
65 private void newPlanet() {
66 for (int i = index; i == index; index = random.nextInt(COUNT));
67 delay = random.nextInt(25000) + 10000;
68 lastTime = System.currentTimeMillis();
69 positionX = random.nextInt(screenW - 60);
70 positionY = -planetGraphics[index].getHeight();
71 }
72 }