Beispiel #1
0
    /// <summary>
    /// Called every frame when this action needs to be performed.
    /// Applies a fleeing steering force on the given steerable
    /// </summary>
    public override void Execute(Steerable steerable)
    {
        base.Execute(steerable);

        // If the action has elapsed its timer
        if (useTimer && timeActive > timer)
        {
            // Inform subscribers that the action is completed. This stops the action's execution.
            ActionCompleted();

            // Reset the timer for the next time the action is performed.
            ResetTimer();
        }

        if (targetTransform)
        {
            // If player's lights are on, seek player
            if (targetTransform.gameObject.CompareTag("Player"))
            {
                Player player = targetTransform.gameObject.GetComponent <Player>();
                if (player.IsDetectable)
                {
                    // Override the steerable's min/max speed
                    if (overrideSteerableSpeed)
                    {
                        steerable.MinSpeed = minSpeed;
                        steerable.MaxSpeed = maxSpeed;
                    }
                    // Override the steerable's max force
                    if (overrideMaxForce)
                    {
                        steerable.MaxForce = maxForce;
                    }
                }
                else
                {
                    // The player is hidden. Thus, the fish should stop fleeing
                    //ActionCompleted();
                }
            }
            else
            {
                // Override the steerable's min/max speed
                if (overrideSteerableSpeed)
                {
                    steerable.MinSpeed = minSpeed;
                    steerable.MaxSpeed = maxSpeed;
                }
                // Override the steerable's max force
                if (overrideMaxForce)
                {
                    steerable.MaxForce = maxForce;
                }
            }

            steerable.AddFleeForce(targetTransform.position, strengthMultiplier);
        }
    }
Beispiel #2
0
    public override TaskStatus OnUpdate()
    {
        // Cycle through each steering behavior to apply on this GameObject
        for (int i = 0; i < steeringBehaviors.Length; i++)
        {
            // Stores the steering behavior to apply
            SteeringBehavior steeringBehavior = steeringBehaviors[i];

            // Switch the type of the steering behavior and add it to the GameObject
            switch (steeringBehavior.type)
            {
            case SteeringBehaviorType.Seek:
                steerable.AddSeekForce(steeringBehavior.targetTransform.Value.position, steeringBehavior.strengthMultiplier);
                break;

            case SteeringBehaviorType.Arrival:
                steerable.AddArrivalForce(steeringBehavior.targetTransform.Value.position, steeringBehavior.slowingRadius, steeringBehavior.strengthMultiplier);
                break;

            case SteeringBehaviorType.Pursue:
                steerable.AddPursueForce(steeringBehavior.targetSteerable, steeringBehavior.strengthMultiplier);
                break;

            case SteeringBehaviorType.Flee:
                steerable.AddFleeForce(steeringBehavior.targetTransform.Value.position, steeringBehavior.strengthMultiplier);
                break;

            case SteeringBehaviorType.Evade:
                steerable.AddEvadeForce(steeringBehavior.targetSteerable, steeringBehavior.strengthMultiplier);
                break;

            case SteeringBehaviorType.Wander:
                steerable.AddWanderForce(steeringBehavior.circleDistance, steeringBehavior.circleRadius,
                                         steeringBehavior.angleChange, steeringBehavior.strengthMultiplier);
                break;

            case SteeringBehaviorType.ObstacleAvoidance:
                steerable.AddObstacleAvoidanceForce(steeringBehavior.obstacleAvoidanceForce, steeringBehavior.maxObstacleViewDistance,
                                                    steeringBehavior.obstacleLayer, steeringBehavior.strengthMultiplier);
                break;

            case SteeringBehaviorType.Separation:
                steerable.AddSeparationForce(steeringBehavior.strengthMultiplier);
                break;

            case SteeringBehaviorType.Alignment:
                steerable.AddAlignmentForce(steeringBehavior.strengthMultiplier);
                break;

            case SteeringBehaviorType.Cohesion:
                steerable.AddCohesionForce(steeringBehavior.strengthMultiplier);
                break;
            }
        }

        // Apply the forces on the steerable that is performing this action
        steerable.ApplyForces(Time.deltaTime);

        // If the stopping condition has been met, return success
        if (stoppingCondition.Complete())
        {
            // Once the stopping condition is met, stop the steerable movement
            steerable.StopMoving();

            return(TaskStatus.Success);
        }
        // Else, if the stopping condition has not yet been met
        else
        {
            // Return 'TaskStatus.Running', so that the 'Steer' action can run until the stopping condition is met
            return(TaskStatus.Running);
        }
    }
    public override void Execute(Steerable steerable)
    {
        base.Execute(steerable);

        // If the action has elapsed its timer
        if (useTimer && timeActive > timer)
        {
            //Debug.Log("Flee timer elapsed.");

            // Inform subscribers that the action is completed. This stops the action's execution.
            ActionCompleted();

            // Reset the timer for the next time the action is performed.
            ResetTimer();
        }

        if (targetTransform)
        {
            // If player's lights are on, seek player
            if (targetTransform.gameObject.CompareTag("Player"))
            {
                Player player = targetTransform.gameObject.GetComponent<Player>();
                if (player.IsDetectable())
                {
                    // Override the steerable's min/max speed
                    if (overrideSteerableSpeed)
                    {
                        steerable.MinSpeed = minSpeed;
                        steerable.MaxSpeed = maxSpeed;
                    }
                    // Override the steerable's max force
                    if (overrideMaxForce)
                    {
                        steerable.MaxForce = maxForce;
                    }

                    //Debug.Log("FLEE THE PLAYER [Flee.Execute()]");
                }
                else
                {
                    // The player is hidden. Thus, the fish should stop fleeing
                    //ActionCompleted();
                }
            }
            else
            {
                // Override the steerable's min/max speed
                if (overrideSteerableSpeed)
                {
                    steerable.MinSpeed = minSpeed;
                    steerable.MaxSpeed = maxSpeed;
                }
                // Override the steerable's max force
                if (overrideMaxForce)
                {
                    steerable.MaxForce = maxForce;
                }
            }

            steerable.AddFleeForce(targetTransform.position, strengthMultiplier);
        }
    }
