From ac937eb56324245b37bc63c2f149389bb89fa256 Mon Sep 17 00:00:00 2001 From: Arthur Bols Date: Sat, 5 May 2018 17:31:08 +0200 Subject: [PATCH] added castSpell --- OGP1718-Worms/src/worms/model/Food.java | 3 ++ OGP1718-Worms/src/worms/model/Projectile.java | 29 +++++++++++-- OGP1718-Worms/src/worms/model/World.java | 43 +++++++++++++++---- OGP1718-Worms/src/worms/model/Worm.java | 15 +++++-- 4 files changed, 75 insertions(+), 15 deletions(-) diff --git a/OGP1718-Worms/src/worms/model/Food.java b/OGP1718-Worms/src/worms/model/Food.java index ea40d67..c445642 100644 --- a/OGP1718-Worms/src/worms/model/Food.java +++ b/OGP1718-Worms/src/worms/model/Food.java @@ -34,6 +34,9 @@ public class Food extends GameObject { public void poison() { this.poisonous = true; } + public void heal() { + this.poisonous = false; + } private boolean poisonous = false; } diff --git a/OGP1718-Worms/src/worms/model/Projectile.java b/OGP1718-Worms/src/worms/model/Projectile.java index 79bf361..eff8345 100644 --- a/OGP1718-Worms/src/worms/model/Projectile.java +++ b/OGP1718-Worms/src/worms/model/Projectile.java @@ -4,12 +4,15 @@ public class Projectile extends GameObject { private static double rho = 7800; private double hitpoints; private double force; + private Type type; - public Projectile(World world, double[] location, double mass, double hitpoints, double force) { + public Projectile(World world, double[] location, double mass, double hitpoints, double force, Projectile.Type type) { super(world, location, calcRadius(mass)); setHitPoints(hitpoints); setMass(mass); setForce(force); + + this.type = type; } public static double calcRadius(double mass) { @@ -19,6 +22,21 @@ public class Projectile extends GameObject { public void setHitPoints(double value) { this.hitpoints = value; } + + public void setRandomHitPoints() { + + } + + public double getHitPoints() { + return this.hitpoints; + } + + public void increaseHitPoints(double value) { + + this.hitpoints += value; + if (this.type == Type.RIFLE && this.hitpoints > 10) this.hitpoints = 10; + else if (this.type == Type.BAZOOKA && this.hitpoints > 7) this.hitpoints = 7; + } public void setMass(double mass) { this.mass = mass; @@ -27,6 +45,9 @@ public class Projectile extends GameObject { public void setForce(double force) { this.force = force; } - - -} + + public enum Type { + RIFLE, + BAZOOKA; + } +} \ No newline at end of file diff --git a/OGP1718-Worms/src/worms/model/World.java b/OGP1718-Worms/src/worms/model/World.java index 29e38b9..6a0e1e3 100644 --- a/OGP1718-Worms/src/worms/model/World.java +++ b/OGP1718-Worms/src/worms/model/World.java @@ -527,25 +527,52 @@ public class World { if (item1 instanceof Worm && item2 instanceof Worm) { - System.out.println("test"); + if (((Worm) item1).getTeam().getName().equals(((Worm) item2).getTeam().getName())) { + long hitpoints = (long) Math.floor((((Worm) item1).getHitPoints() + ((Worm) item2).getHitPoints()) / 2); + ((Worm) item1).setHitPoints(hitpoints); + ((Worm) item2).setHitPoints(hitpoints); + + } else { + Worm lgWorm = (Worm) item1; + Worm smWorm = (Worm) item2; + + if (item1.getRadius() < item2.getRadius()) { + lgWorm = (Worm) item2; + smWorm = (Worm) item1; + } + + long lgHitPoints = lgWorm.getHitPoints(); + if (lgHitPoints < 5) { + lgWorm.setHitPoints(0); + smWorm.incrementHitPoints(lgHitPoints); + } else { + smWorm.incrementHitPoints(5); + lgWorm.decreaseHitPoints(5); + } + } } - else if (item1 instanceof Worm && item2 instanceof Food || - item1 instanceof Food && item2 instanceof Worm) { - System.out.println("test"); + else if (item1 instanceof Worm && item2 instanceof Food) { + ((Worm) item1).eat((Food) item2, false); } else if (item1 instanceof Worm && item2 instanceof Projectile || item1 instanceof Projectile && item2 instanceof Worm) { - System.out.println("test"); + // TODO hit by projectile, projectile gets random hit points } else if (item1 instanceof Food && item2 instanceof Food) { - System.out.println("test"); + if (((Food) item1).isPoisonous()) ((Food) item1).heal(); + else ((Food) item1).poison(); + + if (((Food) item2).isPoisonous()) ((Food) item2).heal(); + else ((Food) item2).poison(); } else if (item1 instanceof Food && item2 instanceof Projectile || item1 instanceof Projectile && item2 instanceof Food) { - System.out.println("test"); + item1.terminate(); + item2.terminate(); } else if (item1 instanceof Projectile && item2 instanceof Projectile) { - System.out.println("test"); + ((Projectile) item1).increaseHitPoints(2); + ((Projectile) item2).increaseHitPoints(2); } else { throw new IllegalArgumentException(); diff --git a/OGP1718-Worms/src/worms/model/Worm.java b/OGP1718-Worms/src/worms/model/Worm.java index ee4323d..bfe54ef 100644 --- a/OGP1718-Worms/src/worms/model/Worm.java +++ b/OGP1718-Worms/src/worms/model/Worm.java @@ -981,7 +981,7 @@ public class Worm extends GameObject { * | new.getHitPoints() == hitpoints */ @Raw - private void setHitPoints(long hitPoints) { + public void setHitPoints(long hitPoints) { if (hitPoints <= 0) terminate(); this.hitPoints = hitPoints; @@ -1016,6 +1016,8 @@ public class Worm extends GameObject { setHitPoints(getHitPoints() + value); } + + //=================================================================================== // endregion @@ -1129,6 +1131,10 @@ public class Worm extends GameObject { // =================================================================================== // endregion + public void eat(Food food) { + this.eat(food, true); + } + /** * The worm eats food and grows. * @@ -1150,9 +1156,9 @@ public class Worm extends GameObject { * @post Let the worm eat if necessary. * |checkEat() */ - public void eat(Food food) { + public void eat(Food food, boolean terminate) { - food.terminate(); + if (terminate) food.terminate(); double radius = getRadius(); double changeRadius = radius * 0.1; @@ -1231,6 +1237,9 @@ public class Worm extends GameObject { } } } + + + // region firing and projectiles //=================================================================================== private double[] locationProj;