mirror of
https://github.com/2006-Scape/apollo.git
synced 2026-07-07 00:38:46 +00:00
Add a listener for sector events.
This commit is contained in:
@@ -13,19 +13,24 @@ import org.apollo.game.model.Entity;
|
|||||||
public final class Sector {
|
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;
|
public static final int SECTOR_SIZE = 8;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The {@link SectorCoordinates} of this sector.
|
* The sector coordinates of this sector.
|
||||||
*/
|
*/
|
||||||
private final SectorCoordinates coordinates;
|
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.
|
* Creates a new sector.
|
||||||
@@ -54,7 +59,11 @@ public final class Sector {
|
|||||||
* @return {@code true} if the entity was added, otherwise {@code false}.
|
* @return {@code true} if the entity was added, otherwise {@code false}.
|
||||||
*/
|
*/
|
||||||
public boolean addEntity(Entity entity) {
|
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;
|
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.
|
* 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}.
|
* @return {@code true} if the entity was removed, otherwise {@code false}.
|
||||||
*/
|
*/
|
||||||
public boolean removeEntity(Entity entity) {
|
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);
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user