public InjuryState(ActionStateId id) : base(id) { #region Injury To CommonNull AddTransition( (command, addOutput) => FsmTransition.SimpleCommandHandler(command, FsmInput.InjuryFinished), null, (int)ActionStateId.CommonNull, null, 0, new[] { FsmInput.InjuryFinished }); #endregion #region Interrupt AddTransition( (command, addOutput) => { if (command.IsMatch(FsmInput.InterruptAction)) { FsmOutput.Cache.SetValue(AnimatorParametersHash.Instance.InterruptHash, AnimatorParametersHash.Instance.InterruptName, AnimatorParametersHash.Instance.InterruptEnable, CharacterView.FirstPerson | CharacterView.ThirdPerson, true); addOutput(FsmOutput.Cache); command.Handled = true; return(FsmStateResponseType.Reenter); } return(FsmStateResponseType.Pass); }, null, (int)ActionStateId.CommonNull, null, 0, new[] { FsmInput.InterruptAction }); #endregion }
public UnarmState(ActionStateId id) : base(id) { #region Unarm To CommonNull AddTransition( (command, addOutput) => FsmTransition.SimpleCommandHandler(command, FsmInput.HolsterEndFinished), null, (int)ActionStateId.CommonNull, null, 0, new[] { FsmInput.HolsterEndFinished }); #endregion #region Interrupt AddTransition( (command, addOutput) => { if (command.IsMatch(FsmInput.InterruptSwitchWeapon)) { FsmOutput.Cache.SetValue(FsmOutputType.InterruptAction, (int)FsmInput.HolsterStartFinished); addOutput(FsmOutput.Cache); FsmOutput.Cache.SetValue(FsmOutputType.InterruptAction, (int)FsmInput.HolsterEndFinished); addOutput(FsmOutput.Cache); command.Handled = true; return(FsmStateResponseType.Reenter); } return(FsmStateResponseType.Pass); }, null, (int)ActionStateId.CommonNull, null, 0, new[] { FsmInput.InterruptSwitchWeapon }); #endregion }
public DiveTransition(short id, short target, int duration, float fromValue, float toValue, FsmInput enterInput) : base(id, target, duration) { _fromValue = fromValue; _toValue = toValue; _simpleTransferCondition = (command, addOutput) => FsmTransition.SimpleCommandHandler(command, enterInput); }
public static void Apply <TEventEnum, TStateEnum>(FSMWrapper <TEventEnum> fsmw) where TStateEnum : IConvertible where TEventEnum : IConvertible { if (!(fsmw is IFSMEventRouteSpecifier <TEventEnum, TStateEnum>)) { return; } foreach (TStateEnum s in Enum.GetValues(typeof(TStateEnum))) { var sn = Enum.GetName(typeof(TStateEnum), s); FsmState state = fsmw.targetFsm.GetState(sn); var transitions = (fsmw as IFSMEventRouteSpecifier <TEventEnum, TStateEnum>).EventsFrom(s); var transitionNames = transitions.Select( (x) => Enum.GetName(typeof(TEventEnum), x)); var missingTransitions = transitionNames.Where( x => state.Transitions.All(y => y.EventName != x)); state.Transitions = state.Transitions.Concat( missingTransitions.Select(x => { var t = new FsmTransition(); t.FsmEvent = fsmw.targetFsm.GetEvent(x); return(t); })).ToArray(); foreach (var t in state.Transitions) { if (transitionNames.Contains(t.EventName)) { t.ColorIndex = fsmw.eventColour; t.LinkStyle = FsmTransition.CustomLinkStyle.Circuit; } } } }
/// <summary> /// Adds a transition between the two states. This transition will not have /// and sort of action, simply just changes the states. /// </summary> /// <param name="prevState">The previous state the state machine was in</param> /// <param name="destinationState">The destination state the state machine will end in</param> /// <param name="stateAction">The key of the transition, this will be an action of some sort. ex. Jump</param> /// <param name="transition">The transition class implimented for this transition</param> /// public void AddTransition(TStateEnum prevState, TStateEnum destinationState, TStateAction stateAction, FsmTransition <TStateEnum> transition) { //Get the transitions for the prevState var cStateTrans = _transitions[prevState]; //If the Transition Dictionary does not have the current state's transitions... if (cStateTrans == null) { // ...Then log an error to inform a developer... Debug.LogError("Current State for transition not found"); // ...and exit out of the add transition method. return; } // If the Transition Dictionary does not contain the passed Transition Key... if (!cStateTrans.ContainsKey(stateAction)) { // ...add it to the transition Dictionary, as well as the new transition. cStateTrans.Add(stateAction, transition); } else { // ...else Log a warning to the developer that it already exists... Debug.LogWarning("Transition: " + stateAction + " - Overwritten"); // ...and override it with the new transition. cStateTrans[stateAction] = transition; } }
public static FsmTransition Copy(this FsmTransition transition) { FsmTransition newtransition = new FsmTransition(transition); newtransition.ToFsmState = transition.ToFsmState; return(newtransition); }
public IFsmTransition CreateTransition(IFsmState from, IFsmState to, IFsmTransitionCondition Condition) { if (from.Fsm != this) { throw new ArgumentException(); } if (to.Fsm != this) { throw new ArgumentException(); } var fromState = _states[from.Id]; var toState = _states[to.Id]; if (fromState.IsDeleted || toState.IsDeleted) { throw new ArgumentException(); } var transition = new FsmTransition(this, fromState, toState, Condition); fromState.RegisterOutTransition(transition); toState.RegisterInTransition(transition); _transitions.Add(transition); return(transition); }
private async void Fsm_StateChanged(object sender, FsmTransition fsmStateChangedInfoDto) { await StateService.SendFsmStateChangedAsync(fsmStateChangedInfoDto); var fsmTransition = new FsmTransition { Time = fsmStateChangedInfoDto.Time, Fsm = fsmStateChangedInfoDto.Fsm, Input = fsmStateChangedInfoDto.Input, LastState = fsmStateChangedInfoDto.LastState, ActState = fsmStateChangedInfoDto.ActState, InputMessage = fsmStateChangedInfoDto.InputMessage }; using ApplicationDbContext dbContext = new (ConnectionString); using IUnitOfWork unitOfWork = new UnitOfWork(dbContext); try { await unitOfWork.FsmTransitions.AddAsync(fsmTransition); await unitOfWork.SaveChangesAsync(); } catch (Exception ex) { Log.Error($"Fsm_StateChanged;Failed to save transition; ex: {ex.Message}"); } }
public DyingTransitionState(PostureStateId id) : base(id) { AddTransition( (command, action) => FsmTransition.SimpleCommandHandler(command, FsmInput.DyingTransitionFinished), (command, action) => FsmTransitionResponseType.NoResponse, (int)PostureStateId.Dying, null, 0, new [] { FsmInput.DyingTransitionFinished } ); }
public static void AddTransition(this FsmState state, FsmEvent fsmEvent, FsmState toState) { FsmTransition transition = new FsmTransition() { FsmEvent = fsmEvent, ToFsmState = toState }; state.Transitions = state.Transitions.Append(transition).ToArray(); }
public void RegisterOutTransition(FsmTransition t) { if (t.From != this || t.IsDeleted) { throw new ArgumentException(); } _outTransitions.Add(t); }
public void RegisterInTransition(FsmTransition t) { if (t.To != this || t.IsDeleted) { throw new ArgumentException(); } _inTransitions.Add(t); }
//private static LoggerAdapter _logger = new LoggerAdapter(typeof(LadderState)); public ExitLadderState(MovementStateId id) : base(id) { #region ladder to exitladder AddTransition( (command, addOutput) => FsmTransition.SimpleCommandHandler(command, FsmInput.ExitLadderFinished), null, (int)MovementStateId.Idle, null, 0, new[] { FsmInput.ExitLadderFinished }); #endregion }
public SpecialFireState(ActionStateId id) : base(id) { #region SpecialFire To SpecialFireHold AddTransition( (command, addOutput) => FsmTransition.SimpleCommandHandler(command, FsmInput.FireFinished), null, (int)ActionStateId.SpecialFireHold, null, 0, new[] { FsmInput.FireFinished }); #endregion }
public void SetUp() { fsm = ScriptableObject.CreateInstance <FiniteStateMachine>(); LoggerFsmState state1 = ScriptableObject.CreateInstance <LoggerFsmState>(); state1.name = "State1"; AssetDatabase.CreateAsset(state1, ASSET_PATH + "State1.asset"); LoggerFsmState state2 = ScriptableObject.CreateInstance <LoggerFsmState>(); state2.name = "State2"; AssetDatabase.CreateAsset(state2, ASSET_PATH + "State2.asset"); AssetDatabase.SaveAssets(); fsm.AddStateInstance("One", state1); fsm.AddStateInstance("Two", state2); fsm.AddStateInstance("Three", state1); fsm.AddStateInstance("Four", state2); FsmParameter triggerParameter = FsmParameter.NewBoolean("testTrigger"); FsmParameter booleanParameter = FsmParameter.NewBoolean("testBool"); FsmParameter integerParameter = FsmParameter.NewInteger("testInt"); FsmParameter floatParameter = FsmParameter.NewFloat("testFloat"); fsm.AddParameter(triggerParameter); fsm.AddParameter(booleanParameter); fsm.AddParameter(integerParameter); fsm.AddParameter(floatParameter); FsmTransition transition1to2 = new FsmTransition(); transition1to2.AddCondition(FsmCondition.NewTrigger(triggerParameter)); FsmTransition transition2to3 = new FsmTransition(); transition2to3.AddCondition(FsmCondition.NewEquals(booleanParameter, true)); FsmTransition transition3to4 = new FsmTransition(); transition3to4.AddCondition(FsmCondition.NewGreaterThan(integerParameter, 3)); FsmTransition transition4to1 = new FsmTransition(); transition4to1.AddCondition(FsmCondition.NewSmallerThan(floatParameter, -1f)); fsm.AddTransition("One", "Two", transition1to2); fsm.AddTransition("Two", "Three", transition2to3); fsm.AddTransition("Three", "Four", transition3to4); fsm.AddTransition("Four", "One", transition4to1); // Clean up if asset already exists AssetDatabase.DeleteAsset(ASSET_PATH); }
/// <summary> /// Adds the transition. /// </summary> /// <param name="fsmTransition">The FSM transition.</param> internal void AddTransition(FsmTransition fsmTransition) { var transition = new Transition <IDomainEvent <TEntity> >(fsmTransition.SourceState, fsmTransition.TargetState, @event => @event.GetType() == fsmTransition.DomainEvent && @event.CanApply(Root)); transition.OnTransition += InvokeOnTransition; transition.Description = fsmTransition.Weight.ToString(CultureInfo.InvariantCulture); _finiteStateMachine.AddTransition(transition); _fsmTransitions.Add(transition, fsmTransition); }
public static void GenerateTemplateBad() { string effectName = "testBad"; var fsmTemplate = (FsmTemplate)CreateInstance(typeof(FsmTemplate)); fsmTemplate.Category = "HotspotEffect"; fsmTemplate.fsm = new Fsm(); fsmTemplate.fsm.Reset(null); var startEvent = new FsmEvent("START " + effectName); var startTransition = new FsmTransition { FsmEvent = startEvent, ToState = effectName }; var applyState = new FsmState(fsmTemplate.fsm) { Name = effectName, Position = new Rect(130, 60, 128, 16), Transitions = new[] { new FsmTransition { FsmEvent = FsmEvent.Finished, ToState = "After " + effectName } } }; var finishState = new FsmState(fsmTemplate.fsm) { Name = "After " + effectName, Position = new Rect(130, 120, 128, 16) }; var finishAction = new DebugLog(); finishState.Actions = new FsmStateAction[] { finishAction }; finishState.SaveActions(); FsmState[] _states = fsmTemplate.fsm.States; ArrayUtility.Add(ref _states, finishState); ArrayUtility.Add(ref _states, applyState); fsmTemplate.fsm.States = _states; fsmTemplate.fsm.Events = new[] { startEvent }; fsmTemplate.fsm.GlobalTransitions = new[] { startTransition }; fsmTemplate.fsm.Name = effectName; fsmTemplate.fsm.UsedInTemplate = fsmTemplate; AssetDatabase.CreateAsset(fsmTemplate, @"Assets\Playmaker Custom Templates\test\" + effectName + ".asset"); AssetDatabase.SaveAssets(); }
public static void GenerateFsmTemplate() { FsmTemplate fsmTemplate = (FsmTemplate)CreateInstance(typeof(FsmTemplate)); fsmTemplate.Category = "test"; fsmTemplate.fsm = new Fsm(); fsmTemplate.fsm.Reset(null); fsmTemplate.fsm.Name = "Test Fsm Template"; fsmTemplate.fsm.UsedInTemplate = fsmTemplate; FsmState lastState = new FsmState(fsmTemplate.fsm) { Name = "Done", Position = new Rect(130, 120, 128, 15) }; FsmState myState = new FsmState(fsmTemplate.fsm) { Name = "My State", Position = new Rect(130, 60, 128, 30), Transitions = new[] { new FsmTransition { FsmEvent = FsmEvent.Finished, ToState = lastState.Name } } }; FsmEvent startEvent = new FsmEvent("TEST EVENT"); FsmTransition startTransition = new FsmTransition { FsmEvent = startEvent, ToState = myState.Name }; //var finishAction = new FinishHotspotAction(); //// Documentation says: "Called in the editor when an action is added to a state or reset". Not clear if via code is needed, apparently it does not generate errors. ////finishAction.Reset(); //finishState.Actions = new FsmStateAction[] { finishAction }; //finishState.SaveActions(); fsmTemplate.fsm.States = new[] { myState, lastState }; fsmTemplate.fsm.Events = new[] { startEvent }; fsmTemplate.fsm.GlobalTransitions = new[] { startTransition }; AssetDatabase.CreateAsset(fsmTemplate, @"Assets\Playmaker Custom Templates\test\" + "Test" + ".asset"); AssetDatabase.SaveAssets(); }
public static FsmState CreateDiveMoveState() { MovementState state = new MovementState(MovementStateId.DiveMove); #region DiveMove to Idle state.AddTransition(new DiveTransition( state.AvailableTransitionId(), (command, addOutput) => FsmTransition.SimpleCommandHandler(command, FsmInput.Idle), (command, addOutput) => { if (command.IsMatch(FsmInput.Sprint) || command.IsMatch(FsmInput.Run) || command.IsMatch(FsmInput.Walk)) { return(FsmTransitionResponseType.ForceEnd); } return(FsmTransitionResponseType.NoResponse); }, (int)MovementStateId.Idle, (normalizedTime, addOutput) => { FsmOutput.Cache.SetValue(AnimatorParametersHash.Instance.MotionHash, AnimatorParametersHash.Instance.MotionName, AnimatorParametersHash.Instance.MotionlessValue, CharacterView.FirstPerson | CharacterView.ThirdPerson); addOutput(FsmOutput.Cache); }, SingletonManager.Get <CharacterStateConfigManager>().GetMovementTransitionTime(MovementInConfig.DiveMove, MovementInConfig.Idle), null, true), new[] { FsmInput.Idle }); #endregion #region DiveMove to move state.AddTransition(new DiveTransition( state.AvailableTransitionId(), (command, addOutput) => { if (command.IsMatch(FsmInput.Sprint) || command.IsMatch(FsmInput.Run) || command.IsMatch(FsmInput.Walk)) { command.Handled = true; return(true); } return(false); }, null, (int)MovementStateId.Walk, null, 0), new[] { FsmInput.Sprint, FsmInput.Run, FsmInput.Walk }); #endregion return(state); }
public FireState(ActionStateId id) : base(id) { #region Fire To CommonNull(due to animation end) AddTransition( (command, addOutput) => FsmTransition.SimpleCommandHandler(command, FsmInput.FireFinished), null, (int)ActionStateId.CommonNull, null, 0, new[] { FsmInput.FireFinished }); #endregion #region Fire to CommonNull(due to interrupt) AddTransition( (command, addOutput) => { if (command.IsMatch(FsmInput.Fire)) { FsmOutput.Cache.SetValue(AnimatorParametersHash.Instance.FireEndHash, AnimatorParametersHash.Instance.FireEndName, AnimatorParametersHash.Instance.FireEndEnableValue, CharacterView.FirstPerson | CharacterView.ThirdPerson, true); addOutput(FsmOutput.Cache); return(FsmStateResponseType.Reenter); } return(FsmStateResponseType.Pass); }, null, (int)ActionStateId.CommonNull, null, 0, new[] { FsmInput.Fire }); AddTransition( (command, addOutput) => { if (command.IsMatch(FsmInput.SightsFire)) { FsmOutput.Cache.SetValue(AnimatorParametersHash.Instance.FireEndHash, AnimatorParametersHash.Instance.FireEndName, AnimatorParametersHash.Instance.FireEndEnableValue, CharacterView.FirstPerson | CharacterView.ThirdPerson, true); addOutput(FsmOutput.Cache); return(FsmStateResponseType.Reenter); } return(FsmStateResponseType.Pass); }, null, (int)ActionStateId.CommonNull, null, 0, new[] { FsmInput.SightsFire }); #endregion }
public FsmState(AssetNameResolver namer, AssetTypeValueField field) { name = field.Get("name").GetValue().AsString(); description = field.Get("description").GetValue().AsString(); colorIndex = (byte)field.Get("colorIndex").GetValue().AsUInt(); position = new UnityRect(field.Get("position")); isBreakpoint = field.Get("isBreakpoint").GetValue().AsBool(); isSequence = field.Get("isSequence").GetValue().AsBool(); hideUnused = field.Get("hideUnused").GetValue().AsBool(); transitions = new FsmTransition[field.Get("transitions").GetChildrenCount()]; for (int i = 0; i < transitions.Length; i++) { transitions[i] = new FsmTransition(field.Get("transitions")[i]); } actionData = new ActionData(namer, field.Get("actionData")); }
public static FsmEvent AddFsmTransition(this FsmState state, string eventName, string toState) { var ret = FsmEvent.GetFsmEvent(eventName); FsmTransition[] origTransitions = state.Transitions; FsmTransition[] transitions = new FsmTransition[origTransitions.Length + 1]; origTransitions.CopyTo(transitions, 0); transitions[origTransitions.Length] = new FsmTransition { ToState = toState, ToFsmState = state.Fsm.GetState(toState), FsmEvent = ret }; state.Transitions = transitions; return(ret); }
public void SetUp() { fsm = ScriptableObject.CreateInstance <FiniteStateMachine>(); state1 = ScriptableObject.CreateInstance <TestFsmState>(); state1.name = "State1"; state2 = ScriptableObject.CreateInstance <TestFsmState>(); state2.name = "State2"; fsm.AddStateInstance("One", state1); fsm.AddStateInstance("Two", state2); fsm.AddStateInstance("Three", state1); fsm.AddStateInstance("Four", state2); triggerParameter = FsmParameter.NewBoolean("testTrig"); booleanParameter = FsmParameter.NewBoolean("testBool"); integerParameter = FsmParameter.NewInteger("testInt"); floatParameter = FsmParameter.NewFloat("testFloat"); fsm.AddParameter(triggerParameter); fsm.AddParameter(booleanParameter); fsm.AddParameter(integerParameter); fsm.AddParameter(floatParameter); transition1to2 = new FsmTransition(); transition1to2.AddCondition(FsmCondition.NewTrigger(triggerParameter)); transition2to3 = new FsmTransition(); FsmCondition equalsCondition = FsmCondition.NewEquals(booleanParameter, true); transition2to3.AddCondition(equalsCondition); transition3to4 = new FsmTransition(); FsmCondition greaterCondition = FsmCondition.NewGreaterThan(integerParameter, 3); transition3to4.AddCondition(greaterCondition); transition4to1 = new FsmTransition(); FsmCondition smallerCondition = FsmCondition.NewSmallerThan(floatParameter, -1f); transition4to1.AddCondition(smallerCondition); fsm.AddTransition("One", "Two", transition1to2); fsm.AddTransition("Two", "Three", transition2to3); fsm.AddTransition("Three", "Four", transition3to4); fsm.AddTransition("Four", "One", transition4to1); }
/// <summary> /// Add new global transition from the given event to the state name to the given PlayMaker FSM. /// </summary> /// <param name="fsm">The PlayMaker FSM to add global transition for.</param> /// <param name="ev">The event triggering the transition.</param> /// <param name="stateName">The state this transition activates.</param> static public void AddNewGlobalTransition(PlayMakerFSM fsm, FsmEvent ev, string stateName) { FsmTransition[] oldTransitions = fsm.FsmGlobalTransitions; List <FsmTransition> temp = new List <FsmTransition>(); foreach (FsmTransition t in oldTransitions) { temp.Add(t); } FsmTransition transition = new FsmTransition(); transition.FsmEvent = ev; transition.ToState = stateName; temp.Add(transition); fsm.Fsm.GlobalTransitions = temp.ToArray(); }
public static FsmTransition AddTransition(this FsmState state, FsmEvent fsmEvent, FsmState toState) { FsmTransition[] transitions = new FsmTransition[state.Transitions.Length + 1]; state.Transitions.CopyTo(transitions, 0); FsmTransition t = new FsmTransition { FsmEvent = fsmEvent, ToFsmState = toState, ToState = toState.Name, }; transitions[state.Transitions.Length] = t; state.Transitions = transitions; return(t); }
public static void addTransition(this FsmState self, string eventName, string toState) { List <FsmTransition> transitions = self.Transitions.ToList(); FsmTransition trans = new FsmTransition { ToState = toState, FsmEvent = FsmEvent.EventListContains(eventName) ? FsmEvent.GetFsmEvent(eventName) : new FsmEvent(eventName) }; transitions.Add(trans); self.Transitions = transitions.ToArray(); }
public static void AddTransition(this FsmState self, string eventName, string toState) { FsmTransition[] transitions = new FsmTransition[self.Transitions.Length + 1]; Array.Copy(self.Transitions, transitions, self.Transitions.Length); self.Transitions = transitions; FsmTransition trans = new FsmTransition { ToState = toState, FsmEvent = FsmEvent.EventListContains(eventName) ? FsmEvent.GetFsmEvent(eventName) : new FsmEvent(eventName) }; self.Transitions[self.Transitions.Length - 1] = trans; }
private string DetermineNextState(FsmTransition transition) { //next state MUST be determined after executing procedure, because in procedure there can be setstate string nextStateToSet; if (!string.IsNullOrEmpty(_overriddenNextState)) { //if overridden state variable is set and we successfully get it's value, set it as next state nextStateToSet = _overriddenNextState; } else { //if not, just determine state from transition nextStateToSet = transition.DetermineActualTargetState(); } return(nextStateToSet); }
public static void RemoveFsmTransition(this FsmState state, string eventName) { FsmTransition[] origTransitions = state.Transitions; FsmTransition[] newTransitions = new FsmTransition[origTransitions.Length - 1]; int i; int foundInt = 0; for (i = 0; i < newTransitions.Length; i++) { if (origTransitions[i].EventName == eventName) { foundInt = 1; } newTransitions[i] = origTransitions[i + foundInt]; } state.Transitions = newTransitions; }
public static FsmEvent AddFsmGlobalTransitions(this PlayMakerFSM fsm, string globalEventName, string toState) { var ret = new FsmEvent(globalEventName) { IsGlobal = true }; FsmTransition[] origTransitions = fsm.FsmGlobalTransitions; FsmTransition[] transitions = new FsmTransition[origTransitions.Length + 1]; origTransitions.CopyTo(transitions, 0); transitions[origTransitions.Length] = new FsmTransition { ToState = toState, ToFsmState = fsm.GetState(toState), FsmEvent = ret }; fsm.Fsm.GlobalTransitions = transitions; return(ret); }
public static void IntrospectFsmTransitions(FsmTransition[] fsmTransitions,string listName,XmlElement parentElement) { if (fsmTransitions==null || fsmTransitions.Length==0) { return; } XmlElement _ListElement = IntrospectionXmlProxy.AddElement(parentElement,listName); foreach(FsmTransition _transition in fsmTransitions) { XmlElement _transitionElement = IntrospectionXmlProxy.AddElement(_ListElement,"Transition"); IntrospectionXmlProxy.AddElement(_transitionElement,"EventName",_transition.EventName); IntrospectionXmlProxy.AddElementIfNotEmpty(_transitionElement,"ToState",_transition.ToState); if (_transition.ColorIndex!=0) { IntrospectionXmlProxy.AddElement(_transitionElement,"ColorIndex",_transition.ColorIndex.ToString()); } if (_transition.LinkConstraint != FsmTransition.CustomLinkConstraint.None) { IntrospectionXmlProxy.AddElement(_transitionElement,"LinkConstraint",_transition.LinkConstraint.ToString()); } if (_transition.LinkStyle != FsmTransition.CustomLinkStyle.Default) { IntrospectionXmlProxy.AddElement(_transitionElement,"LinkStyle",_transition.LinkStyle.ToString()); } } }