Fix drop position calculations

git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@199 72836036-5685-4462-b002-a69064685172
This commit is contained in:
Bennet Gerlach 2011-05-09 21:56:45 +02:00
parent 5767486e83
commit 5b0aed8fd0
2 changed files with 41 additions and 35 deletions

View file

@ -3,4 +3,8 @@ package jrummikub.model;
/** Class managing a {@link Player}'s {@link Stone}s */ /** Class managing a {@link Player}'s {@link Stone}s */
public class Hand extends StoneTray<Stone> implements IHand { public class Hand extends StoneTray<Stone> implements IHand {
@Override
protected Position fixInvalidDrop(Stone stone, Position pos) {
return null;
}
} }

View file

@ -22,11 +22,6 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
LEFT, RIGHT, TOP, BOTTOM; LEFT, RIGHT, TOP, BOTTOM;
} }
/*
* (non-Javadoc)
*
* @see jrummikub.model.IStoneTray#pickUp(jrummikub.model.Position)
*/
@Override @Override
public E pickUp(Position position) { public E pickUp(Position position) {
for (Map.Entry<E, Pair<E, Position>> i : objects.entrySet()) { for (Map.Entry<E, Pair<E, Position>> i : objects.entrySet()) {
@ -39,12 +34,10 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
if (position.getY() < currentPosition.getY()) { if (position.getY() < currentPosition.getY()) {
continue; continue;
} }
if (position.getX() > currentPosition.getX() if (position.getX() > currentPosition.getX() + currentObject.getWidth()) {
+ currentObject.getWidth()) {
continue; continue;
} }
if (position.getY() > currentPosition.getY() if (position.getY() > currentPosition.getY() + currentObject.getHeight()) {
+ currentObject.getHeight()) {
continue; continue;
} }
// Position is inside the current object // Position is inside the current object
@ -54,11 +47,6 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
return null; return null;
} }
/*
* (non-Javadoc)
*
* @see jrummikub.model.IStoneTray#drop(E, jrummikub.model.Position)
*/
@Override @Override
public void drop(E object, Position position) { public void drop(E object, Position position) {
if (object != null) { if (object != null) {
@ -68,32 +56,35 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private void drop(E object, Position position, Direction direction) { private void drop(E object, Position position, Direction direction) {
objects.put(object, new Pair<E, Position>(object, position));
for (Pair<E, Position> i : ((Map<E, Pair<E, Position>>) objects.clone()) for (Pair<E, Position> i : ((Map<E, Pair<E, Position>>) objects.clone())
.values()) { .values()) {
Position currentPosition = i.getSecond(); Direction newDirection = direction;
E currentObject = i.getFirst(); E currentObject = i.getFirst();
if (!objectsOverlap(object, position, currentObject, if (currentObject == object)
currentPosition)) { continue;
Position currentPosition = getPosition(currentObject);
if (!objectsOverlap(object, position, currentObject, currentPosition)) {
continue; continue;
} }
// Object would be placed inside the current object // Object would be placed inside the current object
if (direction == null) { if (newDirection == null) {
direction = getMoveDirection(object, position, i); newDirection = getMoveDirection(object, position, i);
} }
Position newPosition = null; Position newPosition = null;
// Move object to avoid overlap // Move object to avoid overlap
switch (direction) { switch (newDirection) {
case TOP: case TOP:
newPosition = new Position(currentPosition.getX(), newPosition = new Position(currentPosition.getX(), position.getY()
position.getY() - currentObject.getHeight()); - currentObject.getHeight());
break; break;
case BOTTOM: case BOTTOM:
newPosition = new Position(currentPosition.getX(), newPosition = new Position(currentPosition.getX(), position.getY()
position.getY() + object.getHeight()); + object.getHeight());
break; break;
case LEFT: case LEFT:
newPosition = new Position(position.getX() newPosition = new Position(position.getX() - currentObject.getWidth(),
- currentObject.getWidth(), currentPosition.getY()); currentPosition.getY());
break; break;
case RIGHT: case RIGHT:
newPosition = new Position(position.getX() + object.getWidth(), newPosition = new Position(position.getX() + object.getWidth(),
@ -101,10 +92,23 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
break; break;
} }
objects.remove(i.getFirst()); objects.remove(currentObject);
drop(currentObject, newPosition, direction); drop(currentObject, newPosition, direction);
} }
objects.put(object, new Pair<E, Position>(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 **/ /** Tests whether two objects overlap **/
@ -156,15 +160,13 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
float blockingRight = blocking.getSecond().getX() float blockingRight = blocking.getSecond().getX()
+ blocking.getFirst().getWidth(); + blocking.getFirst().getWidth();
float overlapRight = Math.min(objectRight, blockingRight); float overlapRight = Math.min(objectRight, blockingRight);
float overlapLeft = Math.max(position.getX(), blocking.getSecond() float overlapLeft = Math.max(position.getX(), blocking.getSecond().getX());
.getX());
float overlapX = overlapRight - overlapLeft; float overlapX = overlapRight - overlapLeft;
float objectBottom = position.getY() + object.getHeight(); float objectBottom = position.getY() + object.getHeight();
float blockingBottom = blocking.getSecond().getY() float blockingBottom = blocking.getSecond().getY()
+ blocking.getFirst().getHeight(); + blocking.getFirst().getHeight();
float overlapBottom = Math.min(objectBottom, blockingBottom); float overlapBottom = Math.min(objectBottom, blockingBottom);
float overlapTop = Math float overlapTop = Math.max(position.getY(), blocking.getSecond().getY());
.max(position.getY(), blocking.getSecond().getY());
float overlapY = overlapBottom - overlapTop; float overlapY = overlapBottom - overlapTop;
// vertical or horizontal Shift // vertical or horizontal Shift
// TODO magic factor // TODO magic factor