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:
parent
5767486e83
commit
5b0aed8fd0
2 changed files with 41 additions and 35 deletions
|
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ import jrummikub.util.Pair;
|
||||||
* or {@link StoneSet}s.
|
* or {@link StoneSet}s.
|
||||||
*
|
*
|
||||||
* @param <E>
|
* @param <E>
|
||||||
* Type of positioned objects (must implement Sizeable)
|
* Type of positioned objects (must implement Sizeable)
|
||||||
*/
|
*/
|
||||||
public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
||||||
protected HashMap<E, Pair<E, Position>> objects = new HashMap<E, Pair<E, Position>>();
|
protected HashMap<E, Pair<E, Position>> objects = new HashMap<E, Pair<E, Position>>();
|
||||||
|
@ -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
|
||||||
|
@ -207,11 +209,11 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
||||||
try {
|
try {
|
||||||
StoneTray<E> copy = (StoneTray<E>) super.clone();
|
StoneTray<E> copy = (StoneTray<E>) super.clone();
|
||||||
copy.objects = (HashMap<E, Pair<E, Position>>) objects.clone();
|
copy.objects = (HashMap<E, Pair<E, Position>>) objects.clone();
|
||||||
|
|
||||||
return copy;
|
return copy;
|
||||||
} catch (CloneNotSupportedException e) {
|
} catch (CloneNotSupportedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue