Android Open Source - project2 Enemy






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. j  a v a 2s .co m*/
import java.util.Random;

import android.util.Log;

import com.google.android.gms.maps.model.LatLng;

public class Enemy extends NPC {
    
  // TODO: Health, attack strength, gold stored, items, ... Whatever goes here
  private int health;
  private int exp;
  private int USD;
  private int attack;
  
  public Enemy() {
    super(); 
    type = BLOB;
    health = 400; 
    exp = 5;
    USD = ( 1 + (int) (Math.random() * 4));
    attack = ( 1 + (int) (Math.random() * 1)); // 1 - 2 attack strength
  }
  
  public Enemy(LatLng player) {
    super(player); 
    type = BLOB;
    health = ( 4 + (int) (Math.random() * 12));
    exp = 5;
    USD = ( 1 + (int) (Math.random() * 4));
    attack = ( 1 + (int) (Math.random() * 1)); // 1 - 2 attack strength
  }
  
  public void move(LatLng player) {
    Random rand = new Random(); 
    boolean notice = notice(player); 
    boolean coinflip1 = rand.nextBoolean(); // If this returns false, the enemy will not pursue the user regardless of noticing
    boolean coinflip2 = rand.nextBoolean(); // If this returns false, the enemy will not pursue the user regardless of noticing
    if (!withinBounds(player) || (notice && (coinflip1 || coinflip2))) { // 75% chance of moving to player if notice
      if (notice) {
        type = NOTICEBLOB; 
      } else {
        type = BLOB; 
      }
      double jumpDist; 
      double distBetween = getDistBetween(player); 
      if (distBetween > ACTIONRADIUS) { // TODO: Tweak these numbers! 
        jumpDist = JUMPDIST; 
      } else {
        jumpDist = JUMPDIST * 2; 
      }
      double choiceLat = nextDouble(jumpDist); 
      double choiceLon = nextDouble(jumpDist); 
      if (player.latitude > position.latitude) { // If the user is above the enemy
        if (player.longitude > position.longitude) {
          randDir = 0; 
          position = new LatLng(position.latitude + choiceLat, position.longitude + choiceLon); 
        } else if (player.longitude < position.longitude) {
          randDir = 1; 
          position = new LatLng(position.latitude + choiceLat, position.longitude - choiceLon); 
        } else {
          randDir = rand.nextInt(2); 
          position = new LatLng(position.latitude + choiceLat, position.longitude); 
        }
      } else if (player.latitude < position.latitude) { // If the user is below the enemy
        if (player.longitude > position.longitude) {
          randDir = 2; 
          position = new LatLng(position.latitude - choiceLat, position.longitude + choiceLon); 
        } else if (player.longitude < position.longitude) {
          randDir = 3; 
          position = new LatLng(position.latitude - choiceLat, position.longitude - choiceLon); 
        } else {
          randDir = rand.nextInt(2) + 2; 
          position = new LatLng(position.latitude - choiceLat, position.longitude); 
        }
      } else { // If they're the same latitude
        if (player.longitude > position.longitude) {
          randDir = rand.nextInt(2); 
          if (randDir == 1) {
            randDir = 2; 
          }
          position = new LatLng(position.latitude, position.longitude + choiceLon); 
        } else if (player.longitude < position.longitude) {
          randDir = rand.nextInt(2) + 1; 
          if (randDir == 2) {
            randDir = 3; 
          }
          position = new LatLng(position.latitude, position.longitude - choiceLon); 
        } else {
          position = new LatLng(position.latitude, position.longitude); 
        }
      }
    } else {
      if (!notice) {
        type = BLOB; 
      }
      randomMove(player); 
    }
  }
  
public boolean getHurt(int painAmt) {
    
    health -= painAmt;
    String pain = painAmt + "";
    Log.e("PAIN", pain);
    if(health <= 0) {
      //enemy is removed form map... how do?
      return true;
    }
    return false;
  }
  
  public int dropExp() {
    
    return exp;
  }
  
  public int dropUSD() {
    
    return USD;
  }
  
  public int attackPlayer() {
    return attack;
  }
  
  public boolean isAlive() {
    
    if(health <= 0) {
      return false;
    }
    return true;
    
  }
  
}




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