Esempio n. 1
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();
            }
        }
    }