Example #1
0
    void UpdateArrival()
    {
        var leaderPos = (Vector2)leader.transform.position;

        var leaderDir = SteeringBehaviour.GetDirection(leader);

        var arrivalTarget = leaderPos - leaderDir * behindDistance;

        arrival.enabled = true;
        arrival.SetTarget(arrivalTarget);
    }
Example #2
0
    void PickNewPerch()
    {
        int r;

        if (perches.Length > 0)
        {
            r = Random.Range(0, perches.Length + 1);
            if (r >= perches.Length)
            {
                // no perch! just flock
                target = null;
            }
            else
            {
                target = perches[r];
                steerPerch.SetTarget(target);
            }
        }
    }
Example #3
0
    private void DetermineSubsumptionBehavior()
    {
        // Level 4 -> Flee from enemies when at low hp
        if (!fleeEnemyBehavior.isInhibited)
        {
            if (isScared)
            {
                var closestEnemy = FindClosestEnemy();
                currentEnemyFleeing = closestEnemy == null ? null : closestEnemy.transform;

                fleeEnemyBehavior.SetTarget(currentEnemyFleeing);

                // We are scared and activly fleeing
                if (currentEnemyFleeing != null && WithinFleeRadius(currentEnemyFleeing.position))
                {
                    fleeEnemyBehavior.InhibitLowerBehaviors();
                    heading = fleeEnemyBehavior.GetBehaviorHeading();
                }
                // We are scared but not activly fleeing
                else
                {
                    fleeEnemyBehavior.ResumeLowerBehaviors();
                }
            }
            // We are no longer scared
            else
            {
                fleeEnemyBehavior.ResumeLowerBehaviors();
            }
        }

        // Level 3 -> Persue enemies, be aggresive, and attack.
        if (!persueBehavior.isInhibited)
        {
            GameObject newTarget = null;
            // Resources have depleted.
            if (currentResourcePersued == null)
            {
                newTarget = FindClosestEnemy();
            }
            // Find the closes enemy to our resource.
            else
            {
                newTarget = FindClosestEnemyToResource(currentResourcePersued);
            }

            // Only set new target if it is not the same as old target
            if (currentEnemyPersued != newTarget)
            {
                currentEnemyPersued = newTarget;

                persueBehavior.SetTarget(currentEnemyPersued);
                attackBehavior.SetTarget(currentEnemyPersued);
            }

            if (currentEnemyPersued != null && WithinRadiusOfFood(transform.position) && WithinRadiusOfFood(currentEnemyPersued.transform.position))
            {
                persueBehavior.InhibitLowerBehaviors();
                heading = persueBehavior.GetBehaviorHeading();
            }
            else
            {
                persueBehavior.ResumeLowerBehaviors();
            }
        }

        // Level 1 -> Eat food if it is in radius.
        if (!eatBehavior.isInhibited)
        {
            eatBehavior.SetTarget(currentResourcePersued);
        }

        // Level 1 -> Seek food and eat if within radius.
        if (!arrivalBehavior.isInhibited)
        {
            currentResourcePersued = FindNearestFood();
            arrivalBehavior.SetTarget(currentResourcePersued);
            heading = arrivalBehavior.GetBehaviorHeading();
        }


        // Level 0 -> Avoid obstacles
        if (!avoidObstacleBehavior.isInhibited)
        {
            var currentObstacle = FindClosestObstacle();
            avoidObstacleBehavior.SetTarget(currentObstacle);

            if (currentObstacle != null)
            {
                heading = avoidObstacleBehavior.GetBehaviorHeading();
            }
        }
    }