コード例 #1
0
 private void PlayerSightSensor()
 {
     if (playerAround)
     {
         Vector3 direction = (player.transform.position - transform.position).normalized;
         if (Physics.Raycast(transform.position, direction, out hitInfo, Mathf.Infinity, rayLayers))
         {
             if (hitInfo.collider.CompareTag("Player"))
             {
                 worldState.AddStates(GOAP_States.PlayerInSite);
             }
             else
             {
                 worldState.RemoveState(GOAP_States.PlayerInSite);
             }
         }
     }
 }
コード例 #2
0
    private void Plan()
    {
        for (int i = 0; i < goals.Count; i++)
        {
            if (goals[i].isValid(this))
            {
                goal = goals[i];
                break;
            }
        }

        if (worldState.CompareState(goal.goalStates) == 0)
        {
            return;
        }

        currentPlan.Clear();
        Stack <SimulationStep> sim = new Stack <SimulationStep>();

        GOAP_Action[] simPlan = new GOAP_Action[maxPlanDepth];

        int minDepth = int.MaxValue;

        sim.Push(new SimulationStep(new GOAP_StatesList(worldState), null, 0));

        List <GOAP_States> targetStates = new List <GOAP_States>(goal.goalStates.states);

        while (sim.Count != 0)
        {
            currentSimData = sim.Pop();
            simPlan[currentSimData.depth] = currentSimData.action;

            if (currentSimData.depth > minDepth)
            {
                continue;
            }

            if (currentSimData.worldState.CompareState(goal.goalStates) == 0 || currentSimData.depth >= maxPlanDepth)
            {
                if (currentSimData.depth < minDepth)
                {
                    currentPlan.Clear();
                    for (int i = 0; i <= currentSimData.depth; i++)
                    {
                        if (simPlan[i] != null)
                        {
                            if (!currentPlan.Contains(simPlan[i]))
                            {
                                currentPlan.Enqueue(simPlan[i]);
                            }
                        }
                    }
                    minDepth = currentSimData.depth;
                }
            }
            else
            {
                for (int i = 0; i < actions.Count; i++)
                {
                    if (actions[i].isValid(this))
                    {
                        GOAP_StatesList newSimState = new GOAP_StatesList(currentSimData.worldState);
                        newSimState.AddStates(actions[i].resultStates);
                        sim.Push(new SimulationStep(newSimState, actions[i], (currentSimData.depth + 1)));
                    }
                }
            }
        }
    }