Ejemplo n.º 1
0
    // Update is called once per frame
    private void Update()
    {
        BrainTransition triggeredTransition = null;

        foreach (KeyValuePair <string, BrainTransition> transition in sm_currentState.transitions)
        {
            if (transition.Value.isTriggered)
            {
                triggeredTransition          = transition.Value;
                transition.Value.isTriggered = false;
                break;
            }
        }

        if (triggeredTransition != null)
        {
            BrainNode targetState = triggeredTransition.targetState;

            triggeredTransition.ExecuteTransition();

            sm_currentState = targetState;
        }
        else
        {
            sm_currentState.Execute();
            if (m_nextState != null)
            {
                sm_currentState = m_nextState;
                m_nextState     = null;
            }
        }
    }
Ejemplo n.º 2
0
    private void LoadBeaviorTree(BrainState t_state)
    {
        // Read JSON...
        string jsonPath   = Application.persistentDataPath + "/" + t_state.nodeName + ".json";
        string jsonString = File.ReadAllText(jsonPath);

        JSONObject SMJson = (JSONObject)JSON.Parse(jsonString);

        BehaviorTree bt = new BehaviorTree
        {
            btName = t_state.nodeName
        };

        bt.bt_initialNode = new BrainNode();
        SelectNodeType(out bt.bt_initialNode, SMJson["entryNode"]["nodeName"], SMJson["entryNode"]["nodeType"]);

        if (!bt_nodes.ContainsKey(bt.bt_initialNode.nodeName))
        {
            bt_nodes.Add(bt.bt_initialNode.nodeName, bt.bt_initialNode);
        }

        // Rest of the states
        foreach (JSONObject btNode in SMJson["nodes"])
        {
            BrainNode newNode = new BrainNode();
            SelectNodeType(out newNode, btNode["nodeName"], btNode["nodeType"]);

            if (!bt_nodes.ContainsKey(newNode.nodeName))
            {
                bt_nodes.Add(newNode.nodeName, newNode);
            }
        }

        // Entry point transitions
        foreach (JSONArray jsonTransition in SMJson["entryNode"]["outputs"])
        {
            BrainTransition transition = new BrainTransition
            {
                targetState = bt_nodes[jsonTransition[1]]
            };

            bt.bt_initialNode.transitions.Add(jsonTransition[1], transition);
        }

        // Rest of the transitions
        foreach (JSONObject state in SMJson["nodes"])
        {
            foreach (JSONArray jsonTransition in state["outputs"])
            {
                BrainTransition transition = new BrainTransition
                {
                    targetState = bt_nodes[jsonTransition[1]]
                };

                bt_nodes[jsonTransition[0]].transitions.Add(jsonTransition[1], transition);
            }
        }

        t_state.behavior = bt;
    }
Ejemplo n.º 3
0
    private void LoadBrain()
    {
        // Read JSON...
        string jsonPath   = Application.persistentDataPath + "/StateMachine.json";
        string jsonString = File.ReadAllText(jsonPath);

        JSONObject SMJson = (JSONObject)JSON.Parse(jsonString);

        // Entry point
        BrainState sm_entryState = new BrainState
        {
            nodeName = SMJson["entryNode"]["nodeName"]
        };

        sm_states.Add(sm_entryState.nodeName, sm_entryState);

        sm_initialState = sm_entryState;

        LoadBeaviorTree(sm_entryState);

        // Rest of the states

        foreach (JSONObject state in SMJson["nodes"])
        {
            BrainState newState = new BrainState
            {
                nodeName = state["nodeName"]
            };

            sm_states.Add(newState.nodeName, newState);

            LoadBeaviorTree(newState);
        }

        // Entry point transitions
        foreach (JSONArray jsonTransition in SMJson["entryNode"]["outputs"])
        {
            BrainTransition transition = new BrainTransition
            {
                targetState = sm_states[jsonTransition[1]]
            };

            sm_entryState.transitions.Add(jsonTransition[1], transition);
        }

        // Rest of the transitions
        foreach (JSONObject state in SMJson["nodes"])
        {
            foreach (JSONArray jsonTransition in state["outputs"])
            {
                BrainTransition transition = new BrainTransition
                {
                    targetState = sm_states[jsonTransition[1]]
                };

                sm_states[jsonTransition[0]].transitions.Add(jsonTransition[1], transition);
            }
        }
    }