public void GotoState(StateBehaviour state) { if (!_can_state_swap) return; OnExitState(); _state_behaviour = state; }
public void PopState() { if (!_can_state_swap) return; OnExitState(); _state_behaviour = _state_behaviour._last_state; }
public void PushState(StateBehaviour state) { if (!_can_state_swap) return; OnExitState(); state._last_state = _state_behaviour; _state_behaviour = state; }
private void GoToState(EAnimalState stateType) { if (_stateBehaviours.ContainsKey(stateType)) { _currentStateBehaviour?.OnStateExit(); _currentStateBehaviour = _stateBehaviours[stateType]; _currentStateBehaviour.OnStateEnter(); } }
private void Start() { if (!me) { me = this; } else { Destroy(this); } }
public override void setupState(StateBehaviour state) { // if(state is StateEat){ // StateEat stateEat = (StateEat)state; // stateEat.food = currentFood; // } else { // throw new System.Exception("Can't setup the state : " + state.ToString()); // } }
// Start is called before the first frame update public void Start() { agent = GetComponent <NavMeshAgent>(); animator = GetComponent <Animator>(); blackboard = GetComponent <Blackboard>(); state = GetComponent <StateBehaviour>(); global = GameObject.Find("GlobalBlackboard").GetComponent <GlobalBlackboard>(); player = global.GetGameObjectVar("Player"); vision = transform.GetComponentInChildren <Vision>(); //InvokeRepeating("Sniff",1f,1f); maxAwakeTime = awakeTime; }
public void ChangeState(T nextState) { // swap states and call their exit and enter methods StateBehaviour <T> current = states[currentState]; StateBehaviour <T> next = states[nextState]; currentState = nextState; if (initialized) { current.OnExit(); next.OnEnter(); } }
private void GoToStateInternal(int stateType) { if (StateBehaviours.ContainsKey(stateType)) { CurrentStateBehaviour?.OnStateExit(); CurrentStateBehaviour = StateBehaviours[stateType]; CurrentStateBehaviour.OnStateEnter(); } else { Debug.LogError($"State Id[{stateType}] doesn't Exist in the Dictionary."); } }
public override void setupState(StateBehaviour state) { if (state is StateEat) { StateEat stateEat = (StateEat)state; stateEat.food = currentFood; } else if (state is StateWalkTo) { } else { throw new System.Exception("Can't setup the state : " + state.ToString()); } }
public bool ChangeState(T nextState) { StateBehaviour <T> currentStateBehaviour = stateBehaviours[currentState]; StateBehaviour <T> nextStateBehaviour = stateBehaviours[nextState]; if (nextStateBehaviour.CanEnterState()) { currentState = nextState; if (initialized) { currentStateBehaviour.ExitState(); nextStateBehaviour.EnterState(); } return(true); } return(false); }
public static void DrawCustomEditor(StateBehaviour state, SerializedObject so) { SerializedProperty prop; #region Draw State's Properties prop = so.GetIterator(); prop.Next(true); prop.NextVisible(false); while (prop.NextVisible(false)) { EditorGUILayout.PropertyField(prop, true); } #endregion EditorUtility.SetDirty(state); }
public void Goals(int goals) { Console.WriteLine("Goals scored by the player: " + goals); if (goals < 5) { this.currentState = new FirstClass(); } if (goals >= 5 && goals < 10) { this.currentState = new SecondClass(); } if (goals >= 10) { this.currentState = new ThirdClass(); } currentState.ExecuteCommand(this); }
public override void setupState(StateBehaviour state) { if (state is StateHunt) { StateHunt stateEat = (StateHunt)state; stateEat.prey = this.currentPrey; stateEat.attackDistance = this.attackDistance; } else if (state is StateAttack) { StateAttack stateAttack = (StateAttack)state; stateAttack.damage = this.damage; } else { throw new System.Exception("Can't setup the state : " + state.ToString()); } }
public override void setupState(StateBehaviour state) { }
public void Init() { go = CreateGameObject("stateTest"); behaviour = go.AddComponent<StateBehaviour>(); engine = go.GetComponent<StateEngine>(); }
private void SwitchToMoving() { _currentState = StateBehaviour.Moving; }
public State FindStateContainsBehaviour( StateBehaviour behaviour ) { foreach( State state in _States ) { if( state.Contains( behaviour ) ) { return state; } } return null; }
public abstract void setupState(StateBehaviour state);
public void Init() { go = CreateGameObject("stateTest"); behaviour = go.AddComponent <StateBehaviour>(); engine = go.GetComponent <StateEngine>(); }
public static void DrawBehaviourBranches( StateBehaviour behaviour,SerializedProperty property ) { if( property.type == "StateLink" ) { if( property.isArray ) { for( int i=0;i<property.arraySize;i++ ) { SerializedProperty stateLinkProperty = property.GetArrayElementAtIndex( i ); DrawBranchStateLink( behaviour,stateLinkProperty ); } } else { DrawBranchStateLink( behaviour,property ); } } }
private void SwitchToAttack() { _currentState = StateBehaviour.Attack; StartCoroutine(AttackCoroutine()); }
public void SafePopState() { if (!_can_state_swap || _state_behaviour._last_state == null) return; OnExitState(); _state_behaviour = _state_behaviour._last_state; }
static void DrawBranchStateLink( StateBehaviour behaviour,SerializedProperty property ) { bool lineEnable = property.FindPropertyRelative( "lineEnable" ).boolValue; int stateID = property.FindPropertyRelative( "stateID" ).intValue; Vector2 lineStart = property.FindPropertyRelative( "lineStart" ).vector2Value; Vector2 lineStartTangent = property.FindPropertyRelative( "lineStartTangent" ).vector2Value; Vector2 lineEnd = property.FindPropertyRelative( "lineEnd" ).vector2Value; Vector2 lineEndTangent = property.FindPropertyRelative( "lineEndTangent" ).vector2Value; bool lineColorChanged = property.FindPropertyRelative( "lineColorChanged" ).boolValue; Color lineColor = Color.white; if( lineColorChanged ) { lineColor = property.FindPropertyRelative( "lineColor" ).colorValue; } if( lineEnable && stateID != 0 ) { Vector2 shadowPos = Vector2.one * 3; BezierArrow( lineStart+shadowPos,lineStartTangent+shadowPos,lineEnd+shadowPos,lineEndTangent+shadowPos,new Color( 0,0,0,1.0f),5.0f,16.0f ); BezierArrow( lineStart,lineStartTangent,lineEnd,lineEndTangent,lineColor,5.0f,16.0f ); } }
public void RegisterState(T state, StateBehaviour <T> stateBehaviour) { states.Add(state, stateBehaviour); }
private void OnEnable() { if (target == null) { return; } stateBehaviour = (StateBehaviour)target; m_type = this.serializedObject.FindProperty("type"); m_textTag = this.serializedObject.FindProperty("textTag"); m_textToSet = this.serializedObject.FindProperty("textToSet"); m_dialogueType = this.serializedObject.FindProperty("dialogueType"); m_dialogueId = this.serializedObject.FindProperty("dialogueID"); m_dialogueAnswerIntParam = this.serializedObject.FindProperty("answerIntParameter"); m_interactionType = this.serializedObject.FindProperty("interactionType"); m_interactiveObjectsTag = this.serializedObject.FindProperty("interactiveObjectsTag"); m_enableInteraction = this.serializedObject.FindProperty("enableInteraction"); m_enableInput = this.serializedObject.FindProperty("enableInput"); m_enableLook = this.serializedObject.FindProperty("enableLook"); m_enableMovement = this.serializedObject.FindProperty("enableMovement"); m_cutsceneType = this.serializedObject.FindProperty("cutsceneType"); m_animatedObjectTag = this.serializedObject.FindProperty("animatedObjectTag"); m_animatedObjectTrigger = this.serializedObject.FindProperty("animatedObjectTrigger"); m_animatedObjectBool = this.serializedObject.FindProperty("animatedObjectBool"); m_animatedObjectBoolValue = this.serializedObject.FindProperty("animatedObjectBoolValue"); m_timelineAsset = this.serializedObject.FindProperty("timelineAsset"); m_videoPlayerTag = this.serializedObject.FindProperty("videoPlayerTag"); m_videoUrl = this.serializedObject.FindProperty("videoUrl"); m_videoClip = this.serializedObject.FindProperty("videoClip"); m_playVideo = this.serializedObject.FindProperty("playVideo"); m_objectToActivateTag = this.serializedObject.FindProperty("objectToActivateTag"); m_activateObject = this.serializedObject.FindProperty("activateObject"); m_asyncLoad = this.serializedObject.FindProperty("asyncLoad"); m_sceneToLoad = this.serializedObject.FindProperty("sceneToLoad"); m_autoLoad = this.serializedObject.FindProperty("autoLoad"); m_increaseStage = this.serializedObject.FindProperty("increaseStage"); m_increaseCustomInt = this.serializedObject.FindProperty("increaseCustomInt"); m_customInt = this.serializedObject.FindProperty("customInt"); // TODO m_clearActions = this.serializedObject.FindProperty("clearActionsOnInterruption"); m_transitions = this.serializedObject.FindProperty("transitions"); m_timeToWait = this.serializedObject.FindProperty("timeToWait"); m_type_content = GetGUIContent <StateBehaviour>(m_type, true); m_textTag_content = GetGUIContent <StateBehaviour>(m_textTag, true); m_textToSet_content = GetGUIContent <StateBehaviour>(m_textToSet, true); m_dialogueType_content = GetGUIContent <StateBehaviour>(m_dialogueType, true); m_dialogueId_content = GetGUIContent <StateBehaviour>(m_dialogueId, true); m_dialogueAnswerIntParam_content = GetGUIContent <StateBehaviour>(m_dialogueAnswerIntParam, true); m_interactionType_content = GetGUIContent <StateBehaviour>(m_interactionType, true); m_interactiveObjectTag_content = GetGUIContent <StateBehaviour>(m_interactiveObjectsTag, true); m_enableInteraction_content = GetGUIContent <StateBehaviour>(m_enableInteraction, true); m_enableInput_content = GetGUIContent <StateBehaviour>(m_enableInput, true); m_enableLook_content = GetGUIContent <StateBehaviour>(m_enableLook, true); m_enableMovement_content = GetGUIContent <StateBehaviour>(m_enableMovement, true); m_cutsceneType_content = GetGUIContent <StateBehaviour>(m_cutsceneType, true); m_animatedObjectTag_content = GetGUIContent <StateBehaviour>(m_animatedObjectTag, true); m_animatedObjectTrigger_content = GetGUIContent <StateBehaviour>(m_animatedObjectTrigger, true); m_animatedObjectBool_content = GetGUIContent <StateBehaviour>(m_animatedObjectBool, true); m_animatedObjectBoolValue_content = GetGUIContent <StateBehaviour>(m_animatedObjectBoolValue, true); m_timelineAsset_content = GetGUIContent <StateBehaviour>(m_timelineAsset, true); m_videoPlayerTag_content = GetGUIContent <StateBehaviour>(m_videoPlayerTag, true); m_videoUrl_content = GetGUIContent <StateBehaviour>(m_videoUrl, true); m_videoClip_content = GetGUIContent <StateBehaviour>(m_videoClip, true); m_playVideo_content = GetGUIContent <StateBehaviour>(m_playVideo, true); m_objectToActivateTag_content = GetGUIContent <StateBehaviour>(m_objectToActivateTag, true); m_activateObject_content = GetGUIContent <StateBehaviour>(m_activateObject, true); m_asyncLoad_content = GetGUIContent <StateBehaviour>(m_asyncLoad, true); m_sceneToLoad_content = GetGUIContent <StateBehaviour>(m_sceneToLoad, true); m_autoLoad_content = GetGUIContent <StateBehaviour>(m_autoLoad, true); m_increaseStage_content = GetGUIContent <StateBehaviour>(m_increaseStage, true); m_increaseCustomInt_content = GetGUIContent <StateBehaviour>(m_increaseCustomInt, true); m_customInt_content = GetGUIContent <StateBehaviour>(m_customInt, true); // TODO m_clearActions_content = GetGUIContent<StateBehaviour>(m_clearActions, true); //m_transitions_content = GetGUIContent<StateBehaviour>(m_transitions, true); m_timeToWaitOnEnd_content = GetGUIContent <StateBehaviour>(m_timeToWait, true); }
static void CopyBehaviour( StateBehaviour source,StateBehaviour dest,bool checkStateLink ) { if( dest == null ) { return; } ArborFSMInternal stateMachine = dest.stateMachine; int stateID = dest.stateID; bool expanded = dest.expanded; EditorUtility.CopySerialized( source,dest ); dest.expanded = expanded; SerializedObject serializedObject = new SerializedObject( dest ); serializedObject.Update(); SerializedProperty stateMachineProperty = serializedObject.FindProperty( "_StateMachine" ); stateMachineProperty.objectReferenceValue = stateMachine; SerializedProperty stateIDProperty = serializedObject.FindProperty( "_StateID" ); stateIDProperty.intValue = stateID; if( checkStateLink ) { SerializedProperty iterator = serializedObject.GetIterator(); while( iterator.NextVisible(true) ) { if( iterator.type == "StateLink" ) { if( iterator.isArray ) { for( int i=0;i<iterator.arraySize;i++ ) { SerializedProperty stateLinkProperty = iterator.GetArrayElementAtIndex( i ); SerializedProperty property = stateLinkProperty.FindPropertyRelative( "stateID" ); if( property.intValue == stateID || stateMachine != source.stateMachine || stateMachine.GetStateFromID( property.intValue ) == null ) { property.intValue = 0; } } } else { SerializedProperty property = iterator.FindPropertyRelative( "stateID" ); if( property.intValue == stateID || stateMachine != source.stateMachine || stateMachine.GetStateFromID( property.intValue ) == null ) { property.intValue = 0; } } } } } serializedObject.ApplyModifiedProperties(); serializedObject.Dispose(); }
public static void RestoreBehaviour( State state,StateBehaviour sourceBehaviour ) { ComponentUtility.EditorAddComponent cachedAddComponent = ComponentUtility.editorAddComponent; ComponentUtility.editorAddComponent = null; StateBehaviour destBehaviour = state.AddBehaviour( sourceBehaviour.GetType () ); if( destBehaviour != null ) { CopyBehaviour( sourceBehaviour,destBehaviour,false ); } ComponentUtility.editorAddComponent = cachedAddComponent; }
public static bool BehaviourTitlebar( Rect position,bool foldout,StateBehaviour behaviour ) { int controlId = GUIUtility.GetControlID(s_BehaviourTitlebarHash,EditorGUIUtility.native, position); Event current = Event.current; //foldout = EditorGUI.Foldout( position,foldout,GUIContent.none,s_BehaviourTitlebar ); Rect iconPosition = new Rect(position.x + (float) s_BehaviourTitlebar.padding.left , position.y + (float)s_BehaviourTitlebar.padding.top, 16f, 16f ); Rect checkPosition = new Rect( iconPosition.xMax,iconPosition.y,16f,16f ); Rect popupPosition = new Rect( position.xMax - (float)s_BehaviourTitlebar.padding.right - 2.0f - 16.0f, iconPosition.y, 16f, 16f); Rect helpPosition = new Rect( popupPosition.x - 18.0f, iconPosition.y, 16f, 16f); Rect textPosition = new Rect( checkPosition.xMax + 4.0f, iconPosition.y, helpPosition.x - iconPosition.xMax - 4.0f - 4.0f, iconPosition.height); string titleName = behaviour.GetType().Name; object[] objs = behaviour.GetType ().GetCustomAttributes( typeof(BehaviourTitle),false ); if( objs!=null && objs.Length > 0 ) { BehaviourTitle attr = (BehaviourTitle)objs[0]; titleName = attr.titleName; } System.Type classType = behaviour.GetType(); string helpUrl = "http://caitsithware.com/wordpress/assetstore/arbor"; string helpTooltip = "Open Arbor Document"; object[] attributes = classType.GetCustomAttributes( typeof(BehaviourHelp),false ); if( attributes != null && attributes.Length > 0 ) { BehaviourHelp help = attributes[0] as BehaviourHelp; helpUrl = help.url; helpTooltip = string.Format("Open Reference for {0}.", classType.Name ); } EditorGUI.BeginChangeCheck(); bool behaviourEnabled = EditorGUI.Toggle( checkPosition,behaviour.behaviourEnabled ); if( EditorGUI.EndChangeCheck() ) { Undo.RecordObject( behaviour,( !behaviourEnabled? "Disable" : "Enable" ) + " Behaviour" ); behaviour.behaviourEnabled = behaviourEnabled; EditorUtility.SetDirty( behaviour ); } HelpButton( helpPosition,helpUrl,helpTooltip ); EventType typeForControl = current.GetTypeForControl(controlId); switch (typeForControl) { case EventType.MouseDown: if( popupPosition.Contains( current.mousePosition ) ) { GenericMenu menu = new GenericMenu(); State state = behaviour.state; int index = -1; StateBehaviour[] behaviours = state.behaviours; for( int i=0;i<behaviours.Length;i++ ) { if( behaviours[i] == behaviour ) { index = i; break; } } if( index >= 1 ) { menu.AddItem( GetTextContent("Move Up"),false,MoveUpBehaviourContextMenu,new KeyValuePair<State,int>(state,index) ); } else { menu.AddDisabledItem( GetTextContent("Move Up") ); } if( index < behaviours.Length -1 ) { menu.AddItem( GetTextContent("Move Down"),false,MoveDownBehaviourContextMenu,new KeyValuePair<State,int>(state,index) ); } else { menu.AddDisabledItem( GetTextContent("Move Down") ); } menu.AddItem ( GetTextContent("Copy"),false,CopyBehaviourContextMenu,behaviour ); if( _CopyBehaviour != null && _CopyBehaviour.GetType () == behaviour.GetType () ) { menu.AddItem ( GetTextContent("Paste"),false,PasteBehaviourContextMenu,behaviour ); } else { menu.AddDisabledItem( GetTextContent("Paste") ); } menu.AddItem( GetTextContent("Delete"),false,DeleteBehaviourContextMenu,behaviour ); menu.AddSeparator( "" ); MonoScript script = MonoScript.FromMonoBehaviour( behaviour ); if( script != null ) { menu.AddItem( GetTextContent("Edit Script"),false,EditScriptBehaviourContextMenu,script ); } else { menu.AddDisabledItem( GetTextContent("Edit Script") ); } menu.DropDown( popupPosition ); current.Use(); } else if( checkPosition.Contains ( current.mousePosition ) ) { if (current.button == 0 && (Application.platform != RuntimePlatform.OSXEditor || !current.control) ) { } } else if( position.Contains( current.mousePosition ) ) { if (current.button == 0 && (Application.platform != RuntimePlatform.OSXEditor || !current.control) ) { GUIUtility.hotControl = GUIUtility.keyboardControl = controlId; current.Use(); } } break; case EventType.MouseUp: if( GUIUtility.hotControl == controlId ) { GUIUtility.hotControl = 0; if( position.Contains(current.mousePosition) ) { foldout = !foldout; } current.Use(); } break; case EventType.KeyDown: if (GUIUtility.keyboardControl == controlId) { if( current.keyCode == KeyCode.LeftArrow ) { foldout = false; current.Use(); } if( current.keyCode == KeyCode.RightArrow ) { foldout = true; current.Use(); break; } } break; case EventType.Repaint: s_BehaviourTitlebar.Draw ( position,GUIContent.none,controlId,foldout ); GUIStyle.none.Draw( iconPosition,new GUIContent( AssetPreview.GetMiniThumbnail(behaviour) ),controlId,foldout ); s_BehaviourTitlebarText.Draw( textPosition, new GUIContent( ObjectNames.NicifyVariableName( titleName ) ),controlId,foldout ); s_BehaviourTitlebarText.Draw( popupPosition, s_ContextPopupContent,controlId,foldout ); break; } return foldout; }
public Player() { this.currentState = new FirstClass(); }
private void SwitchToIdle() { _currentState = StateBehaviour.Idle; }
public Editor GetBehaviourEditor( StateBehaviour behaviour ) { Editor editor = null; if( !_BehaviourEditors.TryGetValue( behaviour,out editor ) ) { editor = Editor.CreateEditor( behaviour ); _BehaviourEditors.Add( behaviour,editor ); } return editor; }
public void Initialize <T>(StateBehaviour entity) { // Define States var values = Enum.GetValues(typeof(T)); stateLookup = new Dictionary <Enum, StateMapping>(); for (int i = 0; i != values.Length; ++i) { var mapping = new StateMapping((Enum)values.GetValue(i)); stateLookup.Add(mapping.state, mapping); } // Reflect methods var methods = entity.GetType().GetMethods(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic); // Bind methods to states var separator = "_".ToCharArray(); for (int i = 0; i != methods.Length; ++i) { var names = methods[i].Name.Split(separator); // Ignore functions without an underscore if (names.Length <= 1) { continue; } Enum key; try { key = (Enum)Enum.Parse(typeof(T), names[0]); } catch (ArgumentException) { // Some things (evetns, properties) generate automatic method. Ignore these for (int j = 0; j != ignoredNames.Length; ++j) { if (names[0] == ignoredNames[j]) { goto SkipWarning; } } Debug.LogWarning("Method with name " + methods[i].Name + " could not resolve a matching state. Check method spelling"); continue; SkipWarning: continue; } var targetState = stateLookup[key]; switch (names[1]) { case "Enter": if (methods[i].ReturnType == typeof(IEnumerator)) { targetState.Enter = CreateDelegate <Func <IEnumerator> >(methods[i], entity); } else { var action = CreateDelegate <Action>(methods[i], entity); targetState.Enter = () => { action(); return(null); }; } break; case "Exit": if (methods[i].ReturnType == typeof(IEnumerator)) { targetState.Exit = CreateDelegate <Func <IEnumerator> >(methods[i], entity); } else { var action = CreateDelegate <Action>(methods[i], entity); targetState.Exit = () => { action(); return(null); }; } break; case "Finally": targetState.Finally = CreateDelegate <Action>(methods[i], entity); break; case "Update": targetState.Update = CreateDelegate <Action>(methods[i], entity); break; case "LateUpdate": targetState.LateUpdate = CreateDelegate <Action>(methods[i], entity); break; case "FixedUpdate": targetState.FixedUpdate = CreateDelegate <Action>(methods[i], entity); break; case "TriggerEnter": targetState.TriggerEnter = CreateDelegate <Action <Collider> >(methods[i], entity); break; case "TriggerStay": targetState.TriggerStay = CreateDelegate <Action <Collider> >(methods[i], entity); break; case "TriggerExit": targetState.TriggerExit = CreateDelegate <Action <Collider> >(methods[i], entity); break; case "CollisionEnter": targetState.CollisionEnter = CreateDelegate <Action <Collision> >(methods[i], entity); break; case "CollisionStay": targetState.CollisionStay = CreateDelegate <Action <Collision> >(methods[i], entity); break; case "CollisionExit": targetState.CollisionExit = CreateDelegate <Action <Collision> >(methods[i], entity); break; case "TriggerEnter2D": targetState.TriggerEnter2D = CreateDelegate <Action <Collider2D> >(methods[i], entity); break; case "TriggerStay2D": targetState.TriggerStay2D = CreateDelegate <Action <Collider2D> >(methods[i], entity); break; case "TriggerExit2D": targetState.TriggerExit2D = CreateDelegate <Action <Collider2D> >(methods[i], entity); break; case "CollisionEnter2D": targetState.CollisionEnter2D = CreateDelegate <Action <Collision2D> >(methods[i], entity); break; case "CollisionStay2D": targetState.CollisionStay2D = CreateDelegate <Action <Collision2D> >(methods[i], entity); break; case "CollisionExit2D": targetState.CollisionExit2D = CreateDelegate <Action <Collision2D> >(methods[i], entity); break; } } }
public static bool BehaviourTitlebar( bool foldout,StateBehaviour behaviour ) { Rect position = GUILayoutUtility.GetRect(GUIContent.none, s_BehaviourTitlebar ); return BehaviourTitlebar( position,foldout,behaviour ); }
static void CopyBehaviourContextMenu( object obj ) { StateBehaviour behaviour = obj as StateBehaviour; if( _CopyBehaviour != null ) { Object.DestroyImmediate( _CopyBehaviour ); _CopyBehaviour = null; } _CopyBehaviour = clipboard.AddComponent( behaviour.GetType () ) as StateBehaviour; EditorUtility.CopySerialized( behaviour,_CopyBehaviour ); }
public virtual void stateDone(StateBehaviour state) { //avalia os atributos do estado e toma a decisão para que estado ir }