public static bool GetClosestAgent(AIAgent requester, Vector2 requesterPosition, List <AIAgent> options, out AIAgent closestSiblin, out float closestSiblinSqrDistance, AIAgent exception = null)
    {
        bool areClosestAgent = false;

        closestSiblin            = null;
        closestSiblinSqrDistance = float.MaxValue;
        for (int i = 0; i < options.Count; i++)
        {
            AIAgent tempAgent = options[i];

            if (tempAgent == requester || tempAgent == exception)
            {
                continue;
            }

            float tempSqrDistance = Vector2Utilities.SqrDistance(requesterPosition, tempAgent.transform.position);
            if (tempSqrDistance < closestSiblinSqrDistance)
            {
                areClosestAgent          = true;
                closestSiblin            = tempAgent;
                closestSiblinSqrDistance = tempSqrDistance;
            }
        }
        return(areClosestAgent);
    }
    protected float GetWeight(Vector2 position, Vector2 destination, float areaEffectDistance, float totalInfluenceDistance, bool linear)
    {
        float sqrDistance               = Vector2Utilities.SqrDistance(position, destination);
        float sqrAreaEfectDistance      = Mathf.Pow(areaEffectDistance, 2);
        float sqrTotalInfluenceDistance = Mathf.Pow(totalInfluenceDistance, 2);

        if (sqrAreaEfectDistance < sqrDistance)
        {
            return(0);
        }
        else if (sqrDistance <= sqrTotalInfluenceDistance)
        {
            return(1);
        }


        float weight;

        if (linear)
        {
            weight = 1 - ((Mathf.Sqrt(sqrDistance) - totalInfluenceDistance) / (areaEffectDistance - totalInfluenceDistance));
        }
        else
        {
            weight = 1 - (sqrDistance - sqrTotalInfluenceDistance) / (sqrAreaEfectDistance - sqrTotalInfluenceDistance);
        }


        return(weight);
    }
    private List <ITargetable> GetPrioritizedTargets(List <ITargetable> filteredPosibleTargets, Vector2 position)
    {
        bool haveAgent = false;

        ITargetable closestPosibleTarget    = null;//estructuras?
        float       closestStructureSqrDist = float.MaxValue;
        AIAgent     closestAgent            = null;
        float       closestAgentSqrDist     = float.MaxValue;

        foreach (ITargetable targetable in filteredPosibleTargets)
        {
            Vector2 targetPos = targetable.GameObject.transform.position;
            if (targetable is AIAgent)
            {
                haveAgent = true;
                float sqrDist = Vector2Utilities.SqrDistance(targetPos, position);
                if (sqrDist < closestAgentSqrDist)
                {
                    closestAgent        = (AIAgent)targetable;
                    closestAgentSqrDist = sqrDist;
                }
            }
            else
            {
                if (haveAgent)
                {
                    continue;
                }

                float sqrDist = Vector2Utilities.SqrDistance(targetPos, position);
                if (sqrDist < closestStructureSqrDist)
                {
                    closestPosibleTarget    = targetable;
                    closestStructureSqrDist = sqrDist;
                }
            }
        }

        List <ITargetable> returnTargets = new List <ITargetable>();

        if (haveAgent)
        {
            returnTargets = closestAgent.parent.children.ConvertAll(x => (ITargetable)x);
            return(returnTargets);
        }
        else if (closestPosibleTarget != null)
        {
            returnTargets.Add(closestPosibleTarget);
            return(returnTargets);
        }
        else
        {
            return(null);
        }
    }
Example #4
0
    public IBehaviourSet GetBehaviourSet(AIAgent requester)
    {
        Vector2 pos = requester.transform.position;
        Vector2 des = requester.Destination;

        if (ObstacleToDestination(pos, des, requester.data.radious, raycastMask))
        {
            return(interferenceBS);
        }

        float sqrDist = Vector2Utilities.SqrDistance(pos, des);

        if (sqrDist <= Mathf.Pow(requester.action.BaseData.rangeOfAction + requester.data.reachDestinationMargin, 2))
        {
            return(actingBS);
        }

        return(defaultBS);
    }
    public IBehaviourSet GetBehaviourSet(AIAgent requester)
    {
        Vector2 pos = requester.transform.position;
        Vector2 des = requester.Destination;

        if (ObstacleToDestination(pos, des, requester.data.radious, raycastMask))
        {
            return(interferenceBS);
        }
        else if (Vector2Utilities.SqrDistance(pos, des) <= Mathf.Pow(requester.data.idleBSRange, 2) && !requester.parent.Moving)
        {
            if (requester.debug)
            {
                Debug.Log($"returning parkingBS");
            }
            return(idleBS);
        }

        return(defaultBS);
    }
Example #6
0
    public bool UseStuckBehaviour(out Vector2?stuckDestination)
    {
        if (currentlyUsingSB)
        {
            if (!Moving)
            {
                stuckDestination = currentStuckDestination = CalculateStuckDestination(parent.Data.obstacleLayerMask);
            }


            float sqrDistToStuckDest = Vector2Utilities.SqrDistance((Vector2)transform.position, currentStuckDestination);
            if (sqrDistToStuckDest < Mathf.Pow(data.reachDestinationMargin, 2) || InActionRangeToDestination)
            {
                stuckDestination = null;
                currentlyUsingSB = false;
                return(false);
            }
            else
            {
                stuckDestination = currentStuckDestination;
                return(true);
            }
            //debo revisar si es que se vuelva a atrapar luego de ya estar atrapado.
        }
        else
        {
            if (!Moving && !InActionRangeToDestination && !BSJustChanged)
            {
                stuckDestination = currentStuckDestination = CalculateStuckDestination(parent.Data.obstacleLayerMask);
                currentlyUsingSB = true;
                ResetMovementDelta();
                return(true);
            }
            else
            {
                stuckDestination = null;
                currentlyUsingSB = false;
                return(false);
            }
        }
    }
    public static Vector2 Arribe(AIAgent requester, Vector2 position, Vector2 destinationPoint, float deltaTime, bool decelerate = true, bool useStopRadious = true)
    {
        AIAgentData requesterData = requester.data;

        float sqrDist = Vector2Utilities.SqrDistance(destinationPoint, position);

        //in stop radious
        if (sqrDist <= Mathf.Pow(requesterData.stopRadious, 2) && useStopRadious)
        {
            return(position);
        }
        //in slow down radious
        else if (sqrDist < Mathf.Pow(requesterData.slowDownStartRadious, 2) && decelerate)
        {
            Vector2 direction = (destinationPoint - position).normalized;

            float dist              = Mathf.Sqrt(sqrDist);
            float speedMult         = dist / requesterData.slowDownStartRadious;
            float movementMagnitude = requesterData.maxSpeed * speedMult * deltaTime;
            //prevent overshooting
            if (dist <= movementMagnitude)
            {
                return(destinationPoint);
            }
            return(position + direction * movementMagnitude);
        }
        else
        {
            float dist = Mathf.Sqrt(sqrDist);
            float movementMagnitude = requesterData.maxSpeed * deltaTime;
            if (dist <= movementMagnitude)
            {
                return(destinationPoint);
            }

            Vector2 direction = (destinationPoint - position) / dist;
            return(position + direction * movementMagnitude);
        }
    }