2011-04-30 15:45:11 +02:00
|
|
|
package jrummikub.model;
|
|
|
|
|
2011-05-03 17:13:10 +02:00
|
|
|
import java.util.HashMap;
|
2011-04-30 21:17:14 +02:00
|
|
|
import java.util.Iterator;
|
2011-05-03 17:13:10 +02:00
|
|
|
import java.util.Map;
|
2011-04-30 21:17:14 +02:00
|
|
|
|
|
|
|
import jrummikub.util.Pair;
|
|
|
|
|
2011-04-30 23:13:22 +02:00
|
|
|
/**
|
|
|
|
* A StoneTray is a collection of positioned objects (for example {@link Stone}s
|
|
|
|
* or {@link StoneSet}s.
|
2011-05-02 04:46:00 +02:00
|
|
|
*
|
|
|
|
* @param <E>
|
2011-05-09 21:56:45 +02:00
|
|
|
* Type of positioned objects (must implement Sizeable)
|
2011-04-30 23:13:22 +02:00
|
|
|
*/
|
2011-05-04 16:23:10 +02:00
|
|
|
public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
2011-05-09 00:33:34 +02:00
|
|
|
protected HashMap<E, Pair<E, Position>> objects = new HashMap<E, Pair<E, Position>>();
|
2011-04-30 23:13:22 +02:00
|
|
|
|
2011-05-02 00:31:30 +02:00
|
|
|
/** Possible move directions in case of overlapping Stones/Sets */
|
|
|
|
|
2011-05-09 23:29:01 +02:00
|
|
|
protected static enum Direction {
|
2011-05-02 00:31:30 +02:00
|
|
|
LEFT, RIGHT, TOP, BOTTOM;
|
|
|
|
}
|
|
|
|
|
2011-05-04 16:23:10 +02:00
|
|
|
@Override
|
2011-04-30 15:45:11 +02:00
|
|
|
public E pickUp(Position position) {
|
2011-05-09 00:33:34 +02:00
|
|
|
for (Map.Entry<E, Pair<E, Position>> i : objects.entrySet()) {
|
|
|
|
Position currentPosition = i.getValue().getSecond();
|
2011-05-03 17:13:10 +02:00
|
|
|
E currentObject = i.getKey();
|
2011-04-30 23:13:22 +02:00
|
|
|
// Tests if position is left of, above ... the current object
|
|
|
|
if (position.getX() < currentPosition.getX()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (position.getY() < currentPosition.getY()) {
|
|
|
|
continue;
|
|
|
|
}
|
2011-05-09 21:56:45 +02:00
|
|
|
if (position.getX() > currentPosition.getX() + currentObject.getWidth()) {
|
2011-04-30 23:13:22 +02:00
|
|
|
continue;
|
|
|
|
}
|
2011-05-09 21:56:45 +02:00
|
|
|
if (position.getY() > currentPosition.getY() + currentObject.getHeight()) {
|
2011-04-30 23:13:22 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Position is inside the current object
|
2011-05-03 17:13:10 +02:00
|
|
|
objects.remove(i.getKey());
|
2011-04-30 23:13:22 +02:00
|
|
|
return currentObject;
|
|
|
|
}
|
|
|
|
return null;
|
2011-04-30 15:45:11 +02:00
|
|
|
}
|
|
|
|
|
2011-05-04 16:23:10 +02:00
|
|
|
@Override
|
2011-04-30 15:45:11 +02:00
|
|
|
public void drop(E object, Position position) {
|
2011-05-03 19:06:15 +02:00
|
|
|
if (object != null) {
|
|
|
|
drop(object, position, null);
|
|
|
|
}
|
2011-04-30 15:45:11 +02:00
|
|
|
}
|
2011-04-30 21:17:14 +02:00
|
|
|
|
2011-04-30 23:13:22 +02:00
|
|
|
private void drop(E object, Position position, Direction direction) {
|
2011-05-09 23:29:01 +02:00
|
|
|
Pair<Position, Direction> update = fixInvalidDrop(object, position,
|
|
|
|
direction);
|
|
|
|
if (update != null) {
|
|
|
|
position = update.getFirst();
|
|
|
|
direction = update.getSecond();
|
|
|
|
}
|
|
|
|
|
2011-05-10 04:02:46 +02:00
|
|
|
dropUnchecked(object, position, direction);
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
private void dropUnchecked(E object, Position position, Direction direction) {
|
2011-05-09 21:56:45 +02:00
|
|
|
objects.put(object, new Pair<E, Position>(object, position));
|
2011-05-09 00:33:34 +02:00
|
|
|
for (Pair<E, Position> i : ((Map<E, Pair<E, Position>>) objects.clone())
|
|
|
|
.values()) {
|
2011-05-09 21:56:45 +02:00
|
|
|
Direction newDirection = direction;
|
2011-05-09 00:33:34 +02:00
|
|
|
E currentObject = i.getFirst();
|
2011-05-09 21:56:45 +02:00
|
|
|
if (currentObject == object)
|
|
|
|
continue;
|
|
|
|
Position currentPosition = getPosition(currentObject);
|
|
|
|
if (!objectsOverlap(object, position, currentObject, currentPosition)) {
|
2011-04-30 23:13:22 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Object would be placed inside the current object
|
2011-05-09 21:56:45 +02:00
|
|
|
if (newDirection == null) {
|
|
|
|
newDirection = getMoveDirection(object, position, i);
|
2011-04-30 23:13:22 +02:00
|
|
|
}
|
|
|
|
Position newPosition = null;
|
2011-05-03 17:13:09 +02:00
|
|
|
// Move object to avoid overlap
|
2011-05-09 21:56:45 +02:00
|
|
|
switch (newDirection) {
|
2011-04-30 23:13:22 +02:00
|
|
|
case TOP:
|
2011-05-09 21:56:45 +02:00
|
|
|
newPosition = new Position(currentPosition.getX(), position.getY()
|
|
|
|
- currentObject.getHeight());
|
2011-04-30 23:13:22 +02:00
|
|
|
break;
|
|
|
|
case BOTTOM:
|
2011-05-09 21:56:45 +02:00
|
|
|
newPosition = new Position(currentPosition.getX(), position.getY()
|
|
|
|
+ object.getHeight());
|
2011-04-30 23:13:22 +02:00
|
|
|
break;
|
|
|
|
case LEFT:
|
2011-05-09 21:56:45 +02:00
|
|
|
newPosition = new Position(position.getX() - currentObject.getWidth(),
|
|
|
|
currentPosition.getY());
|
2011-04-30 23:13:22 +02:00
|
|
|
break;
|
|
|
|
case RIGHT:
|
|
|
|
newPosition = new Position(position.getX() + object.getWidth(),
|
|
|
|
currentPosition.getY());
|
|
|
|
break;
|
|
|
|
}
|
2011-05-04 16:23:10 +02:00
|
|
|
|
2011-05-09 21:56:45 +02:00
|
|
|
objects.remove(currentObject);
|
2011-05-10 01:39:33 +02:00
|
|
|
drop(currentObject, newPosition, newDirection);
|
2011-04-30 23:13:22 +02:00
|
|
|
}
|
2011-05-09 21:56:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether the object may be placed on the given position, computes new
|
|
|
|
* position if not
|
|
|
|
*
|
|
|
|
* @param object
|
|
|
|
* to be dropped
|
2011-05-09 23:29:01 +02:00
|
|
|
* @param dir
|
2011-05-09 21:56:45 +02:00
|
|
|
* @param p
|
|
|
|
* the object is dropped at
|
|
|
|
* @return null if the drop is valid, new position otherwise
|
|
|
|
*/
|
2011-05-09 23:29:01 +02:00
|
|
|
protected Pair<Position, Direction> fixInvalidDrop(E object, Position pos,
|
|
|
|
Direction dir) {
|
2011-05-09 21:56:45 +02:00
|
|
|
return null;
|
2011-04-30 23:13:22 +02:00
|
|
|
}
|
2011-04-30 21:17:14 +02:00
|
|
|
|
2011-05-02 02:00:50 +02:00
|
|
|
/** Tests whether two objects overlap **/
|
|
|
|
private boolean objectsOverlap(E object1, Position position1, E object2,
|
|
|
|
Position position2) {
|
|
|
|
// Tests if position is left of, above ... the current object
|
|
|
|
if (position1.getX() + object1.getWidth() <= position2.getX()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (position1.getY() + object1.getHeight() <= position2.getY()) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-05-02 04:46:00 +02:00
|
|
|
if (position1.getX() >= position2.getX() + object2.getWidth()) {
|
2011-05-02 02:00:50 +02:00
|
|
|
return false;
|
|
|
|
}
|
2011-05-02 04:46:00 +02:00
|
|
|
if (position1.getY() >= position2.getY() + object2.getHeight()) {
|
2011-05-02 02:00:50 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-04-30 23:13:22 +02:00
|
|
|
private Direction getMoveDirection(E object, Position position,
|
2011-05-09 00:33:34 +02:00
|
|
|
Pair<E, Position> blocking) {
|
|
|
|
boolean isVertical = getMoveOrientation(object, position, blocking);
|
2011-04-30 23:13:22 +02:00
|
|
|
float objectMidpointX = position.getX() + object.getWidth() / 2;
|
|
|
|
float objectMidpointY = position.getY() + object.getHeight() / 2;
|
2011-05-09 00:33:34 +02:00
|
|
|
float blockingMidpointX = blocking.getSecond().getX()
|
|
|
|
+ blocking.getFirst().getWidth() / 2;
|
|
|
|
float blockingMidpointY = blocking.getSecond().getY()
|
|
|
|
+ blocking.getFirst().getHeight() / 2;
|
2011-04-30 23:13:22 +02:00
|
|
|
if (isVertical) {
|
|
|
|
if (objectMidpointY < blockingMidpointY) {
|
|
|
|
return Direction.BOTTOM;
|
|
|
|
} else {
|
|
|
|
return Direction.TOP;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (objectMidpointX < blockingMidpointX) {
|
|
|
|
return Direction.RIGHT;
|
|
|
|
} else {
|
|
|
|
return Direction.LEFT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-04-30 21:17:14 +02:00
|
|
|
|
2011-05-09 00:33:34 +02:00
|
|
|
private boolean getMoveOrientation(E object, Position position,
|
|
|
|
Pair<E, Position> blocking) {
|
2011-04-30 23:13:22 +02:00
|
|
|
float objectRight = position.getX() + object.getWidth();
|
2011-05-09 00:33:34 +02:00
|
|
|
float blockingRight = blocking.getSecond().getX()
|
|
|
|
+ blocking.getFirst().getWidth();
|
2011-04-30 23:13:22 +02:00
|
|
|
float overlapRight = Math.min(objectRight, blockingRight);
|
2011-05-09 21:56:45 +02:00
|
|
|
float overlapLeft = Math.max(position.getX(), blocking.getSecond().getX());
|
2011-04-30 23:13:22 +02:00
|
|
|
float overlapX = overlapRight - overlapLeft;
|
|
|
|
float objectBottom = position.getY() + object.getHeight();
|
2011-05-09 00:33:34 +02:00
|
|
|
float blockingBottom = blocking.getSecond().getY()
|
|
|
|
+ blocking.getFirst().getHeight();
|
2011-04-30 23:13:22 +02:00
|
|
|
float overlapBottom = Math.min(objectBottom, blockingBottom);
|
2011-05-09 21:56:45 +02:00
|
|
|
float overlapTop = Math.max(position.getY(), blocking.getSecond().getY());
|
2011-04-30 23:13:22 +02:00
|
|
|
float overlapY = overlapBottom - overlapTop;
|
|
|
|
// vertical or horizontal Shift
|
|
|
|
// TODO magic factor
|
|
|
|
return overlapX > overlapY;
|
2011-04-30 21:17:14 +02:00
|
|
|
}
|
|
|
|
|
2011-05-04 16:23:10 +02:00
|
|
|
/*
|
|
|
|
* (non-Javadoc)
|
2011-05-02 04:46:00 +02:00
|
|
|
*
|
2011-05-04 16:23:10 +02:00
|
|
|
* @see jrummikub.model.IStoneTray#getPosition(E)
|
2011-05-02 04:46:00 +02:00
|
|
|
*/
|
2011-05-04 16:23:10 +02:00
|
|
|
@Override
|
2011-04-30 23:13:22 +02:00
|
|
|
public Position getPosition(E object) {
|
2011-05-09 00:33:34 +02:00
|
|
|
return objects.get(object).getSecond();
|
2011-04-30 23:13:22 +02:00
|
|
|
}
|
2011-04-30 21:17:14 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public Iterator<Pair<E, Position>> iterator() {
|
2011-05-09 00:33:34 +02:00
|
|
|
return objects.values().iterator();
|
2011-04-30 21:17:14 +02:00
|
|
|
}
|
2011-04-30 23:13:22 +02:00
|
|
|
|
2011-05-04 16:23:10 +02:00
|
|
|
/*
|
|
|
|
* (non-Javadoc)
|
|
|
|
*
|
|
|
|
* @see jrummikub.model.IStoneTray#pickUp(E)
|
|
|
|
*/
|
|
|
|
@Override
|
2011-05-10 01:39:32 +02:00
|
|
|
public boolean pickUp(E object) {
|
|
|
|
return null != objects.remove(object);
|
2011-05-03 17:13:11 +02:00
|
|
|
}
|
2011-05-04 16:23:10 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* (non-Javadoc)
|
|
|
|
*
|
|
|
|
* @see jrummikub.model.IStoneTray#clone()
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("unchecked")
|
2011-05-03 19:06:12 +02:00
|
|
|
@Override
|
2011-05-04 16:23:10 +02:00
|
|
|
public IStoneTray<E> clone() {
|
2011-05-04 19:39:17 +02:00
|
|
|
try {
|
|
|
|
StoneTray<E> copy = (StoneTray<E>) super.clone();
|
2011-05-09 00:33:34 +02:00
|
|
|
copy.objects = (HashMap<E, Pair<E, Position>>) objects.clone();
|
2011-05-09 21:56:45 +02:00
|
|
|
|
2011-05-04 19:39:17 +02:00
|
|
|
return copy;
|
|
|
|
} catch (CloneNotSupportedException e) {
|
|
|
|
e.printStackTrace();
|
2011-05-09 21:56:45 +02:00
|
|
|
|
2011-05-04 19:39:17 +02:00
|
|
|
return null;
|
|
|
|
}
|
2011-05-03 19:06:12 +02:00
|
|
|
}
|
2011-05-03 17:13:11 +02:00
|
|
|
|
2011-05-04 19:09:39 +02:00
|
|
|
@Override
|
|
|
|
public int getSize() {
|
|
|
|
return objects.size();
|
|
|
|
}
|
|
|
|
|
2011-04-30 15:45:11 +02:00
|
|
|
}
|