2011-04-30 15:45:11 +02:00
|
|
|
package jrummikub.model;
|
|
|
|
|
2011-04-30 23:13:22 +02:00
|
|
|
import java.util.ArrayList;
|
2011-04-30 21:17:14 +02:00
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
public class StoneTray<E extends Sizeable> implements
|
|
|
|
Iterable<Pair<E, Position>> {
|
|
|
|
protected List<Pair<E, Position>> objects = new ArrayList<Pair<E, Position>>();
|
|
|
|
|
2011-05-02 00:31:30 +02:00
|
|
|
/** Possible move directions in case of overlapping Stones/Sets */
|
|
|
|
|
|
|
|
private static enum Direction {
|
|
|
|
LEFT, RIGHT, TOP, BOTTOM;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-30 15:45:11 +02:00
|
|
|
/**
|
|
|
|
* Removes object from tray and returns it
|
|
|
|
*
|
|
|
|
* @param position
|
|
|
|
* position of the object that will be removed
|
|
|
|
*/
|
|
|
|
public E pickUp(Position position) {
|
2011-04-30 23:13:22 +02:00
|
|
|
for (Pair<E, Position> 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;
|
2011-04-30 15:45:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds object to the tray
|
|
|
|
*
|
|
|
|
* @param object
|
|
|
|
* object to add to Hand
|
|
|
|
* @param position
|
|
|
|
* {@link Position} to put the object
|
|
|
|
*/
|
|
|
|
public void drop(E object, Position position) {
|
2011-04-30 23:13:22 +02:00
|
|
|
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) {
|
|
|
|
for (Pair<E, Position> 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<E, Position>(object, position));
|
|
|
|
}
|
2011-04-30 21:17:14 +02:00
|
|
|
|
2011-04-30 23:13:22 +02:00
|
|
|
private Direction getMoveDirection(E object, Position position,
|
|
|
|
Pair<E, Position> 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-04-30 21:17:14 +02:00
|
|
|
|
2011-04-30 23:13:22 +02:00
|
|
|
private boolean getMoveOrientationn(E object, Position position,
|
|
|
|
Pair<E, Position> 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;
|
2011-04-30 21:17:14 +02:00
|
|
|
}
|
|
|
|
|
2011-04-30 23:13:22 +02:00
|
|
|
public Position getPosition(E object) {
|
|
|
|
for (Pair<E, Position> i : objects) {
|
|
|
|
if (object.equals(i.getFirst())) {
|
|
|
|
return i.getSecond();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2011-04-30 21:17:14 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public Iterator<Pair<E, Position>> iterator() {
|
2011-04-30 23:13:22 +02:00
|
|
|
return objects.iterator();
|
2011-04-30 21:17:14 +02:00
|
|
|
}
|
2011-04-30 23:13:22 +02:00
|
|
|
|
2011-04-30 15:45:11 +02:00
|
|
|
}
|