public override Vector2 CalculateMove(LifeAgent agent, List <Transform> context, Life life)
    {
        List <Transform> filteredContext = (filter == null) ? context : filter.Filter(agent, context);

        if (filteredContext.Count == 0)
        {
            return(Vector2.zero);
        }

        Vector2 move = Vector2.zero;

        foreach (Transform item in filteredContext)
        {
            float distance               = Vector2.Distance(item.position, agent.transform.position);
            float distancePercent        = distance / life.neighborRadius;
            float inverseDistancePercent = 1 - distancePercent;
            float weight = inverseDistancePercent / filteredContext.Count;


            Vector2 direction = (item.position - agent.transform.position) * weight;

            move += direction;
        }

        return(move);
    }
    public override Vector2 CalculateMove(LifeAgent agent, List <Transform> context, Life life)
    {
        if (context.Count == 0)
        {
            return(Vector2.zero);
        }

        Vector2          avoidanceMove   = Vector2.zero;
        int              numAvoid        = 0;
        List <Transform> filteredContext = (filter == null) ? context : filter.Filter(agent, context);

        foreach (Transform item in filteredContext)
        {
            if (Vector2.SqrMagnitude(item.position - agent.transform.position) < life.SquareAvoidanceRadius)
            {
                numAvoid++;
                avoidanceMove += (Vector2)(agent.transform.position - item.position);
            }
        }
        if (numAvoid > 0)
        {
            avoidanceMove /= numAvoid;
        }
        return(avoidanceMove);
    }
Example #3
0
    public override Vector2 CalculateMove(LifeAgent agent, List <Transform> context, Life life)
    {
        if (context.Count == 0)
        {
            //maintain same direction
            return(agent.transform.up);
        }

        //add all points together and average
        Vector2          alignmentMove   = Vector2.zero;
        List <Transform> filteredContext = (filter == null) ? context : filter.Filter(agent, context);
        int count = 0;

        foreach (Transform item in filteredContext)
        {
            if (Vector2.SqrMagnitude(item.position - agent.transform.position) <= life.SquareSmallRadius)
            {
                alignmentMove += (Vector2)item.transform.up;
                count++;
            }
        }
        if (count != 0)
        {
            alignmentMove /= count;
        }


        return(alignmentMove);
    }
    public override Vector2 CalculateMove(LifeAgent agent, List <Transform> context, Life life)
    {
        if (context.Count == 0)
        {
            return(Vector2.zero);
        }

        //add all points together and get the average
        Vector2          cohesionMove    = Vector2.zero;
        List <Transform> filteredContext = (filter == null) ? context : filter.Filter(agent, context);
        int count = 0;

        foreach (Transform item in filteredContext)
        {
            if (Vector2.SqrMagnitude(item.position - agent.transform.position) <= life.SquareSmallRadius)
            {
                cohesionMove += (Vector2)item.position;
                count++;
            }
        }
        if (count != 0)
        {
            cohesionMove /= count;
        }


        //create offset from agent position

        //direction from a to b = b - a
        cohesionMove -= (Vector2)agent.transform.position;

        return(cohesionMove);
    }
Example #5
0
    public override Vector2 CalculateMove(LifeAgent agent, List <Transform> context, Life life)
    {
        //if no neighbours, return no adjustment
        if (context.Count == 0)
        {
            return(Vector2.zero);
        }

        //add all points together and average
        Vector2 cohesionMove = Vector2.zero;
        //if(filter == null) {context} else{filter.Filter(agent, context)}
        List <Transform> filteredContext = (filter == null) ? context : filter.Filter(agent, context);
        int count = 0;

        foreach (Transform item in filteredContext)
        {
            if (Vector2.SqrMagnitude(item.position - agent.transform.position) <= life.SquareSmallRadius)
            {
                cohesionMove += (Vector2)item.position;
                count++;
            }
        }
        if (count != 0)
        {
            cohesionMove /= count;
        }


        //create offset from agent position
        cohesionMove -= (Vector2)agent.transform.position;
        cohesionMove  = Vector2.SmoothDamp(agent.transform.up, cohesionMove, ref currentVelocity, agentSmoothTime);
        return(cohesionMove);
    }
Example #6
0
    public override Vector2 CalculateMove(LifeAgent agent, List <Transform> context, Life life)
    {
        if (context.Count == 0)
        {
            return(Vector2.zero);
        }



        List <Transform> filteredContext = (filter == null) ? context : filter.Filter(agent, context);

        List <Transform> obstacleContext = (filter == null) ? context : obstaclesFilter.Filter(agent, context);


        if (filteredContext.Count == 0)
        {
            return(Vector2.zero);
        }

        //find nearest obstacle to hide behind
        float     nearestDistance = float.MaxValue;
        Transform nearestObstacle = null;

        foreach (Transform item in obstacleContext)
        {
            float distance = Vector2.Distance(item.position, agent.transform.position);
            if (distance < nearestDistance)
            {
                nearestDistance = distance;
                nearestObstacle = item;
            }
        }
        //if no obstacle
        if (nearestObstacle == null)
        {
            return(Vector2.zero);
        }

        //find best hiding spot
        Vector2 hidePosition = Vector2.zero;

        foreach (Transform item in filteredContext)
        {
            Vector2 obstacleDirection = nearestObstacle.position - item.position;

            obstacleDirection = obstacleDirection.normalized * hideBehindObstacleDistance;

            hidePosition += ((Vector2)nearestObstacle.position) + obstacleDirection;
        }
        hidePosition /= filteredContext.Count;


        //for debug only
        Debug.DrawRay(hidePosition, Vector2.up * 1f);

        return(hidePosition - (Vector2)agent.transform.position);
    }
