improved passable

This commit is contained in:
2018-04-12 17:24:08 +02:00
parent 51dbd29285
commit dd2190910b
7 changed files with 94 additions and 57 deletions

View File

@@ -1,24 +1,27 @@
import worms.facade.Facade;
import worms.facade.IFacade;
import worms.model.Team;
import worms.model.World;
import worms.model.Worm;
import worms.util.Coordinate;
import java.io.Console;
public class Main {
private static final double EPS = 1e-4;
private static boolean[][] passableMap = new boolean[][] { //
{ false, false, false, false }, //
{ true, true, true, true }, //
{ true, true, true, true }, //
{ false, false, false, false } };
public static void main(String[] args) {
Worm worm1 = new Worm(Coordinate.create(0.0, 0.0), 0.0, "Alpha", 1.0);
Worm worm2 = new Worm(Coordinate.create(0.0, 0.0), 0.0, "Beta", 1.0);
Worm worm3 = new Worm(Coordinate.create(0.0, 0.0), 0.0, "Gamma", 1.0);
Worm worm4 = new Worm(Coordinate.create(0.0, 0.0), 0.0, "Delta", 1.0);
IFacade facade = new Facade();
World world = facade.createWorld(4.0, 4.0, passableMap);
Team team1 = new Team("TestTeam");
Worm worm = facade.createWorm(world, new double[] { 1, 1.5 }, Math.PI / 2, 0.5, "Test", null);
System.out.println(world.isPassable(worm.getLocationArray(), worm.getRadius()));
team1.addWorm(worm1);
team1.addWorm(worm2);
team1.addWorm(worm3);
team1.addWorm(worm4);
team1.getAllWormsOfTeam();
}
}

View File

