From 5b0aed8fd0615604e0d9e04977c5282e746207ee Mon Sep 17 00:00:00 2001 From: Bennet Gerlach Date: Mon, 9 May 2011 21:56:45 +0200 Subject: Fix drop position calculations git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@199 72836036-5685-4462-b002-a69064685172 --- src/jrummikub/model/Hand.java | 4 +++ src/jrummikub/model/StoneTray.java | 72 ++++++++++++++++++++------------------ 2 files changed, 41 insertions(+), 35 deletions(-) (limited to 'src/jrummikub/model') diff --git a/src/jrummikub/model/Hand.java b/src/jrummikub/model/Hand.java index 73ffab5..644ae94 100644 --- a/src/jrummikub/model/Hand.java +++ b/src/jrummikub/model/Hand.java @@ -3,4 +3,8 @@ package jrummikub.model; /** Class managing a {@link Player}'s {@link Stone}s */ public class Hand extends StoneTray implements IHand { + @Override + protected Position fixInvalidDrop(Stone stone, Position pos) { + return null; + } } diff --git a/src/jrummikub/model/StoneTray.java b/src/jrummikub/model/StoneTray.java index 94b09be..52079d0 100644 --- a/src/jrummikub/model/StoneTray.java +++ b/src/jrummikub/model/StoneTray.java @@ -11,7 +11,7 @@ import jrummikub.util.Pair; * or {@link StoneSet}s. * * @param - * Type of positioned objects (must implement Sizeable) + * Type of positioned objects (must implement Sizeable) */ public class StoneTray implements IStoneTray { protected HashMap> objects = new HashMap>(); @@ -22,11 +22,6 @@ public class StoneTray implements IStoneTray { LEFT, RIGHT, TOP, BOTTOM; } - /* - * (non-Javadoc) - * - * @see jrummikub.model.IStoneTray#pickUp(jrummikub.model.Position) - */ @Override public E pickUp(Position position) { for (Map.Entry> i : objects.entrySet()) { @@ -39,12 +34,10 @@ public class StoneTray implements IStoneTray { if (position.getY() < currentPosition.getY()) { continue; } - if (position.getX() > currentPosition.getX() - + currentObject.getWidth()) { + if (position.getX() > currentPosition.getX() + currentObject.getWidth()) { continue; } - if (position.getY() > currentPosition.getY() - + currentObject.getHeight()) { + if (position.getY() > currentPosition.getY() + currentObject.getHeight()) { continue; } // Position is inside the current object @@ -54,11 +47,6 @@ public class StoneTray implements IStoneTray { return null; } - /* - * (non-Javadoc) - * - * @see jrummikub.model.IStoneTray#drop(E, jrummikub.model.Position) - */ @Override public void drop(E object, Position position) { if (object != null) { @@ -68,32 +56,35 @@ public class StoneTray implements IStoneTray { @SuppressWarnings("unchecked") private void drop(E object, Position position, Direction direction) { + objects.put(object, new Pair(object, position)); for (Pair i : ((Map>) objects.clone()) .values()) { - Position currentPosition = i.getSecond(); + Direction newDirection = direction; E currentObject = i.getFirst(); - if (!objectsOverlap(object, position, currentObject, - currentPosition)) { + if (currentObject == object) + continue; + Position currentPosition = getPosition(currentObject); + if (!objectsOverlap(object, position, currentObject, currentPosition)) { continue; } // Object would be placed inside the current object - if (direction == null) { - direction = getMoveDirection(object, position, i); + if (newDirection == null) { + newDirection = getMoveDirection(object, position, i); } Position newPosition = null; // Move object to avoid overlap - switch (direction) { + switch (newDirection) { case TOP: - newPosition = new Position(currentPosition.getX(), - position.getY() - currentObject.getHeight()); + newPosition = new Position(currentPosition.getX(), position.getY() + - currentObject.getHeight()); break; case BOTTOM: - newPosition = new Position(currentPosition.getX(), - position.getY() + object.getHeight()); + newPosition = new Position(currentPosition.getX(), position.getY() + + object.getHeight()); break; case LEFT: - newPosition = new Position(position.getX() - - currentObject.getWidth(), currentPosition.getY()); + newPosition = new Position(position.getX() - currentObject.getWidth(), + currentPosition.getY()); break; case RIGHT: newPosition = new Position(position.getX() + object.getWidth(), @@ -101,10 +92,23 @@ public class StoneTray implements IStoneTray { break; } - objects.remove(i.getFirst()); + objects.remove(currentObject); drop(currentObject, newPosition, direction); } - objects.put(object, new Pair(object, position)); + } + + /** + * Checks whether the object may be placed on the given position, computes new + * position if not + * + * @param object + * to be dropped + * @param p + * the object is dropped at + * @return null if the drop is valid, new position otherwise + */ + protected Position fixInvalidDrop(E object, Position pos) { + return null; } /** Tests whether two objects overlap **/ @@ -156,15 +160,13 @@ public class StoneTray implements IStoneTray { float blockingRight = blocking.getSecond().getX() + blocking.getFirst().getWidth(); float overlapRight = Math.min(objectRight, blockingRight); - float overlapLeft = Math.max(position.getX(), blocking.getSecond() - .getX()); + float overlapLeft = Math.max(position.getX(), blocking.getSecond().getX()); float overlapX = overlapRight - overlapLeft; float objectBottom = position.getY() + object.getHeight(); float blockingBottom = blocking.getSecond().getY() + blocking.getFirst().getHeight(); float overlapBottom = Math.min(objectBottom, blockingBottom); - float overlapTop = Math - .max(position.getY(), blocking.getSecond().getY()); + float overlapTop = Math.max(position.getY(), blocking.getSecond().getY()); float overlapY = overlapBottom - overlapTop; // vertical or horizontal Shift // TODO magic factor @@ -207,11 +209,11 @@ public class StoneTray implements IStoneTray { try { StoneTray copy = (StoneTray) super.clone(); copy.objects = (HashMap>) objects.clone(); - + return copy; } catch (CloneNotSupportedException e) { e.printStackTrace(); - + return null; } } -- cgit v1.2.3