Exemple #1
0
    /// <summary>
    /// Copies components on this entity at <paramref name="indices"/> in the <see cref="ExampleComponentsLookup"/> 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(ExampleEntity 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 >= ExampleComponentsLookup.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(ExampleComponentsLookup)),
                          HINT);
            }

            if (!HasComponent(index))
            {
                continue;
            }

            if (!copyToEntity.HasComponent(index) || replaceExisting)
            {
                var component = GetComponent(index);
                copyToEntity.CopyComponentTo(component);
            }
        }
    }
Exemple #2
0
    /// <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(ExampleEntity copyToEntity, bool replaceExisting)
    {
        for (var i = 0; i < ExampleComponentsLookup.TotalComponents; ++i)
        {
            if (!HasComponent(i))
            {
                continue;
            }

            if (!copyToEntity.HasComponent(i) || replaceExisting)
            {
                var component = GetComponent(i);
                copyToEntity.CopyComponentTo(component);
            }
        }
    }
Exemple #3
0
    /// <summary>
    /// Copies all components on this entity to <paramref name="copyToEntity"/>.
    /// </summary>
    public void CopyTo(ExampleEntity copyToEntity)
    {
        for (var i = 0; i < ExampleComponentsLookup.TotalComponents; ++i)
        {
            if (HasComponent(i))
            {
                if (copyToEntity.HasComponent(i))
                {
                    throw new EntityAlreadyHasComponentException(
                              i,
                              "Cannot copy component '" +
                              ExampleComponentsLookup.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(ExampleEntity entity)
    {
        for (var i = 0; i < _components.Count; i++)
        {
            var component = _components[i];
            if (_components[i] == null)
            {
                continue;
            }

            var index = ExampleComponentsLookup.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(ExampleComponentsLookup));
            }
        }
    }