/// <summary> /// Called when an Update message has arrived, applies the new state to the entity. /// </summary> /// <param name="entityId">The unique identifier for the entity.</param> /// <param name="payload">The object that will be used to apply the entity's starting state.</param> /// <param name="isReckoning">True if this is a reckoning update.</param> /// <param name="time">The time the message was sent, used for projecting the state to current time.</param> private void UpdateEntity(long entityId, long ownerId, object payload, bool isReckoning, double time) { INetworkEntity targetEntity = mEntities.Where(e => e.EntityId == entityId && e.OwnerId == ownerId).SingleOrDefault(); // TODO: automatically create entity? // ignore if null, entity creation message may not have arrived if (targetEntity != null) { targetEntity.UpdateState(payload, time, isReckoning); BroadcastIfServer(entityId, targetEntity.OwnerId, payload, isReckoning ? NetworkMessageType.Update : NetworkMessageType.Reckoning); } else { mLog.Debug("Couldn't find entity to update: " + entityId); } }
/// <summary> /// Called when a Create message has arrived, gets an entity from the game screen. /// </summary> /// <param name="ownerId">The NetworkId of the peer that controls the new entity.</param> /// <param name="entityId">The unique identifier for the entity.</param> /// <param name="payload">The object that will be used to apply the entity's starting state.</param> /// <param name="time">The time the message was sent, used for projecting the state to current time.</param> private void CreateEntity(long ownerId, long entityId, object payload, double time) { // check if already exists. INetworkEntity targetEntity = mEntities.Where(e => e.EntityId == entityId && e.OwnerId == ownerId).SingleOrDefault(); if (targetEntity == null) { targetEntity = GameArena.RequestCreateEntity(ownerId, entityId, payload); } else { mLog.Warning("Attempted to create entity for ID that already exists: " + entityId); } targetEntity.OwnerId = ownerId; targetEntity.EntityId = entityId; targetEntity.UpdateState(payload, time); mEntities.Add(targetEntity); BroadcastIfServer(entityId, ownerId, payload, NetworkMessageType.Create); }