Exemplo n.º 1
0
    public override Vector3 CalculateMove(OnlineHerdAgent agent, List <Transform> context, OnlineHerd herd)
    {
        // if no neighbors return no adjustment
        if (context.Count == 0)
        {
            return(Vector3.zero);
        }

        Vector3          cohesionMove  = Vector3.zero;
        List <Transform> filterContext = (filter == null) ? context : filter.Filter(agent, context); // this is a filtered behavior

        // if no neighbors return no adjustment
        if (filterContext.Count == 0)
        {
            return(Vector3.zero);
        }
        foreach (Transform item in filterContext)  // add all points together and average
        {
            cohesionMove += item.position;
        }
        cohesionMove /= filterContext.Count; // average, cohesionMove is now the transform of the destination

        // create offset from agent position, the actual move of the agent
        if (cohesionMove == Vector3.zero)
        {
            return(cohesionMove);
        }
        cohesionMove -= agent.transform.position;

        return(cohesionMove);
    }
Exemplo n.º 2
0
    public override Vector3 CalculateMove(OnlineHerdAgent agent, List <Transform> context, OnlineHerd herd)
    {
        // handle data mismatch
        if (behaviors.Length != weights.Length)
        {
            Debug.LogError("Data missmatch in " + name, this);
            return(Vector3.zero);
        }

        // set up move
        Vector3 move = Vector3.zero;

        // iterate through behaviors
        for (int i = 0; i < behaviors.Length; i++)
        {
            Vector3 partialMove = behaviors[i].CalculateMove(agent, context, herd) * weights[i]; // do a behavior

            if (partialMove != Vector3.zero)
            {
                if (partialMove.sqrMagnitude > weights[i] * weights[i]) // the weight caps the magnitude of each behavior
                {
                    partialMove.Normalize();
                    partialMove *= weights[i];
                }

                move += partialMove;
            }
        }

        move.y = 0; // bison don't jump on their own
        return(move);
    }
Exemplo n.º 3
0
    public override Vector3 CalculateMove(OnlineHerdAgent agent, List <Transform> context, OnlineHerd herd)
    {
        // if no neighbors return no adjustment
        if (context.Count == 0)
        {
            return(Vector3.zero);
        }

        // add all points together and average
        Vector3 avoidanceMove = Vector3.zero;
        int     nAvoid        = 0;

        List <Transform> filterContext = (filter == null) ? context : filter.Filter(agent, context); // this is a filtered behavior

        foreach (Transform item in filterContext)
        {
            if (Vector3.SqrMagnitude(item.position - agent.transform.position) < herd.SquareAvoidanceRadius) // if the distance to the item is within the avoidance radius
            {
                Vector3 dist = agent.transform.position - item.position;
                nAvoid++;
                // float forceMult = (dist.sqrMagnitude / herd.neighborRadius * herd.neighborRadius) * 100;
                // float forceMult = herd.neighborRadius - Vector3.Distance(agent.transform.position, item.position);
                //Debug.Log("player is " + Vector3.Distance(agent.transform.position, item.position) + " away.");

                avoidanceMove += dist; // add vector pointing away from item
            }
        }

        if (nAvoid > 0)
        {
            avoidanceMove /= nAvoid;             // average, avoidanceMove is now the transform of the destination
        }
        return(avoidanceMove);
    }
    public override Vector3 CalculateMove(OnlineHerdAgent agent, List <Transform> context, OnlineHerd herd)
    {
        // if no neighbors return no adjustment
        if (context.Count == 0)
        {
            return(Vector3.zero);
        }

        // add all points together and average
        Vector3 avoidanceMove = Vector3.zero;
        int     nAvoid        = 0;

        List <Transform> filterContext = (filter == null) ? context : filter.Filter(agent, context); // this is a filtered behavior

        // if no filtered neighbors return no adjustment
        if (filterContext.Count == 0)
        {
            return(Vector3.zero);
        }

        foreach (Transform item in filterContext)
        {
            if (Vector3.SqrMagnitude(item.position - agent.transform.position) < herd.SquareIdleAvoidanceRadius) // if the distance to the item is within the avoidance radius
            {
                nAvoid++;
                avoidanceMove += agent.transform.position - item.position; // add vector pointing away from item
            }
        }

        if (nAvoid > 0)
        {
            avoidanceMove /= nAvoid;             // average, avoidanceMove is now the transform of the destination
        }
        return(avoidanceMove);
    }
