/// <summary> /// Returns the component array for a specific type of component. /// </summary> public ComponentArray <TComponent> GetComponentArray <TComponent>() where TComponent : struct, IComponent <TComponent> { for (int componentTypeID = 0; componentTypeID < this.componentArrays.Length; componentTypeID++) { ComponentArray <TComponent> componentArray = this.componentArrays[componentTypeID] as ComponentArray <TComponent>; if (componentArray != null) { return(componentArray); } } throw new ArgumentException("The component type has not been registered.", nameof(TComponent)); }
/// <summary> /// Updates all <typeparamref name="TComponent"/> data to interpolated values between two other components. /// </summary> public void Interpolate(ComponentArray <TComponent> otherA, ComponentArray <TComponent> otherB, float amount) { otherB.componentStates.CopyTo(this.componentStates); for (int entityID = 0; entityID < this.Capacity; entityID++) { if (otherA.componentStates[entityID] && otherB.componentStates[entityID]) { this.components[entityID].Interpolate(otherA.components[entityID], otherB.components[entityID], amount); } else { this.components[entityID] = otherB.components[entityID]; } } }
/// <summary> /// Reads and overwrites the current state of all component data from a binary source, basing incoming data on a previous component array's data. /// </summary> public void Deserialize(ComponentArray <TComponent> previousComponentArray, IReader reader) { this.componentStates.Deserialize(reader); this.serializedStates.Deserialize(reader); for (int entityID = 0; entityID < this.Capacity; entityID++) { if (this.serializedStates[entityID]) { this.components[entityID].Deserialize(reader); } else if (previousComponentArray != null) { // Components not explicitly sent are assumed to be the same as previously (the case where the component does not exist is a don't-care) this.components[entityID] = previousComponentArray.components[entityID]; } } }
/// <summary> /// Writes the state of all component data to a binary source, only writing data that has changed from a previous component array. /// </summary> public void Serialize(ComponentArray <TComponent> previousComponentArray, IWriter writer) { // Only serialize components that are attached and that have changed compared to the previous component state for (int entityID = 0; entityID < this.Capacity; entityID++) { this.serializedStates[entityID] = this.componentStates[entityID] && (previousComponentArray == null || !previousComponentArray.components[entityID].Equals(this.components[entityID])); } this.componentStates.Serialize(writer); this.serializedStates.Serialize(writer); for (int entityID = 0; entityID < this.Capacity; entityID++) { if (!this.serializedStates[entityID]) { continue; } this.components[entityID].Serialize(writer); } }
/// <summary> /// Copies a single entity's <typeparamref name="TComponent"/> data to another entity's components in another component array. /// </summary> public void CopyEntityTo(int thisEntityID, ComponentArray <TComponent> otherComponentArray, int otherEntityID) { otherComponentArray.components[otherEntityID] = this.components[thisEntityID]; otherComponentArray.componentStates[otherEntityID] = this.componentStates[thisEntityID]; }
/// <summary> /// Copies all <typeparamref name="TComponent"/> data to another component array. /// </summary> public void CopyTo(ComponentArray <TComponent> other) { Array.Copy(this.components, other.components, this.Capacity); this.componentStates.CopyTo(other.componentStates); }