Add a listener for sector events.

This commit is contained in:
Major-
2014-02-15 22:04:55 +00:00
parent eb4a655649
commit e57e3b35df
2 changed files with 50 additions and 6 deletions
+30 -6
View File
@@ -13,19 +13,24 @@ import org.apollo.game.model.Entity;
public final class Sector {
/**
* The width and height of the sector, in tiles.
* The width and height of a sector, in tiles.
*/
public static final int SECTOR_SIZE = 8;
/**
* The {@link SectorCoordinates} of this sector.
* The sector coordinates of this sector.
*/
private final SectorCoordinates coordinates;
/**
* A {@link List} of every {@link Entity} in this sector.
* A list of every entity in this sector.
*/
private final List<Entity> entities = new ArrayList<Entity>();
private final List<Entity> entities = new ArrayList<>();
/**
* A list of listeners registered to this sector.
*/
private final List<SectorListener> listeners = new ArrayList<>();
/**
* Creates a new sector.
@@ -54,7 +59,11 @@ public final class Sector {
* @return {@code true} if the entity was added, otherwise {@code false}.
*/
public boolean addEntity(Entity entity) {
return entities.add(entity);
if (entities.add(entity)) {
notifyListeners(entity);
return true;
}
return false;
}
/**
@@ -66,6 +75,17 @@ public final class Sector {
return coordinates;
}
/**
* Notifies the listeners registered to this sector that an update has occurred.
*
* @param entity The entity that was updated.
*/
public void notifyListeners(Entity entity) {
for (SectorListener listener : listeners) {
listener.execute(this, entity);
}
}
/**
* Removes a {@link Entity} from this sector.
*
@@ -73,7 +93,11 @@ public final class Sector {
* @return {@code true} if the entity was removed, otherwise {@code false}.
*/
public boolean removeEntity(Entity entity) {
return entities.remove(entity);
if (entities.remove(entity)) {
notifyListeners(entity);
return true;
}
return false;
}
}
@@ -0,0 +1,20 @@
package org.apollo.game.model.sector;
import org.apollo.game.model.Entity;
/**
* A class that should be extended by listeners that execute actions when an entity is added or removed from the sector.
*
* @author Major
*/
public abstract class SectorListener {
/**
* Executes the action for this listener.
*
* @param The sector that was updated.
* @param entity The affected entity.
*/
public abstract void execute(Sector sector, Entity entity);
}