Esempio n. 1
0
	public void HandleChangeState(GameMsg msg)
    {
        // swap states
        ChangeStateMsg statemsg = msg as ChangeStateMsg;
        if (statemsg != null)
        {
#if BRAIN_DEBUG
            Debug.Log("Got ChangeStateMsg = " + statemsg.state);
#endif
            // convert string to classname
            Type type = brainStates.GetType();
            if (type == null)
                return;

            PropertyInfo state = type.GetProperty(statemsg.state);
            if (state == null)
            {
                Debug.Log("Brain : HandleChangeState(" + statemsg.state + ") Can't find state!");
                return;
            }

            BrainState next = state.GetValue(brainStates, null) as BrainState;
            if (next != null)
            {
                // save name
                next.Name = statemsg.state;
                next.Args = statemsg.args;

                // cleanup old state
                if (current != null)
                    current.Cleanup();

                // set
                current = next;

                // init new state
                if (current != null)
                    current.Init();
            }
            else
            {
                Debug.LogWarning("ChangeStateMsg: unknown state - " + statemsg.state);
            }
        }
    }