/// <summary> /// Copies components on this entity at <paramref name="indices"/> in the <see cref="MyTest2ComponentsLookup"/> to /// <paramref name="copyToEntity"/>. If <paramref name="replaceExisting"/> is true any of the components that /// <paramref name="copyToEntity"/> has that this entity has will be replaced, otherwise they will be skipped. /// </summary> public void CopyTo(MyTest2Entity copyToEntity, bool replaceExisting, params int[] indices) { for (var i = 0; i < indices.Length; ++i) { var index = indices[i]; // Validate that the index is within range of the component lookup if (index < 0 && index >= MyTest2ComponentsLookup.TotalComponents) { const string OUT_OF_RANGE_WARNING = "Component Index [{0}] is out of range for [{1}]."; const string HINT = "Please ensure any CopyTo indices are valid."; throw new IndexOutOfLookupRangeException( string.Format(OUT_OF_RANGE_WARNING, index, nameof(MyTest2ComponentsLookup)), HINT); } if (!HasComponent(index)) { continue; } if (!copyToEntity.HasComponent(index) || replaceExisting) { var component = GetComponent(index); copyToEntity.CopyComponentTo(component); } } }
/// <summary> /// Copies all components on this entity to <paramref name="copyToEntity"/>; if <paramref name="replaceExisting"/> /// is true any of the components that <paramref name="copyToEntity"/> has that this entity has will be replaced, /// otherwise they will be skipped. /// </summary> public void CopyTo(MyTest2Entity copyToEntity, bool replaceExisting) { for (var i = 0; i < MyTest2ComponentsLookup.TotalComponents; ++i) { if (!HasComponent(i)) { continue; } if (!copyToEntity.HasComponent(i) || replaceExisting) { var component = GetComponent(i); copyToEntity.CopyComponentTo(component); } } }
/// <summary> /// Copies all components on this entity to <paramref name="copyToEntity"/>. /// </summary> public void CopyTo(MyTest2Entity copyToEntity) { for (var i = 0; i < MyTest2ComponentsLookup.TotalComponents; ++i) { if (HasComponent(i)) { if (copyToEntity.HasComponent(i)) { throw new EntityAlreadyHasComponentException( i, "Cannot copy component '" + MyTest2ComponentsLookup.ComponentNames[i] + "' to " + this + "!", "If replacement is intended, please call CopyTo() with `replaceExisting` set to true."); } var component = GetComponent(i); copyToEntity.CopyComponentTo(component); } } }
/// <summary> /// Applies all components in the blueprint to <paramref name="entity"/>. /// </summary> /// <param name="entity"></param> public void ApplyToEntity(MyTest2Entity entity) { for (var i = 0; i < _components.Count; i++) { var component = _components[i]; if (_components[i] == null) { continue; } var index = MyTest2ComponentsLookup.GetComponentIndex(component); if (index != -1) { entity.CopyComponentTo(component); } else { Debug.LogWarningFormat( JCMG.EntitasRedux.RuntimeConstants.COMPONENT_INDEX_DOES_NOT_EXIST_FOR_TYPE_FORMAT, component.GetType(), typeof(MyTest2ComponentsLookup)); } } }