Exemplo n.º 5
0
    // keeps track of all bison that exit
    private void OnTriggerExit(Collider other)
    {
        OnlineHerdAgent exitAgent = other.gameObject.GetComponent <OnlineHerdAgent>();

        if (exitAgent)
        {
            exitAgent.inHerdBox = false;
            BisonBeingHerded.Remove(exitAgent);
        }
    }
Exemplo n.º 6
0
    // keeps track of all bison that enter
    private void OnTriggerEnter(Collider other)
    {
        OnlineHerdAgent newAgent = other.gameObject.GetComponent <OnlineHerdAgent>();

        if (newAgent.AgentHerd == boxHerd)
        {
            newAgent.inHerdBox = true;
            BisonBeingHerded.Add(newAgent);
        }
    }
    public override Vector3 CalculateMove(OnlineHerdAgent agent, List <Transform> context, OnlineHerd herd)
    {
        Vector3 centerOffset = center - agent.transform.position;
        float   t            = centerOffset.magnitude / radius;

        if (t < 0.95)
        {
            return(Vector3.zero);         // if within 95% of the circle, don't move towards the center
        }
        return(centerOffset * t * t * t); // stronger the further from the center
    }
    public override Vector3 CalculateMove(OnlineHerdAgent agent, List <Transform> context, OnlineHerd herd)
    {
        // If there are no bison or players nearby, we chillin
        if (context.Count == 0)
        {
            return(Vector3.zero);
        }

        // This is what will be returned in the end
        Vector3 direction = Vector3.zero;

        //This should return the player(s), if there are any
        List <Transform> filterContext = (filter == null) ? context : filter.Filter(agent, context);

        // For each player
        foreach (Transform item in filterContext)
        {
            //Use this to see distance between bison and player(s), and increase speed depending on this
            float distance = Vector3.Distance(item.transform.position, agent.transform.position);

            //Assuming the max distance in front of a player that this behavior triggers is 6
            //multiplier output ranges from 1x to 7x
            float multiplier = 7f - distance;

            //This line makes the output instead range from 1/6 to 7/6,
            //where 1/6 is multiplier if 6 units away, and 7/6 is multiplier if 0 units away
            // (So you move slightly faster than player if right on top of them, and slower than them if further away)
            multiplier /= 6f;

            Vector3 playerDirection  = item.transform.forward;
            Vector3 bisonDirection   = agent.transform.forward;
            float   directionOutcome = Vector3.Dot(playerDirection, bisonDirection);

            //If bison is pointing in the opposite direction, double multiplier
            if (directionOutcome == -1)
            {
                multiplier *= 2;
            }

            //If bison is poining in perpendicular direction, 1.5x multiplier
            else if (directionOutcome == 0)
            {
                multiplier *= 1.5f;
            }

            direction = playerDirection;
            //direction *= multiplier;        // Comment this out if you don't want speed to change
        }

        direction *= 6; // Make sure it's visible
        return(direction);
    }
Exemplo n.º 9
0
    // Looks at  all objects in the original context, then returns a new list with only the filtered objects
    public override List <Transform> Filter(OnlineHerdAgent agent, List <Transform> original)
    {
        List <Transform> filtered = new List <Transform>(); // list to return

        foreach (Transform item in original)
        {
            HerdAgent itemAgent = item.GetComponent <HerdAgent>();
            if (itemAgent != null && itemAgent.AgentHerd == agent.AgentHerd) // if this item is in the same herd
            {
                filtered.Add(item);                                          // add it to the list
            }
        }
        return(filtered);
    }
Exemplo n.º 10
0
    public override List <Transform> Filter(OnlineHerdAgent agent, List <Transform> original)
    {
        List <Transform> filtered = new List <Transform>();    // the list to return

        foreach (Transform item in original)                   // for each transform
        {
            if (mask == (mask | (1 << item.gameObject.layer))) // if the gameObject is on the right layer
            {
                filtered.Add(item);                            // add it to the filtered list
            }
        }

        return(filtered);
    }
