package net.sf.arianne.marboard.client.core;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import marauroa.common.game.RPObject;
import net.sf.arianne.marboard.client.entity.Entity;
import net.sf.arianne.marboard.client.entity.shape.Shape;
import net.sf.arianne.marboard.client.entity.shape.ZIndexComparator;
/**
* the current white board, with all shapes and meta objects
*
* @author hendrik
*/
public class CurrentBoard implements Iterable<Shape> {
private UpdateObserver observer = null;
private Map<RPObject.ID, Entity> entities = new HashMap<RPObject.ID, Entity>();
private Set<Shape> shapes = Collections.synchronizedSet(new TreeSet<Shape>(new ZIndexComparator()));
/**
* clears the board
*/
public void clear() {
entities.clear();
shapes.clear();
fire();
}
/**
* adds an object to the board, which automatically creates the corresponding entity
*
* @param rpobject object
*/
public void add(RPObject rpobject) {
Entity entity = EntityFactory.create(rpobject);
entities.put(rpobject.getID(), entity);
if (entity instanceof Shape) {
shapes.add((Shape) entity);
fire();
}
}
/**
* removes the object
*
* @param rpobject object
*/
public void remove(RPObject rpobject) {
Entity entity = entities.remove(rpobject.getID());
if ((entity != null) && (entity instanceof Shape)) {
shapes.remove(entity);
fire();
}
}
/**
* iterates over all visible shapes (not meta entities).
*
* @return Iterator
*/
@SuppressWarnings("unchecked")
public Iterator<Shape> iterator() {
return new LinkedList(shapes).iterator();
}
/**
* notify observer
*/
private void fire() {
if (observer != null) {
observer.update();
}
}
/**
* sets a observer for update notifications
*
* @param observer UpdateObserver
*/
public void setObserver(UpdateObserver observer) {
this.observer = observer;
}
}