コード例 #1
0
    /// <summary>
    /// Copies components on this entity at <paramref name="indices"/> in the <see cref="Test2ComponentsLookup"/> 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(Test2Entity 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 >= Test2ComponentsLookup.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(Test2ComponentsLookup)),
                          HINT);
            }

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

            if (!copyToEntity.HasComponent(index) || replaceExisting)
            {
                var component = GetComponent(index);
                copyToEntity.CopyComponentTo(component);
            }
        }
    }
コード例 #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(Test2Entity copyToEntity, bool replaceExisting)
    {
        for (var i = 0; i < Test2ComponentsLookup.TotalComponents; ++i)
        {
            if (!HasComponent(i))
            {
                continue;
            }

            if (!copyToEntity.HasComponent(i) || replaceExisting)
            {
                var component = GetComponent(i);
                copyToEntity.CopyComponentTo(component);
            }
        }
    }
コード例 #3
0
    void when_executing()
    {
        MultiReactiveSystemSpy system = null;
        TestEntity             e1     = null;
        Test2Entity            e2     = null;

        before = () => {
            var contexts = new Contexts();
            system = new MultiReactiveSystemSpy(contexts);
            system.executeAction = entities => {
                foreach (var e in entities)
                {
                    e.nameAge.age += 10;
                }
            };

            e1 = contexts.test.CreateEntity();
            e1.AddNameAge("Max", 42);

            e2 = contexts.test2.CreateEntity();
            e2.AddNameAge("Jack", 24);

            system.Execute();
        };

        it["processes entities from different contexts"] = () => {
            system.entities.Length.should_be(2);
            system.entities.should_contain(e1);
            system.entities.should_contain(e2);

            e1.nameAge.age.should_be(52);
            e2.nameAge.age.should_be(34);
        };

        it["executes once"] = () => {
            system.didExecute.should_be(1);
        };

        it["can ToString"] = () => {
            system.ToString().should_be("MultiReactiveSystem(MultiReactiveSystemSpy)");
        };
    }
コード例 #4
0
        private void SetupTriggeredSystem()
        {
            var contexts = new Contexts();

            _multiReactiveSystemSpy = new MultiReactiveSystemSpy(contexts);
            _multiReactiveSystemSpy.executeAction = entities =>
            {
                foreach (var e in entities)
                {
                    e.NameAge.age += 10;
                }
            };

            e1 = contexts.Test.CreateEntity();
            e1.AddNameAge("Max", 42);

            e2 = contexts.Test2.CreateEntity();
            e2.AddNameAge("Jack", 24);

            _multiReactiveSystemSpy.Execute();
        }
コード例 #5
0
    /// <summary>
    /// Copies all components on this entity to <paramref name="copyToEntity"/>.
    /// </summary>
    public void CopyTo(Test2Entity copyToEntity)
    {
        for (var i = 0; i < Test2ComponentsLookup.TotalComponents; ++i)
        {
            if (HasComponent(i))
            {
                if (copyToEntity.HasComponent(i))
                {
                    throw new EntityAlreadyHasComponentException(
                              i,
                              "Cannot copy component '" +
                              Test2ComponentsLookup.ComponentNames[i] +
                              "' to " +
                              this +
                              "!",
                              "If replacement is intended, please call CopyTo() with `replaceExisting` set to true.");
                }

                var component = GetComponent(i);
                copyToEntity.CopyComponentTo(component);
            }
        }
    }
コード例 #6
0
    /// <summary>
    /// Applies all components in the blueprint to <paramref name="entity"/>.
    /// </summary>
    /// <param name="entity"></param>
    public void ApplyToEntity(Test2Entity entity)
    {
        for (var i = 0; i < _components.Count; i++)
        {
            var component = _components[i];
            if (_components[i] == null)
            {
                continue;
            }

            var index = Test2ComponentsLookup.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(Test2ComponentsLookup));
            }
        }
    }
コード例 #7
0
    void when_executing()
    {
        context["when triggered"] = () => {
            MultiReactiveSystemSpy system = null;
            TestEntity             e1     = null;
            Test2Entity            e2     = null;

            before = () => {
                var contexts = new Contexts();
                system = new MultiReactiveSystemSpy(contexts);
                system.executeAction = entities => {
                    foreach (var e in entities)
                    {
                        e.nameAge.age += 10;
                    }
                };

                e1 = contexts.test.CreateEntity();
                e1.AddNameAge("Max", 42);

                e2 = contexts.test2.CreateEntity();
                e2.AddNameAge("Jack", 24);

                system.Execute();
            };

            it["processes entities from different contexts"] = () => {
                system.entities.Length.should_be(2);
                system.entities.should_contain(e1);
                system.entities.should_contain(e2);

                e1.nameAge.age.should_be(52);
                e2.nameAge.age.should_be(34);
            };

            it["executes once"] = () => {
                system.didExecute.should_be(1);
            };

            it["retains once even when multiple collectors contain entity"] = () => {
                system.didExecute.should_be(1);
            };

            it["can ToString"] = () => {
                system.ToString().should_be("MultiReactiveSystem(MultiReactiveSystemSpy)");
            };
        };



        context["when multiple collectors are triggered with same entity"] = () => {
            MultiTriggeredMultiReactiveSystemSpy system = null;
            TestEntity e1 = null;

            before = () => {
                var contexts = new Contexts();
                system = new MultiTriggeredMultiReactiveSystemSpy(contexts);
                e1     = contexts.test.CreateEntity();
                e1.AddNameAge("Max", 42);

                e1.RemoveNameAge();

                system.Execute();
            };

            it["executes once"] = () => {
                system.didExecute.should_be(1);
            };

            it["merges collected entities and removes duplicates"] = () => {
                system.entities.Length.should_be(1);
            };

            it["clears merged collected entities"] = () => {
                system.Execute();
                system.didExecute.should_be(1);
            };
        };
    }