Exemplo n.º 11
0
    List <Transform> GetNearbyObjects(OnlineHerdAgent agent)
    {
        int layerMask = ~(1 << 10);                        // used to ignore objects in layer 10

        List <Transform> context = new List <Transform>(); // the list to return

        // gets all colliders near the agent
        Collider[] contextColliders = Physics.OverlapSphere(agent.transform.position, neighborRadius, layerMask, QueryTriggerInteraction.Collide);

        // For panicking
        //if (contextColliders.Length > crowdingThreshhold) // If i'm crowded, panic
        //{
        //    agent.state = 2;
        //    agent.agentBody.drag = 0.1f;
        //}
        agent.nearPlayer = false;
        agent.inHill     = false;
        int drovingNeighbors = 0;

        foreach (Collider c in contextColliders)
        {
            if (c != agent.AgentCollider) // if the collider isn't its own
            {
                context.Add(c.transform); // add it to the context
                if (c.gameObject.layer == 12 && c.gameObject.GetComponent <OnlineHerdAgent>().inHerdBox)
                {
                    drovingNeighbors += 1;
                }
            }

            if (c.gameObject.layer == 11) // check if the player is near
            {
                //agent.state = 1;
                agent.nearPlayer = true;
            }
            else if (c.gameObject.tag == "Hill")
            {
                agent.inHill = true;
            }
        }

        // If the bison is in a player's herd box or near enough bison taht are, it will drove
        if (agent.inHerdBox || (drovingNeighbors >= joinDroveThresh))
        {
            agent.Drove();
        }

        return(context);
    }
    public override Vector3 CalculateMove(OnlineHerdAgent agent, List <Transform> context, OnlineHerd herd)
    {
        // if no neighbors, maintain current alignment
        if (context.Count == 0)
        {
            return(Vector3.zero);
        }

        // add all neighbor's alignment together and average
        Vector3          alignmentMove = Vector3.zero;
        List <Transform> filterContext = (filter == null) ? context : filter.Filter(agent, context); // this is a filtered behavior

        foreach (Transform item in filterContext)
        {
            alignmentMove += item.GetComponent <Rigidbody>().velocity; // add the facing direction
        }
        alignmentMove /= context.Count;                                // average, alignmentMove is now the destination alignment

        return(alignmentMove);
    }
    public override Vector3 CalculateMove(OnlineHerdAgent agent, List <Transform> context, OnlineHerd herd)
    {
        // if no neighbors, maintain current alignment
        if (context.Count == 0)
        {
            return(agent.transform.forward);
        }

        // add all neighbor's alignment together and average
        Vector3          alignmentMove = Vector3.zero;
        List <Transform> filterContext = (filter == null) ? context : filter.Filter(agent, context); // this is a filtered behavior

        foreach (Transform item in filterContext)
        {
            if (Vector3.SqrMagnitude(item.position - agent.transform.position) < herd.SquareAvoidanceRadius * 1.25f) // if the distance to the item is within the avoidance radius
            {
                alignmentMove += item.transform.forward;                                                             // add the facing direction
            }
        }
        alignmentMove /= context.Count; // average, alignmentMove is now the destination alignment

        return(alignmentMove);
    }
    public override Vector3 CalculateMove(OnlineHerdAgent agent, List <Transform> context, OnlineHerd herd)
    {
        // if no neighbors return no adjustment
        if (context.Count == 0)
        {
            return(Vector3.zero);
        }

        // add all points together and average
        Vector3          cohesionMove  = Vector3.zero;
        List <Transform> filterContext = (filter == null) ? context : filter.Filter(agent, context); // this is a filtered behavior

        foreach (Transform item in filterContext)
        {
            cohesionMove += item.position;
        }
        cohesionMove /= context.Count; // average

        // create offset from agent position, the actual move of the agent
        cohesionMove -= agent.transform.position;
        cohesionMove  = Vector3.SmoothDamp(agent.transform.forward, cohesionMove, ref currentVelocity, agentSmoothTime); // this is what makes this "smoothed"

        return(cohesionMove);
    }
Exemplo n.º 15
0
    public void SpawnBison(int count, Vector3 origin)
    {
        for (int i = 0; i < count; i++)
        {
            Vector3 spawn = (Vector3)Random.insideUnitCircle * 7.5f;
            spawn.z = spawn.y; // we want z
            spawn.y = 2;       // not y

            spawn += origin;

            // Yo, it's a new bison
            OnlineHerdAgent newAgent = Instantiate(
                agentPrefab,
                spawn,
                Quaternion.Euler(Vector3.up * Random.Range(0f, 360f)), // random start rotation
                transform                                              // herd position
                );
            newAgent.name = "Bison " + agents.Count;
            newAgent.Initialize(this); // agent has startup function
            agents.Add(newAgent);
            NetworkServer.Spawn(newAgent.gameObject);
            Debug.Log("spawned bison over network");
        }
    }
Exemplo n.º 16
0
 public abstract List <Transform> Filter(OnlineHerdAgent agent, List <Transform> original);
Exemplo n.º 17
0
 public abstract Vector3 CalculateMove(OnlineHerdAgent agent, List <Transform> context, OnlineHerd herd);