package code.kalter.longflight; import code.kalter.longflight.screen.About; import code.kalter.longflight.screen.Crash; import code.kalter.longflight.screen.EnableSound; import code.kalter.longflight.screen.Game; import code.kalter.longflight.screen.HighScore; import code.kalter.longflight.screen.Loading; import code.kalter.longflight.screen.Menu; import code.kalter.longflight.screen.Pause; import code.kalter.longflight.screen.Screen; import code.kalter.longflight.screen.SelectShip; import code.kalter.longflight.screen.Splash; import java.io.IOException; import javax.microedition.lcdui.Display; import javax.microedition.media.MediaException; import javax.microedition.midlet.MIDlet; /** * Главный класс * * @author KalterFive */ public final class LongFlight extends MIDlet { public static LongFlight link; // screens public static final int SPLASH = 0; public static final int GAME = 1; public static final int MENU = 2; public static final int SELECT_SHIP = 3; public static final int ABOUT = 4; public static final int HIGH_SCORE = 5; public static final int LOADING = 6; public static final int PAUSE = 7; public static final int ENABLE_SOUND = 8; public static final int CRASH = 9; private Sound music; private Screen[] screen; private int screenID; private int previosScreenID; public LongFlight() { screenID = SPLASH; link = this; try { screen = new Screen[]{ new Splash(), new Game(), new Menu(), new SelectShip(), new About(), new HighScore(), new Loading(), new Pause(), new EnableSound(), new Crash() }; } catch (Exception e) { catchException(e); } } // override public void startApp() { Display.getDisplay(this).setCurrent(screen[ENABLE_SOUND]); screen[ENABLE_SOUND].start(); } // override public void pauseApp() { // TODO } // override public void destroyApp(boolean unconditional) { try { if (music != null) { music.stop(); } } catch (MediaException e) { catchException(e); } notifyDestroyed(); } public void catchException(Exception e) { if (screen != null) { screen[screenID].stop(); } System.out.println(e.getMessage()); e.printStackTrace(); destroyApp(true); } public void setScreen(int screenID) { previosScreenID = this.screenID; screen[this.screenID].stop(); screen[screenID].start(); this.screenID = screenID; Display.getDisplay(this).setCurrent(screen[screenID]); } // принудительно, то есть без остановки текущего public void $setScreen(int screenID) { previosScreenID = this.screenID; screen[this.screenID = screenID].start(); Display.getDisplay(this).setCurrent(screen[screenID]); } public int getPreviosScreenID() { return previosScreenID; } // для облегчения жизни public void sleep(long millis) { try { Thread.sleep(millis); } catch (InterruptedException interruptedException) { catchException(interruptedException); } } public void playMusic() { try { music = new Sound() {{ add("/song/monster.mid", "audio/midi"); add("/song/mobscene.mid", "audio/midi"); add("/song/iha.mid", "audio/midi"); play(); }}; } catch (IOException e) { catchException(e); } catch (MediaException e) { catchException(e); } } }