refactoring jeej

This commit is contained in:
2018-05-23 21:38:44 +02:00
parent f3d6d971b9
commit 98de279626
6 changed files with 112 additions and 89 deletions

View File

@@ -46,7 +46,7 @@ public abstract class GameObject {
setLocation(location); setLocation(location);
setRadius(radius); setRadius(radius);
if (isValidWorld(world)) world.add(this); if (isValidWorld(world)) world.add(this);
this.world = world; setWorld(world);
} }
// =================================================================================== // ===================================================================================
@@ -272,7 +272,7 @@ public abstract class GameObject {
public double getDistance(GameObject o) { public double getDistance(GameObject o) {
return getDistance(o.getLocation(), this.radius); return getDistance(o.getLocation(), o.getRadius());
} }
public double getDistance(Coordinate otherLocation, double radius) { public double getDistance(Coordinate otherLocation, double radius) {

View File

@@ -6,13 +6,12 @@ import java.util.List;
public abstract class Projectile extends GameObject implements IJumpable { public abstract class Projectile extends GameObject implements IJumpable {
private static final int rho = 7800;
// region constructor // region constructor
//=================================================================================== //===================================================================================
public static final double G = 5.0; public static final double G = 5.0;
public static final double FORCE_TIME = 0.5; public static final double FORCE_TIME = 0.5;
private static final int rho = 7800;
protected Projectile (Worm worm, double mass, double force) { protected Projectile (Worm worm, double mass, double force) {
super(worm.getWorld(), calcLocation(worm.getLocation(), worm.getOrientation(), worm.getRadius(), mass), calcRadius(mass)); super(worm.getWorld(), calcLocation(worm.getLocation(), worm.getOrientation(), worm.getRadius(), mass), calcRadius(mass));
@@ -33,17 +32,21 @@ public abstract class Projectile extends GameObject implements IJumpable {
return orientation; return orientation;
} }
private final double orientation;
public static double calcRadius(double mass) { public static double calcRadius(double mass) {
return pow(((mass / 1000.0 / rho) * (3.0 / (4.0 * PI))), 1.0 / 3.0); return pow(((mass / 1000.0 / rho) * (3.0 / (4.0 * PI))), 1.0 / 3.0);
} }
public double getForce() { public double getForce() {
return this.force; return this.force;
} }
protected final double force; protected final double force;
private final double orientation;
// =================================================================================== // ===================================================================================
// endregion // endregion

View File

@@ -9,7 +9,6 @@ import static java.lang.Math.sin;
public class Rifle extends Projectile { public class Rifle extends Projectile {
public Rifle(Worm worm) { public Rifle(Worm worm) {
super(worm, 10, 1.5); super(worm, 10, 1.5);
} }

View File

@@ -33,8 +33,6 @@ public class Team {
} }
} }
//===================================================================================
// endregion
/** /**
* Check whether the given object has a valid name for a object. * Check whether the given object has a valid name for a object.
@@ -54,6 +52,10 @@ public class Team {
return this.name.equals(((Team) obj).getName()); return this.name.equals(((Team) obj).getName());
} }
//===================================================================================
// endregion
// region changesTeam // region changesTeam
//=================================================================================== //===================================================================================
@@ -165,16 +167,6 @@ public class Team {
return this.wormCollection.contains(worm); return this.wormCollection.contains(worm);
} }
/**
* Gives all the worms of the team.
*
* @return All the worms of the team in a Collection.
* |result == wormCollection
*/
public Collection<Worm> getAllWormsOfTeam() {
return wormCollection;
}
/** /**
* Gives all the worms of the team. * Gives all the worms of the team.
* *
@@ -231,6 +223,8 @@ public class Team {
//=================================================================================== //===================================================================================
// endregion // endregion
// region mass // region mass
//=================================================================================== //===================================================================================
@@ -254,6 +248,8 @@ public class Team {
//=================================================================================== //===================================================================================
// endregion // endregion
// region name // region name
//=================================================================================== //===================================================================================
@@ -294,7 +290,8 @@ public class Team {
//=================================================================================== //===================================================================================
// endregion // endregion
// region terminate
// region destructor
//=================================================================================== //===================================================================================
/** /**

View File

@@ -7,6 +7,9 @@ import java.util.stream.Collectors;
public class World { public class World {
// region constructor / destructor
//===================================================================================
/** /**
* @param width * @param width
* @param height * @param height
@@ -42,6 +45,37 @@ public class World {
} }
/**
* @return ...
* result = this.terminated
*/
public boolean isTerminated() {
return this.terminated;
}
/**
* @post ...
* |teams.clear()
* @post ...
* |this.terminated = true
*/
public void terminate() {
Set<GameObject> gameObjectList = new HashSet<>(getGameObjects());
gameObjectList.forEach(GameObject::terminate);
this.teams.clear();
this.terminated = true;
}
private boolean terminated = false;
// ===================================================================================
// endregion
// region game (Total) // region game (Total)
//=================================================================================== //===================================================================================
@@ -152,6 +186,27 @@ public class World {
private final double height; private final double height;
/**
* @return ...
* |result == this.lengtX
*/
public double getLengthX() {
return this.lengthX;
}
/**
* @return ...
* |result == this.lengthY
*/
public double getLengthY() {
return this.lengthY;
}
private final double lengthX;
private final double lengthY;
// =================================================================================== // ===================================================================================
// endregion // endregion
@@ -168,37 +223,16 @@ public class World {
return map; return map;
} }
/**
* @return ...
* result = this.terminated
*/
public boolean isTerminated() {
return this.terminated;
}
/**
* @post ...
* |teams.clear()
* @post ...
* |this.terminated = true
*/
public void terminate() {
Set<GameObject> gameObjectList = new HashSet<>(getGameObjects());
gameObjectList.forEach(g -> g.terminate());
this.teams.clear();
this.terminated = true;
}
private final boolean[][] map; private final boolean[][] map;
private boolean terminated = false;
// =================================================================================== // ===================================================================================
// endregion // endregion
// region passable (Total) // region passable (Total)
//=================================================================================== //===================================================================================
@@ -266,12 +300,6 @@ public class World {
double lenX = center[0] + radius * Math.cos(i); double lenX = center[0] + radius * Math.cos(i);
double lenY = center[1] + radius * Math.sin(i); double lenY = center[1] + radius * Math.sin(i);
// if (i < 1.58 && i > 1.57) {
// lenY -= 0.0000000001;
// }
// else if (i < 0.79 && i > 0.78 ) {
// lenX -= 0.0000000001;
// }
if (center[0] + radius == lenX) { if (center[0] + radius == lenX) {
lenX -= 0.0000000001; lenX -= 0.0000000001;
} }
@@ -304,31 +332,11 @@ public class World {
public boolean isAdjacent(Coordinate center, double radius) { public boolean isAdjacent(Coordinate center, double radius) {
double maxDistance = radius * 0.1; double maxDistance = radius * 0.1;
double length = lengthX;
if (lengthX > lengthY) length = lengthY;
return isPassable(center, radius) && !isPassable(center, radius + maxDistance + 0.00001); return isPassable(center, radius) && !isPassable(center, radius + maxDistance + 0.00001);
} }
/**
* @return ...
* |result == this.lengtX
*/
public double getLengthX() {
return this.lengthX;
}
/**
* @return ...
* |result == this.lengthY
*/
public double getLengthY() {
return this.lengthY;
}
private final double lengthX;
private final double lengthY;
/** /**
* @param x * @param x
@@ -447,7 +455,6 @@ public class World {
obj.setWorld(null); obj.setWorld(null);
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
obj.setWorld(this);
getGameObjects().add(obj); getGameObjects().add(obj);
} }
@@ -522,7 +529,7 @@ public class World {
public void castSpell() { public void castSpell() {
Set<GameObject> gameObjects = getGameObjects(); Set<GameObject> gameObjects = this.gameObjects;
int size = gameObjects.size(); int size = gameObjects.size();
if (size < 2) throw new IllegalStateException("Less than 2 objects"); if (size < 2) throw new IllegalStateException("Less than 2 objects");
int nb1 = new Random().nextInt(size); int nb1 = new Random().nextInt(size);
@@ -547,18 +554,23 @@ public class World {
if (item1 instanceof Worm && item2 instanceof Worm) { if (item1 instanceof Worm && item2 instanceof Worm) {
if ((((Worm) item1).getTeam() != null && ((Worm) item2).getTeam() != null) && ((Worm) item1).getTeam().getName().equals(((Worm) item2).getTeam().getName())) {
long hitpoints = (long) Math.floor((((Worm) item1).getHitPoints() + ((Worm) item2).getHitPoints()) / 2); Worm worm1 = (Worm) item1;
((Worm) item1).setHitPoints(hitpoints); Worm worm2 = (Worm) item2;
((Worm) item2).setHitPoints(hitpoints);
if ((worm1.getTeam() != null && worm2.getTeam() != null) && worm1.getTeam().getName().equals(worm2.getTeam().getName())) {
long hitpoints = (long) Math.floor((worm1.getHitPoints() + worm2.getHitPoints()) / 2);
worm1.setHitPoints(hitpoints);
worm2.setHitPoints(hitpoints);
} else { } else {
Worm lgWorm = (Worm) item1; Worm lgWorm = worm1;
Worm smWorm = (Worm) item2; Worm smWorm = worm2;
if (lgWorm.getRadius() < smWorm.getRadius()) { if (lgWorm.getRadius() < smWorm.getRadius()) {
lgWorm = (Worm) item2; lgWorm = worm2;
smWorm = (Worm) item1; smWorm = worm1;
} }
long lgHitPoints = lgWorm.getActionPoints(); long lgHitPoints = lgWorm.getActionPoints();
if (lgHitPoints < 5) { if (lgHitPoints < 5) {
@@ -594,11 +606,15 @@ public class World {
} }
else if (item1 instanceof Food && item2 instanceof Food) { else if (item1 instanceof Food && item2 instanceof Food) {
if (((Food) item1).isPoisonous()) ((Food) item1).heal();
else ((Food) item1).poison();
if (((Food) item2).isPoisonous()) ((Food) item2).heal(); Food food1 = (Food) item1;
else ((Food) item2).poison(); Food food2 = (Food) item2;
if (food1.isPoisonous()) food1.heal();
else food1.poison();
if (food2.isPoisonous()) food2.heal();
else food2.poison();
} }
else if (item1 instanceof Food && item2 instanceof Projectile || else if (item1 instanceof Food && item2 instanceof Projectile ||
item1 instanceof Projectile && item2 instanceof Food) { item1 instanceof Projectile && item2 instanceof Food) {

View File

@@ -1004,6 +1004,10 @@ public class Worm extends GameObject implements IJumpable{
// =================================================================================== // ===================================================================================
// endregion // endregion
// region eat
//===================================================================================
public void eat(Food food) { public void eat(Food food) {
this.eat(food, true); this.eat(food, true);
} }
@@ -1094,6 +1098,10 @@ public class Worm extends GameObject implements IJumpable{
} }
// ===================================================================================
// endregion
// region firing and projectiles // region firing and projectiles
//=================================================================================== //===================================================================================
@@ -1116,7 +1124,6 @@ public class Worm extends GameObject implements IJumpable{
decrementActionPoints(10); decrementActionPoints(10);
return new Rifle(this); return new Rifle(this);
} else { } else {
double force = Bazooka.calcForce(this.actionPoints);
decrementActionPoints(25); decrementActionPoints(25);
return new Bazooka(this); return new Bazooka(this);
} }
@@ -1132,6 +1139,7 @@ public class Worm extends GameObject implements IJumpable{
// =================================================================================== // ===================================================================================
// endregion // endregion
// region program // region program
//=================================================================================== //===================================================================================