/* * MongMidlet.java * * Created on August 4, 2005, 10:43 PM */ package com.stgames.mong; import com.stgames.ISticktailGame; //#if JSR82 import com.stgames.bluetooth.BluetoothSerialPort; import com.stgames.bluetooth.BluetoothSerialPortListener; import com.stgames.bluetooth.ConnectionCanvas; import com.stgames.highscores.HighscoreConstants; //#endif import com.stgames.highscores.HighScoresCanvas; import com.stgames.highscores.HighscoreConstants; import com.stgames.highscores.IHighScores; import com.stgames.mong.splash.AboutScreen; import com.stgames.util.ConversionUtil; import java.io.IOException; import javax.microedition.media.Control; import javax.microedition.media.Manager; import javax.microedition.media.MediaException; import javax.microedition.media.Player; import javax.microedition.media.PlayerListener; import javax.microedition.media.control.VolumeControl; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import javax.microedition.rms.RecordStoreException; import com.stgames.mong.settings.MongSettings; /** * * @author Marcel Heijmans * @author Iwan Eising */ //#if !(JSR82) //# public class MongMIDlet extends MIDlet implements CommandListener, PlayerListener, IMongComm, ISticktailGame { //#else public class MongMIDlet extends MIDlet implements BluetoothSerialPortListener, CommandListener, PlayerListener, IMongComm, ISticktailGame { private BluetoothSerialPort bluetoothSerialPort = null; private ConnectionCanvas connCanvas; //#endif //#ifdef SCREEN_128x128 //# public static final String imageBase = "/res/128x128/"; //# public static final String imageExt = ".jpg"; //#elifdef SCREEN_176x208 //-- public static final String imageBase = "/res/176x208/"; //-- public static final String imageExt = ".jpg"; //#elifdef SAMSUNG //# public static final String imageBase = "/res/png208x208/"; //# public static final String imageExt = ".png"; //#elifdef SCREEN_352x416 //# public static final String imageBase = "/res/352x416/"; //# public static final String imageExt = ".jpg"; //#elifdef SCREEN_640x320 //# public static final String imageBase = "/res/640x320/"; //# public static final String imageExt = ".jpg"; //#else public static final String imageBase = "/res/208x208/"; public static final String imageExt = ".jpg"; //#endif private boolean connectToMong = false; private MongCanvas gCanvas; private MongSettings mongSettings = null; private Player backgroundMusicPlayer = null; private boolean playingTrack = false; /** Creates a new instance of MongMidlet */ public MongMIDlet() { getMongSettings(); } private Command exitCommand;//GEN-BEGIN:MVDFields private Command aboutCommand; private Command menuCommand; private Form helpForm; private Command helpCommand; private Form settingsForm; private Command settingsCommand; private Command singleCommand; private Command startCommand; private Command joinCommand; private Command stopCommand; private Command leaveCommand; private com.stgames.mong.menu.MenuCanvas menuCanvas; private Command cancelSettings; private Command okSettings; private TextField tfGamerName; private TextField tfGamerTag; private ChoiceGroup chbgrpLevel; private ChoiceGroup chbgrpMisc; private TextField tfNrPlayers; private com.stgames.mong.splash.SplashScreen splashScreen; private com.stgames.highscores.HighScoresCanvas highscoresScreen; private com.stgames.mong.GameOverCanvas gameOverScreen; private Form userInfoForm; private TextField tfUserInfoGamer; private TextField tfUserInfoGamerTag; private Command userInfoForm_ok; private TextField tfHighScoreURL; private ChoiceGroup chbgrpVolume;//GEN-END:MVDFields //GEN-LINE:MVDMethods /** This method initializes UI of the application.//GEN-BEGIN:MVDInitBegin */ private void initialize() {//GEN-END:MVDInitBegin // Insert pre-init code here getDisplay().setCurrent(get_splashScreen());//GEN-LINE:MVDInitInit // Insert post-init code here }//GEN-LINE:MVDInitEnd /** Called by the system to indicate that a command has been invoked on a particular displayable.//GEN-BEGIN:MVDCABegin * @param command the Command that ws invoked * @param displayable the Displayable on which the command was invoked */ public void commandAction(Command command, Displayable displayable) {//GEN-END:MVDCABegin // Insert global pre-action code here if (displayable == settingsForm) {//GEN-BEGIN:MVDCABody if (command == cancelSettings) {//GEN-END:MVDCABody // Insert pre-action code here getDisplay().setCurrent(get_menuCanvas());//GEN-LINE:MVDCAAction26 // Insert post-action code here } else if (command == okSettings) {//GEN-LINE:MVDCACase26 try { mongSettings.setGamer(tfGamerName.getString()); mongSettings.setGamerTag(tfGamerTag.getString()); mongSettings.setMaxPlayers(Integer.parseInt(tfNrPlayers.getString())); mongSettings.setLevel(chbgrpLevel.getSelectedIndex()); mongSettings.setVolume(chbgrpVolume.getSelectedIndex()); mongSettings.setClassicTheme(chbgrpMisc.isSelected(0)); mongSettings.setVibration(chbgrpMisc.isSelected(1)); mongSettings.setSound(chbgrpMisc.isSelected(2)); mongSettings.setOnlineStats(chbgrpMisc.isSelected(3)); mongSettings.setHighscoreURL(tfHighScoreURL.getString()); mongSettings.storeSettings(); playMongTrack(); } catch (RecordStoreException rse) { // TODO Show alert. } getDisplay().setCurrent(get_menuCanvas());//GEN-LINE:MVDCAAction28 // Insert post-action code here }//GEN-BEGIN:MVDCACase28 } else if (displayable == helpForm) { if (command == menuCommand) {//GEN-END:MVDCACase28 // Insert pre-action code here getDisplay().setCurrent(get_menuCanvas());//GEN-LINE:MVDCAAction9 // Insert post-action code here }//GEN-BEGIN:MVDCACase9 } else if (displayable == userInfoForm) { if (command == userInfoForm_ok) {//GEN-END:MVDCACase9 try { mongSettings.setGamer(tfUserInfoGamer.getString()); mongSettings.setGamerTag(tfUserInfoGamerTag.getString()); mongSettings.storeSettings(); } catch (RecordStoreException rse) { // TODO Show alert. } getDisplay().setCurrent(get_menuCanvas());//GEN-LINE:MVDCAAction54 // Insert post-action code here }//GEN-BEGIN:MVDCACase54 }//GEN-END:MVDCACase54 // Insert global post-action code here }//GEN-LINE:MVDCAEnd public javax.microedition.lcdui.Display getDisplay () {//GEN-FIRST:MVDGetDisplay return Display.getDisplay(this); }//GEN-LAST:MVDGetDisplay /** * This method should exit the midlet. */ public void exitMIDlet () {//GEN-FIRST:MVDExitMidlet }//GEN-LAST:MVDExitMidlet /** This method returns instance for exitCommand component and should be called instead of accessing exitCommand field directly.//GEN-BEGIN:MVDGetBegin3 * @return Instance for exitCommand component */ public Command get_exitCommand() { if (exitCommand == null) {//GEN-END:MVDGetBegin3 // Insert pre-init code here exitCommand = new Command("Exit", "Exit", Command.EXIT, 1);//GEN-LINE:MVDGetInit3 // Insert post-init code here }//GEN-BEGIN:MVDGetEnd3 return exitCommand; }//GEN-END:MVDGetEnd3 /** This method returns instance for aboutCommand component and should be called instead of accessing aboutCommand field directly.//GEN-BEGIN:MVDGetBegin5 * @return Instance for aboutCommand component */ public Command get_aboutCommand() { if (aboutCommand == null) {//GEN-END:MVDGetBegin5 // Insert pre-init code here aboutCommand = new Command("About", "About", Command.OK, 1);//GEN-LINE:MVDGetInit5 // Insert post-init code here }//GEN-BEGIN:MVDGetEnd5 return aboutCommand; }//GEN-END:MVDGetEnd5 /** This method returns instance for menuCommand component and should be called instead of accessing menuCommand field directly.//GEN-BEGIN:MVDGetBegin6 * @return Instance for menuCommand component */ public Command get_menuCommand() { if (menuCommand == null) {//GEN-END:MVDGetBegin6 // Insert pre-init code here menuCommand = new Command("Back", "Back", Command.BACK, 1);//GEN-LINE:MVDGetInit6 // Insert post-init code here }//GEN-BEGIN:MVDGetEnd6 return menuCommand; }//GEN-END:MVDGetEnd6 /** This method returns instance for helpForm component and should be called instead of accessing helpForm field directly.//GEN-BEGIN:MVDGetBegin8 * @return Instance for helpForm component */ public Form get_helpForm() { if (helpForm == null) {//GEN-END:MVDGetBegin8 // Insert pre-init code here helpForm = new Form("Instruction", new Item[0]);//GEN-BEGIN:MVDGetInit8 helpForm.addCommand(get_menuCommand()); helpForm.setCommandListener(this);//GEN-END:MVDGetInit8 // Insert post-init code here }//GEN-BEGIN:MVDGetEnd8 return helpForm; }//GEN-END:MVDGetEnd8 /** This method returns instance for helpCommand component and should be called instead of accessing helpCommand field directly.//GEN-BEGIN:MVDGetBegin10 * @return Instance for helpCommand component */ public Command get_helpCommand() { if (helpCommand == null) {//GEN-END:MVDGetBegin10 // Insert pre-init code here helpCommand = new Command("Help", "Instructions", Command.OK, 1);//GEN-LINE:MVDGetInit10 // Insert post-init code here }//GEN-BEGIN:MVDGetEnd10 return helpCommand; }//GEN-END:MVDGetEnd10 /** This method returns instance for settingsForm component and should be called instead of accessing settingsForm field directly.//GEN-BEGIN:MVDGetBegin11 * @return Instance for settingsForm component */ public Form get_settingsForm() { if (settingsForm == null) {//GEN-END:MVDGetBegin11 // Insert pre-init code here settingsForm = new Form("Settings", new Item[] {//GEN-BEGIN:MVDGetInit11 get_tfGamerName(), get_tfGamerTag(), get_chbgrpLevel(), get_chbgrpMisc(), get_chbgrpVolume(), get_tfHighScoreURL(), get_tfNrPlayers() }); settingsForm.addCommand(get_cancelSettings()); settingsForm.addCommand(get_okSettings()); settingsForm.setCommandListener(this);//GEN-END:MVDGetInit11 // Insert post-init code here }//GEN-BEGIN:MVDGetEnd11 return settingsForm; }//GEN-END:MVDGetEnd11 /** This method returns instance for settingsCommand component and should be called instead of accessing settingsCommand field directly.//GEN-BEGIN:MVDGetBegin12 * @return Instance for settingsCommand component */ public Command get_settingsCommand() { if (settingsCommand == null) {//GEN-END:MVDGetBegin12 // Insert pre-init code here settingsCommand = new Command("Settings", "Settings", Command.OK, 1);//GEN-LINE:MVDGetInit12 // Insert post-init code here }//GEN-BEGIN:MVDGetEnd12 return settingsCommand; }//GEN-END:MVDGetEnd12 /** This method returns instance for singleCommand component and should be called instead of accessing singleCommand field directly.//GEN-BEGIN:MVDGetBegin13 * @return Instance for singleCommand component */ public Command get_singleCommand() { if (singleCommand == null) {//GEN-END:MVDGetBegin13 // Insert pre-init code here singleCommand = new Command("Play", "Play", Command.OK, 1);//GEN-LINE:MVDGetInit13 // Insert post-init code here }//GEN-BEGIN:MVDGetEnd13 return singleCommand; }//GEN-END:MVDGetEnd13 /** This method returns instance for startCommand component and should be called instead of accessing startCommand field directly.//GEN-BEGIN:MVDGetBegin15 * @return Instance for startCommand component */ public Command get_startCommand() { if (startCommand == null) {//GEN-END:MVDGetBegin15 // Insert pre-init code here startCommand = new Command("Start", "Start Mong", Command.OK, 1);//GEN-LINE:MVDGetInit15 // Insert post-init code here }//GEN-BEGIN:MVDGetEnd15 return startCommand; }//GEN-END:MVDGetEnd15 /** This method returns instance for joinCommand component and should be called instead of accessing joinCommand field directly.//GEN-BEGIN:MVDGetBegin18 * @return Instance for joinCommand component */ public Command get_joinCommand() { if (joinCommand == null) {//GEN-END:MVDGetBegin18 // Insert pre-init code here joinCommand = new Command("Join", "Join Mong", Command.OK, 1);//GEN-LINE:MVDGetInit18 // Insert post-init code here }//GEN-BEGIN:MVDGetEnd18 return joinCommand; }//GEN-END:MVDGetEnd18 /** This method returns instance for stopCommand component and should be called instead of accessing stopCommand field directly.//GEN-BEGIN:MVDGetBegin20 * @return Instance for stopCommand component */ public Command get_stopCommand() { if (stopCommand == null) {//GEN-END:MVDGetBegin20 // Insert pre-init code here stopCommand = new Command("Stop", "Stop waiting", Command.BACK, 1);//GEN-LINE:MVDGetInit20 // Insert post-init code here }//GEN-BEGIN:MVDGetEnd20 return stopCommand; }//GEN-END:MVDGetEnd20 /** This method returns instance for leaveCommand component and should be called instead of accessing leaveCommand field directly.//GEN-BEGIN:MVDGetBegin22 * @return Instance for leaveCommand component */ public Command get_leaveCommand() { if (leaveCommand == null) {//GEN-END:MVDGetBegin22 // Insert pre-init code here leaveCommand = new Command("Back", Command.BACK, 1);//GEN-LINE:MVDGetInit22 // Insert post-init code here }//GEN-BEGIN:MVDGetEnd22 return leaveCommand; }//GEN-END:MVDGetEnd22 /** This method returns instance for menuCanvas component and should be called instead of accessing menuCanvas field directly.//GEN-BEGIN:MVDGetBegin24 * @return Instance for menuCanvas component */ public com.stgames.mong.menu.MenuCanvas get_menuCanvas() { if (menuCanvas == null) {//GEN-END:MVDGetBegin24 // Insert pre-init code here menuCanvas = new com.stgames.mong.menu.MenuCanvas(this);//GEN-LINE:MVDGetInit24 menuCanvas.showMenu(); }//GEN-BEGIN:MVDGetEnd24 return menuCanvas; }//GEN-END:MVDGetEnd24 /** This method returns instance for cancelSettings component and should be called instead of accessing cancelSettings field directly.//GEN-BEGIN:MVDGetBegin25 * @return Instance for cancelSettings component */ public Command get_cancelSettings() { if (cancelSettings == null) {//GEN-END:MVDGetBegin25 // Insert pre-init code here cancelSettings = new Command("Cancel", Command.CANCEL, 1);//GEN-LINE:MVDGetInit25 // Insert post-init code here }//GEN-BEGIN:MVDGetEnd25 return cancelSettings; }//GEN-END:MVDGetEnd25 /** This method returns instance for okSettings component and should be called instead of accessing okSettings field directly.//GEN-BEGIN:MVDGetBegin27 * @return Instance for okSettings component */ public Command get_okSettings() { if (okSettings == null) {//GEN-END:MVDGetBegin27 // Insert pre-init code here okSettings = new Command("Save", Command.OK, 1);//GEN-LINE:MVDGetInit27 // Insert post-init code here }//GEN-BEGIN:MVDGetEnd27 return okSettings; }//GEN-END:MVDGetEnd27 /** This method returns instance for tfGamerName component and should be called instead of accessing tfGamerName field directly.//GEN-BEGIN:MVDGetBegin29 * @return Instance for tfGamerName component */ public TextField get_tfGamerName() { if (tfGamerName == null) {//GEN-END:MVDGetBegin29 // Insert pre-init code here tfGamerName = new TextField("Gamer name", null, 12, TextField.ANY);//GEN-LINE:MVDGetInit29 if ((mongSettings.getGamer() != null) && (mongSettings.getGamer().length() > 0) && (!"STG".equals(mongSettings.getGamer()))) { tfGamerName.setString(mongSettings.getGamer()); } else { tfGamerName.setString("Sticktail"); } }//GEN-BEGIN:MVDGetEnd29 return tfGamerName; }//GEN-END:MVDGetEnd29 /** This method returns instance for tfGamerTag component and should be called instead of accessing tfGamerTag field directly.//GEN-BEGIN:MVDGetBegin30 * @return Instance for tfGamerTag component */ public TextField get_tfGamerTag() { if (tfGamerTag == null) {//GEN-END:MVDGetBegin30 // Insert pre-init code here tfGamerTag = new TextField("Gamer Tag", null, 3, TextField.ANY);//GEN-LINE:MVDGetInit30 if ((mongSettings.getGamerTag() != null) && (mongSettings.getGamerTag().length() > 0) && (!"STG".equals(mongSettings.getGamerTag()))) { tfGamerTag.setString(mongSettings.getGamerTag()); } else { tfGamerTag.setString("STG"); } }//GEN-BEGIN:MVDGetEnd30 return tfGamerTag; }//GEN-END:MVDGetEnd30 /** This method returns instance for chbgrpLevel component and should be called instead of accessing chbgrpLevel field directly.//GEN-BEGIN:MVDGetBegin31 * @return Instance for chbgrpLevel component */ public ChoiceGroup get_chbgrpLevel() { if (chbgrpLevel == null) {//GEN-END:MVDGetBegin31 // Insert pre-init code here chbgrpLevel = new ChoiceGroup("Difficulty level\n", Choice.EXCLUSIVE, new String[] {//GEN-BEGIN:MVDGetInit31 "Easy", "Normal", "Hard", "Extreme" }, new Image[] { null, null, null, null }); chbgrpLevel.setSelectedFlags(new boolean[] { false, true, false, false });//GEN-END:MVDGetInit31 chbgrpLevel.setSelectedIndex(0,mongSettings.getLevel()==HighscoreConstants.LEVEL_EASY); chbgrpLevel.setSelectedIndex(1,mongSettings.getLevel()==HighscoreConstants.LEVEL_NORMAL); chbgrpLevel.setSelectedIndex(2,mongSettings.getLevel()==HighscoreConstants.LEVEL_HARD); chbgrpLevel.setSelectedIndex(3,mongSettings.getLevel()==HighscoreConstants.LEVEL_EXTREME); }//GEN-BEGIN:MVDGetEnd31 return chbgrpLevel; }//GEN-END:MVDGetEnd31 /** This method returns instance for chbgrpMisc component and should be called instead of accessing chbgrpMisc field directly.//GEN-BEGIN:MVDGetBegin36 * @return Instance for chbgrpMisc component */ public ChoiceGroup get_chbgrpMisc() { if (chbgrpMisc == null) {//GEN-END:MVDGetBegin36 // Insert pre-init code here chbgrpMisc = new ChoiceGroup("Various settings", Choice.MULTIPLE, new String[] {//GEN-BEGIN:MVDGetInit36 "Classic theme", "Enable vibration", "Enable sound", "Online statistics" }, new Image[] { null, null, null, null }); chbgrpMisc.setSelectedFlags(new boolean[] { false, false, false, false });//GEN-END:MVDGetInit36 boolean[] selections = {mongSettings.isClassicTheme(), mongSettings.isVibration(), mongSettings.isSound(), mongSettings.isOnlineStats()}; chbgrpMisc.setSelectedFlags(selections); }//GEN-BEGIN:MVDGetEnd36 return chbgrpMisc; }//GEN-END:MVDGetEnd36 /** This method returns instance for tfNrPlayers component and should be called instead of accessing tfNrPlayers field directly.//GEN-BEGIN:MVDGetBegin39 * @return Instance for tfNrPlayers component */ public TextField get_tfNrPlayers() { if (tfNrPlayers == null) {//GEN-END:MVDGetBegin39 // Insert pre-init code here tfNrPlayers = new TextField("Max nr players", null, 1, TextField.NUMERIC);//GEN-LINE:MVDGetInit39 tfNrPlayers.setString(""+mongSettings.getMaxPlayers()); }//GEN-BEGIN:MVDGetEnd39 return tfNrPlayers; }//GEN-END:MVDGetEnd39 /** This method returns instance for splashScreen component and should be called instead of accessing splashScreen field directly.//GEN-BEGIN:MVDGetBegin40 * @return Instance for splashScreen component */ public com.stgames.mong.splash.SplashScreen get_splashScreen() { if (splashScreen == null) {//GEN-END:MVDGetBegin40 // Insert pre-init code here Displayable nextScreen = get_menuCanvas(); String gamerTag = mongSettings.getGamerTag(); if ((gamerTag == null) || (gamerTag.length() <= 0) || ("STG".equals(gamerTag))) { nextScreen = get_userInfoForm(); } splashScreen = new com.stgames.mong.splash.SplashScreen(getDisplay(), nextScreen);//GEN-LINE:MVDGetInit40 // Insert post-init code here }//GEN-BEGIN:MVDGetEnd40 return splashScreen; }//GEN-END:MVDGetEnd40 /** This method returns instance for highscoresScreen component and should be called instead of accessing highscoresScreen field directly.//GEN-BEGIN:MVDGetBegin41 * @return Instance for highscoresScreen component */ public com.stgames.highscores.HighScoresCanvas get_highscoresScreen() { if (highscoresScreen == null) {//GEN-END:MVDGetBegin41 // Insert pre-init code here highscoresScreen = new com.stgames.highscores.HighScoresCanvas(null, null, null, null, null);//GEN-LINE:MVDGetInit41 // Insert post-init code here }//GEN-BEGIN:MVDGetEnd41 return highscoresScreen; }//GEN-END:MVDGetEnd41 /** This method returns instance for gameOverScreen component and should be called instead of accessing gameOverScreen field directly.//GEN-BEGIN:MVDGetBegin43 * @return Instance for gameOverScreen component */ public com.stgames.mong.GameOverCanvas get_gameOverScreen() { if (gameOverScreen == null) {//GEN-END:MVDGetBegin43 // Insert pre-init code here gameOverScreen = new com.stgames.mong.GameOverCanvas(this);//GEN-LINE:MVDGetInit43 // Insert post-init code here }//GEN-BEGIN:MVDGetEnd43 return gameOverScreen; }//GEN-END:MVDGetEnd43 /** This method returns instance for userInfoForm component and should be called instead of accessing userInfoForm field directly.//GEN-BEGIN:MVDGetBegin49 * @return Instance for userInfoForm component */ public Form get_userInfoForm() { if (userInfoForm == null) {//GEN-END:MVDGetBegin49 // Insert pre-init code here userInfoForm = new Form("User settings", new Item[] {//GEN-BEGIN:MVDGetInit49 get_tfUserInfoGamer(), get_tfUserInfoGamerTag() }); userInfoForm.addCommand(get_userInfoForm_ok()); userInfoForm.setCommandListener(this);//GEN-END:MVDGetInit49 // Insert post-init code here }//GEN-BEGIN:MVDGetEnd49 return userInfoForm; }//GEN-END:MVDGetEnd49 /** This method returns instance for tfUserInfoGamer component and should be called instead of accessing tfUserInfoGamer field directly.//GEN-BEGIN:MVDGetBegin51 * @return Instance for tfUserInfoGamer component */ public TextField get_tfUserInfoGamer() { if (tfUserInfoGamer == null) {//GEN-END:MVDGetBegin51 // Insert pre-init code here tfUserInfoGamer = new TextField("Gamer name", null, 12, TextField.ANY);//GEN-LINE:MVDGetInit51 if ((mongSettings.getGamer() != null) && (mongSettings.getGamer().length() > 0) && (!"STG".equals(mongSettings.getGamer()))) { tfUserInfoGamer.setString(mongSettings.getGamer()); } else { tfUserInfoGamer.setString("Sticktail"); } }//GEN-BEGIN:MVDGetEnd51 return tfUserInfoGamer; }//GEN-END:MVDGetEnd51 /** This method returns instance for tfUserInfoGamerTag component and should be called instead of accessing tfUserInfoGamerTag field directly.//GEN-BEGIN:MVDGetBegin52 * @return Instance for tfUserInfoGamerTag component */ public TextField get_tfUserInfoGamerTag() { if (tfUserInfoGamerTag == null) {//GEN-END:MVDGetBegin52 // Insert pre-init code here tfUserInfoGamerTag = new TextField("Gamer tag", null, 3, TextField.ANY | TextField.NON_PREDICTIVE);//GEN-LINE:MVDGetInit52 if ((mongSettings.getGamerTag() != null) && (mongSettings.getGamerTag().length() > 0) && (!"STG".equals(mongSettings.getGamerTag()))) { tfUserInfoGamerTag.setString(mongSettings.getGamerTag()); } else { tfUserInfoGamerTag.setString("STG"); } // Insert post-init code here }//GEN-BEGIN:MVDGetEnd52 return tfUserInfoGamerTag; }//GEN-END:MVDGetEnd52 /** This method returns instance for userInfoForm_ok component and should be called instead of accessing userInfoForm_ok field directly.//GEN-BEGIN:MVDGetBegin53 * @return Instance for userInfoForm_ok component */ public Command get_userInfoForm_ok() { if (userInfoForm_ok == null) {//GEN-END:MVDGetBegin53 // Insert pre-init code here userInfoForm_ok = new Command("Save", Command.OK, 1);//GEN-LINE:MVDGetInit53 // Insert post-init code here }//GEN-BEGIN:MVDGetEnd53 return userInfoForm_ok; }//GEN-END:MVDGetEnd53 /** This method returns instance for tfHighScoreURL component and should be called instead of accessing tfHighScoreURL field directly.//GEN-BEGIN:MVDGetBegin63 * @return Instance for tfHighScoreURL component */ public TextField get_tfHighScoreURL() { if (tfHighScoreURL == null) {//GEN-END:MVDGetBegin63 // Insert pre-init code here tfHighScoreURL = new TextField("High-score URL", null, 255, TextField.URL);//GEN-LINE:MVDGetInit63 if ((mongSettings.getHighscoreURL() != null) && (mongSettings.getHighscoreURL().length() > 0)) { tfHighScoreURL.setString(mongSettings.getHighscoreURL()); } }//GEN-BEGIN:MVDGetEnd63 return tfHighScoreURL; }//GEN-END:MVDGetEnd63 /** This method returns instance for chbgrpVolume component and should be called instead of accessing chbgrpVolume field directly.//GEN-BEGIN:MVDGetBegin64 * @return Instance for chbgrpVolume component */ public ChoiceGroup get_chbgrpVolume() { if (chbgrpVolume == null) {//GEN-END:MVDGetBegin64 // Insert pre-init code here chbgrpVolume = new ChoiceGroup("Volume", Choice.EXCLUSIVE, new String[] {//GEN-BEGIN:MVDGetInit64 "Whisper mode", "Something in between", "Screaming loud" }, new Image[] { null, null, null }); chbgrpVolume.setSelectedFlags(new boolean[] { false, true, false });//GEN-END:MVDGetInit64 chbgrpVolume.setSelectedIndex(0,mongSettings.getLevel()==MongSettings.VOLUME_WHISPER); chbgrpVolume.setSelectedIndex(1,mongSettings.getLevel()==MongSettings.VOLUME_SOMETHING_IN_BETWEEN); chbgrpVolume.setSelectedIndex(2,mongSettings.getLevel()==MongSettings.VOLUME_SCREAMING_LOUD); // Insert post-init code here }//GEN-BEGIN:MVDGetEnd64 return chbgrpVolume; }//GEN-END:MVDGetEnd64 public void startApp() { if (gCanvas != null) { try { if ((backgroundMusicPlayer != null) && (backgroundMusicPlayer.getState() == Player.STARTED)) { backgroundMusicPlayer.start(); } } catch (MediaException ex) { backgroundMusicPlayer = null; } gCanvas.continueGame(); } else { initialize(); playMongTrack(); } } public void showMenu() { Display display = Display.getDisplay(this); display.setCurrent(get_menuCanvas()); } public void showGameoverSequence() { Display display = Display.getDisplay(this); display.setCurrent(get_gameOverScreen()); gameOverScreen.doGameOverSequence(); gameOverScreen = null; display.setCurrent(gCanvas); } public void showHighScoreScreen(IHighScores scores) { HighScoresCanvas screen = new HighScoresCanvas(Display.getDisplay(this),get_menuCanvas(), scores, imageBase, imageExt); } public void showAboutScreen() { AboutScreen screen = new AboutScreen(this,Display.getDisplay(this),get_menuCanvas()); } public void playSinglePlayerMong() { Display display = Display.getDisplay(this); MongSpace space = new MongSpace(this); gCanvas = new MongCanvas(this, -1, space); gCanvas.setSinglePlayerGame(true); gCanvas.start(); display.setCurrent(gCanvas); } public void doSettings() { Display display = Display.getDisplay(this); try { getMongSettings().loadSettings(); display.setCurrent(get_settingsForm()); } catch (RecordStoreException rse) { // TODO show allert. } } public void playMultiPlayerMong(boolean host) { if (host) { initBTServer(); } else { initBTClient(); } } public void pauseApp() { if (gCanvas != null) { try { if (backgroundMusicPlayer != null) { backgroundMusicPlayer.stop(); } } catch (MediaException ex) { backgroundMusicPlayer = null; } } gCanvas.pauseGame(); } public void destroyApp(boolean unconditional) { if ((backgroundMusicPlayer != null) && (backgroundMusicPlayer.getState() != Player.CLOSED)) { backgroundMusicPlayer.close(); } System.gc(); } protected void initBTServer() { //#if JSR82 connCanvas = new ConnectionCanvas(this, false); connCanvas.start(); getDisplay().setCurrent(connCanvas); if (bluetoothSerialPort == null) { bluetoothSerialPort = new BluetoothSerialPort(this, BluetoothSerialPort.BLUETOOTH_MASTER); } bluetoothSerialPort.open(); //#endif } protected void initBTClient() { //#if JSR82 connCanvas = new ConnectionCanvas(this, false); connCanvas.start(); getDisplay().setCurrent(connCanvas); bluetoothSerialPort = new BluetoothSerialPort(this, BluetoothSerialPort.BLUETOOTH_SLAVE); bluetoothSerialPort.open(); //#endif } //#if JSR82 public void connected(int index) { Display display = Display.getDisplay(this); MongSpace space = new MongSpace(this); // playerNumber int playerNumber = 1; if (bluetoothSerialPort.isMaster()) { playerNumber = 0; } connCanvas.setConnected(true); gCanvas = new MongCanvas(this, playerNumber, space); gCanvas.start(); display.setCurrent(gCanvas); connectToMong = true; } public void disconnected(int index) { showMenu(); } // Message Format public static final int MSG_COMMAND = 0; public static final byte CMD_GAME_SYNCH = 0x00; // 0000 0000 public static final byte CMD_GAME_START = 0x01; // 0000 0001 public static final byte CMD_GAME_OVER = 0x02; // 0000 0010 public static final byte CMD_GAME_END = 0x03; // 0000 0011 public static final byte MSG_PLAYER_ID = 1; // BYTE public static final byte MSG_PLAYERTAG = 2; // 3 BYTEs public static final byte CMD_GAME_JOIN = 0x04; // 0000 0100 public static final byte CMD_GAME_LEAVE = 0x05; // 0000 0101 public static final byte CMD_BALL = 0x10; // 0001 0000 public static final byte MSG_BALL_ID = 1; // BYTE public static final byte MSG_BALL_X = 2; // INT public static final byte MSG_BALL_Y = 3; // INT public static final byte MSG_BALL_DX = 4; // INT public static final byte MSG_BALL_DY = 5; // INT public static final byte CMD_SCORE = 0x20; // 0010 0000 // public static final byte MSG_PLAYER_ID = 1; // BYTE public static final byte MSG_SCORE = 2; // n INTs // public static final int COMM_SYNCH_STATE_IDX = 1; // public static final int COMM_SYNCH_PLAYER_IDX = 2; // public static final int COMM_SYNCH_SPACE = 3; // public static final int COMM_SYNCH_INFO = 4; public void onMessage(int index, byte[] message) { // Strip the command code; int[] param = ConversionUtil.byteArrayToIntArray(message, 1); switch(message[MSG_COMMAND]) { case CMD_BALL: param = ConversionUtil.byteArrayToIntArray(message, 2); // Message: BALL_ID, BALL_X, BALL_Y, DX, DY gCanvas.setBall((int)message[MSG_BALL_ID], param); break; case CMD_SCORE: // Message: PLAYER, SCORE gCanvas.setScores(param); break; case CMD_GAME_SYNCH: // Message: COMMAND break; case CMD_GAME_START: // Message: COMMAND break; case CMD_GAME_OVER: // Message: PLAYER break; case CMD_GAME_END: // Message: COMMAND break; case CMD_GAME_JOIN: // Message: PLAYER, PLAYER_TAG, SPACE break; case CMD_GAME_LEAVE: // Message: STATE, PLAYER, SPACE, INFO break; } } public boolean sendBallPosition(int ball, int toPlayer, int[] relativeBallPos) { byte[] message = ConversionUtil.intArrayToByteArray(relativeBallPos, 2); message[MSG_COMMAND] = CMD_BALL; message[MSG_BALL_ID] = (byte)ball; if (bluetoothSerialPort != null) { // bluetoothSerialPort.send(toPlayer, message); // TODO Mong should determine whic player should get the ball // Now just braodcast to all (which is one) bluetoothSerialPort.send(message); } return true; } public boolean sendScore(int player, int[] score) { byte[] message = ConversionUtil.intArrayToByteArray(score, 2); message[MSG_COMMAND] = CMD_SCORE; message[MSG_PLAYER_ID] = (byte)player; if (bluetoothSerialPort != null) { bluetoothSerialPort.send(message); } return true; } public boolean isConnectedToMong() { return connectToMong; } //#endif public MongSettings getMongSettings() { if (mongSettings == null) { mongSettings = new MongSettings(); try { mongSettings.loadSettings(); } catch (RecordStoreException rse) { mongSettings = new MongSettings(); } } return mongSettings; } public void setMongSettings(MongSettings mongSettings) { this.mongSettings = mongSettings; } public String getImageBase() { return imageBase; } public void startBackgroundPlayer() { if (backgroundMusicPlayer != null) { if (backgroundMusicPlayer.getState() != Player.STARTED) { try { backgroundMusicPlayer.start(); } catch (MediaException ex) { backgroundMusicPlayer = null; } finally { playingTrack = false; } } } } public void stopBackgroundPlayer() { if ((backgroundMusicPlayer != null) && (backgroundMusicPlayer.getState() == Player.STARTED)) { try { backgroundMusicPlayer.stop(); } catch (MediaException ex) { backgroundMusicPlayer = null; } } } public void loadMidiTrack(int track) { try { if (backgroundMusicPlayer != null) { backgroundMusicPlayer.close(); } backgroundMusicPlayer = Manager.createPlayer(getClass().getResourceAsStream("/res/sound/Mong_" + track + ".mid"), "audio/midi"); backgroundMusicPlayer.addPlayerListener(this); backgroundMusicPlayer.setLoopCount(-1); backgroundMusicPlayer.realize(); backgroundMusicPlayer.prefetch(); setVolume(); } catch (MediaException ex) { backgroundMusicPlayer = null; ex.printStackTrace(); } catch (IOException ex) { backgroundMusicPlayer = null; ex.printStackTrace(); } } protected void playMongTrack() { try { if ((mongSettings != null) && (mongSettings.isSound())) { if (!playingTrack) { if (backgroundMusicPlayer != null) { if (backgroundMusicPlayer.getState() != Player.CLOSED) { backgroundMusicPlayer.stop(); backgroundMusicPlayer.close(); } } backgroundMusicPlayer = Manager.createPlayer(getClass().getResourceAsStream("/res/sound/Mong_3.mid"), "audio/midi"); backgroundMusicPlayer.addPlayerListener(this); backgroundMusicPlayer.setLoopCount(-1); backgroundMusicPlayer.realize(); backgroundMusicPlayer.prefetch(); setVolume(); backgroundMusicPlayer.start(); playingTrack = true; } else { setVolume(); } } else { if (backgroundMusicPlayer != null) { backgroundMusicPlayer.stop(); backgroundMusicPlayer.close(); playingTrack = false; } } } catch (MediaException ex) { backgroundMusicPlayer = null; ex.printStackTrace(); } catch (IOException ex) { backgroundMusicPlayer = null; ex.printStackTrace(); } catch (Exception ex) { backgroundMusicPlayer = null; ex.printStackTrace(); } } protected void setVolume() { VolumeControl volumeControl = (VolumeControl) backgroundMusicPlayer.getControl("javax.microedition.media.control.VolumeControl"); if (volumeControl != null) { switch(mongSettings.getVolume()) { case MongSettings.VOLUME_WHISPER: volumeControl.setLevel(15); break; case MongSettings.VOLUME_SOMETHING_IN_BETWEEN: volumeControl.setLevel(50); break; case MongSettings.VOLUME_SCREAMING_LOUD: volumeControl.setLevel(95); break; } } } public void playerUpdate(Player player, String string, Object object) { } } /* * $Id: MongMIDlet.java,v 1.69 2006/01/09 21:12:06 iwan Exp $ * $Log: MongMIDlet.java,v $ * Revision 1.69 2006/01/09 21:12:06 iwan * Added volume settings * * Revision 1.68 2006/01/08 21:54:47 iwan * Updated background-melody implementation. * * Revision 1.67 2006/01/07 23:18:43 iwan * New midi files and new support in the game. * * Revision 1.66 2006/01/06 23:30:43 iwan * Now there's a background sound. * * Revision 1.65 2006/01/02 20:49:56 iwan * New AboutScreen implementation that draws the text on the display instead of using an image. * * Revision 1.64 2006/01/02 18:55:35 iwan * New support for 640x320 screens. * * Revision 1.63 2006/01/01 19:38:07 iwan * New HighScoreCanvas call. * * Revision 1.62 2005/12/13 18:32:26 iwan * Image optimizations * * Revision 1.61 2005/12/12 23:13:31 iwan * Support for Nokia N90 dimensions and now you can pauze the game because you were called. * * Revision 1.60 2005/12/07 21:07:37 iwan * start and pause app implemented * * Revision 1.59 2005/12/07 20:32:46 iwan * *** empty log message *** * * Revision 1.58 2005/11/27 21:39:46 iwan * Started working on the pauseApp functionality. * * Revision 1.57 2005/11/16 21:39:01 iwan * Implementing the new re-located highscore functionality * */