Ejemplo n.º 1
0
Archivo: World.cs Proyecto: PushoN/ecs
        public TState CreateState()
        {
            var state = PoolClass <TState> .Spawn();

            state.entityId = default;
            state.tick     = default;
            return(state);
        }
Ejemplo n.º 2
0
        public void Do(int iterationCounter)
        {
            // Sort circles based on the distance to center
            var instance = PoolClass <Circle.CirclesComparer> .Spawn();

            instance.center = this.mPackingCenter;
            System.Array.Sort(this.circles.innerArray.arr, 0, this.circles.Count, instance);
            PoolClass <Circle.CirclesComparer> .Recycle(ref instance);

            for (int k = 0; k < iterationCounter; ++k)
            {
                var minSeparationSq = this.mMinSeparation * this.mMinSeparation;
                for (var i = 0; i < this.circles.Count - 1; i++)
                {
                    for (var j = i + 1; j < this.circles.Count; j++)
                    {
                        if (i == j)
                        {
                            continue;
                        }

                        FLOAT2 AB = this.circles[j].mCenter - this.circles[i].mCenter;
                        float  r  = this.circles[i].mRadius + this.circles[j].mRadius;

                        // Length squared = (dx * dx) + (dy * dy);
                        var   d        = AB.sqrMagnitude - minSeparationSq;
                        float minSepSq = System.Math.Min(d, minSeparationSq);
                        d -= minSepSq;

                        if (d < r * r - 0.01f)
                        {
                            AB.Normalize();

                            AB *= (float)((r - System.Math.Sqrt(d)) * 0.5f);

                            this.circles[j].mCenter += AB;
                            this.circles[i].mCenter -= AB;
                        }
                    }
                }

                var damping = 0.1f / (float)iterationCounter;
                for (var i = 0; i < this.circles.Count; i++)
                {
                    var v = this.circles[i].mCenter - this.mPackingCenter;
                    v *= damping;
                    this.circles[i].mCenter -= v;
                }
            }
        }
Ejemplo n.º 3
0
Archivo: World.cs Proyecto: PushoN/ecs
        public void Register <TEntity>(ref Filter <TEntity> filterRef, bool freeze, bool restore) where TEntity : struct, IEntity
        {
            var code     = WorldUtilities.GetKey <TEntity>();
            var capacity = this.GetCapacity <TEntity>(code);

            if (filterRef == null)
            {
                filterRef = PoolClass <Filter <TEntity> > .Spawn();

                filterRef.Initialize(capacity);
                filterRef.SetFreeze(freeze);
            }
            else
            {
                filterRef.SetFreeze(freeze);
            }

            if (freeze == false)
            {
                IList list;
                if (this.filtersCache.TryGetValue(code, out list) == true)
                {
                    ((List <Filter <TEntity> >)list).Add(filterRef);
                }
                else
                {
                    list = PoolList <Filter <TEntity> > .Spawn(capacity);

                    ((List <Filter <TEntity> >)list).Add(filterRef);
                    this.filtersCache.Add(code, list);
                }
            }

            if (restore == true)
            {
                // Update entities cache
                for (int i = 0; i < filterRef.Count; ++i)
                {
                    var item = filterRef[i];
                    var list = PoolList <TEntity> .Spawn(capacity);

                    list.Add(item);
                    this.AddEntity(item, updateFilters: false);
                }

                this.UpdateFilters <TEntity>(code);
            }
        }
Ejemplo n.º 4
0
Archivo: World.cs Proyecto: PushoN/ecs
        public void Register <TEntity>(ref Components <TEntity, TState> componentsRef, bool freeze, bool restore) where TEntity : struct, IEntity
        {
            var code     = WorldUtilities.GetKey <TEntity>();
            var capacity = 100;

            if (componentsRef == null)
            {
                componentsRef = PoolClass <Components <TEntity, TState> > .Spawn();

                componentsRef.Initialize(capacity);
                componentsRef.SetFreeze(freeze);
            }
            else
            {
                componentsRef.SetFreeze(freeze);
            }

            if (freeze == false)
            {
                if (this.componentsCache.ContainsKey(code) == true)
                {
                    this.componentsCache[code] = componentsRef;
                }
                else
                {
                    this.componentsCache.Add(code, componentsRef);
                }
            }

            /*if (restore == true) {
             *
             *  var data = componentsRef.GetData();
             *  foreach (var item in data) {
             *
             *      var components = item.Value;
             *      for (int i = 0, count = components.Count; i < count; ++i) {
             *
             *          this.AddComponent<TEntity, IComponent<TState, TEntity>>(Entity.Create<TEntity>(item.Key), components[i]);
             *
             *      }
             *
             *  }
             *
             * }*/
        }
Ejemplo n.º 5
0
        public void Initialize(int capacity)
        {
            this.list = PoolRefList <Entity> .Spawn(capacity);

            this.archetypes = PoolClass <ArchetypeEntities> .Spawn();
        }