fix projectile, refactoring
This commit is contained in:
@@ -19,6 +19,11 @@ public class Bazooka extends Projectile {
|
|||||||
return nb;
|
return nb;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected int getImpactHitPoints() {
|
||||||
|
return this.hitPoints * (int) this.force;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void setHitPoints(int value) {
|
protected void setHitPoints(int value) {
|
||||||
if (value > 7) value = 7;
|
if (value > 7) value = 7;
|
||||||
@@ -26,8 +31,6 @@ public class Bazooka extends Projectile {
|
|||||||
if (value == 0) value++;
|
if (value == 0) value++;
|
||||||
else value--;
|
else value--;
|
||||||
}
|
}
|
||||||
double force = getForce();
|
|
||||||
value = value* (int) force;
|
|
||||||
super.hitPoints = value;
|
super.hitPoints = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -258,7 +258,11 @@ public abstract class GameObject {
|
|||||||
|
|
||||||
Coordinate otherLocation = o.getLocation();
|
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) {
|
public double getAngle(GameObject o) {
|
||||||
|
@@ -26,7 +26,7 @@ public abstract class Projectile extends GameObject {
|
|||||||
public double getForce() {
|
public double getForce() {
|
||||||
return this.force;
|
return this.force;
|
||||||
}
|
}
|
||||||
private final double force;
|
protected final double force;
|
||||||
|
|
||||||
|
|
||||||
protected int hitPoints;
|
protected int hitPoints;
|
||||||
@@ -37,6 +37,8 @@ public abstract class Projectile extends GameObject {
|
|||||||
return this.hitPoints;
|
return this.hitPoints;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected abstract int getImpactHitPoints();
|
||||||
|
|
||||||
protected abstract void setHitPoints(int value) throws IllegalArgumentException;
|
protected abstract void setHitPoints(int value) throws IllegalArgumentException;
|
||||||
|
|
||||||
public void incrementHitPoints(int value) {
|
public void incrementHitPoints(int value) {
|
||||||
@@ -83,7 +85,7 @@ public abstract class Projectile extends GameObject {
|
|||||||
return (getForce() / (getMass() / 1000)) * FORCE_TIME;
|
return (getForce() / (getMass() / 1000)) * FORCE_TIME;
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getJumpTime(double jumpTimeStep) {
|
public double getJumpTime(double timeStep) {
|
||||||
World world = getWorld();
|
World world = getWorld();
|
||||||
|
|
||||||
if (world == null) {
|
if (world == null) {
|
||||||
@@ -93,33 +95,26 @@ public abstract class Projectile extends GameObject {
|
|||||||
double v = jumpVelocity();
|
double v = jumpVelocity();
|
||||||
double time = 0;
|
double time = 0;
|
||||||
double a = getOrientation();
|
double a = getOrientation();
|
||||||
Coordinate loc = getLocation();
|
|
||||||
double radius = getRadius();
|
double radius = getRadius();
|
||||||
Coordinate newLoc;
|
List<Worm> worms = world.getGameObjectsByClass(Worm.class);
|
||||||
|
|
||||||
while (true) {
|
if (!world.isPassable(this.location) ||
|
||||||
time += jumpTimeStep;
|
worms.stream().anyMatch(w -> w.getDistance(this) < 0)) {
|
||||||
double x = loc.getX() + v * time * cos(a);
|
return 0.0;
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
return time;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public double getDistance(Coordinate start, Coordinate end) {
|
public double getDistance(Coordinate start, Coordinate end) {
|
||||||
return Math.sqrt(Math.pow(Math.abs(start.getX() - end.getX()), 2) +
|
return Math.sqrt(Math.pow(Math.abs(start.getX() - end.getX()), 2) +
|
||||||
@@ -138,11 +133,11 @@ public abstract class Projectile extends GameObject {
|
|||||||
double v = jumpVelocity();
|
double v = jumpVelocity();
|
||||||
double t = getJumpTime(jumpTimeStep);
|
double t = getJumpTime(jumpTimeStep);
|
||||||
double a = getOrientation();
|
double a = getOrientation();
|
||||||
Coordinate newLocation = Coordinate.create(getLocation().toArray());
|
|
||||||
|
Coordinate newLocation = this.location;
|
||||||
|
|
||||||
List<Worm> worms = getWorld().getGameObjectsByClass(Worm.class);
|
List<Worm> worms = getWorld().getGameObjectsByClass(Worm.class);
|
||||||
|
|
||||||
|
|
||||||
if (!getWorld().isAdjacent(getLocation(),getRadius())) {
|
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);
|
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){
|
public void hit(Worm worm){
|
||||||
|
|
||||||
worm.decreaseHitPoints(getHitPoints());
|
worm.decreaseHitPoints(getImpactHitPoints());
|
||||||
terminate();
|
terminate();
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -19,6 +19,11 @@ public class Rifle extends Projectile {
|
|||||||
return ThreadLocalRandom.current().nextInt(1,6) * 2;
|
return ThreadLocalRandom.current().nextInt(1,6) * 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected int getImpactHitPoints() {
|
||||||
|
return this.hitPoints;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void setHitPoints(int value) {
|
protected void setHitPoints(int value) {
|
||||||
if (value > 10) value = 10;
|
if (value > 10) value = 10;
|
||||||
|
@@ -560,7 +560,6 @@ public class World {
|
|||||||
lgWorm = (Worm) item2;
|
lgWorm = (Worm) item2;
|
||||||
smWorm = (Worm) item1;
|
smWorm = (Worm) item1;
|
||||||
}
|
}
|
||||||
|
|
||||||
long lgHitPoints = lgWorm.getActionPoints();
|
long lgHitPoints = lgWorm.getActionPoints();
|
||||||
if (lgHitPoints < 5) {
|
if (lgHitPoints < 5) {
|
||||||
lgWorm.setActionPoints(0);
|
lgWorm.setActionPoints(0);
|
||||||
@@ -580,8 +579,8 @@ public class World {
|
|||||||
else if (item1 instanceof Worm && item2 instanceof Projectile ||
|
else if (item1 instanceof Worm && item2 instanceof Projectile ||
|
||||||
item1 instanceof Projectile && item2 instanceof Worm) {
|
item1 instanceof Projectile && item2 instanceof Worm) {
|
||||||
|
|
||||||
Worm worm = null;
|
Worm worm;
|
||||||
Projectile proj = null;
|
Projectile proj;
|
||||||
if (item1 instanceof Worm) {
|
if (item1 instanceof Worm) {
|
||||||
worm = (Worm) item1;
|
worm = (Worm) item1;
|
||||||
proj = (Projectile) item2;
|
proj = (Projectile) item2;
|
||||||
|
@@ -110,7 +110,7 @@ public class Worm extends GameObject {
|
|||||||
throws IllegalArgumentException {
|
throws IllegalArgumentException {
|
||||||
super(world, location, radius);
|
super(world, location, radius);
|
||||||
|
|
||||||
this.orientation = orientation;
|
setOrientation(orientation);
|
||||||
|
|
||||||
if (!isValidMinRadius(minRadius))
|
if (!isValidMinRadius(minRadius))
|
||||||
throw new IllegalArgumentException("Invalid min radius");
|
throw new IllegalArgumentException("Invalid min radius");
|
||||||
@@ -1088,59 +1088,26 @@ public class Worm extends GameObject {
|
|||||||
World world = this.world;
|
World world = this.world;
|
||||||
Coordinate location = this.location;
|
Coordinate location = this.location;
|
||||||
if (!world.isAdjacent(location, radius)) {
|
if (!world.isAdjacent(location, radius)) {
|
||||||
Coordinate newLoc = Coordinate.create(location.getX() + changeRadius, location.getY());
|
|
||||||
|
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)) {
|
if (world.isAdjacent(newLoc, radius)) {
|
||||||
this.radius = radius;
|
this.radius = radius;
|
||||||
setLocation(newLoc);
|
setLocation(newLoc);
|
||||||
setRadius(radius);
|
setRadius(radius);
|
||||||
return;
|
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();
|
terminate();
|
||||||
return;
|
} else {
|
||||||
}
|
|
||||||
}
|
|
||||||
setLocation(location);
|
setLocation(location);
|
||||||
setRadius(radius);
|
setRadius(radius);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If the worm can eat food (depends on his location), the worms eats the food.
|
* If the worm can eat food (depends on his location), the worms eats the food.
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user