finished abstract class

This commit is contained in:
2018-04-07 21:35:09 +02:00
parent d2fac7e27b
commit ed3843fc1e
4 changed files with 79 additions and 113 deletions

View File

@@ -1,5 +1,6 @@
package worms.model; package worms.model;
import be.kuleuven.cs.som.annotate.Raw;
import worms.util.Coordinate; import worms.util.Coordinate;
import static java.lang.Math.PI; import static java.lang.Math.PI;
@@ -8,12 +9,12 @@ import static java.lang.Math.round;
public abstract class GameObject { public abstract class GameObject {
public GameObject(World world, Coordinate location, double radius) { // public GameObject(World world, Coordinate location, double radius) {
//
this.world = world; // this.world = world;
this.location = location; // this.location = location;
this.radius = radius; // this.radius = radius;
} // }
private World world; private World world;
@@ -28,6 +29,7 @@ public abstract class GameObject {
public boolean isTerminated() { public boolean isTerminated() {
return this.terminated; return this.terminated;
} }
public void terminate() { public void terminate() {
this.terminated = true; this.terminated = true;
} }
@@ -35,29 +37,72 @@ public abstract class GameObject {
/**
* this variable contains the location of the worm (a Coordinate)
*/
private Coordinate location; private Coordinate location;
public double[] getLocation() {
return null; /**
* Return the location of the game object
* the location of the worm expresses the place of the game object
* in the play area
*/
Coordinate getLocation() {
return this.location;
}
public double[] getLocationArray() {
return this.location.toArray();
} }
private void setLocation(Coordinate location) { /**
* set the location of the worm to the given location
*
* @param location
* the new location for the game object
* @post the new location of the game object is equal to the given location
* |new.getLocation() == location
* @throws IllegalArgumentException
* the given location is not a valid location for a game object
* |! isValidLocation(location)
*/
void setLocation(Coordinate location) throws IllegalArgumentException {
if (!isValidLocation(location)) throw new IllegalArgumentException();
this.location = location; this.location = location;
} }
private double radius;
/**
* This variable contains the radius of the game object
*/
double radius;
/**
* Return the radius of the game object
* the radius of the game object expresses half of the
* width of the game object
*/
public double getRadius(){ public double getRadius(){
return this.radius; return this.radius;
} }
abstract void setRadius(); abstract void setRadius(double radius);
boolean isValidLocation(Coordinate location) {
double radius = getRadius();
return !Double.isNaN(location.getCoordinateX()) &&
!Double.isNaN(location.getCoordinateY()) &&
!(location.getCoordinateX() - radius < 0) &&
!(location.getCoordinateX() + radius > getWorld().getWidth()) &&
!(location.getCoordinateY() + radius > getWorld().getHeight()) &&
!(location.getCoordinateY() - radius < 0);
}
private double mass; private double mass;
public double getMass() { public double getMass() {
return this.mass; return this.mass;
} }
private void setMass() {
this.mass = (double) round(rho * (4.0 / 3.0 * PI * pow(radius, 3))); @Raw
} abstract void setMass(double radius);
private final double rho = 0;
} }

View File

@@ -217,13 +217,13 @@ public class World {
return all; return all;
} }
// public Set<Team> getAllTeams() { public Set<Team> getAllTeams() {
//
// Set<Team> teams = new HashSet<Team>(); Set<Team> teams = new HashSet<Team>();
// for(Worm worm: getWormList()) { for(Worm worm: getWormList()) {
// teams.add(worm.getTeam()); teams.add(worm.getTeam());
// } }
// } }

View File

@@ -32,7 +32,7 @@ import java.util.Random;
* *
* URL: https://github.com/KUL-ogp/ogp1718-project-bols-dereu * URL: https://github.com/KUL-ogp/ogp1718-project-bols-dereu
*/ */
public class Worm { public class Worm extends GameObject {
// region constructor // region constructor
//=================================================================================== //===================================================================================
@@ -129,61 +129,6 @@ public class Worm {
setHitPoints(startHitPoints); setHitPoints(startHitPoints);
} }
//===================================================================================
// endregion
// region location
//===================================================================================
/**
* Return the location of the worm
* the location of the worm expresses the place of the worm
* in the play area
*/
@Basic @Immutable @Raw
public Coordinate getLocation() {
return this.location;
}
/**
* check whether the given location is a valid location for the worm
*
* @param location
* the location to check
* @return True if and only if the location is not equal to null and the coordinates of
* the worm are numbers
* |result == ( (location != null) && (location.getCoordinateX() != null) && (location.getCoordinateY() != null)
* |&& (!Double.isNaN(location.getCoordinateX())) && (!Double.isNaN(location.getCoordinateY())) )
*/
public static boolean isValidLocation(Coordinate location) {
return location != null && !Double.isNaN(location.getCoordinateX()) && !Double.isNaN(location.getCoordinateY());
}
/**
* this variable contains the location of the worm (a Coordinate)
*/
private Coordinate location;
/**
* set the location of the worm to the given location
*
* @param location
* the new location for the worm
* @post the new location of the worm is equal to the given location
* |new.getLocation() == location
* @throws IllegalArgumentException
* the given location is not a valid location for a worm
* |! isValidLocation(location)
*/
@Raw
private void setLocation(Coordinate location) throws IllegalArgumentException {
if (!isValidLocation(location))
throw new IllegalArgumentException("Illegal value for location");
this.location = location;
}
//=================================================================================== //===================================================================================
// endregion // endregion
@@ -241,15 +186,7 @@ public class Worm {
// region Shape mass/radius // region Shape mass/radius
//=================================================================================== //===================================================================================
/**
* Return the radius of the worm
* the radius of the worm expresses half of the
* width of the worm
*/
@Basic @Raw
public double getRadius() {
return this.radius;
}
/** /**
* check whether the given radius is a valid minimum radius for the worm * check whether the given radius is a valid minimum radius for the worm
@@ -274,7 +211,7 @@ public class Worm {
* the given radius is not a valid radius for any worm * the given radius is not a valid radius for any worm
* |! canHaveAsMinRadius(radius) * |! canHaveAsMinRadius(radius)
*/ */
@Raw @Raw @Override
public void setRadius(double radius) throws IllegalArgumentException { public void setRadius(double radius) throws IllegalArgumentException {
if (!canHaveAsRadius(radius)) if (!canHaveAsRadius(radius))
throw new IllegalArgumentException("Invalid radius"); throw new IllegalArgumentException("Invalid radius");
@@ -293,12 +230,6 @@ public class Worm {
return this.minRadius; return this.minRadius;
} }
/**
* This variable contains the radius of the worm
*/
private double radius;
/** /**
* This variable contains the minimum value of the radius * This variable contains the minimum value of the radius
*/ */
@@ -341,8 +272,8 @@ public class Worm {
* rho * (4 / 3 * Math.PI * Math.pow(radius, 3)) * rho * (4 / 3 * Math.PI * Math.pow(radius, 3))
* |new.getMass() == rho * (4 / 3 * Math.PI * Math.pow(radius, 3)) * |new.getMass() == rho * (4 / 3 * Math.PI * Math.pow(radius, 3))
*/ */
@Raw @Raw @Override
private void setMass(double radius) { protected void setMass(double radius) {
final double rho = 1062.0; final double rho = 1062.0;
double mass = round(rho * (4.0 / 3.0 * PI * pow(radius, 3))); double mass = round(rho * (4.0 / 3.0 * PI * pow(radius, 3)));
this.mass = mass; this.mass = mass;
@@ -823,23 +754,7 @@ public class Worm {
public void incrementHitPoints(long value) { public void incrementHitPoints(long value) {
setHitPoints(getHitPoints() - value); setHitPoints(getHitPoints() - value);
} }
/**
* terminate the worm
*/
public void terminate() {
terminate = true;
setLocation(Coordinate.create(Double.NaN, Double.NaN));
}
public boolean isAlive() {
if (terminate == false) {
return true;
}
return false;
}
private boolean terminate = false;
//=================================================================================== //===================================================================================
// endregion // endregion

View File

@@ -49,6 +49,12 @@ public class Coordinate extends Tuple<Double, Double> {
throw new IllegalArgumentException("Invalid parameter arr"); throw new IllegalArgumentException("Invalid parameter arr");
return new Coordinate(arr[0], arr[1]); return new Coordinate(arr[0], arr[1]);
} }
public double[] toArray() {
return new double[]{getCoordinateX(), getCoordinateY()};
}
@Basic @Immutable @Basic @Immutable
public double getCoordinateX() { public double getCoordinateX() {
return this.item1; return this.item1;