DEADSOFTWARE

License project under GNU GPL 3.
[LongFlight.git] / src / code / kalter / longflight / LongFlight.java
1 package code.kalter.longflight;
3 import code.kalter.longflight.screen.About;
4 import code.kalter.longflight.screen.Crash;
5 import code.kalter.longflight.screen.EnableSound;
6 import code.kalter.longflight.screen.Game;
7 import code.kalter.longflight.screen.HighScore;
8 import code.kalter.longflight.screen.Loading;
9 import code.kalter.longflight.screen.Menu;
10 import code.kalter.longflight.screen.Pause;
11 import code.kalter.longflight.screen.Screen;
12 import code.kalter.longflight.screen.SelectShip;
13 import code.kalter.longflight.screen.Splash;
14 import java.io.IOException;
15 import javax.microedition.lcdui.Display;
16 import javax.microedition.media.MediaException;
17 import javax.microedition.midlet.MIDlet;
19 /**
20 * Главный класс
21 *
22 * @author KalterFive
23 */
24 public final class LongFlight extends MIDlet {
26 public static LongFlight link;
28 // screens
29 public static final int SPLASH = 0;
30 public static final int GAME = 1;
31 public static final int MENU = 2;
32 public static final int SELECT_SHIP = 3;
33 public static final int ABOUT = 4;
34 public static final int HIGH_SCORE = 5;
35 public static final int LOADING = 6;
36 public static final int PAUSE = 7;
37 public static final int ENABLE_SOUND = 8;
38 public static final int CRASH = 9;
40 private Sound music;
41 private Screen[] screen;
42 private int screenID;
43 private int previosScreenID;
45 public LongFlight() {
46 screenID = SPLASH;
47 link = this;
48 try {
49 screen = new Screen[]{
50 new Splash(),
51 new Game(),
52 new Menu(),
53 new SelectShip(),
54 new About(),
55 new HighScore(),
56 new Loading(),
57 new Pause(),
58 new EnableSound(),
59 new Crash()
60 };
61 } catch (Exception e) {
62 catchException(e);
63 }
64 }
66 // override
67 public void startApp() {
68 Display.getDisplay(this).setCurrent(screen[ENABLE_SOUND]);
69 screen[ENABLE_SOUND].start();
70 }
72 // override
73 public void pauseApp() {
74 // TODO
75 }
77 // override
78 public void destroyApp(boolean unconditional) {
79 try {
80 if (music != null) {
81 music.stop();
82 }
83 } catch (MediaException e) {
84 catchException(e);
85 }
86 notifyDestroyed();
87 }
89 public void catchException(Exception e) {
90 if (screen != null) {
91 screen[screenID].stop();
92 }
93 System.out.println(e.getMessage());
94 e.printStackTrace();
95 destroyApp(true);
96 }
98 public void setScreen(int screenID) {
99 previosScreenID = this.screenID;
100 screen[this.screenID].stop();
101 screen[screenID].start();
102 this.screenID = screenID;
103 Display.getDisplay(this).setCurrent(screen[screenID]);
106 // принудительно, то есть без остановки текущего
107 public void $setScreen(int screenID) {
108 previosScreenID = this.screenID;
109 screen[this.screenID = screenID].start();
110 Display.getDisplay(this).setCurrent(screen[screenID]);
113 public int getPreviosScreenID() {
114 return previosScreenID;
117 // для облегчения жизни
118 public void sleep(long millis) {
119 try {
120 Thread.sleep(millis);
121 } catch (InterruptedException interruptedException) {
122 catchException(interruptedException);
126 public void playMusic() {
127 try {
128 music = new Sound() {{
129 add("/song/monster.mid", "audio/midi");
130 add("/song/mobscene.mid", "audio/midi");
131 add("/song/iha.mid", "audio/midi");
132 play();
133 }};
134 } catch (IOException e) {
135 catchException(e);
136 } catch (MediaException e) {
137 catchException(e);