@@ -618,7 +618,7 @@ public class Facade implements IFacade {
*/
@Override
public Worm createWorm(World world, double[] location, double direction, double radius, String name, Team team) throws ModelException {
return null;
return new Worm(world, location, direction, radius, name, team);
}
/**

View File

@@ -119,15 +119,15 @@ public abstract class GameObject {
double radius = getRadius();
if (world == null) {
return !Double.isNaN(location.getCoordinateX()) &&
!Double.isNaN(location.getCoordinateY());
return !Double.isNaN(location.getX()) &&
!Double.isNaN(location.getY());
}
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 &&
return !Double.isNaN(location.getX()) &&
!Double.isNaN(location.getY()) &&
!(location.getX() - radius < 0) &&
!(location.getX() + radius > getWorld().getWidth()) &&
!(location.getY() + radius > getWorld().getHeight()) &&
!(location.getY() - radius < 0 &&
!getWorld().isPassable(location));
}

View File

@@ -146,21 +146,42 @@ public class World {
*/
public boolean isPassable(double[] center, double radius) {
int radX = (int) Math.floor(radius / lengthX);
ArrayList<Coordinate> circle = getCircleList(Coordinate.create(center),radius);
for (int x = 0, y = 0; x <= radX; x++, y++) {
double realX = x * radius;
double realY = Math.sqrt(Math.pow(radius, 2) - Math.pow(realX, 2));
if (canHaveAsPartialCircle(realX, realY, center)) return false;
realY = y * radius;
realX = Math.sqrt(Math.pow(radius, 2) + center[0] - Math.pow(realY, 2) + center[1]);
if (canHaveAsPartialCircle(realX, realY, center)) return false;
for (Coordinate cord: circle) {
if (!isPassable(cord.getX(), cord.getY())) return false;
}
return true;
}
private boolean isUnderCircle(Coordinate square, Coordinate center, double radius) {
double angle = Math.atan2(square.getY() - center.getY(), square.getX() - center.getX());
double x = Math.abs(radius * Math.cos(angle) + center.getX());
double y = Math.abs(radius * Math.sin(angle) + center.getY());
return Math.floor(square.getX() / getLengthX()) - Math.floor(x / getLengthX()) == 0 &&
Math.floor(square.getY() / getLengthY()) - Math.floor(y / getLengthY()) == 0;
}
public ArrayList<Coordinate> getCircleList(Coordinate center, double radius) {
double[] pos = {Math.floor(center.getX()), Math.floor(center.getY())};
ArrayList<Coordinate> circle = new ArrayList<Coordinate>();
int parts = (int) Math.ceil(2 * radius / getLengthX());
for (int x = 0; x <= parts; x++) {
for (int y = 0; y <= parts; y++) {
Coordinate temp = Coordinate.create(x * getLengthX(), y * getLengthY());
if (isUnderCircle(temp, center, radius)) {
circle.add(temp);
}
}
}
return circle;
}
/**
*
* @param center ...
@@ -176,6 +197,12 @@ public class World {
return isPassable(center, radius) && !isPassable(center, radius + maxDistance + length);
}
public double getLengthX() {
return this.lengthX;
}
public double getLengthY() {
return this.lengthY;
}
private final double lengthX;
private final double lengthY;

View File

@@ -3,7 +3,6 @@ package worms.model;
import be.kuleuven.cs.som.annotate.*;
import worms.util.Coordinate;
import worms.util.IllegalNameException;
import worms.util.MustNotImplementException;
import static java.lang.Math.*;
@@ -130,6 +129,13 @@ public class Worm extends GameObject {
setHitPoints(startHitPoints);
}
public Worm(World world, double[] location, double direction, double radius, String name, Team team) {
this(Coordinate.create(location), direction, name, radius, 0.25);
setWorld(world);
setTeam(team);
}
//===================================================================================
// endregion
@@ -220,6 +226,7 @@ public class Worm extends GameObject {
this.radius = radius;
final double rho = 1062;
setMass(radius, rho);
setMaxActionPoints(getMass());
}
/**
@@ -469,13 +476,13 @@ public class Worm extends GameObject {
* that the x-coordinate should move (distance is equal to the radius multiplied
* with the cosinus of the orientation)
* |distanceX = this.radius * cos(getOrientation())
* |new.xCoordinate = getLocation().getCoordinateX() + numberSteps * distanceX
* |new.xCoordinate = getLocation().getX() + numberSteps * distanceX
* @post the y-coordinate of the new location of the worm should be the location of
* the old y-coordinate plus the number of steps multiplied with the distance
* that the y-coordinate should move (distance is equal to the radius multiplied
* with the sinus of the orientation)
* |distanceY = this.radius * sin(getOrientation())
* |new.yCoordinate = getLocation().getCoordinateY() + numberSteps * distanceY
* |new.yCoordinate = getLocation().getY() + numberSteps * distanceY
* @post the current value of action actionPoints has changed. The current value of action actionPoints
* minus the cost of moving (abs(cos(theta)) + abs(4 sin(theta)))
* |cost = (long) ceil(abs(cos(getOrientation())) + abs(4 * sin(getOrientation()))) * numberSteps
@@ -511,19 +518,19 @@ public class Worm extends GameObject {
Coordinate currentLocation = getLocation();
double radius = getRadius();
World world = getWorld();
double step = Math.sqrt(Math.pow(world.getWidth(), 2) + Math.pow(world.getHeight(), 2));
double step = Math.sqrt(Math.pow(world.getLengthX(), 2) + Math.pow(world.getLengthY(), 2));
Coordinate nextLoc, prevLoc;
nextLoc = prevLoc = currentLocation;
while(getDistance(currentLocation, nextLoc) < maxDistance) {
prevLoc = nextLoc;
nextLoc = Coordinate.create(nextLoc.getCoordinateX() + step * Math.cos(direction),
nextLoc.getCoordinateY() + step * Math.sin(direction));
nextLoc = Coordinate.create(round((nextLoc.getX() + step * cos(direction)) * 100.0) / 100.0,
round((nextLoc.getY() + step * sin(direction)) * 100.0) / 100.0);
if (!world.isPassable(nextLoc.toArray(), radius)) {
while (!world.isPassable(nextLoc.toArray(), radius)) {
nextLoc = Coordinate.create(nextLoc.getCoordinateX() - 0.1 * Math.cos(direction),
nextLoc.getCoordinateY() - 0.1 * Math.sin(direction));
nextLoc = Coordinate.create(nextLoc.getX() - 0.1 * cos(direction),
nextLoc.getY() - 0.1 * sin(direction));
}
break;
}
@@ -550,8 +557,8 @@ public class Worm extends GameObject {
}
public double getDistance(Coordinate start, Coordinate end) {
return Math.sqrt(Math.pow(Math.abs(start.getCoordinateX() - end.getCoordinateX()), 2) +
Math.pow(Math.abs(start.getCoordinateY() - end.getCoordinateY()), 2));
return Math.sqrt(Math.pow(Math.abs(start.getX() - end.getX()), 2) +
Math.pow(Math.abs(start.getY() - end.getY()), 2));
}
public void collision(Worm basicWorm, Worm... worm) {
@@ -642,7 +649,7 @@ public class Worm extends GameObject {
*
* @post the worm jumps to his new place. The new place is the x-coordinate plus the jump distance
* with the jump velocity
* |setLocation(Coordinate.create(getLocation().getCoordinateX() + jumpDistance(this.jumpVelocity()), getLocation().getCoordinateY()))
* |setLocation(Coordinate.create(getLocation().getX() + jumpDistance(this.jumpVelocity()), getLocation().getY()))
* @post the current action actionPoints should be 0 after a jump
* |setActionPoints(0)
* @throws IllegalStateException
@@ -655,7 +662,7 @@ public class Worm extends GameObject {
if (!canJump())
throw new IllegalStateException();
setLocation(Coordinate.create( getLocation().getCoordinateX() + jumpDistance(this.jumpVelocity()), getLocation().getCoordinateY()));
setLocation(Coordinate.create( getLocation().getX() + jumpDistance(this.jumpVelocity()), getLocation().getY()));
setActionPoints(0);
}
@@ -685,8 +692,8 @@ public class Worm extends GameObject {
* multiplied with the cosinus of the orientation multiplied with delta time. The new y-coordinate is the old y-coordinate plus
* the jump velocity multiplied with the sinus of the orientation multiplied with delta time minus 0,5 times the gravity multiplied
* with the second power of delta time
* |Coordinate.create(getLocation().getCoordinateX() + this.jumpVelocity() * cos(getOrientation()) * deltaTime,
|getLocation().getCoordinateY() + this.jumpVelocity() * sin(getOrientation()) * deltaTime - 0.5 * G * pow(deltaTime, 2))
* |Coordinate.create(getLocation().getX() + this.jumpVelocity() * cos(getOrientation()) * deltaTime,
|getLocation().getY() + this.jumpVelocity() * sin(getOrientation()) * deltaTime - 0.5 * G * pow(deltaTime, 2))
* @throws IllegalArgumentException()
* if the deltaTime is not a number or bigger then jumpTime or smaller then 0
* |if (Double.isNaN(deltaTime) || deltaTime > this.jumpTime() || deltaTime < 0)
@@ -695,8 +702,8 @@ public class Worm extends GameObject {
if (Double.isNaN(deltaTime) || deltaTime > this.jumpTime() || deltaTime < 0)
throw new IllegalArgumentException();
return Coordinate.create(getLocation().getCoordinateX() + this.jumpVelocity() * cos(getOrientation()) * deltaTime,
getLocation().getCoordinateY() + this.jumpVelocity() * sin(getOrientation()) * deltaTime - 0.5 * G * pow(deltaTime, 2));
return Coordinate.create(getLocation().getX() + this.jumpVelocity() * cos(getOrientation()) * deltaTime,
getLocation().getY() + this.jumpVelocity() * sin(getOrientation()) * deltaTime - 0.5 * G * pow(deltaTime, 2));
}
/**
@@ -818,17 +825,17 @@ public class Worm extends GameObject {
// region falling
//===================================================================================
private void fall() {
double[] center = {getLocation().getCoordinateX(), getLocation().getCoordinateY()};
public void fall() {
double[] center = {getLocation().getX(), getLocation().getY()};
Coordinate oldLocation = getLocation();
double endY = getLocation().getCoordinateY();
double endY = getLocation().getY();
if (! getWorld().isAdjacent(center, minRadius)) {
for (double y = oldLocation.getCoordinateY(); y < 0; y--) {
if (y < 0) {
terminate();
}
for (double y = oldLocation.getY(); y < 0; y--) {
if (y < 0) {
terminate();
}
if (getWorld().isAdjacent(center, minRadius)) {
double[] newLoc = {oldLocation.getCoordinateX(), y};
double[] newLoc = {oldLocation.getX(), y};
setLocation(newLoc);
endY = y;
break;
@@ -840,7 +847,7 @@ public class Worm extends GameObject {
//TODO hitpoints en eventuele botsing
double distanceY = getWorld().getHeight();
long cost = 3 * (long) (oldLocation.getCoordinateY() - endY);
long cost = 3 * (long) (oldLocation.getY() - endY);
incrementHitPoints(cost);
}

View File

@@ -52,15 +52,15 @@ public class Coordinate extends Tuple<Double, Double> {
public double[] toArray() {
return new double[]{getCoordinateX(), getCoordinateY()};
return new double[]{getX(), getY()};
}
@Basic @Immutable
public double getCoordinateX() {
public double getX() {
return this.item1;
}
@Basic @Immutable
public double getCoordinateY() {
public double getY() {
return this.item2;
}
}