Main.java :  » Game » linksheep » com » xxj » linksheep » Android Open Source

Android Open Source » Game » linksheep 
linksheep » com » xxj » linksheep » Main.java
package com.xxj.linksheep;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

import com.xxj.fflink.GameConst;
import com.xxj.fflink.GameView;
import com.xxj.fflink.R;
import com.xxj.fflink.activity.GameSetting;
import com.xxj.fflink.activity.GameTopScore;

public class Main extends Activity {

  private static final long serialVersionUID = 1L;

  // : \\\
  public static final int GAME_START = 0;
  public static final int GAME_WIN = 1;
  public static final int GAME_LOST = 2;
  public static final int GAME_PAUSE = 3;

  // 
  public static int gameState = GAME_START;

  // 
  private GameView gameView;
  private HelpView helpView;
  private MusicSwitchView musicSwitchView;

  private Intent intent = new Intent();

  private static MediaPlayer backMusicMP;
  private static MediaPlayer clickMP;

  // public long score;

  @Override
  public void onCreate(Bundle icius) {
    super.onCreate(icius);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.initGame();
    Bundle bundle = this.getIntent().getExtras();
    if (null == bundle) {
      this.setContentView(musicSwitchView);
//       this.setContentView(new GameView(this, GameConst.GameLevel));
    } else {
      boolean isFirstRun = bundle.getBoolean(GameConst.isFirstRun);
      if (isFirstRun) {
        this.setContentView(musicSwitchView);
      } else {
        this.setContentView(new GameMenuView(this));
      }
    }
  }

  private void initGame() {
    musicSwitchView = new MusicSwitchView(this);
    helpView = new HelpView(this);
  }

  private void initBackMusicRes() {
    backMusicMP = Util.initMediaPlayer(this, R.raw.sheep_back, true);
  }

  private void initToneRes() {
    clickMP = Util.initMediaPlayer(this, R.raw.click, false);
  }

  @Override
  protected void onDestroy() {
    if (null != backMusicMP) {
      backMusicMP.stop();
      backMusicMP.release();
      backMusicMP = null;
    }
    super.onDestroy();
  }

  /**
   * 
   */
  public void showMainMenu() {
    this.playClick();
    gameState = GAME_START;
    setContentView(new GameMenuView(this));
  }

  /**
   * 1.
   */
  public void startNewGame() {
    this.playClick();
    gameState = GAME_START;
    gameView = new GameView(this, GameConst.GameLevel);
    setContentView(gameView);
  }

  /**
   * 
   */
  public void startNextGame() {
    this.playClick();
    gameState = GAME_START;
    gameView = new GameView(this, GameConst.GameLevel++);
    setContentView(gameView);
  }

  /**
   * 2.
   */
  public void showOption() {
    this.playClick();
    intent.setClass(this, GameSetting.class);
    this.startActivity(intent);
  }

  /**
   * 3.
   */
  public void showTopScore() {
    this.playClick();
    intent.setClass(this, GameTopScore.class);
    this.startActivity(intent);
  }

  /**
   * 4.
   */
  public void showGameHelp() {
    this.playClick();
    this.setContentView(helpView);
  }

  /**
   * 5.
   */
  public void showMoreGame() {
    this.playClick();
  }

  /**
   * 6.
   */
  public void exitGame() {
    this.finish();
  }

  public void showGameWin(float score) {
    this.setContentView(new GameResultView(this, score, true));
  }

  public void showGameLost(float score) {
    this.setContentView(new GameResultView(this, score, false));
  }

  /**
   * 
   */
  public void switchBackMusic() {
    if (GameConst.isSound) {
      this.initBackMusicRes();
      if (null != backMusicMP && !backMusicMP.isPlaying()) {
        System.out.println("backMusicMP.start();");
        backMusicMP.start();
      }
    } else {
      if (null != backMusicMP && backMusicMP.isPlaying()) {
        backMusicMP.pause();
      }
    }
  }

  /**
   * 
   */
  public void playClick() {
    this.initToneRes();
    if (null != clickMP && !clickMP.isPlaying()) {
      System.out.println("clickMP.start();");
      clickMP.start();
    }
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.