/// <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 mLog?.Info($"Receiving update from {ownerId} to update entity {payload?.GetType()}"); if (targetEntity != null) { if (targetEntity.OwnerId != this.NetworkId || isReckoning) { targetEntity.UpdateFromState(payload, time); } BroadcastIfServer(entityId, targetEntity.OwnerId, payload, isReckoning ? NetworkMessageType.Reckoning : NetworkMessageType.Update ); } else { mLog.Debug("Couldn't find entity to update: " + entityId); } }
/// <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(ulong id, object payload, bool isReckoning, double time) { INetworkEntity targetEntity = entities.Where(e => e.Id == id).SingleOrDefault(); if (targetEntity != null) { targetEntity.UpdateFromState(payload, time, isReckoning); BroadcastIfServer(id, payload, isReckoning ? NetworkMessageType.Reckoning : NetworkMessageType.Update); } else { // ignore if null, entity creation message may not have arrived yet log.Debug("Couldn't find entity to update: " + id); } }
/// <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(ulong id, object payload, double time) { // check entity with ID already exists INetworkEntity targetEntity = entities.Where(e => e.Id == id).SingleOrDefault(); if (targetEntity == null) { targetEntity = GameArena?.HandleCreateEntity(id, payload); // force correct unique ID targetEntity.Id = id; } targetEntity.UpdateFromState(payload, time, false); entities.Add(targetEntity); BroadcastIfServer(id, payload, NetworkMessageType.Create); }
/// <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) { // this is a brand new entity, get a new ID if (entityId == -1) { if (Role == NetworkRole.Server) { entityId = GetUniqueEntityId(); } else { var msg = "Something went wrong, client received bad entity ID."; mLog.Error(msg); throw new RedGrinException(msg); } } // check entity with ID already exists INetworkEntity targetEntity = mEntities.Where(e => e.EntityId == entityId).SingleOrDefault(); if (targetEntity == null) { targetEntity = GameArena?.RequestCreateEntity(ownerId, payload); } else { var msg = "Attempted to create entity for ID that already exists: " + entityId; mLog?.Error(msg); throw new RedGrinException(msg); } targetEntity.UpdateFromState(payload, time); targetEntity.OwnerId = ownerId; targetEntity.EntityId = entityId; mEntities.Add(targetEntity); BroadcastIfServer(entityId, ownerId, payload, NetworkMessageType.Create); }