Ejemplo n.º 1
0
        public void TestExclude()
        {
            FasterDictionary <int, int> test1 = new FasterDictionary <int, int>();
            FasterDictionary <int, int> test2 = new FasterDictionary <int, int>();

            for (int i = 0; i < 100; ++i)
            {
                test1.Add(i, i);
            }

            for (int i = 0; i < 200; i += 2)
            {
                test2.Add(i, i);
            }

            test1.Exclude(test2);

            Assert.AreEqual(50, test1.count);

            for (int i = 1; i < 100; i += 2)
            {
                Assert.IsTrue(test1.ContainsKey(i));
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Note: unluckily I didn't design the serialization system to be component order independent, so unless
        /// I do something about it, this method cannot be optimized, the logic of the component order must stay
        /// untouched (no reordering, no use of dictionaries). Components order must stay as it comes, as
        /// well as extracomponents order.
        /// Speed, however, is not a big issue for this class, as the data is always composed once per entity descriptor
        /// at static constructor time
        /// </summary>
        /// <returns></returns>
        IComponentBuilder[] Construct(int extraComponentsLength, IComponentBuilder[] extraComponents)
        {
            IComponentBuilder[] MergeLists
                (IComponentBuilder[] startingComponents, IComponentBuilder[] newComponents, int newComponentsLength)
            {
                var startComponents =
                    new FasterDictionary <RefWrapper <IComponentBuilder, ComponentBuilderComparer>, IComponentBuilder>();
                var xtraComponents =
                    new FasterDictionary <RefWrapper <IComponentBuilder, ComponentBuilderComparer>, IComponentBuilder>();

                for (uint i = 0; i < startingComponents.Length; i++)
                {
                    startComponents
                    [new RefWrapper <IComponentBuilder, ComponentBuilderComparer>(startingComponents[i])] =
                        startingComponents[i];
                }

                for (uint i = 0; i < newComponentsLength; i++)
                {
                    xtraComponents[new RefWrapper <IComponentBuilder, ComponentBuilderComparer>(newComponents[i])] =
                        newComponents[i];
                }

                xtraComponents.Exclude(startComponents);

                if (newComponentsLength != xtraComponents.count)
                {
                    newComponentsLength = xtraComponents.count;

                    uint index = 0;
                    foreach (var couple in xtraComponents)
                    {
                        newComponents[index++] = couple.key.type;
                    }
                }

                IComponentBuilder[] componentBuilders =
                    new IComponentBuilder[newComponentsLength + startingComponents.Length];

                Array.Copy(startingComponents, 0, componentBuilders, 0, startingComponents.Length);
                Array.Copy(newComponents, 0, componentBuilders, startingComponents.Length, newComponentsLength);

                var entityInfoComponentIndex = FetchEntityInfoComponent(componentBuilders);

                DBC.ECS.Check.Assert(entityInfoComponentIndex != -1);

                componentBuilders[entityInfoComponentIndex] = new ComponentBuilder <EntityInfoComponent>(
                    new EntityInfoComponent
                {
                    componentsToBuild = componentBuilders
                });

                return(componentBuilders);
            }

            if (extraComponentsLength == 0)
            {
                return(_componentsToBuild);
            }

            var safeCopyOfExtraComponents = new IComponentBuilder[extraComponentsLength];

            Array.Copy(extraComponents, safeCopyOfExtraComponents, extraComponentsLength);

            return(MergeLists(_componentsToBuild, safeCopyOfExtraComponents, extraComponentsLength));
        }