gvsu/cs654/final-proj/Player.java
josh 6fce0de00a changes in x and y affecting Players equality properly
git-svn-id: svn://anubis/gvsu@99 45c1a28c-8058-47b2-ae61-ca45b979098e
2008-04-13 20:27:55 +00:00

84 lines
2.3 KiB
Java

import java.util.*;
public class Player extends GameItem implements Cloneable
{
public static final double DEFAULT_RADIUS = 0.06;
public static final double COLLIDE_DAMAGE = 0.002;
public static final double SHOT_DELAY = 4.0;
public String name;
public double health;
public double r;
public double dr;
public long lastShotTime;
public Player(String name)
{
this.name = name;
this.radius = DEFAULT_RADIUS;
this.health = 1.0;
this.r = Math.random() * Math.PI * 2;
this.x = 0; /* will be assigned by world */
this.y = 0; /* will be assigned by world */
this.dr = 0;
this.dx = 0;
this.dy = 0;
}
public Player clone()
{
Player p = new Player(name);
p.radius = radius;
p.health = health;
p.r = r;
p.x = x;
p.y = y;
p.dr = dr;
p.dx = dx;
p.dy = dy;
return p;
}
public boolean equals(Player other)
{
return this.name.equals(other.name) &&
this.radius == other.radius &&
this.health == other.health &&
this.r == other.r &&
this.dr == other.dr &&
this.dx == other.dx &&
this.dy == other.dy &&
this.x == other.x &&
this.y == other.y;
}
public String toString()
{
return String.format("%s:%.3f:%.3f:%.3f:%.3f:%.3f:%.3f:%.3f:%.3f",
name, radius, health, x, y, r, dr, dx, dy);
}
public void fromString(String s)
{
StringTokenizer st = new StringTokenizer(s, ":");
Vector<String> tokens = new Vector<String>();
while (st.hasMoreTokens())
{
tokens.add(st.nextToken());
}
String[] arr = tokens.toArray(new String[1]);
if (arr.length != 9)
return;
name = arr[0];
radius = Double.parseDouble(arr[1]);
health = Double.parseDouble(arr[2]);
x = Double.parseDouble(arr[3]);
y = Double.parseDouble(arr[4]);
r = Double.parseDouble(arr[5]);
dr = Double.parseDouble(arr[6]);
dx = Double.parseDouble(arr[7]);
dy = Double.parseDouble(arr[8]);
}
}