From f2ce7d4f778cc48fe47a56ac0c89a07c74abe7e0 Mon Sep 17 00:00:00 2001 From: Andreas Jakobsson Date: Wed, 7 Jan 2009 15:16:59 +0100 Subject: [PATCH] First try in collision detection --- src/se/umu/cs/dit06ajnajs/ATDController.java | 111 ++++++++++++++++++++++++++- src/se/umu/cs/dit06ajnajs/agent/Unit.java | 46 +++++++---- 2 files changed, 141 insertions(+), 16 deletions(-) diff --git a/src/se/umu/cs/dit06ajnajs/ATDController.java b/src/se/umu/cs/dit06ajnajs/ATDController.java index 5a8a0ff..2cc77c9 100644 --- a/src/se/umu/cs/dit06ajnajs/ATDController.java +++ b/src/se/umu/cs/dit06ajnajs/ATDController.java @@ -2,6 +2,7 @@ package se.umu.cs.dit06ajnajs; import java.awt.Graphics; +import java.awt.Point; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; @@ -67,8 +68,19 @@ public class ATDController { view.repaintGame(); while (running) { - // Update all agents List agents = model.getAgents(); + List units = new ArrayList(); + + // Check for collisions + for (Agent agent : agents) { + // Extract all units + if(agent instanceof Unit) { + units.add((Unit)agent); + } + } + collisionDetection(units); + + // Update all agents List deadAgents = new ArrayList(); for (Agent agent : agents) { if (agent.isAlive()) { @@ -119,6 +131,103 @@ public class ATDController { } } } + + /** + * Examins the units position to each other and pauses units that are + * about to collide + * + * //TODO units should not pause, they should slow down. + * + */ + private void collisionDetection(List units) { + for (Unit unit : units) { + + // Calculate next position + Direction direction = unit.getDirection(); + int x = unit.getX(); + int y = unit.getY(); + int speed = unit.getSpeed(); + Point newPoint = null; + switch (direction) { + case UP: + newPoint = new Point(x, y - speed); + case DOWN: + newPoint = new Point(x, y + speed); + case LEFT: + newPoint = new Point(x - speed, y); + case RIGHT: + newPoint = new Point(x + speed, y); + default: + System.err.println("Unit has not got a valid Direction"); + // TODO catch nullpointerexception? + } + + // The current units next position + int nextX1 = newPoint.x; + int nextX2 = nextX1 + unit.getWidth(); + int nextY1 = newPoint.y; + int nextY2 = nextY1 + unit.getHeight(); + + + // Look for collisions + for (Unit otherUnit : units) { + if (unit != otherUnit) { + int otherX1 = otherUnit.getX(); + int otherX2 = otherX1 + otherUnit.getWidth(); + int otherY1 = otherUnit.getY(); + int otherY2 = otherY1 + otherUnit.getHeight(); + + switch (direction) { + case UP: + // FLUMMIG ALGORITMBESKRIVNING FÖR UP: + // (unit1 övre kant kommer över unit2 nedre kant i y-led) + // otherY2 < nextY1 + // && + // (unit1 vänster kant ligger mellan unit2 kanter ELLER unit1 höger kant ligger mellan unit2 kanter + // otherX1 < nextX1 < otherY2 || otherX1 < nextX1 < otherY2 + + if( (nextY1 < otherY2) && + ( ((nextX1 > otherX1) && (nextX1 < otherX2)) || ((nextX2 > otherX1) && (nextX2 < otherX2)))) { + // Collision! + this.setCollision(unit, otherUnit); + } + break; + case DOWN: + if( (nextY2 > otherY1) && ( ((nextX1 > otherX1) && (nextX1 < otherX2)) || ((nextX2 > otherX1) && (nextX2 < otherX2)))) { + // Collision! + + } + break; + case LEFT: + if( (nextX2 > otherX1) && ( ((nextY1 > otherY1) && (nextY1 < otherY2)) || ((nextY2 > otherY1) && (nextY2 < otherY2)))) { + // Collision! + } + break; + case RIGHT: + if( (nextX1 < otherX2) && ( ((nextY1 > otherY1) && (nextY1 < otherY2)) || ((nextY2 > otherY1) && (nextY2 < otherY2)))) { + // Collision! + } + break; + default: + System.err.println("Unit has not got a valid Direction"); + // TODO catch nullpointerexception? + break; + } + } + } + } + } + + /** + * Pauses the unit + * //TODO Implement slower speed, not pause. The method does not + * use the other unit in this implementation + * @param unit + * @param otherUnit + */ + private void setCollision(Unit unit, Unit otherUnit) { + unit.setPause(true); + } } /* Connect listeners to View *************************************/ diff --git a/src/se/umu/cs/dit06ajnajs/agent/Unit.java b/src/se/umu/cs/dit06ajnajs/agent/Unit.java index 4864eba..f4941de 100644 --- a/src/se/umu/cs/dit06ajnajs/agent/Unit.java +++ b/src/se/umu/cs/dit06ajnajs/agent/Unit.java @@ -28,6 +28,7 @@ public abstract class Unit implements Agent, Paintable, Cloneable { private Direction direction; private Map map; private boolean alive; + private boolean pause; public Unit(int x, int y, int width, int height, int speed, int health, int cost, Image image, Direction direction, Map map) { @@ -43,6 +44,7 @@ public abstract class Unit implements Agent, Paintable, Cloneable { this.map = map; this.alive = true; + this.pause = false; } public void setImage(Image img) { @@ -54,23 +56,33 @@ public abstract class Unit implements Agent, Paintable, Cloneable { } public void act() { - // Check if this unit has health left - if (this.health >= 0) { - Point nextPos = getNextPosition(); - // TODO check for collision on next position - move(nextPos); - - // Land on current square - MapSquare currentSquare= map.getMapSquareAtPoint(centerX, centerY); - if (currentSquare instanceof Traversable) { - ((Traversable) currentSquare).landOn(this); - } - } else { - // Kill unit - this.alive = false; - } + + // Check if this unit has health left + if (this.health >= 0) { + // Check if this unit is active + if (!pause) { + Point nextPos = getNextPosition(); + // TODO check for collision on next position + move(nextPos); + + // Land on current square + MapSquare currentSquare= map.getMapSquareAtPoint(centerX, centerY); + if (currentSquare instanceof Traversable) { + ((Traversable) currentSquare).landOn(this); + } + } + else { + // Toggle pause back to false; + pause = false; + } + } else { + // Kill unit + this.alive = false; + } } + + public Point getNextPosition() { switch (direction) { case UP: @@ -229,4 +241,8 @@ public abstract class Unit implements Agent, Paintable, Cloneable { + " is not Cloneable"); } } + + public void setPause(boolean state) { + this.pause = state; + } } \ No newline at end of file -- 2.11.4.GIT