Fix the re-addition of StaticObjects to the game world.

This commit is contained in:
Steve
2015-04-10 07:00:36 -04:00
parent 6bffd5c0a1
commit 84373635a1
2 changed files with 273 additions and 262 deletions
+10 -11
View File
@@ -1,13 +1,6 @@
package org.apollo.game.model; package org.apollo.game.model;
import java.io.BufferedInputStream; import com.google.common.base.Preconditions;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
import org.apollo.Service; import org.apollo.Service;
import org.apollo.fs.IndexedFileSystem; import org.apollo.fs.IndexedFileSystem;
import org.apollo.fs.decoder.GameObjectDecoder; import org.apollo.fs.decoder.GameObjectDecoder;
@@ -37,7 +30,13 @@ import org.apollo.util.MobRepository;
import org.apollo.util.NameUtil; import org.apollo.util.NameUtil;
import org.apollo.util.plugin.PluginManager; import org.apollo.util.plugin.PluginManager;
import com.google.common.base.Preconditions; import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
/** /**
* The world class is a singleton which contains objects like the {@link MobRepository} for players and NPCs. It should * The world class is a singleton which contains objects like the {@link MobRepository} for players and NPCs. It should
@@ -385,12 +384,12 @@ public final class World {
} }
/** /**
* Adds entities to regions in the {@link RegionRepository}. * Adds entities to regions in the {@link RegionRepository}. By default, we do not notify listeners.
* *
* @param entities The entities. * @param entities The entities.
*/ */
private void placeEntities(Entity... entities) { private void placeEntities(Entity... entities) {
Arrays.stream(entities).forEach(entity -> regions.fromPosition(entity.getPosition()).addEntity(entity)); Arrays.stream(entities).forEach(entity -> regions.fromPosition(entity.getPosition()).addEntity(entity, false));
} }
} }
+30 -18
View File
@@ -1,15 +1,9 @@
package org.apollo.game.model.area; package org.apollo.game.model.area;
import java.util.ArrayList; import com.google.common.base.MoreObjects;
import java.util.Arrays; import com.google.common.base.Preconditions;
import java.util.Collections; import com.google.common.collect.ImmutableList;
import java.util.HashMap; import com.google.common.collect.ImmutableSet;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.apollo.game.message.impl.RegionUpdateMessage; import org.apollo.game.message.impl.RegionUpdateMessage;
import org.apollo.game.model.Direction; import org.apollo.game.model.Direction;
import org.apollo.game.model.Position; import org.apollo.game.model.Position;
@@ -19,10 +13,8 @@ import org.apollo.game.model.entity.Entity;
import org.apollo.game.model.entity.Entity.EntityType; import org.apollo.game.model.entity.Entity.EntityType;
import org.apollo.game.model.entity.obj.GameObject; import org.apollo.game.model.entity.obj.GameObject;
import com.google.common.base.MoreObjects; import java.util.*;
import com.google.common.base.Preconditions; import java.util.stream.Collectors;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
/** /**
* An 8x8 area of the map. * An 8x8 area of the map.
@@ -41,7 +33,7 @@ public final class Region {
@Override @Override
public void execute(Region region, Entity entity, EntityUpdateType update) { public void execute(Region region, Entity entity, EntityUpdateType update) {
EntityType type = entity.getEntityType(); EntityType type = entity.getEntityType();
if (type != EntityType.PLAYER && type != EntityType.NPC && (type != EntityType.STATIC_OBJECT || update == EntityUpdateType.REMOVE)) { if (type != EntityType.PLAYER && type != EntityType.NPC) {
region.record(entity, update); region.record(entity, update);
} }
} }
@@ -116,13 +108,14 @@ public final class Region {
} }
/** /**
* Adds a {@link Entity} from to Region. Note that this does not spawn the Entity, or do any other action other than * Adds a {@link Entity} to the Region. Note that this does not spawn the Entity, or do any other action other than
* register it to this Region. * register it to this Region.
* *
* @param entity The Entity. * @param entity The Entity.
* @param notify A flag indicating whether the {@link RegionListener}s for this Region should be notified.
* @throws IllegalArgumentException If the Entity does not belong in this Region. * @throws IllegalArgumentException If the Entity does not belong in this Region.
*/ */
public void addEntity(Entity entity) { public void addEntity(Entity entity, boolean notify) {
Position position = entity.getPosition(); Position position = entity.getPosition();
checkPosition(position); checkPosition(position);
@@ -133,17 +126,33 @@ public final class Region {
System.out.println("Adding entity " + entity + " to " + entity.getPosition()); System.out.println("Adding entity " + entity + " to " + entity.getPosition());
} }
if (notify) {
notifyListeners(entity, EntityUpdateType.ADD); notifyListeners(entity, EntityUpdateType.ADD);
} }
}
/**
* Adds a {@link Entity} to the Region. Note that this does not spawn the Entity, or do any other action other than
* register it to this Region.
* <p/>
* By default, this method notifies RegionListeners for this region of the addition.
*
* @param entity The Entity.
* @throws IllegalArgumentException If the Entity does not belong in this Region.
*/
public void addEntity(Entity entity) {
addEntity(entity, true);
}
/** /**
* Checks if this Region contains the specified Entity. * Checks if this Region contains the specified Entity.
* <p> * <p/>
* This method operates in constant time. * This method operates in constant time.
* *
* @param entity The Entity. * @param entity The Entity.
* @return {@code true} if this Region contains the Entity, otherwise {@code false}. * @return {@code true} if this Region contains the Entity, otherwise {@code false}.
*/ */
public boolean contains(Entity entity) { public boolean contains(Entity entity) {
Position position = entity.getPosition(); Position position = entity.getPosition();
Set<Entity> local = entities.get(position); Set<Entity> local = entities.get(position);
@@ -304,7 +313,10 @@ public final class Region {
int height = entity.getPosition().getHeight(); int height = entity.getPosition().getHeight();
updates.get(height).add(message); updates.get(height).add(message);
if (entity.getEntityType() != EntityType.STATIC_OBJECT || type == EntityUpdateType.REMOVE) {
snapshots.get(height).add(message); snapshots.get(height).add(message);
} }
}
} }