overall improvements
This commit is contained in:
@@ -2,31 +2,29 @@ package worms.model;
|
|||||||
|
|
||||||
import static java.lang.Math.*;
|
import static java.lang.Math.*;
|
||||||
|
|
||||||
public class Food {
|
public class Food extends GameObject {
|
||||||
// region location
|
|
||||||
//===================================================================================
|
public Food(World world, double[] location) {
|
||||||
|
setWorld(world);
|
||||||
|
setLocation(location);
|
||||||
//===================================================================================
|
}
|
||||||
// endregion
|
|
||||||
|
|
||||||
// region shape
|
// region shape
|
||||||
//===================================================================================
|
//===================================================================================
|
||||||
|
|
||||||
final double radius = 0.20;
|
private final double radius = 0.20;
|
||||||
|
|
||||||
public double getMass() {
|
@Override
|
||||||
return this.mass;
|
public double getRadius() {
|
||||||
}
|
return this.radius;
|
||||||
|
}
|
||||||
public void setMass() {
|
|
||||||
|
@Override
|
||||||
|
public void setMass(double radius) {
|
||||||
final double rho = 150;
|
final double rho = 150;
|
||||||
double m = rho * (4/3 * PI * pow(radius, 3));
|
this.mass = rho * (4/3 * PI * pow(radius, 3));
|
||||||
this.mass = m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public double mass;
|
|
||||||
|
|
||||||
//===================================================================================
|
//===================================================================================
|
||||||
// endregion
|
// endregion
|
||||||
|
|
||||||
|
@@ -3,21 +3,15 @@ package worms.model;
|
|||||||
import be.kuleuven.cs.som.annotate.Raw;
|
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.pow;
|
|
||||||
import static java.lang.Math.round;
|
|
||||||
|
|
||||||
public abstract class GameObject {
|
public abstract class GameObject {
|
||||||
|
|
||||||
// public GameObject(World world, Coordinate location, double radius) {
|
|
||||||
//
|
|
||||||
// this.world = world;
|
|
||||||
// this.location = location;
|
|
||||||
// this.radius = radius;
|
|
||||||
// }
|
|
||||||
|
|
||||||
private World world;
|
private World world;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return World
|
||||||
|
*/
|
||||||
public World getWorld() {
|
public World getWorld() {
|
||||||
return this.world;
|
return this.world;
|
||||||
}
|
}
|
||||||
@@ -26,10 +20,17 @@ public abstract class GameObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
public boolean isTerminated() {
|
public boolean isTerminated() {
|
||||||
return this.terminated;
|
return this.terminated;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Terminate the worm
|
||||||
|
*/
|
||||||
public void terminate() {
|
public void terminate() {
|
||||||
this.terminated = true;
|
this.terminated = true;
|
||||||
}
|
}
|
||||||
@@ -66,7 +67,16 @@ public abstract class GameObject {
|
|||||||
* the given location is not a valid location for a game object
|
* the given location is not a valid location for a game object
|
||||||
* |! isValidLocation(location)
|
* |! isValidLocation(location)
|
||||||
*/
|
*/
|
||||||
|
void setLocation(double[] location) throws IllegalArgumentException {
|
||||||
|
|
||||||
|
Coordinate locationCoordinate = Coordinate.create(location);
|
||||||
|
|
||||||
|
if (!isValidLocation(locationCoordinate)) throw new IllegalArgumentException();
|
||||||
|
this.location = locationCoordinate;
|
||||||
|
}
|
||||||
|
|
||||||
void setLocation(Coordinate location) throws IllegalArgumentException {
|
void setLocation(Coordinate location) throws IllegalArgumentException {
|
||||||
|
|
||||||
if (!isValidLocation(location)) throw new IllegalArgumentException();
|
if (!isValidLocation(location)) throw new IllegalArgumentException();
|
||||||
this.location = location;
|
this.location = location;
|
||||||
}
|
}
|
||||||
@@ -85,7 +95,7 @@ public abstract class GameObject {
|
|||||||
public double getRadius(){
|
public double getRadius(){
|
||||||
return this.radius;
|
return this.radius;
|
||||||
}
|
}
|
||||||
abstract void setRadius(double radius);
|
//abstract void setRadius(double radius);
|
||||||
|
|
||||||
boolean isValidLocation(Coordinate location) {
|
boolean isValidLocation(Coordinate location) {
|
||||||
double radius = getRadius();
|
double radius = getRadius();
|
||||||
@@ -94,11 +104,12 @@ public abstract class GameObject {
|
|||||||
!(location.getCoordinateX() - radius < 0) &&
|
!(location.getCoordinateX() - radius < 0) &&
|
||||||
!(location.getCoordinateX() + radius > getWorld().getWidth()) &&
|
!(location.getCoordinateX() + radius > getWorld().getWidth()) &&
|
||||||
!(location.getCoordinateY() + radius > getWorld().getHeight()) &&
|
!(location.getCoordinateY() + radius > getWorld().getHeight()) &&
|
||||||
!(location.getCoordinateY() - radius < 0);
|
!(location.getCoordinateY() - radius < 0 &&
|
||||||
|
!getWorld().isPassable(location));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private double mass;
|
double mass;
|
||||||
public double getMass() {
|
public double getMass() {
|
||||||
return this.mass;
|
return this.mass;
|
||||||
}
|
}
|
||||||
|
@@ -1,5 +1,7 @@
|
|||||||
package worms.model;
|
package worms.model;
|
||||||
|
|
||||||
|
import worms.util.Coordinate;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class World {
|
public class World {
|
||||||
@@ -132,6 +134,10 @@ public class World {
|
|||||||
return this.map[(int) Math.floor(location[1] / lengthY)][(int) Math.floor(location[0] / lengthX)];
|
return this.map[(int) Math.floor(location[1] / lengthY)][(int) Math.floor(location[0] / lengthX)];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isPassable(Coordinate location) {
|
||||||
|
return this.isPassable(location.toArray());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param center ...
|
* @param center ...
|
||||||
@@ -223,6 +229,7 @@ public class World {
|
|||||||
for(Worm worm: getWormList()) {
|
for(Worm worm: getWormList()) {
|
||||||
teams.add(worm.getTeam());
|
teams.add(worm.getTeam());
|
||||||
}
|
}
|
||||||
|
return teams;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -212,7 +212,7 @@ public class Worm extends GameObject {
|
|||||||
* 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 @Override
|
@Raw
|
||||||
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");
|
||||||
@@ -800,4 +800,20 @@ public class Worm extends GameObject {
|
|||||||
|
|
||||||
//===================================================================================
|
//===================================================================================
|
||||||
// endregion
|
// endregion
|
||||||
|
|
||||||
|
|
||||||
|
// region team
|
||||||
|
//===================================================================================
|
||||||
|
public Team getTeam() {
|
||||||
|
return this.team;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTeam(Team team) {
|
||||||
|
this.team = team;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Team team;
|
||||||
|
|
||||||
|
// ===================================================================================
|
||||||
|
// endregion
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user