Advertisement
Guest User

Untitled

a guest
Feb 16th, 2012
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.60 KB | None | 0 0
  1. public class SurfacePanel extends DrawablePanel
  2. {
  3.     private GameStates currentState;
  4.     private GameStates oldState;
  5.    
  6.     private int width;
  7.     private int height;
  8.    
  9.     private FPSManager fps;
  10.     public InputManager im;
  11.     private SoundManager sm;
  12.     private PhysicsManager pm;
  13.     private MusicManager mm;
  14.    
  15.     private Sprite mainFox;
  16.    
  17.     private TitleScreen ts;
  18.     private ContinousScreen cous;
  19.     private PauseScreen ps;
  20.     private SinglePlayScreen sp;
  21.    
  22.     public static float scrollRate = -17.0f / 100.0f;
  23.    
  24.     // semi arbitrary
  25.     private Paint textPaint = new Paint();
  26.     private Sprite loadingStar;
  27.    
  28.     // might be changed to an image
  29.     private custString pauseText;
  30.    
  31.     public static float scale;
  32.     public static float startHeight;
  33.    
  34.     private HighScores highScores;
  35.    
  36.     @SuppressWarnings("unused")
  37.     private Context context;
  38.  
  39.     // construct our objects
  40.     public SurfacePanel(Context context)
  41.     {
  42.         super(context);
  43.        
  44.         LoadedResources.preLoad();
  45.        
  46.         this.context = context;
  47.         SurfacePanel.scale = getResources().getDisplayMetrics().density;
  48.        
  49.         //fix dat scale
  50.         int tempwidth = LoadedResources.getBackgroundONE(getResources()).getWidth();
  51.         if(tempwidth == 1072)
  52.             SurfacePanel.scale = 1.0f;
  53.         else if(tempwidth == 800)
  54.             SurfacePanel.scale = .75f;
  55.         else
  56.             SurfacePanel.scale = 1.5f;
  57.        
  58.         scrollRate = (-17.0f / 100.0f) / 1.5f * scale;
  59.        
  60.         Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
  61.         width = display.getWidth();
  62.         height = display.getHeight();
  63.        
  64.         fps = new FPSManager();
  65.         im = new InputManager();
  66.        
  67.         sm = new SoundManager(context);
  68.        
  69.         pm = new PhysicsManager(width, height);
  70.         pm.setScrollRate(scrollRate);
  71.        
  72.         mm = new MusicManager(context, R.raw.pulse);
  73.         mm.setLooping(true);
  74.         mm.addFade(new SoundFade(0, 0, 1, 3000));
  75.         mm.play(0);
  76.        
  77.         currentState = GameStates.Loading;
  78.         oldState = GameStates.TitleScreen;
  79.        
  80.         ts = new TitleScreen();
  81.         cous = new ContinousScreen(width, height);
  82.         ps = new PauseScreen();
  83.         sp = new SinglePlayScreen(width, height);
  84.        
  85.         pauseText = new custString(getResources(), "PAUSE", (int) (6 * scale), (int) (22 * scale));
  86.         pauseText.setSize((int) (20 * scale));
  87.        
  88.         // semi arbitrary
  89.         textPaint.setColor(Color.WHITE);
  90.         textPaint.setStrokeWidth(8);
  91.         textPaint.setStyle(Style.FILL);
  92.         textPaint.setAntiAlias(true);
  93.         textPaint.setTextSize(16 * scale);
  94.     }
  95.    
  96.     // load in our resources
  97.     public void onInitialize()
  98.     {
  99.         // really silly way of doing this
  100.         highScores = XMLHandler.readSerialFile("highscores", HighScores.class);
  101.        
  102.         if (highScores == null)
  103.             highScores = new HighScores();
  104.        
  105.         // originally in constructor
  106.         mainFox = XMLHandler.readSerialFile(getResources(), R.raw.foxmain, Sprite.class);
  107.         loadingStar = XMLHandler.readSerialFile(getResources(), R.raw.star, Sprite.class);
  108.  
  109.         LoadedResources.load(getResources());
  110.  
  111.         loadingStar.onInitialize(LoadedResources.getStar(getResources()), (int) (width / 2 - (25.0f / 1.5f * scale) / 2.0f), height / 2, (int)(25.0f / 1.5f * scale), (int)(24.0f  / 1.5f * scale));
  112.        
  113.         ts.onInitialize(getResources(), R.drawable.titlescreen, mm);
  114.         ps.onInitialize(getResources(), 0);
  115.        
  116.         pm.setPlayer(mainFox);
  117.         mainFox.onInitialize(LoadedResources.getMainFox(), sm, (int) (width / 3.0f), -100, (int)(82.0f / 1.5f * scale), (int)(54.0f / 1.5f * scale));
  118.         mainFox.setAnimation(CharStates.Running);
  119.        
  120.         startHeight = height - LoadedResources.getBackground1(getResources()).getHeight() - 100f / 1.5f * SurfacePanel.scale;
  121.        
  122.         smSetup();
  123.     }
  124.    
  125.     private void smSetup()
  126.     {
  127.         sm.addSound(1, R.raw.footstep);
  128.         sm.addSound(2, R.raw.pkup1);
  129.         sm.addSound(3, R.raw.death);
  130.         sm.addSound(4, R.raw.rumbling1);
  131.         sm.addSound(5, R.raw.landing);
  132.     }
  133.    
  134.     public void onUpdate(long gameTime)
  135.     {
  136.         fps.onUpdate(gameTime);
  137.         float delta = fps.getDelta();
  138.         sm.onUpdate(delta);
  139.         mm.onUpdate(delta);
  140.        
  141.         if (currentState == GameStates.TitleScreen)
  142.             onTitleScreen(delta);
  143.         else if (currentState == GameStates.SinglePlay)
  144.         {
  145.             mainFox.onUpdate(fps.getDelta());
  146.             pm.onUpdate(delta);
  147.             if (!sp.onUpdate(delta))
  148.             {
  149.                 currentState = GameStates.TitleScreen;
  150.                 oldState = GameStates.SinglePlay;
  151.             }
  152.             checkForUserPause();
  153.         }
  154.         else if (currentState == GameStates.Continous)
  155.         {
  156.             mainFox.onUpdate(delta);
  157.             pm.onUpdate(delta);
  158.             cous.onUpdate(delta);
  159.             checkForUserPause();
  160.         }
  161.         else if (currentState == GameStates.Pause)
  162.             onPauseScreen();
  163.         else if (currentState == GameStates.Loading)
  164.             onLoadingScreen(delta);
  165.     }
  166.    
  167.     public void onDraw(Canvas canvas)
  168.     {
  169.         super.onDraw(canvas);
  170.        
  171.         if (currentState == GameStates.TitleScreen)
  172.             ts.onDraw(canvas, currentState);
  173.         else if (currentState == GameStates.SinglePlay)
  174.         {
  175.             // BigAnimate.onDraw(canvas); now handled inside single play
  176.             sp.onDraw(canvas);
  177.             pauseText.onDraw(canvas);
  178.         }
  179.         else if (currentState == GameStates.Continous)
  180.         {
  181.             cous.onDraw(canvas);
  182.             pauseText.onDraw(canvas);
  183.             mainFox.onDraw(canvas);
  184.         }
  185.         else if (currentState == GameStates.Pause && oldState == GameStates.SinglePlay)
  186.         {
  187.             sp.onDraw(canvas);
  188.             ps.onDraw(canvas, GameStates.SinglePlay);
  189.         }
  190.         else if (currentState == GameStates.Pause && oldState == GameStates.Continous)
  191.         {
  192.             cous.onDraw(canvas);
  193.             ps.onDraw(canvas, GameStates.Continous);
  194.         }
  195.         else if (currentState == GameStates.Loading)
  196.             onDrawLoadingScreen(canvas);
  197.     }
  198.    
  199.     private void onLoadingScreen(float delta)
  200.     {
  201.         loadingStar.onUpdate(delta);
  202.        
  203.         if (oldState == GameStates.SinglePlay)
  204.             if (sp.getInitialized())
  205.             {
  206.                 oldState = GameStates.Loading;
  207.                 currentState = GameStates.SinglePlay;
  208.             }
  209.        
  210.         if (oldState == GameStates.TitleScreen)
  211.         {
  212.             if (mm.isLoaded() && sm.isAllLoaded())
  213.             // if(sm.isLoaded(0) == LoadStates.complete)
  214.             {
  215.                 oldState = GameStates.Loading;
  216.                 currentState = GameStates.TitleScreen;
  217.             }
  218.         }
  219.     }
  220.    
  221.     private void onDrawLoadingScreen(Canvas canvas)
  222.     {
  223.         loadingStar.onDraw(canvas);
  224.         canvas.drawText("Loading...", width / 2 - textPaint.measureText("Loading...") / 2, height / 2 + (25.0f / 1.5f * scale) + (24.0f / 1.5f * scale), textPaint);
  225.     }
  226.    
  227.     private void checkForUserPause()
  228.     {
  229.         for (int i = 0; i < im.fingerCount; i++)
  230.         {
  231.             if (im.getReleased(i))
  232.             {
  233.                 if (pauseText.fingertap((int) im.getY(i), (int) im.getY(i)))
  234.                 {
  235.                     oldState = currentState;
  236.                     currentState = GameStates.Pause;
  237.                     mm.setVolume(.20f);
  238.                 }
  239.             }
  240.         }
  241.     }
  242.    
  243.     // also special
  244.     private void onPauseScreen()
  245.     {
  246.         GameStates newState = GameStates.Pause;
  247.        
  248.         for (int i = 0; i < im.fingerCount; i++)
  249.         {
  250.             if (im.getReleased(i))
  251.             {
  252.                 newState = ps.onTouch((int) im.getX(i), (int) im.getY(i));
  253.                
  254.                 if (newState != GameStates.Pause)
  255.                 {
  256.                     if(oldState == GameStates.Continous)
  257.                         HighScores.addScore(cous.getScore());
  258.                    
  259.                     break;
  260.                 }
  261.             }
  262.         }
  263.        
  264.         if (newState == GameStates.Quit)
  265.         {
  266.             onUserQuit();
  267.         }
  268.         else if (newState == GameStates.TitleScreen)
  269.         {
  270.             if(oldState == GameStates.SinglePlay)
  271.                 sp.Release();
  272.            
  273.             oldState = GameStates.Pause;
  274.             currentState = GameStates.TitleScreen;
  275.             //mm.addFade(new SoundFade(0, 1, 0, 3000));
  276.             mm.ChangeSongs(R.raw.pulse, new SoundFade(0, 1, 0, 3000), new SoundFade(0, 0, 1, 3000));
  277.         }
  278.         else if (newState == GameStates.Resume)
  279.         {
  280.             currentState = oldState;
  281.             oldState = GameStates.Pause;
  282.             mm.setVolume(1f);
  283.         }
  284.     }
  285.    
  286.     // special cause it handles a lot of stuff.
  287.     // should really be inside of ts
  288.     private void onTitleScreen(float delta)
  289.     {
  290.         // sounds
  291.         ts.onUpdate(delta);
  292.        
  293.         GameStates newState = GameStates.TitleScreen;
  294.        
  295.         for (int i = 0; i < im.fingerCount; i++)
  296.         {
  297.             if (im.getReleased(i))
  298.             {
  299.                 newState = ts.onTouch((int) im.getX(i), (int) im.getY(i));
  300.                
  301.                 if (newState != GameStates.TitleScreen)
  302.                     break;
  303.             }
  304.         }
  305.        
  306.         if (newState == GameStates.Quit)
  307.         {
  308.             onUserQuit();
  309.         }
  310.         else if (newState == GameStates.SinglePlay)
  311.         {
  312.             purgeManagers();
  313.             sp = new SinglePlayScreen(width, height);
  314.             sp.onInitialize(getResources(), im, pm, sm, mm, R.raw.level, mainFox);
  315.             oldState = GameStates.SinglePlay;
  316.             currentState = GameStates.Loading;
  317.            
  318.             ts.titleScreenCurrentSong = 0;
  319.             ts.titleScreenSoundTime = 3000000;
  320.             mm.addFade(new SoundFade(0, 1, 0, 3000));
  321.         }
  322.         else if (newState == GameStates.Continous)
  323.         {
  324.             purgeManagers();
  325.             cous = new ContinousScreen(width, height);
  326.             cous.onInitialize(getResources(), im, pm, sm, mainFox);
  327.             oldState = GameStates.TitleScreen;
  328.             currentState = GameStates.Continous;
  329.            
  330.             mm.ChangeSongs(R.raw.catchinglightning, new SoundFade(0, 1, 0, 3000), new SoundFade(0, 0, 1, 3000));
  331.         }
  332.        
  333.         // ran out of time!
  334.         /*
  335.          * else if (newState == GameStates.HighScore) { oldState =
  336.          * GameStates.TitleScreen; currentState = GameStates.HighScore; }
  337.          */
  338.     }
  339.    
  340.     private void purgeManagers()
  341.     {
  342.         pm.purge();
  343.         // sm.purge();
  344.     }
  345.    
  346.     public void onUserPause()
  347.     {
  348.         if(this.currentState != GameStates.TitleScreen && this.currentState != GameStates.Loading && this.currentState != GameStates.Pause)
  349.         {
  350.             this.oldState = this.currentState;
  351.             this.currentState = GameStates.Pause;
  352.         }
  353.         else if(this.currentState == GameStates.Pause)
  354.         {
  355.             this.currentState = this.oldState;
  356.             this.oldState = GameStates.Pause;
  357.         }
  358.     }
  359.    
  360.     public void onScreenPause(SharedPreferences.Editor ed)
  361.     {
  362.         // when the game is paused by outside shit.
  363.         if(currentState != GameStates.Pause && currentState != GameStates.Loading)
  364.         {
  365.             this.oldState = this.currentState;
  366.             this.currentState = GameStates.Pause;
  367.         }
  368.        
  369.         mm.stop();
  370.         int currentSong = mm.getCurrentSong();
  371.         XMLHandler.writeSerialFile(highScores, "highscores");
  372.        
  373.         ed.putInt("currentSong", currentSong);
  374.         ed.putInt("oldState", this.oldState.ordinal());
  375.         ed.putInt("SPLevel", sp.getCurrentLevel());
  376.        
  377.         this.stopThread();
  378.     }
  379.    
  380.     public void onScreenResume(SharedPreferences ed)
  381.     {
  382.         //set the states first
  383.         //hand loading and title and others
  384.         //play through entire loaded game level 1
  385.         //fix sm
  386.        
  387.         int lastscreen = ed.getInt("oldState", GameStates.TitleScreen.ordinal());
  388.  
  389.         this.smSetup();
  390.        
  391.         if(lastscreen == GameStates.SinglePlay.ordinal())
  392.         {          
  393.             if(!sp.getInitialized())
  394.             {
  395.                 sp.setLevel(ed.getInt("SPLevel", 1));
  396.                 sp.onInitialize(getResources(), im, pm, sm, mm, R.raw.level, mainFox);
  397.                
  398.                 oldState = GameStates.SinglePlay;
  399.                 currentState = GameStates.Loading;
  400.                 mm.stop();
  401.             }      
  402.             else
  403.             {
  404.                 oldState = GameStates.SinglePlay;
  405.                 currentState = GameStates.Pause;
  406.                 mm.ChangeSongs(ed.getInt("currentSong",R.raw.pulse), null, new SoundFade(0, 0, 1, 3000));
  407.                 mm.play(0);
  408.             }
  409.         }
  410.         else if( lastscreen == GameStates.Continous.ordinal())
  411.         {
  412.             if(!cous.getInitialized())
  413.                 cous.onInitialize(getResources(), im, pm, sm, mainFox);
  414.            
  415.             currentState = GameStates.Pause;
  416.             oldState = GameStates.Continous;
  417.            
  418.             mm.ChangeSongs(R.raw.catchinglightning, null, new SoundFade(0, 0, 1, 3000));
  419.             mm.play(0);
  420.         }
  421.         else
  422.         {
  423.             currentState = GameStates.TitleScreen;
  424.             mm.ChangeSongs(ed.getInt("currentSong",R.raw.pulse), null, new SoundFade(0, 0, 1, 3000));
  425.             mm.play(0);
  426.         }  
  427.        
  428.         try
  429.         {
  430.         this.restartThread();
  431.         }
  432.         catch (Exception e)
  433.         {
  434.             Log.e("JAKOBERR", e.toString());
  435.         }
  436.     }
  437.    
  438.     public void onScreenQuit(SharedPreferences.Editor ed)
  439.     {
  440.         //still gotta save stuff
  441.        
  442.         sm.release();
  443.         mm.release();
  444.     }
  445.    
  446.     public void onUserQuit()
  447.     {
  448.         RunfoxrunActivity.ed.clear();
  449.         RunfoxrunActivity.ed.commit();
  450.        
  451.         mm.stop();
  452.         stopThread();
  453.         XMLHandler.writeSerialFile(highScores, "highscores");
  454.         System.exit(0);//does NOT call onScreenQuit
  455.     }
  456.    
  457. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement