public static void UndoChange(ComponentChange change) { Entity entity = Entities.Find(change.EntityId); Type componentType = change.IsRemoved?change.Before.GetType():change.After.GetType(); if (change.IsRemoved) { // Add new component. if (entity == null) // Add new entity. { entity = Entities.AddEntityFromSync(change.EntityId, change.Before); } else // Add new component. { entity.AddComponent(change.Before); } } else { if (change.Before == null) { // Remove component. entity.RemoveComponent(componentType); } else { // Undo changes in existing component. EntityComponent existingComponent = entity.GetComponent(componentType); existingComponent.AddChange(change.Before); } } }
private static void CalcComponentsChangeInOneEntity(Entity currEntity, Entity prevEntity, List <ComponentChange> changes) { foreach (var currComponent in currEntity.GetComponents()) { var prevComponent = prevEntity.GetComponent(currComponent.GetType()); if (prevComponent == null && currComponent != null) { // Add component. var change = new ComponentChange(currEntity.Id, false, prevComponent, currComponent); changes.Add(change); } else { // Change component. var change = new ComponentChange(currEntity.Id, false, prevComponent, currComponent); if (!prevComponent.Equals(currComponent)) { changes.Add(change); } } } foreach (var prevComponent in prevEntity.GetComponents()) { if (currEntity.GetComponent(prevComponent.GetType()) != null) { continue; } // Remove components. var change = new ComponentChange(currEntity.Id, true, prevComponent, null); changes.Add(change); } }
public void Serialize <T>(T s) where T : IUnifiedSerializer { s.Serialize(ref entityId); bool hasComponentChanges = s.isWriting ? !isRemoval : false; s.Serialize(ref hasComponentChanges); isRemoval = !hasComponentChanges; if (hasComponentChanges) { int numComponentChanges = s.isWriting ? componentChanges.Length : 0; s.Serialize(ref numComponentChanges); if (s.isWriting) { for (int i = 0; i < numComponentChanges; ++i) { componentChanges[i].Serialize(s); } } else { componentChanges = new ComponentChange[numComponentChanges]; for (int i = 0; i < numComponentChanges; ++i) { componentChanges[i] = ComponentChange.Deserialize(s); } } } }
public static ComponentChange MakeRemoval(int componentIndex) { var change = new ComponentChange(); change.componentIndex = componentIndex; change.isRemoval = true; return(change); }
public static ComponentChange MakeUpdate(int componentIndex, IComponent newComponent) { var change = new ComponentChange(); change.componentIndex = componentIndex; change.isRemoval = false; change.SaveState(newComponent); return(change); }
public static ComponentChange Deserialize <T>(T serializer) where T : IUnifiedSerializer { if (serializer.isWriting) { throw new ArgumentException("Can't deserialize a ComponentChange using a reading serializer!"); } var change = new ComponentChange(); change.Serialize(serializer); return(change); }
private static void SaveBeforeState(ComponentChange change) { Entity entity = Entities.Find(change.EntityId); if (entity == null) { return; // Prev component is null. } EntityComponent prevComponent = entity.GetComponent(change.After.GetType()); if (prevComponent == null) { return; } change.SetPrevState(prevComponent); }
protected override void OnUpdate() { var componentChanges = World.GetExistingSystem <SyncSendSystem>().ComponentChanges; var localComponentChanges = componentChanges.AsParallelWriter(); Entities.ForEach((Entity entity, ref WorldPosition_Sync sync, in Translation data, in Simulated simulate) => { uint mask = 0; if (!sync.hasBeenSerialized) { mask = 0xffffffff; } if (HasNoticeableDifference(data.Value, sync.lastSentData.Value, CoherenceLimits.Translation_value_Epsilon)) { mask |= 0b00000000000000000000000000000001; } if (mask != 0 || sync.resendMask != 0) { CheckRange(data.Value.x, CoherenceLimits.Translation_value_Min, CoherenceLimits.Translation_value_Max); CheckRange(data.Value.y, CoherenceLimits.Translation_value_Min, CoherenceLimits.Translation_value_Max); CheckRange(data.Value.z, CoherenceLimits.Translation_value_Min, CoherenceLimits.Translation_value_Max); sync.accumulatedPriority += sync.howImportantAreYou; var componentChange = new ComponentChange { entity = entity, componentType = TypeIds.InternalWorldPosition, mask = mask, resendMask = sync.resendMask, entityHasReceivedConstructor = simulate.hasReceivedConstructor, componentHasReceivedConstructor = sync.hasReceivedConstructor, }; localComponentChanges.Add(sync.accumulatedPriority, componentChange); } }).ScheduleParallel();
EntityChange MakeChangeOf(InputEntity e) { int[] indices = e.GetComponentIndices(); var components = e.GetComponents(); int numComponents = components.Length; var componentChanges = new ComponentChange[numComponents]; for (int i = 0; i < numComponents; ++i) { componentChanges[i] = ComponentChange.MakeUpdate( indices[i], components[i] ); } return(EntityChange.MakeUpdate(e.id.value, componentChanges)); }
public CommandSyncData(RTData data) : base(data) { if (data == null) { return; } uint i = 1; Lag = data.GetLong(i).Value; i++; int count = data.GetInt(i).Value; i++; List <ComponentChange> changes = new List <ComponentChange> (); Changes = new ReadonlyList <ComponentChange> (changes); for (int ind = 0; ind < count; ind++) { ComponentChange currChange = ParseChange(data.GetData(i)); i++; changes.Add(currChange); } }
private void AddChangeToSolution(MyDefinitionId removedComponentId, MyDefinitionId addedComponentId, int numChanged) { for (int i = 0; i < m_solution.Count; ++i) { ComponentChange change = m_solution[i]; if ((change.IsChange() || change.IsAddition()) && change.ToAdd == removedComponentId) { int difference = change.Amount - numChanged; int toChange = Math.Min(numChanged, change.Amount); numChanged -= change.Amount; if (difference > 0) { change.Amount = difference; m_solution[i] = change; } else { m_solution.RemoveAtFast(i); } if (change.IsChange()) { m_solution.Add(ComponentChange.CreateChange(change.ToRemove, addedComponentId, toChange)); } else // change.IsAddition() { m_solution.Add(ComponentChange.CreateAddition(addedComponentId, toChange)); } if (numChanged <= 0) { break; } } } if (numChanged > 0) { m_solution.Add(ComponentChange.CreateChange(removedComponentId, addedComponentId, numChanged)); } }
ComponentChange[] MakeComponentChangesOf(GameEntity e) { var componentChanges = new List <ComponentChange>(); var changed = e.changeFlags.flags; int numComponents = e.totalComponents; for (int i = 0; i < numComponents; ++i) { if (e.HasComponent(i)) { componentChanges.Add(ComponentChange.MakeUpdate(i, e.GetComponent(i))); } else if (changed[i]) { componentChanges.Add(ComponentChange.MakeRemoval(i)); } } return(componentChanges.ToArray()); }
public static void ApplyChange(ComponentChange change) { Entity entity = Entities.Find(change.EntityId); Type componentType = change.IsRemoved?change.Before.GetType():change.After.GetType(); if (change.IsRemoved) { if (entity == null) { Debug.Log("Received intent to remove component from non-existing entity"); return; } else { // Remove component. entity.RemoveComponent(componentType); } } else { EntityComponent existingComponent = entity == null ? null : entity.GetComponent(componentType); if (existingComponent == null) { if (entity == null) // Add new entity. { entity = Entities.AddEntityFromSync(change.EntityId, change.After); } else // Add new component. { entity.AddComponent(change.After); } } else { // Apply changes to existing component. existingComponent.AddChange(change.After); } } }
private void AddRemovalToSolution(MyDefinitionId removedComponentId, int removeCount) { // First search through the changes, whether some of them didn't add the given component. If so, change the change to removal for (int i = 0; i < m_solution.Count; ++i) { ComponentChange change = m_solution[i]; if ((change.IsChange() || change.IsAddition()) && change.ToAdd == removedComponentId) { int difference = change.Amount - removeCount; int toRemove = Math.Min(removeCount, change.Amount); removeCount -= change.Amount; if (difference > 0) { change.Amount = difference; m_solution[i] = change; } else { m_solution.RemoveAtFast(i); } if (change.IsChange()) { m_solution.Add(ComponentChange.CreateRemoval(change.ToRemove, toRemove)); } if (removeCount <= 0) { break; } } } if (removeCount > 0) { m_solution.Add(ComponentChange.CreateRemoval(removedComponentId, removeCount)); } }
public static void CalcComponentsChange(List <Entity> currEntities) { List <ComponentChange> changes = new List <ComponentChange>(); for (int i = 0; i < currEntities.Count; i++) { int prevInd = FindInd(_prevEntities, currEntities[i].Id); if (prevInd == -1) { // Add all components. foreach (var component in currEntities[i].GetComponents()) { var change = new ComponentChange(currEntities[i].Id, false, null, component); changes.Add(change); } } else { CalcComponentsChangeInOneEntity(currEntities[i], _prevEntities[prevInd], changes); } } for (int i = 0; i < _prevEntities.Count; i++) { int currInd = FindInd(currEntities, _prevEntities[i].Id); if (currInd != -1) { continue; } // Entity deleted, all components removed. foreach (var component in _prevEntities[i].GetComponents()) { var change = new ComponentChange(_prevEntities[i].Id, true, component, null); changes.Add(change); } } TimeMachiene.SaveCalculatedChanges(changes, Timer.DeltaTime); }