Android Open Source - project2 Battle Activity






From Project

Back to project page project2.

License

The source code is released under:

MIT License

If you think the Android project project2 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 team2.scdm;
/*  w  w w  .  jav  a  2  s  .c om*/
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.LocationListener;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.Toast;

public class BattleActivity extends Activity implements SensorEventListener {

    //for running away
  private LocationListener locationlistener; 
  private double lat; 
  private double lon;
  private double lastx, lasty, lastz;
  private SensorManager sensormanager;
  private Sensor acc;
  private final float NOISE = (float) 2.0;
  private boolean accInitialized; 
  
  private Enemy enemy;
  private Player player;
  private int placement;
  
  // Some code for accelerometer: 
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_battle);
    accInitialized = false; 
    
    sensormanager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    acc = sensormanager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    sensormanager.registerListener(this, acc, SensorManager.SENSOR_DELAY_NORMAL);
    
    player = new Player("bobby", 0, this);
    enemy = new Enemy();
  }
  
  /**
   * This initializes the activity with the correct enemy and its placement in the NPC ArrayList 
   * from LocationMapActivity.
   * @param enemy
   * @param placement
   */
  public void initialize(Enemy enemy, int placement) {
    this.enemy = enemy;
    this.placement = placement;
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.battle, menu);
    return true;
  }
  
  @Override
  public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // can be ignored
  }
  
  @Override
  public void onSensorChanged(SensorEvent event) {
    ImageView action = (ImageView) findViewById(R.id.action); 
    double x = event.values[0]; 
    double y = event.values[1]; 
    double z = event.values[2]; 
    if (accInitialized) {
      lastx = x; 
      lasty = y; 
      lastz = z; 
      accInitialized = false; 
    } else {
      double diffx = lastx - x; 
      double diffy = lasty - y; 
      double diffz = lastz - z; 
      double magnitude = Math.sqrt(Math.pow(diffx, 2) + Math.pow(diffy, 2)
          + Math.pow(diffz, 2)); 
      
      while(enemy.isAlive() && player.isAlive()) { // TODO: Must access player data
        if (magnitude > NOISE) {
          /*action.setMinimumHeight(20); 
          action.setMinimumWidth((int) magnitude * 10); 
          action.setBackgroundColor(Color.RED); */
          Context context = getApplicationContext();
          CharSequence text = "Shaking happened!";
          int duration = Toast.LENGTH_SHORT;
          
          Toast toast = Toast.makeText(context, text, duration);
          //toast.show();

          enemy.getHurt(player.getWeapon().use()); // TODO: Must access player data
          // Assets.hit.play(1); // this should play the hit sound *************************************
          player.getHurt(enemy.attackPlayer()); // TODO: Must access player data
        } else {
          // Nothing happens
        }
      }
      if (!enemy.isAlive()) {
        player.winBattle(enemy.dropExp(), enemy.dropUSD()); // TODO: Must access player data to add experience, USD        
        Context context = getApplicationContext();
        CharSequence text = "You won the battle. Go home.";
        int duration = Toast.LENGTH_SHORT;
        
        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
        // Given enemy should be removed from map; How to edit the map's NPC ArrayList?
        startActivity(new Intent(this, TitleActivity.class)); // TODO: Change link to GameOverActivity when made
      } else if (!player.isAlive()) { // TODO: Must access player data to view remaining health
        player.loseBattle(); // Must access player data to remove USD, etc
        startActivity(new Intent(this, TitleActivity.class)); // TODO: Change link to GameOverActivity when made
      }
    }
  }

  // Start the battle with initialization of enemy from map
  
  // While the player & enemy are alive:
    // Shaking damages the enemy & player
    // After each shake, the enemy and player are checked for death
      // If the enemy is dead, the enemy is removed from LocationMapActivity, and the player gains USD and exp and is sent back to LocationMapActivity
      // If the player is dead, the player loses USD and exp and is sent to GameOverActivity
  
}




Java Source Code List

team2.scdm.AboutActivity.java
team2.scdm.Armor.java
team2.scdm.Assets.java
team2.scdm.Audio.java
team2.scdm.BattleActivity.java
team2.scdm.Enemy.java
team2.scdm.Folk.java
team2.scdm.GameAudio.java
team2.scdm.GameMusic.java
team2.scdm.GameOverActivity.java
team2.scdm.GameSound.java
team2.scdm.GestureListener.java
team2.scdm.Intro1.java
team2.scdm.Intro2.java
team2.scdm.Intro3.java
team2.scdm.Intro4.java
team2.scdm.Intro5.java
team2.scdm.Intro6.java
team2.scdm.InventoryActivity.java
team2.scdm.Item.java
team2.scdm.LocationMapActivity.java
team2.scdm.Media.java
team2.scdm.MenuActivity.java
team2.scdm.NPC.java
team2.scdm.NameActivity.java
team2.scdm.Player.java
team2.scdm.SettingsActivity.java
team2.scdm.Sound.java
team2.scdm.TitleActivity.java
team2.scdm.Weapon.java