/// <inheritdoc/> public override void OnAwake() { base.OnAwake(); linkStateMachine = new SimpleStateMachine <LinkState>(true /* strict */); SetupStateMachine(); RegisterGameEventListener(GameEvents.onPartCouple, OnPartCoupleEvent); }
public void TestMachine() { var sm = new SimpleStateMachine <State>(strict: true); sm.SetTransitionConstraint(State.One, new State[] { State.Two }); sm.AddStateHandlers( State.One, enterHandler: newState => LogFromTo(sm.currentState, newState), leaveHandler: newState => LogFromTo(sm.currentState, newState)); sm.currentState = State.One; // Start the machine. // Logs: Move from NULL to One if (sm.CheckCanSwitchTo(State.Two)) { sm.currentState = State.Two; // An allowed tranistion. // Logs: Move from One to Two } if (sm.CheckCanSwitchTo(State.Three)) { // This will never happen. sm.currentState = State.Three; } sm.currentState = null; // Stop the machine. // Logs: Move Two to NULL }
// Sets up a sample state machine for the target states. public static void SetupTargetStateModel() { var linkStateMachine = new SimpleStateMachine <LinkState>(true /* strict */); linkStateMachine.SetTransitionConstraint( LinkState.Available, new[] { LinkState.AcceptingLinks, LinkState.RejectingLinks }); linkStateMachine.SetTransitionConstraint( LinkState.AcceptingLinks, new[] { LinkState.Available, LinkState.Linked, LinkState.Locked }); linkStateMachine.SetTransitionConstraint( LinkState.Linked, new[] { LinkState.Available }); linkStateMachine.SetTransitionConstraint( LinkState.Locked, new[] { LinkState.Available }); linkStateMachine.SetTransitionConstraint( LinkState.RejectingLinks, new[] { LinkState.Available, LinkState.Locked }); linkStateMachine.AddStateHandlers( LinkState.Available, enterHandler: x => Debug.Log("Target is available"), leaveHandler: x => Debug.Log("Target is NOT available")); }
public void TestMachine() { var sm = new SimpleStateMachine <State>(strict: false); sm.AddStateHandlers( State.One, enterHandler: oldState => Debug.Log("Now in state ONE"), leaveHandler: newState => Debug.LogFormat("Going into state: {0}", newState)); sm.onAfterTransition += (from, to) => Debug.LogFormat( "Move from {0} to {1}", DbgFormatter.Nullable(from), DbgFormatter.Nullable(to)); sm.currentState = State.One; // Start the machine. // Logs: // Now in state ONE // Move from NULL to One sm.currentState = State.Two; // Logs: // Going into state: Two // Move from One to Two sm.currentState = State.Three; // Logs: Move from Two to Three sm.currentState = null; // Stop the machine. // Logs: Move Three to NULL }
void Awake() { instance = this; canvas = GameObject.FindObjectOfType <Canvas>(); SetState <TitleState>(); }
public void Awake() { this.state = new SimpleStateMachine <PsiState>(this); this.musicPlayer = FindObjectOfType <MusicPlayer>(); this.tilemapRenderers = FindObjectsOfType <TilemapRenderer>(); this.Psi.SetActive(false); }
public void TestCreateStatesWithTransitions() { var elevator = new SimpleStateMachine(); var floor = 0; elevator["UP"] = new SimpleState() { OnEnter = () => { Console.WriteLine("Elevator is going up!"); }, OnUpdate = () => { floor++; }, transitions = { new SimpleStateTransition(() => floor == 25, "TOP") } }; elevator["TOP"] = new SimpleState() { OnEnter = () => { Console.WriteLine("Elevator is at the top!"); }, }; elevator.Change("UP"); while (elevator.state.name != "TOP") { elevator.Update(); } Assert.AreEqual(25, floor); }
public PurchasingEnabledState(SimpleStateMachine <bool> stateMachine) : base(k_StateNameEnabled, stateMachine) { m_UIBlocks.Add(new GooglePlayConfigurationSettingsBlock()); m_UIBlocks.Add(new AppleConfigurationSettingsBlock()); m_UIBlocks.Add(new IapCatalogServiceSettingsBlock()); ModifyActionForEvent(false, HandleDisabling); }
// Use this for initialization void Start () { BCMessenger.Instance.RegisterListener("start_new_game", 0, this.gameObject, "HandleStartNewGame"); BCMessenger.Instance.RegisterListener("start_pre_race", 0, this.gameObject, "HandleStartPreRace"); BCMessenger.Instance.RegisterListener("start_countdown", 0, this.gameObject, "HandleStartCountdown"); m_stateMachine = new SimpleStateMachine ("RaceTower"); m_stateMachine.SetState(UpdatePreStart); }
public SimpleState(SimpleStateMachine.StateDelegate enter, SimpleStateMachine.StateDelegate update, SimpleStateMachine.StateDelegate exit, string name){ this.enter = enter; this.update = update; this.exit = exit; this.name = name; }
public void TestCreateInitialStates() { var statemachine = new SimpleStateMachine(); statemachine["BOOT"] = new SimpleState(); statemachine.Change("BOOT"); Assert.AreEqual("BOOT", statemachine.state.name); }
public void Should_create_a_state_machine() { IStateMachine stateMachine = SimpleStateMachine.Create(); stateMachine.AddStartState(new StartState("Start")); Assert.IsNotNull(stateMachine); Assert.AreEqual(stateMachine.CurrentState, null); Assert.AreEqual(0, stateMachine.History.Count); }
void Start() { BCMessenger.Instance.RegisterListener("connect", 0, this.gameObject, "HandleConnection"); BCMessenger.Instance.RegisterListener("start_race", 0, this.gameObject, "HandleStartRace"); ChildCollider.AttachChildEnterDelegate(this.transform, OnRaceTrigger); m_stateMachine = new SimpleStateMachine ("RaceMaster"); m_stateMachine.SetState(UpdateWaitForPlayers); }
/// <inheritdoc/> public override void OnAwake() { ConfigAccessor.CopyPartConfigFromPrefab(this); base.OnAwake(); LocalizeModule(); linkStateMachine = new SimpleStateMachine <LinkState>(true /* strict */); SetupStateMachine(); GameEvents.onPartCouple.Add(OnPartCoupleEvent); }
public PurchasingEnabledState(SimpleStateMachine <PurchasingServiceToggleEvent> stateMachine) : base(k_StateNameEnabled, stateMachine) { m_GoogleConfigData = new GoogleConfigurationData(); m_UIBlocks.Add(new GooglePlayConfigurationSettingsBlock(m_GoogleConfigData)); m_UIBlocks.Add(new AppleConfigurationSettingsBlock()); m_UIBlocks.Add(new IapCatalogServiceSettingsBlock()); ModifyActionForEvent(PurchasingServiceToggleEvent.Disabled, HandleDisabling); }
public EnabledState(SimpleStateMachine <ServiceEvent> stateMachine, PurchasingProjectSettings provider) : base(k_StateNameEnabled, stateMachine, provider) { topicForNotifications = Notification.Topic.PurchasingService; notLatestPackageInstalledInfo = string.Format(k_NotLatestPackageInstalledInfo, k_PurchasingPackageName); packageInstallationHeadsup = string.Format(k_PackageInstallationHeadsup, k_PurchasingPackageName); duplicateInstallWarning = null; packageInstallationDialogTitle = string.Format(k_PackageInstallationDialogTitle, k_PurchasingPackageName); ModifyActionForEvent(ServiceEvent.Disabled, HandleDisabling); }
void Init() { UIEventListener.Get(inventroyObj).onClick = onMap; UIEventListener.Get(mapObj).onClick = onInventory; m_stateMachine = new SimpleStateMachine(); m_inventroyState.onEnter = () => { inventroyObj.SetActive(true); }; m_inventroyState.onLeave = () => { inventroyObj.SetActive(false); }; m_mapState.onEnter = () => { mapObj.SetActive(true); }; m_mapState.onLeave = () => { mapObj.SetActive(false); }; m_stateMachine.State = m_mapState; }
public static ISimpleStateMachine <TMachineState, TMachineEvent> CreateSimple <TMachineState, TMachineEvent>( TMachineState initialState, IStateMachineConfiguration <TMachineState, TMachineEvent> configuration) { if (configuration is StateMachineDispatcher <TMachineState, TMachineEvent> dispatcher) { var machine = new SimpleStateMachine <TMachineState, TMachineEvent>(initialState, dispatcher); return(machine); } throw new ArgumentException("configuration must not be a custom implementation of the state machine configuration interface."); }
//-Implementation protected void Start() { _ammoInClip = new LimitedFloat(ClipSettings); _stateMachine = new SimpleStateMachine <State>(State.Idle); uiShootController = GetComponent <UIShootController>(); if (uiShootController) { uiShootController.setClipStats( (int)_ammoInClip.getValue(), (int)_ammoInClip.getValue() ); } }
public PurchasingProjectSettings(string path, SettingsScope scopes, IEnumerable <string> keywords = null) : base(path, scopes, k_ServiceName, keywords) { m_StateMachine = new SimpleStateMachine <ServiceEvent>(); m_StateMachine.AddEvent(ServiceEvent.Disabled); m_StateMachine.AddEvent(ServiceEvent.Enabled); m_DisabledState = new DisabledState(m_StateMachine, this); m_EnabledState = new EnabledState(m_StateMachine, this); m_StateMachine.AddState(m_DisabledState); m_StateMachine.AddState(m_EnabledState); }
public async Task TestMethod1() { ServiceContext serviceContext = new ServiceContext(new DebugTracer()); SimpleStateMachine <UnitTest1, TestEvent> stateMachine = new SimpleStateMachine <UnitTest1, TestEvent>(serviceContext, this, 5); TestStateFactory stateFactory = new TestStateFactory(serviceContext); stateMachine.InitState(stateFactory.GetState <StateA>(), true); stateMachine.StartPumpEvents(); stateMachine.PostEvent(TestEvent.A, 0); IState <UnitTest1, TestEvent> lastState = await stateMachine.PostEvent(TestEvent.B, 0); Assert.AreEqual(lastState, stateFactory.GetState <StateA>()); Assert.AreEqual(2, m_CountA); Assert.AreEqual(1, m_CountB); }
public void ConfigureServices(IServiceCollection services) { RabbitConfig rabbitConfig = Configuration.GetSection("RabbitConfig").Get <RabbitConfig>(); services.AddTransient <InMemorySagaRepository <SagaIntsanse> >(); services.AddMassTransit(x => { x.AddConsumer <WriteToDataMartCommandConsumer>(); x.AddConsumer <WriteToFactsCommandConsumer>(); x.AddBus(provider => Bus.Factory.CreateUsingRabbitMq(cfg => { IRabbitMqHost host = cfg.Host(new Uri($"rabbitmq://{rabbitConfig.Host}/"), h => { h.Username(rabbitConfig.User); h.Password(rabbitConfig.Password); }); EndpointConvention.Map <IWriteToFactsCommand>(new Uri($"rabbitmq://{rabbitConfig.Host}/{rabbitConfig.FactsQueue}")); EndpointConvention.Map <IWriteToDataMartCommand>(new Uri($"rabbitmq://{rabbitConfig.Host}/{rabbitConfig.DatamartQueue}")); cfg.ReceiveEndpoint(host, rabbitConfig.InitializeQueue, e => { SimpleStateMachine sm = new SimpleStateMachine(provider.GetService <ILogger <SimpleStateMachine> >(), rabbitConfig); InMemorySagaRepository <SagaIntsanse> rep = provider.GetService <InMemorySagaRepository <SagaIntsanse> >(); e.PrefetchCount = 8; e.StateMachineSaga(sm, rep); }); cfg.ReceiveEndpoint(host, rabbitConfig.FactsQueue, e => { e.ConfigureConsumer <WriteToFactsCommandConsumer>(provider); }); cfg.ReceiveEndpoint(host, rabbitConfig.DatamartQueue, e => { e.ConfigureConsumer <WriteToDataMartCommandConsumer>(provider); }); })); }); services.AddSingleton <IBus>(provider => provider.GetRequiredService <IBusControl>()); services.AddSingleton <Microsoft.Extensions.Hosting.IHostedService, BusService>(); }
protected override void ConfigureInMemoryReceiveEndpoint(IInMemoryReceiveEndpointConfigurator configurator) { _discarded = GetTask <bool>(); _simpleStateMachine = new SimpleStateMachine(x => { _discarded.TrySetResult(true); }); _sagaDbContextFactory = () => new SagaDbContext <SimpleState, SimpleStateMap>(SagaDbContextFactoryProvider.GetLocalDbConnectionString()); _simpleStateRepository = new Lazy <ISagaRepository <SimpleState> >(() => new EntityFrameworkSagaRepository <SimpleState>(_sagaDbContextFactory)); configurator.StateMachineSaga(_simpleStateMachine, _simpleStateRepository.Value); base.ConfigureInMemoryReceiveEndpoint(configurator); }
public void Should_create_a_state_machine_and_start() { IStateMachine stateMachine; StartState start = new StartState("Start"); State search = new State("Search", start); stateMachine = SimpleStateMachine.Create() .AddStartState(start) .AddState(search) .Start(); Assert.IsNotNull(stateMachine); Assert.IsTrue(start.To.Contains(search)); Assert.IsTrue(search.From.Contains(start)); Assert.AreEqual(stateMachine.CurrentState, start); Assert.AreEqual(1, stateMachine.History.Count); Assert.AreEqual("Start", stateMachine.History[0].State.Name); }
public AnalyticsProjectSettings(string path, SettingsScope scopes, IEnumerable <string> keywords = null) : base(path, scopes, k_ServiceName, keywords) { m_StateMachine = new SimpleStateMachine <ServiceEvent>(); m_StateMachine.AddEvent(ServiceEvent.Disabled); m_StateMachine.AddEvent(ServiceEvent.Integrating); m_StateMachine.AddEvent(ServiceEvent.Enabled); m_DisabledState = new DisabledState(m_StateMachine, this); m_IntegrationState = new IntegrationState(m_StateMachine, this); m_EnabledState = new EnabledState(m_StateMachine, this); m_StateMachine.AddState(m_DisabledState); m_StateMachine.AddState(m_IntegrationState); m_StateMachine.AddState(m_EnabledState); m_ValidationPoller = new AnalyticsValidationPoller(); }
public override void OnAwake() { stateMachine = new SimpleStateMachine <State>(strict: true); // State ONE can be transitioned into both TWO and THREE. stateMachine.SetTransitionConstraint( State.One, new[] { State.Two, State.Three }); // State TWO can only get back to ONE. stateMachine.SetTransitionConstraint( State.Two, new[] { State.One }); // State THREE can only get back to ONE. stateMachine.SetTransitionConstraint( State.Three, new[] { State.One }); // No menus available in state ONE. stateMachine.AddStateHandlers( State.One, enterHandler: oldState => { Events["StateTwoMenuAction"].active = false; Events["StateThreeMenuAction"].active = false; }, leaveHandler: newState => Debug.LogFormat("Move from ONE to {0}", newState)); // Only TWO-menu is available in the state TWO. stateMachine.AddStateHandlers( State.Two, enterHandler: oldState => { Events["StateTwoMenuAction"].active = true; Events["StateThreeMenuAction"].active = false; }); // Only THREE-menu is available in the state THREE. stateMachine.AddStateHandlers( State.Three, enterHandler: oldState => { Events["StateTwoMenuAction"].active = false; Events["StateThreeMenuAction"].active = true; }); }
public void TestMachine() { var sm = new SimpleStateMachine <State>(strict: false); sm.AddStateHandlers( State.One, enterHandler: oldState => Debug.Log("Now in state ONE"), leaveHandler: newState => Debug.LogFormat("Going into state: {0}", newState)); sm.onBeforeTransition += (from, to) => Debug.LogFormat( "Before move: current={0}, new={1}", DbgFormatter.Nullable(sm.currentState), DbgFormatter.Nullable(to)); sm.onAfterTransition += (from, to) => Debug.LogFormat( "After move: old={0}, current={1}", DbgFormatter.Nullable(from), DbgFormatter.Nullable(sm.currentState)); sm.currentState = State.One; // Start the machine. // Logs: // Now in state ONE // Before move: current=NULL, new=One // After move: old=NULL, current=One sm.currentState = State.Two; // Logs: // Going into state: Two // Before move: current=One, new=Two // After move: old=One, current=Two sm.currentState = State.Three; // Logs: // Before move: current=Two, new=Three // After move: old=Two, current=Three sm.currentState = null; // Stop the machine. // Logs: // Before move: current=Three, new=NULL // After move: old=Three, current=NULL }
public EnabledState(SimpleStateMachine <ServiceEvent> stateMachine, AnalyticsProjectSettings provider) : base(k_StateNameEnabled, stateMachine, provider) { topicForNotifications = Notification.Topic.AnalyticsService; notLatestPackageInstalledInfo = string.Format(k_NotLatestPackageInstalledInfo, k_AnalyticsPackageName); packageInstallationHeadsup = string.Format(k_PackageInstallationHeadsup, k_AnalyticsPackageName); duplicateInstallWarning = null; packageInstallationDialogTitle = string.Format(k_PackageInstallationDialogTitle, k_AnalyticsPackageName); ModifyActionForEvent(ServiceEvent.Disabled, HandleDisabling); ModifyActionForEvent(ServiceEvent.Integrating, HandleIntegrating); m_AdditionalEvents = new Dictionary <string, AdditionalEvent>() { [k_CustomKey] = new AdditionalEvent() { title = k_CustomTitle, description = k_CustomDesc, learnUrl = AnalyticsConfiguration.instance.customLearnUrl }, [k_MonetizationTitleAndKey] = new AdditionalEvent() { title = k_MonetizationTitleAndKey, description = k_MonetizationDesc, learnUrl = AnalyticsConfiguration.instance.monetizationLearnUrl }, }; }
/// <inheritdoc/> protected override void SetupStateMachine() { base.SetupStateMachine(); linkStateMachine.onAfterTransition += (start, end) => UpdateContextMenu(); linkStateMachine.AddStateHandlers( LinkState.Linked, enterHandler: oldState => { var module = linkTarget as PartModule; PartModuleUtils.InjectEvent(this, DetachConnectorEvent, module); PartModuleUtils.AddEvent(module, _grabConnectorEventInject); }, leaveHandler: newState => { var module = linkTarget as PartModule; PartModuleUtils.WithdrawEvent(this, DetachConnectorEvent, module); PartModuleUtils.DropEvent(module, _grabConnectorEventInject); }); linkStateMachine.AddStateHandlers( LinkState.NodeIsBlocked, enterHandler: oldState => { if (decoupleIncompatibleTargets && coupleNode != null && coupleNode.attachedPart != null) { HostedDebugLog.Warning(this, "Decouple incompatible part from the node: {0}", coupleNode.FindOpposingNode().attachedPart); UISoundPlayer.instance.Play(KASAPI.CommonConfig.sndPathBipWrong); ShowStatusMessage( CannotLinkToPreAttached.Format(coupleNode.attachedPart), isError: true); KASAPI.LinkUtils.DecoupleParts(part, coupleNode.attachedPart); } }, callOnShutdown: false); // The default state is "Locked". All the enter state handlers rely on it, and all the exit // state handlers reset the state back to the default. connectorStateMachine = new SimpleStateMachine <ConnectorState>(); connectorStateMachine.onAfterTransition += (start, end) => { if (end != null) // Do nothing on state machine shutdown. { persistedIsConnectorLocked = isConnectorLocked; if (end == ConnectorState.Locked) { KASAPI.AttachNodesUtils.AddNode(part, coupleNode); } else if (coupleNode.attachedPart == null) { KASAPI.AttachNodesUtils.DropNode(part, coupleNode); } UpdateContextMenu(); } HostedDebugLog.Info(this, "Connector state changed: {0} => {1}", start, end); }; connectorStateMachine.SetTransitionConstraint( ConnectorState.Docked, new[] { ConnectorState.Plugged, ConnectorState.Locked, // External detach. }); connectorStateMachine.SetTransitionConstraint( ConnectorState.Locked, new[] { ConnectorState.Deployed, ConnectorState.Plugged, ConnectorState.Docked, // External attach. }); connectorStateMachine.SetTransitionConstraint( ConnectorState.Deployed, new[] { ConnectorState.Locked, ConnectorState.Plugged, }); connectorStateMachine.SetTransitionConstraint( ConnectorState.Plugged, new[] { ConnectorState.Deployed, ConnectorState.Docked, }); connectorStateMachine.AddStateHandlers( ConnectorState.Locked, enterHandler: oldState => { SaveConnectorModelPosAndRot(); if (oldState.HasValue) // Skip when restoring state. { UISoundPlayer.instance.Play(sndPathLockConnector); } }, leaveHandler: newState => SaveConnectorModelPosAndRot(saveNonPhysical: newState == ConnectorState.Deployed), callOnShutdown: false); connectorStateMachine.AddStateHandlers( ConnectorState.Docked, enterHandler: oldState => { SaveConnectorModelPosAndRot(); cableJoint.SetLockedOnCouple(true); // Align the docking part to the nodes if it's a separate vessel. if (oldState.HasValue && linkTarget.part.vessel != vessel) { AlignTransforms.SnapAlignNodes(linkTarget.coupleNode, coupleNode); linkJoint.SetCoupleOnLinkMode(true); UISoundPlayer.instance.Play(sndPathDockConnector); } }, leaveHandler: newState => { cableJoint.SetLockedOnCouple(false); SaveConnectorModelPosAndRot(saveNonPhysical: newState == ConnectorState.Deployed); linkJoint.SetCoupleOnLinkMode(false); }, callOnShutdown: false); connectorStateMachine.AddStateHandlers( ConnectorState.Plugged, enterHandler: oldState => SaveConnectorModelPosAndRot(), leaveHandler: newState => SaveConnectorModelPosAndRot(saveNonPhysical: newState == ConnectorState.Deployed), callOnShutdown: false); connectorStateMachine.AddStateHandlers( ConnectorState.Deployed, enterHandler: oldState => StartPhysicsOnConnector(), leaveHandler: newState => StopPhysicsOnConnector(), callOnShutdown: false); }
public IntegrationState(SimpleStateMachine <ServiceEvent> stateMachine, AnalyticsProjectSettings provider) : base(k_StateNameIntegration, stateMachine, provider) { ModifyActionForEvent(ServiceEvent.Disabled, HandleDisabling); ModifyActionForEvent(ServiceEvent.Enabled, HandleEnabling); }
protected BaseAnalyticsState(string stateName, SimpleStateMachine <ServiceEvent> stateMachine, AnalyticsProjectSettings provider) : base(stateName, stateMachine, provider) { }
public PurchasingDisabledState(SimpleStateMachine <PurchasingServiceToggleEvent> stateMachine) : base(k_StateNameDisabled, stateMachine) { ModifyActionForEvent(PurchasingServiceToggleEvent.Enabled, HandleEnabling); }
public DisabledState(SimpleStateMachine <ServiceEvent> stateMachine, PurchasingProjectSettings provider) : base(k_StateNameDisabled, stateMachine, provider) { ModifyActionForEvent(ServiceEvent.Enabled, HandleEnabling); }