From 28f1ecd987dbe86a59ce474f3f58eb75838edc68 Mon Sep 17 00:00:00 2001 From: Ida Massow Date: Sat, 30 Apr 2011 23:13:22 +0200 Subject: Juhu, StoneTray ist fertig und getestet!! git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@40 72836036-5685-4462-b002-a69064685172 --- src/jrummikub/model/StoneTray.java | 146 +++++++++++++++++++++++++++++++++---- 1 file changed, 133 insertions(+), 13 deletions(-) (limited to 'src/jrummikub/model') diff --git a/src/jrummikub/model/StoneTray.java b/src/jrummikub/model/StoneTray.java index 6691a45..1eb3b0e 100644 --- a/src/jrummikub/model/StoneTray.java +++ b/src/jrummikub/model/StoneTray.java @@ -1,15 +1,19 @@ package jrummikub.model; +import java.util.ArrayList; import java.util.Iterator; import java.util.List; import jrummikub.util.Pair; -/** A StoneTray is a collection of positioned objects (for example {@link Stone}s or {@link StoneSet}s. */ -public class StoneTray implements Iterable> { - protected List objects; - protected List positions; - +/** + * A StoneTray is a collection of positioned objects (for example {@link Stone}s + * or {@link StoneSet}s. + */ +public class StoneTray implements + Iterable> { + protected List> objects = new ArrayList>(); + /** * Removes object from tray and returns it * @@ -17,9 +21,30 @@ public class StoneTray implements Iterable * position of the object that will be removed */ public E pickUp(Position position) { - + for (Pair i : objects) { + Position currentPosition = i.getSecond(); + E currentObject = i.getFirst(); + // Tests if position is left of, above ... the current object + if (position.getX() < currentPosition.getX()) { + continue; + } + if (position.getY() < currentPosition.getY()) { + continue; + } + if (position.getX() > currentPosition.getX() + + currentObject.getWidth()) { + continue; + } + if (position.getY() > currentPosition.getY() + + currentObject.getHeight()) { + continue; + } + // Position is inside the current object + objects.remove(i); + return currentObject; + } + return null; } - /** * Adds object to the tray @@ -30,20 +55,115 @@ public class StoneTray implements Iterable * {@link Position} to put the object */ public void drop(E object, Position position) { - + drop(object, position, null); } + private void drop(E object, Position position, Direction direction) { + for (Pair i : objects) { + Position currentPosition = i.getSecond(); + E currentObject = i.getFirst(); + // Tests if position is left of, above ... the current object + if (position.getX() + object.getWidth() <= currentPosition.getX()) { + continue; + } + if (position.getY() + object.getHeight() <= currentPosition.getY()) { + continue; + } + if (position.getX() >= currentPosition.getX() + + currentObject.getWidth()) { + continue; + } + if (position.getY() >= currentPosition.getY() + + currentObject.getHeight()) { + continue; + } + // Object would be placed inside the current object + if (direction == null) { + direction = getMoveDirection(object, position, i); + } + Position newPosition = null; + // TODO dinge bewegen + switch (direction) { + case TOP: + newPosition = new Position(i.getSecond().getX(), + position.getY() - currentObject.getHeight()); + break; + case BOTTOM: + newPosition = new Position(currentPosition.getX(), + position.getY() + object.getHeight()); + break; + case LEFT: + newPosition = new Position(position.getX() + - currentObject.getWidth(), currentPosition.getY()); + break; + case RIGHT: + newPosition = new Position(position.getX() + object.getWidth(), + currentPosition.getY()); + break; + } + objects.remove(i); + drop(currentObject, newPosition, direction); + } + objects.add(new Pair(object, position)); + } - public Position getPosition (E object) { + private Direction getMoveDirection(E object, Position position, + Pair blocking) { + boolean isVertical = getMoveOrientationn(object, position, blocking); + float objectMidpointX = position.getX() + object.getWidth() / 2; + float objectMidpointY = position.getY() + object.getHeight() / 2; + float blockingMidpointX = blocking.getSecond().getX() + + blocking.getFirst().getWidth() / 2; + float blockingMidpointY = blocking.getSecond().getY() + + blocking.getFirst().getHeight() / 2; + if (isVertical) { + if (objectMidpointY < blockingMidpointY) { + return Direction.BOTTOM; + } else { + return Direction.TOP; + } + } else { + if (objectMidpointX < blockingMidpointX) { + return Direction.RIGHT; + } else { + return Direction.LEFT; + } + } + } - + private boolean getMoveOrientationn(E object, Position position, + Pair blocking) { + float objectRight = position.getX() + object.getWidth(); + float blockingRight = blocking.getSecond().getX() + + blocking.getFirst().getWidth(); + float overlapRight = Math.min(objectRight, blockingRight); + 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 overlapY = overlapBottom - overlapTop; + // vertical or horizontal Shift + // TODO magic factor + return overlapX > overlapY; } + public Position getPosition(E object) { + for (Pair i : objects) { + if (object.equals(i.getFirst())) { + return i.getSecond(); + } + } + return null; + } @Override public Iterator> iterator() { - // TODO Auto-generated method stub - return null; + return objects.iterator(); } - + } -- cgit v1.2.3