Queue npcs before unregistering, increase maximum messages

This commit is contained in:
atomicint
2016-02-09 13:32:07 -05:00
parent 2e9a7ddaaf
commit c4874acd3c
2 changed files with 23 additions and 5 deletions
@@ -10,7 +10,7 @@ public final class GameConstants {
/** /**
* The maximum amount of messages to process per pulse (per session). * The maximum amount of messages to process per pulse (per session).
*/ */
public static final int MESSAGES_PER_PULSE = 10; public static final int MESSAGES_PER_PULSE = 25;
/** /**
* The delay between consecutive pulses, in milliseconds. * The delay between consecutive pulses, in milliseconds.
+22 -4
View File
@@ -101,6 +101,11 @@ public final class World {
*/ */
private final Queue<Npc> queuedNpcs = new ConcurrentLinkedQueue<>(); private final Queue<Npc> queuedNpcs = new ConcurrentLinkedQueue<>();
/**
* The Queue of Npcs that have yet to be removed from the repository.
*/
private final Queue<Npc> oldNpcs = new ConcurrentLinkedQueue<>();
/** /**
* This world's {@link RegionRepository}. * This world's {@link RegionRepository}.
*/ */
@@ -242,6 +247,7 @@ public final class World {
* Pulses this World. * Pulses this World.
*/ */
public void pulse() { public void pulse() {
unregisterNpcs();
registerNpcs(); registerNpcs();
scheduler.pulse(); scheduler.pulse();
} }
@@ -311,10 +317,7 @@ public final class World {
public void unregister(final Npc npc) { public void unregister(final Npc npc) {
Preconditions.checkNotNull(npc, "Npc may not be null."); Preconditions.checkNotNull(npc, "Npc may not be null.");
Region region = regions.fromPosition(npc.getPosition()); oldNpcs.add(npc);
region.removeEntity(npc);
npcRepository.remove(npc);
} }
/** /**
@@ -363,4 +366,19 @@ public final class World {
} }
} }
} }
/**
* Unregisters all of the {@link Npc}s in the {@link #oldNpcs queue}.
*/
private void unregisterNpcs() {
while (!oldNpcs.isEmpty()) {
Npc npc = oldNpcs.poll();
Region region = regions.fromPosition(npc.getPosition());
region.removeEntity(npc);
npcRepository.remove(npc);
}
}
} }