Beispiel #4
0
    void FixedUpdate()
    {
        // If the stopping condition has been met, stop moving
        if (stoppingCondition.Complete())
        {
            return;
        }

        // Cycle through the steering behaviors to apply on this GameObject
        for (int i = 0; i < steeringBehaviors.Length; i++)
        {
            // Stores the steering behavior to apply
            SteeringBehavior steeringBehavior = steeringBehaviors[i];

            // Switch the type of the steering behavior and add it to the GameObject
            switch (steeringBehavior.type)
            {
            case SteeringBehaviorType.Seek:
                steerable.AddSeekForce(steeringBehavior.targetTransform.position, steeringBehavior.strengthMultiplier);
                break;

            case SteeringBehaviorType.Arrival:
                steerable.AddArrivalForce(steeringBehavior.targetTransform.position, steeringBehavior.slowingRadius, steeringBehavior.strengthMultiplier);
                break;

            case SteeringBehaviorType.Pursue:
                steerable.AddPursueForce(steeringBehavior.targetSteerable, steeringBehavior.strengthMultiplier);
                break;

            case SteeringBehaviorType.Flee:
                steerable.AddFleeForce(steeringBehavior.targetTransform.position, steeringBehavior.strengthMultiplier);
                break;

            case SteeringBehaviorType.Evade:
                steerable.AddEvadeForce(steeringBehavior.targetSteerable, steeringBehavior.strengthMultiplier);
                break;

            case SteeringBehaviorType.Wander:
                steerable.AddWanderForce(steeringBehavior.circleDistance, steeringBehavior.circleRadius,
                                         steeringBehavior.angleChange, steeringBehavior.strengthMultiplier);
                break;

            case SteeringBehaviorType.ObstacleAvoidance:
                steerable.AddObstacleAvoidanceForce(steeringBehavior.obstacleAvoidanceForce, steeringBehavior.maxObstacleViewDistance,
                                                    steeringBehavior.obstacleLayer, steeringBehavior.strengthMultiplier);
                break;

            case SteeringBehaviorType.WallAvoidance:
                steerable.AddWallAvoidanceForce(steeringBehavior.obstacleAvoidanceForce, steeringBehavior.maxObstacleViewDistance,
                                                steeringBehavior.obstacleLayer, steeringBehavior.strengthMultiplier);
                break;

            case SteeringBehaviorType.Separation:
                steerable.AddSeparationForce(steeringBehavior.strengthMultiplier);
                break;

            case SteeringBehaviorType.Alignment:
                steerable.AddAlignmentForce(steeringBehavior.strengthMultiplier);
                break;

            case SteeringBehaviorType.Cohesion:
                steerable.AddCohesionForce(steeringBehavior.strengthMultiplier);
                break;
            }
        }

        // Apply the forces on the steerable that is performing this action
        steerable.ApplyForces(Time.fixedDeltaTime);
    }