Android Open Source - Gamework Base Game Activity






From Project

Back to project page Gamework.

License

The source code is released under:

Apache License

If you think the Android project Gamework listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package cz.robyer.gamework.app.activity;
/*w  w  w. j  ava  2 s . c  o m*/
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
import cz.robyer.gamework.game.GameEvent;
import cz.robyer.gamework.game.GameEventListener;
import cz.robyer.gamework.game.GameStatus;
import cz.robyer.gamework.app.R;
import cz.robyer.gamework.app.game.GameService;

/**
 * This is base activity for all game activities.
 * @author Robert P?sel
 */
public abstract class BaseGameActivity extends BaseActivity implements GameEventListener {
  private static final String TAG = BaseGameActivity.class.getSimpleName();
  
  /** Helper for quitting activity by second press of 'back' */
  protected boolean backClicked = false;

  private AlertDialog ad;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    checkGameRunning();
  }
  
  @Override
  protected void onResume() {
    super.onResume();    
    checkGameRunning();
    overridePendingTransition(0,0); // disable animations

    /** Register listening to game events */
    if (GameService.isRunning())
      getGame().registerListener(this);

    // reset 'back' clicked helper
    backClicked = false;
  }
  
  @Override
  protected void onPause() {
    super.onPause();
    
    /** Unregister listening to game events */
    if (GameService.isRunning())
      getGame().unregisterListener(this);
    
    if (ad != null)
      ad.dismiss();
  }
  
  /**
   * If {@link GameService} is not running then finish this activity and start MainActivity.
   */
  protected void checkGameRunning() {
    if (!GameService.isRunning()) {
      Log.w(TAG, "Game is not running, quitting activity");
      Intent intent = new Intent(this, MainActivity.class);
      intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
      startActivity(intent);
      finish();
    } else if (getGame().getStatus() == GameStatus.GAME_LOST) {
      showGameWinLoseDialog(false);
    } else if (getGame().getStatus() == GameStatus.GAME_WON) {
      showGameWinLoseDialog(true);
    }
  }
  
  /*@Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.game_menu, menu);
    return true;
  }*/

  /**
   * Init buttons bar and handlers for buttons.
   */
  protected void initButtons() {

    final OnClickListener listener = new OnClickListener() {
      
      @Override
      public void onClick(View v) {
        Log.d("BaseGameActivity", "onClick buttons bar");
        Class<?> cls = null;
        if (v.getId() == R.id.btn_map)
          cls = GameMapActivity.class;
        else if (v.getId() == R.id.btn_messages)
          cls = GameMessagesActivity.class;
        else if (v.getId() == R.id.btn_tasks)
          cls = GameObjectivesActivity.class;
        else if (v.getId() == R.id.btn_inventory)
          cls = GameInventoryActivity.class;
        else if (v.getId() == R.id.btn_tools)
          cls = GameToolsActivity.class;
    
        // ignore unknown buttons and link to same activity
        if (cls == null || cls.isInstance(BaseGameActivity.this))
          return;
        
        Intent intent = new Intent(BaseGameActivity.this, cls);
        intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)    
            .addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
          
        startActivity(intent);
      }
    };
    
    View buttons = findViewById(R.id.buttonsbar);
    if (buttons != null) {
      buttons.findViewById(R.id.btn_map).setOnClickListener(listener);
      buttons.findViewById(R.id.btn_messages).setOnClickListener(listener);
      buttons.findViewById(R.id.btn_tasks).setOnClickListener(listener);
      buttons.findViewById(R.id.btn_inventory).setOnClickListener(listener);
      buttons.findViewById(R.id.btn_tools).setOnClickListener(listener);
    }
  }
  
  /* (non-Javadoc)
   * @see cz.robyer.gw_example.game.GameEventListener#receiveEvent(cz.robyer.gw_example.game.GameEvent)
   */
  @Override
  public void receiveEvent(final GameEvent event) {
    switch (event.type) {
    case GAME_LOSE:
      showGameWinLoseDialog(false);    
      break;
    case GAME_WIN:
      showGameWinLoseDialog(true);    
      break;
    default:
      break;
    }
  }
  
  private void showGameWinLoseDialog(final boolean won) {
    runOnUiThread(new Runnable() {
        public void run() {
        String title = "Game";
        if (GameService.isRunning())
          title = getGame().getScenario().getInfo().title;

        ad = new AlertDialog.Builder(BaseGameActivity.this).create();
        ad.setCancelable(false);
        ad.setTitle(title);
        ad.setMessage(won ? "Congratulations, you WON!" : "You just LOST this game!");
        ad.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {  
            dialog.dismiss();
    
            // Stop game service
            stopService(new Intent(BaseGameActivity.this, GameService.class));
              
            // Show main activity
            Intent startMain = new Intent(BaseGameActivity.this, MainActivity.class);
            startMain.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(startMain);
          }
        });
        ad.show();
        }
    });
  }
  
  /**
   * Shortcut for getting instance of {@link GameService}.
   * @return instance
   */
  protected GameService getGame() {
    return (GameService)GameService.getInstance();
  }
  
  /**
   * Quit application on second 'back' press.
   */
  @Override
  public void onBackPressed() {      
    if (!backClicked) {
      backClicked = true;
      Toast.makeText(getApplicationContext(), R.string.press_back_again_to_quit, Toast.LENGTH_LONG).show();      
      return;
    }

    // Stop game service
    Intent intent = new Intent(this, GameService.class);
    stopService(intent);
    
    // Start main activity
    Intent startMain = new Intent(this, MainActivity.class);
    startMain.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(startMain);
    
    //super.onBackPressed();
  }

}




