Android Open Source - ShootEmOff Game Over Activity






From Project

Back to project page ShootEmOff.

License

The source code is released under:

Copyright (c) 2011 Andrey Moiseev, http://o2genum.ru Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),...

If you think the Android project ShootEmOff 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 com.shootemoff.game;
/*w  w  w  . ja v  a  2s  .c o m*/
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.shootemoff.shootemoffgame.R;

public class GameOverActivity extends Activity 
{

  public int game_score;
  
  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game_over);
    
    Bundle b = getIntent().getExtras();
    
    String score_string = b.getString("score");
    score_string = score_string.replaceAll("\\D+","");
    game_score = Integer.parseInt(score_string);
    
    TextView t = (TextView)findViewById(R.id.game_score); 
    //format score into time
    int minutes = game_score/100;
    int seconds = game_score - (100 * minutes);
    score_string = minutes + ":";
    if(seconds < 10){
      score_string += "0";
    }
    score_string += seconds;
    
    t.setText(score_string);
    
  }

  @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_over, menu);
    return true;
  }

  /**
   * @param view
   */
  public void StoreScore(View view)
  {
    TextView t = (TextView)findViewById(R.id.username); 
    String username = t.getText().toString();
    
    Resources res = getResources();
    
    // gathering the predefined variable names
    String[] scores = res.getStringArray(R.array.scores_array);
    String[] score_names = res.getStringArray(R.array.score_names_array);

    SharedPreferences sharedPref = getApplicationContext().getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE);
    
    //read predefined values, used to check if read resaurce is valid
    String defaultValueString = res.getString(R.string.saved_score_default);
    int defaultValue = Integer.parseInt(defaultValueString);
    String defaultName = res.getString(R.string.saved_score_name_default);
    
    String max_scores_saved_string = res.getString(R.string.max_scores_saved);
    int max_scores_saved = Integer.parseInt(max_scores_saved_string);
    
    StorageHandler handler = new StorageHandler(max_scores_saved);
    
    for(int i = 0; i < max_scores_saved; i++){

      int score = sharedPref.getInt(scores[i], defaultValue);

      if(score != defaultValue){
        String name = sharedPref.getString(score_names[i], defaultName);
        //saving name -> values pairs, easier to manipulate when sorting and saving afterwards
        handler.AddScore(name, score);
      }
      else{
        // read until all the values are stored, array might not be full
        break;
      }
    }
    
    //score need to be higher than already saved to be considered valid, or there are no enough scores
    boolean isScoreAdded = handler.AddScore(username, game_score);
    
    if(isScoreAdded == true){
      
      SharedPreferences.Editor editor = sharedPref.edit();
      
      int size = handler.GetSize();
      ScoreObject[] scores_to_save = handler.GetHighScores();
      //distribute the sorted values in the correct sequence
      //TODO: should add date parameter so the oldest values stay higher
      for(int i = 0; i < size; i++){
        editor.putInt(scores[i], scores_to_save[i].score);
        editor.putString(score_names[i], scores_to_save[i].name);
      }
      
      editor.commit();
      
      Toast.makeText(
          GameOverActivity.this,
          "New Score saved",
            Toast.LENGTH_LONG).show();
    }
    else{
      Toast.makeText(
          GameOverActivity.this,
          "Sorry! Your score is too low. You suck!",
            Toast.LENGTH_LONG).show();
    }

    //finish by showing the scoreboard either way score was saved or not
    Intent intent = new Intent (this, ScoreBoardActivity.class);
    startActivity(intent);
  }
  
  public void onBackPressed(){
    super.onBackPressed();
  }

}




Java Source Code List

com.shootemoff.framework.Audio.java
com.shootemoff.framework.FileIO.java
com.shootemoff.framework.Game.java
com.shootemoff.framework.Graphics.java
com.shootemoff.framework.Input.java
com.shootemoff.framework.Pool.java
com.shootemoff.framework.Screen.java
com.shootemoff.framework.Sound.java
com.shootemoff.framework.Vibration.java
com.shootemoff.framework.impl.AndroidAudio.java
com.shootemoff.framework.impl.AndroidFastRenderView.java
com.shootemoff.framework.impl.AndroidFileIO.java
com.shootemoff.framework.impl.AndroidGame.java
com.shootemoff.framework.impl.AndroidGraphics.java
com.shootemoff.framework.impl.AndroidInput.java
com.shootemoff.framework.impl.AndroidOrientationHandler.java
com.shootemoff.framework.impl.AndroidSound.java
com.shootemoff.framework.impl.AndroidVibration.java
com.shootemoff.framework.impl.KeyboardHandler.java
com.shootemoff.framework.impl.MultiTouchHandler.java
com.shootemoff.framework.impl.OrientationHandler.java
com.shootemoff.framework.impl.SingleTouchHandler.java
com.shootemoff.framework.impl.TouchHandler.java
com.shootemoff.game.ControlPad.java
com.shootemoff.game.Core.java
com.shootemoff.game.Dot.java
com.shootemoff.game.GameActivity.java
com.shootemoff.game.GameOverActivity.java
com.shootemoff.game.GameScreen.java
com.shootemoff.game.OptionsObject.java
com.shootemoff.game.Point.java
com.shootemoff.game.ScoreBoardActivity.java
com.shootemoff.game.ScoreObject.java
com.shootemoff.game.SettingsActivity.java
com.shootemoff.game.StartScreenActivity.java
com.shootemoff.game.StorageHandler.java
com.shootemoff.game.VectorF.java
com.shootemoff.game.World.java