Ejemplo n.º 1
0
    protected override JobHandle OnUpdate(JobHandle inputDeps)
    {
        //计算准备
        var concurrent = bufferSystem.CreateCommandBuffer().ToConcurrent();

        var fromArr = fromQuery.ToEntityArray(Allocator.TempJob);
        NativeArray <Momentum_C>  MomentumArr = new NativeArray <Momentum_C>(fromArr.Length, Allocator.Persistent);
        NativeArray <Entity>      toArr       = new NativeArray <Entity>(fromArr.Length, Allocator.Persistent);
        NativeArray <Mover_C>     movers      = new NativeArray <Mover_C>(fromArr.Length, Allocator.Persistent);
        NativeArray <MassPoint_C> masses      = new NativeArray <MassPoint_C>(fromArr.Length, Allocator.Persistent);

        for (int i = 0; i < fromArr.Length; i++)
        {
            Momentum_C momentum = EntityManager.GetComponentData <Momentum_C>(fromArr[i]);
            MomentumArr[i] = momentum;
            toArr[i]       = momentum.target;
            if (momentum.target.Equals(Entity.Null))
            {
                continue;
            }
            movers[i] = EntityManager.GetComponentData <Mover_C>(momentum.target);
            masses[i] = EntityManager.GetComponentData <MassPoint_C>(momentum.target);
        }
        //实例句柄
        MomentumJob job = new MomentumJob();

        job.MomentumArr = MomentumArr;
        job.toArr       = toArr;
        job.masses      = masses;
        job.movers      = movers;
        job.concurrent  = concurrent;
        //挂载句柄
        inputDeps = job.Schedule(MomentumArr.Length, 1, inputDeps);
        inputDeps.Complete();
        for (int i = 0; i < movers.Length; i++)
        {
            EntityManager.SetComponentData(toArr[i], movers[i]);
        }
        //释放资源
        EntityManager.DestroyEntity(fromQuery);
        MomentumArr.Dispose(inputDeps);
        fromArr.Dispose(inputDeps);
        toArr.Dispose(inputDeps);
        movers.Dispose(inputDeps);
        masses.Dispose(inputDeps);

        //返回句柄
        return(inputDeps);
    }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
        public void Execute(Entity entity, int index, ref Spliter_C spliter, ref Translation translation, ref Joiner_C joiner, ref Mover_C mover)
        {
            //校验spliter数据合理性
            if (spliter.direction.Equals(double3.zero))
            {
                return;
            }
            bool3 nanResult = math.isnan(math.normalize(spliter.direction));

            if (nanResult.x || nanResult.y || nanResult.z)
            {
                return;
            }
            //体积过小就无法分离子星体了
            if (joiner.Volume < spliter.volume * 2)
            {
                return;
            }

            // 根据split的方向分离小实体
            Entity   subEntity = concurrent.Instantiate(index, smallStar);
            Joiner_C subJoiner = new Joiner_C()
            {
                Volume = spliter.volume + 0.01
            };
            MassPoint_C subMass = new MassPoint_C()
            {
                Mass = UnitHelper.Range2Mass(subJoiner.Range)
            };
            Mover_C         subMover       = new Mover_C();
            Translation     subTranslation = new Translation();
            NonUniformScale subScale       = new NonUniformScale();
            var             dirNormal      = math.normalize(spliter.direction);
            var             distance       = subJoiner.Range + joiner.Range + 0.01;

            subTranslation.Value = translation.Value + (float3)(dirNormal * distance);
            subScale.Value       = 0.01f;
            concurrent.AddComponent(index, subEntity, subTranslation);
            concurrent.AddComponent(index, subEntity, subJoiner);
            concurrent.AddComponent(index, subEntity, subMass);
            concurrent.AddComponent(index, subEntity, subMover);
            concurrent.AddComponent(index, subEntity, subScale);

            //分离产生的力
            var spliterForce = math.normalize(spliter.direction) * subMass.Mass;

            // 添加子物体受力
            SimapleForceSender_C subForce = new SimapleForceSender_C()
            {
                value = -spliterForce * 2,
                type  = ForceType.external,
                to    = subEntity,
                time  = 0.5f
            };
            Entity fEntity = concurrent.CreateEntity(index);

            concurrent.AddComponent(index, fEntity, subForce);
            //添加子物体动量
            Momentum_C subMomentum = new Momentum_C()
            {
                mass   = subMass.Mass,
                speed  = spliterForce * 2,
                target = subEntity
            };
            Entity mEntity = concurrent.CreateEntity(index);

            concurrent.AddComponent(index, mEntity, subMomentum);
            // 添加主物体动量
            Momentum_C momentum = new Momentum_C()
            {
                mass   = subMass.Mass,
                speed  = -spliterForce * 2,
                target = entity
            };
            Entity mainMEntity = concurrent.CreateEntity(index);

            concurrent.AddComponent(index, mainMEntity, momentum);

            // 减少朱物体mass的质量
            joiner.Volume -= spliter.volume;
            concurrent.RemoveComponent(index, entity, typeof(Spliter_C));
            //UnityEngine.Debug.Log("spliterV:" + spliter.volume);
        }