import java.awt.*; import java.lang.*; import java.util.*; import Location; public abstract class Player { public boolean alive = true; public int X; public int Y; public int id; public String name; protected double angle; protected double rotate; protected boolean command[] = { false, false, false, false }; protected int velocity; protected double velocityAngle; protected int maxVelocity; protected int accel; protected int shields; protected int maxShields; protected int firePower; protected int firePowerStep; protected int maxFirePower; protected int fire[] = {-1, -1, -1, -1, -1}; protected int fireX[] = new int[5]; protected int fireY[] = new int[5]; protected double fireAngle[] = new double[5]; protected int TTF = 0; protected int col = 0; protected int pastX[] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }; protected int pastY[] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }; protected final Color engine[] = { new Color (232, 208, 48), new Color (224, 192, 24), new Color (216, 176, 0), new Color (208, 160, 0), new Color (200, 144, 0), new Color (192, 128, 0), new Color (184, 112, 0), new Color (176, 96, 0), new Color (168, 80, 0), new Color (160, 64, 0), new Color (152, 48, 0), new Color (144, 32, 0), new Color (136, 16, 0), new Color (128, 0, 0), new Color (96, 0, 0), }; public final boolean setCommand(int i, boolean b) { if (command[i] == b) return true; command[i] = b; return false; } public final void update(int i, int p) { if (command[0]) angle -= rotate; if (command[1]) angle += rotate; if (command[2]) addAccel(accel); if (command[3] && TTF == 0) fire(); if (TTF != 0) TTF -= 1; if (p != 0) givePowerUp(p); for (int j = 0; j < 5; j++) { if (fire[j] != -1) { fire[j] += 1; fireX[j] += Math.round((float) (2*maxVelocity*Math.cos(fireAngle[j]))); fireY[j] += Math.round((float) (2*maxVelocity*Math.sin(fireAngle[j]))); if (fire[j] > 15) fire[j] = -1; } } checkAngleBounds(); if (i != 0) changeAngle(i); else col = i; X += Math.round((float) (velocity*Math.cos(velocityAngle))); Y += Math.round((float) (velocity*Math.sin(velocityAngle))); pastX[0] = X; pastY[0] = Y; } public final void spawn(Location l) { X = l.x; Y = l.y; angle = l.angle; velocity = 0; for (int i = 0; i < 5; i++) fire[i] = -1; shields = maxShields; firePower = firePowerStep; alive = true; } public final boolean isAlive() { return alive; } public final boolean checkID(int i) { return (i == id); } public final int getShieldPercent() { return shields*100/maxShields; } public final int getVelocityPercent() { return velocity*100/maxVelocity; } public final int getWeaponPowerPercent() { return (firePower*100)/maxFirePower; } public final String getPacket() { return "#f`" + X + "`" + Y + "`" + getAngle() + "`" + velocity + "`" + getVelAngle() + "`" + shields; } public final String getKeyPacket() { return "#k`" + getBooleanTable(); } protected final String getAngle() { return new String(Double.toString(angle+0.00000001).toCharArray(), 0, 6); } protected final String getVelAngle() { return new String(Double.toString(velocityAngle+0.00000001).toCharArray(), 0, 6); } protected final String getBooleanTable() { String r = ""; for (int i = 0; i < 4; i++) { if (command[i]) r += "1"; else r += "0"; } return r; } public final void setKeys(String s) { for (int i = 0; i < 4; i++) { if (s.charAt(i) == '0') command[i] = false; else command[i] = true; } } public final void setFull(int x, int y, double a, int v, double va, int s) { X = x; Y = y; angle = a; velocity = v; velocityAngle = va; shields = s; if (!alive) { alive = true; shields = maxShields; firePower = firePowerStep; } } public final void hurt(int i) { shields -= i; if (shields < 0) alive = false; } public final void givePowerUp(int i) { if (i == 12) { firePower += 2; if (firePower > maxFirePower) firePower = maxFirePower; } else shields = maxShields; } public final int checkFire(int x, int y) { for (int i = 0; i < 5; i++) { if (fire[i] != -1) { if (fireX[i] > x - 15 && fireX[i] < x + 15 && fireY[i] > y - 15 && fireY[i] < y + 15) { fire[i] = -1; return firePower; } } } return 0; } public final boolean checkKill(int d) { return (alive && (shields - d) < 0); } public abstract void paint(Graphics g, int x, int y, int t); public abstract String getDescription(); public abstract int[] getData(); protected final void checkAngleBounds() { if (angle < 0) angle += 6.283; else if (angle > 6.283) angle -= 6.283; } protected final void addAccel (int a) { double x = (a*Math.cos(angle)) + (velocity*Math.cos(velocityAngle)); double y = (a*Math.sin(angle)) + (velocity*Math.sin(velocityAngle)); velocityAngle = Math.atan2(y, x); velocity = Math.round((float) (Math.sqrt((x*x + y*y)))); if (velocity > maxVelocity) velocity = maxVelocity; } protected final void changeAngle(int i) { if (col != i) { double x = (velocity*Math.cos(velocityAngle)); double y = (velocity*Math.sin(velocityAngle)); if (i == 1) velocityAngle = Math.atan2(y, -x); else velocityAngle = Math.atan2(-y, x); col = i; hurt(maxShields/50); } } protected final void fire() { for (int i = 0; i < 5; i++) { if (fire[i] == -1) { fire[i] = 0; fireX[i] = X + Math.round((float) (2*maxVelocity*Math.cos(angle))); fireY[i] = Y + Math.round((float) (2*maxVelocity*Math.sin(angle))); fireAngle[i] = angle; TTF = 3; break; } } } protected final void setConstants(double a, int mV, int ac, int mS, int fPS, int fP) { rotate = a; maxVelocity = mV; accel = ac; maxShields = mS; firePowerStep = fPS; maxFirePower = fP; } }