Example #7
0
    public override Vector2 CalculateMove(LifeAgent agent, List <Transform> context, Life life)
    {
        if (path == null)
        {
            FindPath(agent, context);
        }

        return(FollowPath(agent));
    }
Example #8
0
    private void Start()
    {
        squareMaxSpeed        = maxSpeed * maxSpeed;
        squareNeighborRadius  = neighborRadius * neighborRadius;
        squareAvoidanceRadius = squareNeighborRadius * avoidanceRadiusMultiplier * avoidanceRadiusMultiplier;
        squareSmallRadius     = squareNeighborRadius * smallRadiusMultiplier * smallRadiusMultiplier;

        for (int i = 0; i < startingCount; i++)
        {
            LifeAgent newAgent = Instantiate(lifePrefab, Random.insideUnitCircle * startingCount * AgentDensity,
                                             Quaternion.Euler(Vector3.forward * Random.Range(0, 360f)), transform);
        }
    }
Example #9
0
    private void FindPath(LifeAgent agent, List <Transform> context)
    {
        List <Transform> filteredContext = (filter == null) ? context : filter.Filter(agent, context);

        if (filteredContext.Count == 0)
        {
            return;
        }

        int randomPath = Random.Range(0, filteredContext.Count);

        path = filteredContext[randomPath].GetComponentInParent <Path>();
    }
Example #10
0
    public bool InRadius(LifeAgent agent)
    {
        waypointDirection = (Vector2)path.waypoints[currentWaypoint].position - (Vector2)agent.transform.position;

        if (waypointDirection.magnitude < path.radius)
        {
            return(true);
        }
        else
        {
            return(false);
        }
    }
    public override List <Transform> Filter(LifeAgent agent, List <Transform> original)
    {
        List <Transform> filtered = new List <Transform>();

        foreach (Transform item in original)
        {
            if (mask == (mask | (1 << item.gameObject.layer)))
            {
                filtered.Add(item);
            }
        }
        return(filtered);
    }
Example #12
0
    public override Vector2 CalculateMove(LifeAgent agent, List <Transform> context, Life life)
    {
        //direction to the center
        //magnitude will = distance to the center
        Vector2 centerOffset = center - (Vector2)agent.transform.position;
        float   t            = centerOffset.magnitude / radius;

        if (t < 0.9f)
        {
            return(Vector2.zero);
        }

        return(centerOffset * t * t);
    }
Example #13
0
    List <Transform> GetNearbyObjects(LifeAgent agent)
    {
        List <Transform> context = new List <Transform>();

        Collider2D[] contextColliders = Physics2D.OverlapCircleAll(agent.transform.position, neighborRadius);
        foreach (Collider2D c in contextColliders)
        {
            if (c != agent.AgentCollider)
            {
                context.Add(c.transform);
            }
        }

        return(context);
    }
Example #14
0
    public override List <Transform> Filter(LifeAgent agent, List <Transform> original)
    {
        List <Transform> filtered = new List <Transform>();

        foreach (Transform item in original)
        {
            LifeAgent itemAgent = item.GetComponent <LifeAgent>();

            if (itemAgent != null)
            {
                if (itemAgent.AgentLife == agent.AgentLife)
                {
                    filtered.Add(item);
                }
            }
        }
        return(filtered);
    }
Example #15
0
    private Vector2 FollowPath(LifeAgent agent)
    {
        if (path == null)
        {
            return(Vector2.zero);
        }

        if (InRadius(agent))
        {
            currentWaypoint++;
            if (currentWaypoint >= path.waypoints.Count)
            {
                currentWaypoint = 0;
            }

            return(Vector2.zero);
        }

        return(waypointDirection);
    }
    public override Vector2 CalculateMove(LifeAgent agent, List <Transform> context, Life life)
    {
        Vector2 move = Vector2.zero;

        for (int i = 0; i < behaviors.Length; i++)
        {
            Vector2 partialMove = behaviors[i].behaviors.CalculateMove(agent, context, life) * behaviors[i].weights;

            if (partialMove != Vector2.zero)
            {
                //make sure the number we get for moving the agent isnt larger than the weight we gave it
                if (partialMove.sqrMagnitude > behaviors[i].weights * behaviors[i].weights)
                {
                    partialMove.Normalize();
                    partialMove *= behaviors[i].weights;
                }
                //bring all the behaviors together as one
                move += partialMove;
            }
        }

        return(move);
    }
Example #17
0
 public abstract Vector2 CalculateMove(LifeAgent agent, List <Transform> context, Life life);
Example #18
0
 public abstract List <Transform> Filter(LifeAgent agent, List <Transform> original);