Back to project page project2.
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.
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; } }