public void RemoveTransition(StatePhase phase, ITransition transition) { if (_transitions.TryGetValue(phase, out var t)) { t.Remove(transition); } }
public virtual void ForceEndState() { if (!(CurrentPhase == StatePhase.EXITING || CurrentPhase == StatePhase.INACTIVE)) { _currentPhase = StatePhase.EXITING; } }
public void ChangeActionState(int state) { currentState = (StatePhase)state; if (onPhaseChanged != null) { onPhaseChanged.Invoke(state); } }
private void Execute(T state, StatePhase phase) { if (_methods.ContainsKey(state)) { StateMethods methods = _methods[state]; methods.Invoke(phase); } }
public void Initialize(IoC context) { Context = context; Phase = StatePhase.Initialized; OnInitialized(); }
private MethodInfo GetMethod(MethodInfo[] methods, T state, StatePhase phase) { string methodName = "On" + state.ToString() + phase.ToString(); var method = (from m in methods where m.Name.Equals(methodName) select m).SingleOrDefault(); return(method); }
///////////////////////////////////////////////////////////////// // Sets movement state ///////////////////////////////////////////////////////////////// public void SetMovementState(MovementState state) { // Set movement state as state currentMoveState = state; // Set current state to start currentStatePhase = StatePhase.START; // Set gravity if (currentMoveState == MovementState.CLIMBING) { GetComponent <Rigidbody>().useGravity = false; } else { GetComponent <Rigidbody>().useGravity = true; } // Stop player from standing if can't stand if (currentMoveState == MovementState.STANDING && !CanStand()) { currentMoveState = MovementState.CROUCHING; } if (currentMoveState == MovementState.VAULTING || currentMoveState == MovementState.SLIDING || currentMoveState == MovementState.MANTLING || (currentMoveState == MovementState.CLIMBING && currentStatePhase == StatePhase.START)) { // Setup for actions actionStarted = true; actionOrigin = transform.position; collider.enabled = false; } else { collider.enabled = true; } // Set collider height if (currentMoveState == MovementState.CROUCHING || currentMoveState == MovementState.SNEAKING) { collider.height = crouchHeight; collider.transform.position = transform.position - (Vector3.up * crouchHeight * 0.51f); } else { collider.height = standHeight; collider.transform.position = transform.position; } // Update animation UpdateAnimation(); }
internal void Invoke(StatePhase phase) { if (_methods.ContainsKey(phase)) { MethodInfo method = _methods[phase]; if (null != method) { method.Invoke(_stateMachine, null); } } }
public void AddTransition(StatePhase phase, ITransition transition) { if (_transitions.TryGetValue(phase, out var t)) { t.Add(transition); } else { var set = new HashSet <ITransition>(); _transitions.Add(phase, set); set.Add(transition); } }
private void ProcessTransitions(StatePhase phase) { if (!_transitions.TryGetValue(phase, out var t)) { return; } foreach (var x in t) { if (x.Evaluate()) { x.Execute(); } } }
/// <summary> /// Switch the state of the StateMachine /// </summary> /// <param name="state">The typeof() of the state being switched to</param> /// <returns>Success of the state change. Will return false if StateMachine is in the middle of a transition</returns> public bool ChangeState(Type state) { if (_stateLookup.ContainsKey(state) == false) throw new ArgumentException("Cannot change to state. Make sure it was added to StateMachine.Initialize", state.Name); if (IsTransitioning) return false; // When coming to change state from the started phase, go immediately to the enter phase _statePhase = (_statePhase == StatePhase.Started) ? StatePhase.Enter : StatePhase.Exit; _nextState = _stateLookup[state]; if (_statePhase == StatePhase.Exit) { exitIterator = _currentState.Exit(); } enterIterator = _nextState.Enter(); return true; }
///////////////////////////////////////////////////////////////// // Updates gameplay and physics ///////////////////////////////////////////////////////////////// private void FixedUpdate() { // reset action started if (actionStarted) { actionStarted = false; } switch (currentMoveState) { case MovementState.VAULTING: switch (currentStatePhase) { case StatePhase.START: if (currentActionTimer < vaultSetupDuration) { transform.position = Vector3.Lerp(actionOrigin, startActionPoint, currentActionTimer / vaultSetupDuration); currentActionTimer += Time.deltaTime; } else { currentStatePhase = StatePhase.MIDDLE; } break; case StatePhase.MIDDLE: if (currentActionTimer < vaultMidDuration) { transform.position = Vector3.Lerp(startActionPoint, middleActionPoint, (currentActionTimer - vaultSetupDuration) / (vaultMidDuration - vaultSetupDuration)); currentActionTimer += Time.deltaTime; } else { currentStatePhase = StatePhase.END; } break; default: if (currentActionTimer < vaultDuration) { transform.position = Vector3.Lerp(middleActionPoint, endActionPoint, (currentActionTimer - vaultMidDuration) / (vaultDuration - vaultMidDuration)); currentActionTimer += Time.deltaTime; } else { SetMovementState(MovementState.STANDING); currentActionTimer = 0.0f; } break; } break; case MovementState.SLIDING: switch (currentStatePhase) { case StatePhase.START: if (currentActionTimer < slideSetupDuration) { transform.position = Vector3.Lerp(actionOrigin, startActionPoint, currentActionTimer / slideSetupDuration); currentActionTimer += Time.deltaTime; } else { currentStatePhase = StatePhase.END; } break; default: if (currentActionTimer < slideDuration) { transform.position = Vector3.Lerp(startActionPoint, endActionPoint, (currentActionTimer - slideSetupDuration) / (slideDuration - slideSetupDuration)); currentActionTimer += Time.deltaTime; } else { SetMovementState(MovementState.STANDING); currentActionTimer = 0.0f; } break; } break; case MovementState.MANTLING: switch (currentStatePhase) { case StatePhase.START: if (currentActionTimer < mantleSetupDuration) { transform.position = Vector3.Lerp(actionOrigin, startActionPoint, currentActionTimer / mantleSetupDuration); currentActionTimer += Time.deltaTime; } else { currentStatePhase = StatePhase.MIDDLE; } break; case StatePhase.MIDDLE: if (currentActionTimer < mantleMidDuration) { transform.position = Vector3.Lerp(startActionPoint, middleActionPoint, (currentActionTimer - mantleSetupDuration) / (mantleMidDuration - mantleSetupDuration)); currentActionTimer += Time.deltaTime; } else { currentStatePhase = StatePhase.END; } break; default: if (currentActionTimer < mantleDuration) { transform.position = Vector3.Lerp(middleActionPoint, endActionPoint, (currentActionTimer - mantleMidDuration) / (mantleDuration - mantleMidDuration)); currentActionTimer += Time.deltaTime; } else { SetMovementState(MovementState.STANDING); currentActionTimer = 0.0f; } break; } break; case MovementState.WALKING: // Set current speed currentSpeed = walkSpeed; break; case MovementState.RUNNING: // Set current speed currentSpeed = runSpeed; break; case MovementState.SNEAKING: // Set current speed currentSpeed = sneakSpeed; break; case MovementState.CLIMBING: // Set current speed currentSpeed = sneakSpeed; switch (currentStatePhase) { case StatePhase.START: if (currentActionTimer < climbDuration) { transform.position = Vector3.Lerp(actionOrigin, startActionPoint, currentActionTimer / climbDuration); currentActionTimer += Time.deltaTime; } else { currentStatePhase = StatePhase.END; currentActionTimer = 0.0f; } break; default: break; } break; case MovementState.JUMPING: case MovementState.FALLING: // Select action while jumping if (canClimb) { SetMovementState(MovementState.CLIMBING); } else if (canMantle) { SetMovementState(MovementState.MANTLING); } else if (canVault) { SetMovementState(MovementState.VAULTING); } break; default: break; } }
public void Initialize(params Type[] states) { if (states.Length < 1) throw new ArgumentNullException("states", "Initialize must be called with states"); _statePhase = StatePhase.Started; _stateLookup = new Dictionary<Type, IFsmState>(); for (int i = 0; i < states.Length; i++) { Type stateType = states[i]; object instance = Activator.CreateInstance(stateType); IFsmState stateInstance = instance as IFsmState; if (stateInstance != null) { SetupState(stateInstance); _stateLookup.Add(states[i], stateInstance); } else { throw new ArgumentException("State class does not implement IFsmState", stateType.Name); } } StartCoroutine(StateChangeRoutine()); }
private IEnumerator StateChangeRoutine() { // Instead of starting a coroutine with every state change // and causing allocations, start a single coroutine at the start // and use that to manage the state changes. See link for details // http://www.gamasutra.com/blogs/WendelinReich/20131109/203841/C_Memory_Management_for_Unity_Developers_part_1_of_3.php while (isActiveAndEnabled) { if (IsTransitioning) { bool isExit = _statePhase == StatePhase.Exit; // Choose which iterator to use var iter = (isExit) ? exitIterator : enterIterator; // Loop through the iterator while (iter.MoveNext()) { yield return iter.Current; } // Exit coroutine complete, move to enter phase if (isExit) { _statePhase = StatePhase.Enter; // Iterate through the enter IEnumerator // So it's possible to change states in one frame iter = enterIterator; while (iter.MoveNext()) { yield return iter.Current; } StateEnterComplete(); } // Enter coroutine complete, move to update phase // and clean up else { StateEnterComplete(); } } yield return null; } }
private void StateEnterComplete() { _statePhase = StatePhase.Update; _currentState = _nextState; _nextState = null; exitIterator = null; enterIterator = null; InternalStateChange(); }
internal void SetMethod(StatePhase phase, MethodInfo method) { _methods[phase] = method; }
public virtual void ForceEndState() { _currentPhase = StatePhase.EXITING; }
public void Stop() { Phase = StatePhase.Stopped; OnStopped(); }
public void Start() { Phase = StatePhase.Started; OnStarted(); }
private void Start() { currentState = StatePhase.Day1; }