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; /*from w w w .j a v a 2 s . c o m*/ import java.util.Random; import com.google.android.gms.maps.model.LatLng; public abstract class NPC { protected LatLng position; protected static final double GENERATIONRADIUS = 0.002; // >0.008 is probably good? protected static final double NOTICERADIUS = 0.00086; protected static final double ACTIONRADIUS = 0.00003; protected static final double MAXRADIUS = 0.01; // If the user changes location dramatically and NPCs are farther than this away, they just die protected static final double JUMPDIST = 0.00007; // 0.00001; // 0.00002; protected boolean initialSetup; protected int randDir; // Random direction public int type; public final static int BLOB = R.drawable.enemy; public final static int NOTICEBLOB = R.drawable.noticeenemy; public final static int DEADBLOB = R.drawable.enemy; public final static int CLOWN = R.drawable.clownone; protected final static int[] friendlyTypes = {CLOWN}; public NPC() { } public NPC(LatLng player) { initialSetup = true; // Math here to generate position based on GENERATIONRADIUS // Generate a random angle: double angle = nextDouble(Math.PI * 2); // Generate a random magnitude between GENERATIONRADIUS and NOTICERADIUS: double magnitude = nextDouble(Math.abs(GENERATIONRADIUS - NOTICERADIUS)); magnitude += NOTICERADIUS; // Calculate coordinate based on angle and magnitude: Random rand = new Random(); int latsign = rand.nextInt(2); if (latsign == 0) latsign = -1; int lonsign = rand.nextInt(2); if (lonsign == 0) lonsign = -1; double latitude = latsign * Math.sin(angle) * magnitude + player.latitude; double longitude = lonsign * Math.cos(angle) * magnitude + player.longitude; position = new LatLng(latitude, longitude); } public void move(LatLng player) { // This is empty because Enemy and Folk movements are pretty different. } protected void randomMove(LatLng player) { // We don't want it to just jitter randomly and stay in the same place. That's stupid. Random rand = new Random(); double jumpDist = JUMPDIST; double choiceLat = nextDouble(jumpDist); double choiceLon = nextDouble(jumpDist); int tossup = rand.nextInt(5); // If their direction has not been initialized or they're heading out of bounds if (initialSetup) { tossup = 0; initialSetup = false; } if (tossup == 0) { randDir = rand.nextInt(4); } switch(randDir) { case 0: // type = R.drawable.debugblue; position = new LatLng(position.latitude + choiceLat, position.longitude + choiceLon); break; case 1: // type = R.drawable.debuggreen; position = new LatLng(position.latitude + choiceLat, position.longitude - choiceLon); break; case 2: // type = R.drawable.debugpurple; position = new LatLng(position.latitude - choiceLat, position.longitude + choiceLon); break; case 3: // type = R.drawable.debugred; position = new LatLng(position.latitude - choiceLat, position.longitude - choiceLon); break; } } protected boolean notice(LatLng player) { double dist = getDistBetween(player); // Is this within the notice radius? return dist < NOTICERADIUS; } public boolean action(LatLng player) { // This means fighting for Enemies and being grabbed for Folk return getDistBetween(player) < ACTIONRADIUS; } public boolean withinBounds(LatLng player) { return getDistBetween(player) < GENERATIONRADIUS; } public boolean withinMaxBounds(LatLng player) { return getDistBetween(player) < MAXRADIUS; } protected double getDistBetween(LatLng player) { // Calculate the distance between this NPC and the player: double latDiff = player.latitude - position.latitude; double lonDiff = player.longitude - position.longitude; return Math.sqrt(Math.pow(latDiff, 2) + Math.pow(lonDiff, 2)); } public LatLng getPosition() { return position; } public int getType() { return type; } protected double nextDouble(double limit) { Random rand = new Random(); return rand.nextDouble() % limit; } }