Example #1
0
        public void Execute(Entity entity, int index, ref Joiner_C joiner)
        {
            //添加移动组件(mover会记录实体信息,AddComponent会覆盖原有组件,所以只能给没有mover的实体单独添加)
            Mover_C mover = new Mover_C();

            concurrent.AddComponent(index, entity, mover);
        }
Example #2
0
    public void InitScene(EntityManager manager, ChunckConfig config)
    {
        var random = new Unity.Mathematics.Random((uint)System.DateTime.Now.Millisecond);

        for (int i = 0; i < config.count; i++)
        {
            var      position = random.NextFloat3(config.center - config.size, config.center + config.size);
            var      volume   = random.NextFloat(config.volumeRange.start, config.volumeRange.start + config.volumeRange.length);
            var      speed    = random.NextFloat3(config.minSpeed, config.maxSpeed);
            var      entity   = manager.Instantiate(config.entity);
            Joiner_C joiner   = new Joiner_C()
            {
                Volume = volume
            };
            Mover_C mover = new Mover_C()
            {
                direction = speed
            };
            Translation translation = new Translation()
            {
                Value = position
            };
            manager.AddComponentData(entity, joiner);
            manager.AddComponentData(entity, mover);
            manager.AddComponentData(entity, translation);
        }
    }
Example #3
0
        [ReadOnly] public NativeArray <MassPoint_C> masses;      //接受者质量组件
        public void Execute(int index)
        {
            if (toArr[index].Equals(Entity.Null))
            {
                return;
            }
            var     momentum  = MomentumArr[index];
            double3 direction = 0;

            //如果mass为0 代表增加单纯的动量(质量不变)
            if (momentum.mass == 0)
            {
                double3 speed = movers[index].direction * masses[index].Mass + momentum.speed;
                direction = speed / masses[index].Mass;
            }
            else
            {
                double3 speed = movers[index].direction * masses[index].Mass + momentum.mass * momentum.speed;
                direction = speed / (masses[index].Mass + momentum.mass);
            }
            Mover_C mover = new Mover_C()
            {
                direction = direction
            };

            movers[index] = mover;
        }
Example #4
0
        public void Execute(int index)
        {
            Entity          entity      = entities[index];
            Joiner_C        joiner      = joiners[index];
            Translation     translation = translations[index];
            Mover_C         mover       = movers[index];
            NonUniformScale scale       = scales[index];
            // 增加的体积
            double addVolume = 0;
            // 增加的质量
            double addMass = 0;
            // 增加的动量
            double3 addMomentum = double3.zero;

            for (int i = 0; i < joiners.Length; i++)
            {
                if (i == index)
                {
                    continue;
                }
                var joiner2 = joiners[i];
                if (joiner.Volume <= 0 || joiner2.Volume <= 0)
                {
                    continue;
                }
                if (joiner2.Range == joiner.Range && entity.Index > entities[i].Index)
                {
                    continue;
                }
                var translation2 = translations[i];
                var dis          = math.distance(translation.Value, translation2.Value);
                if (dis == 0)
                {
                    continue;
                }

                if (joiner.Range + joiner2.Range > dis)
                {
                    //如果被吸收则减少当前体积,如果吸收则增加对方的体积
                    bool   isOut = joiner2.Range > joiner.Range;
                    double v     = isOut ? joiner.Volume : joiner2.Volume;
                    //最少吸收每秒吸收1体积
                    if (v > 0.2)
                    {
                        v = math.max(0.2, v * deltaTime * 2);
                    }
                    if (!isOut)
                    {
                        var m = UnitHelper.Volume2Mass(v);
                        addMass     += m;
                        addMomentum += m * movers[i].direction;
                    }
                    addVolume += isOut ? -v : v;
                }
            }
            joiner.Volume += addVolume;

            //根据物质量显示物体
            if (joiner.Volume < 0.01)
            {
                concurrent.DestroyEntity(index, entity);
                return;
            }

            if (addVolume > 0)
            {
                //添加动量
                Momentum_C momentumIn = new Momentum_C();
                momentumIn.mass   = addMass * 0.1;
                momentumIn.speed  = addMomentum / (addMass + 1);
                momentumIn.target = entity;
                concurrent.AddComponent(index, concurrent.CreateEntity(index), momentumIn);
            }
            scale.Value = (float3)(new double3(1, 1, 1) * 2 * joiner.Range);
            concurrent.SetComponent(index, entity, scale);
            concurrent.SetComponent(index, entity, joiner);
        }