fix projectile, refactoring

This commit is contained in:
2018-05-23 15:59:29 +02:00
parent 38446cabb6
commit 8aa2699d69
6 changed files with 58 additions and 85 deletions

View File

@@ -19,6 +19,11 @@ public class Bazooka extends Projectile {
return nb;
}
@Override
protected int getImpactHitPoints() {
return this.hitPoints * (int) this.force;
}
@Override
protected void setHitPoints(int value) {
if (value > 7) value = 7;
@@ -26,8 +31,6 @@ public class Bazooka extends Projectile {
if (value == 0) value++;
else value--;
}
double force = getForce();
value = value* (int) force;
super.hitPoints = value;
}

View File

@@ -258,7 +258,11 @@ public abstract class GameObject {
Coordinate otherLocation = o.getLocation();
return Math.sqrt(Math.pow((otherLocation.getX() - location.getX()), 2) + Math.pow((otherLocation.getY() - location.getY()), 2)) - o.getRadius() - getRadius();
return Math.round((Math.sqrt(Math.pow((otherLocation.getX() - location.getX()), 2) + Math.pow((otherLocation.getY() - location.getY()), 2)) - o.getRadius() - getRadius()) * 100000000.0) / 100000000.0;
}
public double getDistance(Coordinate otherLocation, double radius) {
return Math.round((Math.sqrt(Math.pow((otherLocation.getX() - location.getX()), 2) + Math.pow((otherLocation.getY() - location.getY()), 2)) - radius - getRadius()) * 100000000.0) / 100000000.0;
}
public double getAngle(GameObject o) {

View File

@@ -26,7 +26,7 @@ public abstract class Projectile extends GameObject {
public double getForce() {
return this.force;
}
private final double force;
protected final double force;
protected int hitPoints;
@@ -37,6 +37,8 @@ public abstract class Projectile extends GameObject {
return this.hitPoints;
}
protected abstract int getImpactHitPoints();
protected abstract void setHitPoints(int value) throws IllegalArgumentException;
public void incrementHitPoints(int value) {
@@ -83,7 +85,7 @@ public abstract class Projectile extends GameObject {
return (getForce() / (getMass() / 1000)) * FORCE_TIME;
}
public double getJumpTime(double jumpTimeStep) {
public double getJumpTime(double timeStep) {
World world = getWorld();
if (world == null) {
@@ -93,32 +95,25 @@ public abstract class Projectile extends GameObject {
double v = jumpVelocity();
double time = 0;
double a = getOrientation();
Coordinate loc = getLocation();
double radius = getRadius();
Coordinate newLoc;
List<Worm> worms = world.getGameObjectsByClass(Worm.class);
while (true) {
time += jumpTimeStep;
double x = loc.getX() + v * time * cos(a);
double y = loc.getY() + v * time * sin(a) - (G * time * time) / 2.0;
newLoc = Coordinate.create(x, y);
if (! world.isPassable(newLoc, radius) || x < 0 || y < 0 || x > world.getWidth() || y > world.getHeight()) {
while (true) {
time -= jumpTimeStep;
newLoc = Coordinate.create(loc.getX() + v * time * cos(a), loc.getY() + v * time * sin(a) - (G * time * time) / 2.0);
if (newLoc.getX() < 0 || newLoc.getY() < 0 || newLoc.getX() > getWorld().getWidth() ||
newLoc.getY() > world.getHeight() || world.isAdjacent(newLoc, radius)) {
if (getDistance(loc, newLoc) < getRadius()) throw new IllegalStateException();
break;
}
}
break;
}
if ((int) round(time) == 10) {
throw new RuntimeException();
}
}
return time;
if (!world.isPassable(this.location) ||
worms.stream().anyMatch(w -> w.getDistance(this) < 0)) {
return 0.0;
}
while(true) {
time += timeStep;
Coordinate newLoc = Coordinate.create(this.location.getX() + v * time * cos(a),
this.location.getY() + v * time * sin(a) - (G * time * time) / 2.0);
if (newLoc.getX() - radius < 0 || newLoc.getY() - radius < 0 || newLoc.getX() + radius > world.getWidth() ||
newLoc.getY() + radius > world.getHeight() || !world.isPassable(newLoc, radius) ||
worms.stream().anyMatch(w -> w.getDistance(newLoc, this.radius) < 0)) {
return time;
}
}
}
public double getDistance(Coordinate start, Coordinate end) {
@@ -138,11 +133,11 @@ public abstract class Projectile extends GameObject {
double v = jumpVelocity();
double t = getJumpTime(jumpTimeStep);
double a = getOrientation();
Coordinate newLocation = Coordinate.create(getLocation().toArray());
Coordinate newLocation = this.location;
List<Worm> worms = getWorld().getGameObjectsByClass(Worm.class);
if (!getWorld().isAdjacent(getLocation(),getRadius())) {
newLocation = Coordinate.create(getLocation().getX() + v * t * cos(a), getLocation().getY() + v * t * sin(a) - (G * t * t) / 2.0);
}
@@ -169,7 +164,7 @@ public abstract class Projectile extends GameObject {
public void hit(Worm worm){
worm.decreaseHitPoints(getHitPoints());
worm.decreaseHitPoints(getImpactHitPoints());
terminate();
}
}

View File

@@ -19,6 +19,11 @@ public class Rifle extends Projectile {
return ThreadLocalRandom.current().nextInt(1,6) * 2;
}
@Override
protected int getImpactHitPoints() {
return this.hitPoints;
}
@Override
protected void setHitPoints(int value) {
if (value > 10) value = 10;

View File

@@ -560,7 +560,6 @@ public class World {
lgWorm = (Worm) item2;
smWorm = (Worm) item1;
}
long lgHitPoints = lgWorm.getActionPoints();
if (lgHitPoints < 5) {
lgWorm.setActionPoints(0);
@@ -580,8 +579,8 @@ public class World {
else if (item1 instanceof Worm && item2 instanceof Projectile ||
item1 instanceof Projectile && item2 instanceof Worm) {
Worm worm = null;
Projectile proj = null;
Worm worm;
Projectile proj;
if (item1 instanceof Worm) {
worm = (Worm) item1;
proj = (Projectile) item2;

View File

@@ -110,7 +110,7 @@ public class Worm extends GameObject {
throws IllegalArgumentException {
super(world, location, radius);
this.orientation = orientation;
setOrientation(orientation);
if (!isValidMinRadius(minRadius))
throw new IllegalArgumentException("Invalid min radius");
@@ -1088,57 +1088,24 @@ public class Worm extends GameObject {
World world = this.world;
Coordinate location = this.location;
if (!world.isAdjacent(location, radius)) {
Coordinate newLoc = Coordinate.create(location.getX() + changeRadius, location.getY());
if (world.isAdjacent(newLoc, radius)) {
this.radius = radius;
setLocation(newLoc);
setRadius(radius);
return;
}
newLoc = Coordinate.create(location.getX() - changeRadius, location.getY());
if (world.isAdjacent(newLoc, radius)) {
setLocation(newLoc);
setRadius(radius);
return;
}
newLoc = Coordinate.create(location.getX(), location.getY() - changeRadius);
if (world.isAdjacent(newLoc, radius)) {
setLocation(newLoc);
setRadius(radius);
return;
}
newLoc = Coordinate.create(location.getX(), location.getY() + changeRadius);
if (world.isAdjacent(newLoc, radius)) {
setLocation(newLoc);
setRadius(radius);
return;
}
newLoc = Coordinate.create(location.getX() - changeRadius, location.getY() - changeRadius);
if (world.isAdjacent(newLoc, radius)) {
setLocation(newLoc);
setRadius(radius);
return;
}
newLoc = Coordinate.create(location.getX() + changeRadius, location.getY() + changeRadius);
if (world.isAdjacent(newLoc, radius)) {
setLocation(newLoc);
setRadius(radius);
return;
}
newLoc = Coordinate.create(location.getX() + changeRadius, location.getY() - changeRadius);
if (world.isAdjacent(newLoc, radius)) {
setLocation(newLoc);
setRadius(radius);
return;
}
newLoc = Coordinate.create(location.getX() - changeRadius, location.getY() + changeRadius);
if (!world.isAdjacent(newLoc, radius)) {
terminate();
return;
for (double x = -1.0; x < 2; x++) {
for (double y = -1.0; y < 2; y++) {
Coordinate newLoc = Coordinate.create(location.getX() + changeRadius * x, location.getY() + changeRadius * y);
if (world.isAdjacent(newLoc, radius)) {
this.radius = radius;
setLocation(newLoc);
setRadius(radius);
return;
}
}
}
terminate();
} else {
setLocation(location);
setRadius(radius);
}
setLocation(location);
setRadius(radius);
}
/**