示例#1
0
    public float3 ComputeCohesion(NativeList <FlockAgentComponent> agents)
    {
        int    neighborCount = 0;
        float3 v             = float3.zero;

        for (int i = 0; i < agents.Length; i++)
        {
            FlockAgentComponent agent = agents[i];
            if (agent.m_currentLocation.Equals(m_currentLocation))
            {
                continue;
            }

            if (math.distance(agent.m_currentLocation, m_currentLocation) < 2.0f && IsInFOV(agent.m_currentLocation))
            {
                v.x += agent.m_currentLocation.x;
                v.z += agent.m_currentLocation.z;
                neighborCount++;
            }
        }

        if (neighborCount <= 0)
        {
            return(float3.zero);
        }

        v  /= neighborCount;
        v  -= m_currentLocation;
        v   = math.normalize(v);
        v.y = 0;
        return(v);
    }
示例#2
0
    public void CalculateSpeed(NativeList <FlockAgentComponent> agents)
    {
        if (agents.Length <= 0)
        {
            return;
        }

        m_speed = 0;

        for (int i = 0; i < agents.Length; i++)
        {
            FlockAgentComponent agent = agents[i];
            m_speed += agent.m_speed;
        }

        m_speed /= agents.Length;

        m_speed = math.clamp(m_speed, 3.0f, 7.0f);
    }
示例#3
0
    public float3 ComputeAlignment(NativeList <FlockAgentComponent> agents)
    {
        var aligementVector = m_fowardDir;

        if (agents.Length <= 0)
        {
            return(aligementVector);
        }

        int neighboursInFOV = 0;

        for (int i = 0; i < agents.Length; i++)
        {
            FlockAgentComponent agent = agents[i];
            if (agent.m_currentLocation.Equals(m_currentLocation))
            {
                continue;
            }

            if (math.distance(agent.m_currentLocation, m_currentLocation) < 3.0f && IsInFOV(agent.m_currentLocation))
            {
                neighboursInFOV++;
                aligementVector += agent.m_fowardDir;
            }
        }

        if (neighboursInFOV <= 0)
        {
            return(m_fowardDir);
        }

        aligementVector  /= neighboursInFOV;
        aligementVector   = math.normalize(aligementVector);
        aligementVector.y = 0;
        return(aligementVector);
    }
示例#4
0
    public float3 ComputeSeparation(NativeList <FlockAgentComponent> agents)
    {
        var avoidanceVector = float3.zero;

        if (agents.Length <= 0)
        {
            return(avoidanceVector);
        }

        int neighboursInFOV = 0;

        for (int i = 0; i < agents.Length; i++)
        {
            FlockAgentComponent agent = agents[i];
            if (agent.m_currentLocation.Equals(m_currentLocation))
            {
                continue;
            }

            if (math.distance(agent.m_currentLocation, m_currentLocation) < 3.5f && IsInFOV(agent.m_currentLocation))
            {
                neighboursInFOV++;
                avoidanceVector += (m_currentLocation - agent.m_currentLocation);
            }
        }

        if (neighboursInFOV <= 0 || avoidanceVector.Equals(float3.zero))
        {
            return(avoidanceVector);
        }

        avoidanceVector  /= neighboursInFOV;
        avoidanceVector   = math.normalize(avoidanceVector);
        avoidanceVector.y = 0;
        return(avoidanceVector);
    }
    protected override void OnUpdate()
    {
        var ecb = m_endCommandBuffer.CreateCommandBuffer().AsParallelWriter();

        var translations = GetComponentDataFromEntity <Translation>(true);
        var flockAgents  = GetComponentDataFromEntity <FlockAgentComponent>(false);
        var flockManager = GetComponentDataFromEntity <FlockManagerComponent>(false);

        var query = GetEntityQuery(ComponentType.ReadOnly <FlockManagerComponent>());
        var ents  = query.ToEntityArray(Allocator.TempJob);


        Entities.ForEach((
                             Entity e,
                             ref FlockManagerComponent flockManager,
                             ref DynamicBuffer <NeighborAgentElements> neighborBuffer) =>
        {
            neighborBuffer.Clear();

            for (int i = 0; i < ents.Length; i++)
            {
                if (math.distance(translations[ents[i]].Value, translations[e].Value) < 5.0f * 2.0f)
                {
                    var buffer = GetBuffer <FlockAgentElement>(ents[i]);
                    for (int j = 0; j < buffer.Length; j++)
                    {
                        neighborBuffer.Add(new NeighborAgentElements()
                        {
                            m_agent = buffer[j].m_agent
                        });
                    }
                }
            }
        }).Run();

        ents.Dispose();

        Entities
        .WithReadOnly(translations)
        .WithReadOnly(flockAgents)
        .ForEach((Entity e, int entityInQueryIndex,
                  ref FlockManagerComponent flockManager,
                  in DynamicBuffer <NeighborAgentElements> neighborBuffer,
                  in DynamicBuffer <FlockAgentElement> agentBuffer) =>
        {
            NativeList <FlockAgentComponent> agents = new NativeList <FlockAgentComponent>(Allocator.Temp);
            float3 centroid = float3.zero;

            for (int i = 0; i < neighborBuffer.Length; i++)
            {
                agents.Add(flockAgents[neighborBuffer[i].m_agent]);
            }

            //Flock around the flockmanager leader
            for (int i = 0; i < agentBuffer.Length; i++)
            {
                Entity agent = agentBuffer[i].m_agent;
                FlockAgentComponent agentComp = flockAgents[agent];

                agentComp.CalculateSpeed(agents);

                var cohesion   = agentComp.ComputeCohesion(agents);
                var alignment  = agentComp.ComputeAlignment(agents);
                var seperation = agentComp.ComputeSeparation(agents);
                var bounds     = agentComp.ComputeBounds(translations[e].Value, 5.0f);
                var desired    = agentComp.CompoteDesired(translations[e].Value);

                var vel = (cohesion * flockManager.cohesionBias) + (seperation * flockManager.seperationBias) + (alignment * flockManager.alignmentBias) + (bounds * flockManager.boundsBias) + (desired * flockManager.desiredBias);
                vel     = math.normalize(vel);

                agentComp.m_velocity = vel;

                ecb.SetComponent(entityInQueryIndex, agent, agentComp);
                centroid += translations[agent].Value;
            }


            flockManager.m_centroid = (centroid / agentBuffer.Length);

            agents.Dispose();
        }).WithName("Flock_Manager").ScheduleParallel();