Java Source Code List

cz.robyer.gamework.app.activity.BaseActivity.java
cz.robyer.gamework.app.activity.BaseGameActivity.java
cz.robyer.gamework.app.activity.GameInventoryActivity.java
cz.robyer.gamework.app.activity.GameMapActivity.java
cz.robyer.gamework.app.activity.GameMessagesActivity.java
cz.robyer.gamework.app.activity.GameObjectivesActivity.java
cz.robyer.gamework.app.activity.GameToolsActivity.java
cz.robyer.gamework.app.activity.HelpActivity.java
cz.robyer.gamework.app.activity.MainActivity.java
cz.robyer.gamework.app.activity.MessageActivity.java
cz.robyer.gamework.app.game.GameService.java
cz.robyer.gamework.app.service.JavaScriptHandler.java
cz.robyer.gamework.app.service.MessageAdapter.java
cz.robyer.gamework.constants.Constants.java
cz.robyer.gamework.game.GameEventBroadcaster.java
cz.robyer.gamework.game.GameEventListener.java
cz.robyer.gamework.game.GameEvent.java
cz.robyer.gamework.game.GameHandler.java
cz.robyer.gamework.game.GameService.java
cz.robyer.gamework.game.GameStatus.java
cz.robyer.gamework.scenario.BaseObject.java
cz.robyer.gamework.scenario.HookableObject.java
cz.robyer.gamework.scenario.IdentificableObject.java
cz.robyer.gamework.scenario.ScenarioInfo.java
cz.robyer.gamework.scenario.Scenario.java
cz.robyer.gamework.scenario.area.Area.java
cz.robyer.gamework.scenario.area.MultiPointArea.java
cz.robyer.gamework.scenario.area.PointArea.java
cz.robyer.gamework.scenario.area.SoundArea.java
cz.robyer.gamework.scenario.helper.EventHookable.java
cz.robyer.gamework.scenario.helper.ScannerHookable.java
cz.robyer.gamework.scenario.helper.TimeHookable.java
cz.robyer.gamework.scenario.hook.Condition.java
cz.robyer.gamework.scenario.hook.Hook.java
cz.robyer.gamework.scenario.message.Message.java
cz.robyer.gamework.scenario.parser.XmlScenarioParser.java
cz.robyer.gamework.scenario.reaction.ActivityReaction.java
cz.robyer.gamework.scenario.reaction.EventReaction.java
cz.robyer.gamework.scenario.reaction.MessageReaction.java
cz.robyer.gamework.scenario.reaction.MultiReaction.java
cz.robyer.gamework.scenario.reaction.Reaction.java
cz.robyer.gamework.scenario.reaction.ReferenceReaction.java
cz.robyer.gamework.scenario.reaction.SoundReaction.java
cz.robyer.gamework.scenario.reaction.VariableReaction.java
cz.robyer.gamework.scenario.reaction.VibrateReaction.java
cz.robyer.gamework.scenario.variable.BooleanVariable.java
cz.robyer.gamework.scenario.variable.DecimalVariable.java
cz.robyer.gamework.scenario.variable.Variable.java
cz.robyer.gamework.utils.GPoint.java
cz.robyer.gamework.utils.Log.java
cz.robyer.gamework.utils.Utils.java