Пример #1
0
    void Start()
    {
        // Timers
        lifeTime        = new Stopwatch();
        takeHealthTimer = new Stopwatch();
        lifeTime.Start();
        takeHealthTimer.Start();

        // Setting up the startup state
        state = new AgentState
        {
            hydrationLevel = 35,
            foodSupply     = 50,
            happy          = false,
            isAtDisco      = false
        };

        // Creating the default strategy behaviours
        walkAround      = new WalkAroundBehaviour(GetComponent <NavMeshAgent>());
        eatFood         = new EatNearbyFoodBehaviour(GetComponent <NavMeshAgent>(), transform, state);
        findWater       = new GetWaterBehaviour(GetComponent <NavMeshAgent>(), transform, state);
        awayFromMonster = new RunAwayFromMonster(GetComponent <NavMeshAgent>(), transform);
        List <AgentBehaviour> behaviours = new List <AgentBehaviour>
        {
            walkAround,
            eatFood,
            findWater,
            awayFromMonster
        };

        homeAndRest = new GetHomeAndRestBehaviour(GetComponent <NavMeshAgent>(), restingPlace, state); // Added later to the default strategy

        defaultStrategy = new BehaviourStrategy(behaviours, (state) => { return(true); });

        ControlCenter = new ControlCenter(gameObject, defaultStrategy);
        ControlCenter.SetStateObject(state);

        // Making additional strategies
        IStrategyManager strategyManager = ControlCenter.StrategyManager;

        strategyManager.EnableMultipleStrategies = true;

        AgentBehaviour goToDisco      = new GoToDiscoBehaviour(GetComponent <NavMeshAgent>(), disco);
        AgentBehaviour dance          = new DanceBehaviour(ControlCenter.CommunicationManager, GetComponent <NavMeshAgent>(), gameObject, state);
        AgentBehaviour goHomeAndSleep = new GetHomeAndSleepBehaviour(GetComponent <NavMeshAgent>(), restingPlace, state);

        List <AgentBehaviour> nightTimeStrategy = new List <AgentBehaviour>
        {
            goToDisco,
            dance,
            goHomeAndSleep
        };
        BehaviourStrategy strategy = new BehaviourStrategy(nightTimeStrategy, (state) =>
        {
            if (GameManager.isNight)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        });

        strategyManager.AddAdditionalStrategy(strategy);

        // Setting up the communication manager
        ICommunicationManager communicationManager = ControlCenter.CommunicationManager;

        communicationManager.EnableBroadcasting = true;
        BroadcastSettings settings = new BroadcastSettings
        {
            AgentTag        = "Agent",
            BroadcastRadius = 20
        };

        settings.SetSuppressibleBehaviours(new List <AgentBehaviour> {
            dance, goToDisco
        });
        communicationManager.SetBroadcastSettings(settings);

        // Add arbitration rules
        ControlCenter.ArbitrationManager.EnableArbitrationRules = true;
        ControlCenter.ArbitrationManager.AddArbitrationRule((inputBehaviors) =>
        {
            if (inputBehaviors.Contains(findWater) && inputBehaviors.Contains(awayFromMonster))
            {
                List <AgentBehaviour> toExecute = new List <AgentBehaviour>
                {
                    findWater
                };
                return(new ArbitrationRule(toExecute, true));
            }
            return(default);