Skip to main content

ChunkEntityManager

A class in Root.Township, Alta.Chunks.

Server: the list of network entities currently in one chunk, used when syncing the chunk to players. Each Chunk has one.

Constructors​

MemberWhat it is
ChunkEntityManager(Chunk chunk)Creates a ChunkEntityManager from chunk.

Fields​

5 private and internal fields
MemberWhat it is
private List<NetworkEntity> entitiesThe entities currently in this chunk. Add refuses duplicates by Identifier.
private static Logger loggerNLog logger for this class.
private static List<int> tempPrioritiesShared scratch list of sync priorities used by Sort.
private static List<NetworkEntity> tempSortShared scratch list used by Cache to sort entities when they all have the same priority.
private static Dictionary<int, List<NetworkEntity>> tempSortMapShared scratch map from sync priority to entities, used by Sort.

Properties​

MemberWhat it is
Chunk Chunk { get; }The chunk this list belongs to.
int Count { get; }Number of entities in the chunk.
IEnumerable<NetworkEntity> Entities { get; }The entities in the chunk.

Events​

MemberWhat it is
event Action<NetworkEntity> AddedRaised when an entity is added.
event Action<NetworkEntity> EnteredWhileUnloadedRaised when a non-embedded entity is put in this chunk while it isn't loaded.
event Action<NetworkEntity> RemovedRaised when an entity is removed.

Methods​

MemberWhat it is
bool Add(NetworkEntity entity)Adds an entity. Refuses destroyed entities and duplicates; if the chunk isn't loaded it raises EnteredWhileUnloaded instead.
NetworkEntity[] Cache(bool isSpawning, Comparison<NetworkEntity> finalSort, out int count)Copies the entities into a pooled array grouped by SyncPriority, highest group first when spawning and reversed when not.
void Clear()Empties the list without raising events.
static Comparison<NetworkEntity> CompareFeet(Player player, bool isSpawning)Sorts by distance to the player's feet: nearest first when spawning, farthest first otherwise. Its distance maths is off (z plus z).
bool Contains(NetworkEntity entity)Whether the entity is in this chunk.
bool Remove(NetworkEntity entity)Removes an entity by identifier and raises Removed. False if it wasn't there.
5 private and internal methods
MemberWhat it is
private static int CompareEntitiesPosition(NetworkEntity a, NetworkEntity b, Vector3 position)Compares two entities by flat distance from a position, for CompareFeet. Bug: it adds z twice instead of squaring it, so the order is only roughly by distance.
private void HandleChunkNotLoaded(NetworkEntity entity)Called by Add when the chunk isn't loaded: logs, and for non-embedded entities raises EnteredWhileUnloaded.
private void OnAdded(NetworkEntity entity)Raises Added after Add puts an entity in the list.
private void OnRemoved(NetworkEntity entity)Raises Removed after Remove takes an entity out of the list.
private void Sort(NetworkEntity[] cache, int firstFail, bool isSpawning, Comparison<NetworkEntity> finalSort, out int count)Used by Cache when the entities have mixed SyncPriority: groups them by priority, orders the groups and applies the final sort in each.

Used in​

Members of other types that take, return or hold a ChunkEntityManager: