public void TestTransitions() { FiniteStateMachine <State> fsm = new FiniteStateMachine <State>(); fsm.AddTransition(State.A, State.B, State.C); fsm.AddTransition(State.B, State.C); fsm.AddTransition(State.C, State.A); fsm.Start(State.A); Assert.IsTrue(fsm.CurrentState == State.A); fsm.TransitionTo(State.B); Assert.IsTrue(fsm.CurrentState == State.B); Assert.That(() => fsm.TransitionTo(State.A), Throws.ArgumentException); fsm.TransitionToIfExists(State.A); Assert.IsTrue(fsm.CurrentState == State.B); fsm.TransitionTo(State.C); Assert.IsTrue(fsm.CurrentState == State.C); fsm.TransitionTo(State.A); Assert.IsTrue(fsm.CurrentState == State.A); fsm.TransitionTo(State.C); Assert.IsTrue(fsm.CurrentState == State.C); Assert.That(() => fsm.TransitionTo(State.B), Throws.ArgumentException); }
private void Awake() { stateMachine = new FiniteStateMachine(); stateMachine.Add(new FiniteStateMachineNode(Move), (int)PlayerInputStates.MOVE); stateMachine.Add(new FiniteStateMachineNode(Turn, TurnEnter, TurnExit), (int)PlayerInputStates.TURN); stateMachine.Start((int)PlayerInputStates.MOVE); }
void Start() { // Save the transform data of the visual-placement card defaultCardLocalPosition = dealerHandPositionPlaceholder.transform.localPosition; cardRotationFaceUp = Quaternion.Euler(0, 180f, 0); cardRotationFaceDown = Quaternion.identity; GameObject tPositioningCard = dealerHandPositionPlaceholder.transform.gameObject; Destroy(tPositioningCard); defaultCardScale = dealerHandPositionPlaceholder.transform.localScale; deckLocalPosition = defaultCardLocalPosition + new Vector3(cardWidth * 5, cardHeight * 1.2f, 0); // Update Deal button with configurable Ante amount buttonDeal.transform.Find("Text").GetComponent <Text>().text = string.Format("Deal (${0} Ante)", ante); bankroll = startBankroll; betThisHand = minimumBet; textFieldBetInput.GetComponent <InputField>().text = minimumBet.ToString(); StringBuilder tInstructions = new StringBuilder(); tInstructions.AppendLine("PAYOFF MULTIPLIER TABLE"); tInstructions.AppendLine("——————————————"); tInstructions.AppendLine("Straight Flush: 50"); tInstructions.AppendLine("Four of a Kind: 25"); tInstructions.AppendLine("Full House: 9"); tInstructions.AppendLine("Flush: 5"); tInstructions.AppendLine("Straight: 4"); tInstructions.AppendLine("Three of a Kind: 3"); tInstructions.AppendLine("Two Pair: 2"); tInstructions.AppendLine("Pair: 1"); drawPokerInstructions = tInstructions.ToString(); // SEVEN-UP tInstructions = new StringBuilder(); tInstructions.AppendLine("PAYOFF MULTIPLIER TABLE"); tInstructions.AppendLine("——————————————"); tInstructions.AppendLine("Straight Flush: 50"); tInstructions.AppendLine("Four of a Kind: 25"); tInstructions.AppendLine("Flush: 9"); tInstructions.AppendLine("Full House: 5"); tInstructions.AppendLine("Straight: 4"); tInstructions.AppendLine("Three of a Kind: 3"); tInstructions.AppendLine("Two Pair: 2"); tInstructions.AppendLine("Pair: 1"); sevenUpDrawPokerInstructions = tInstructions.ToString(); List <Type> tStateTypes = new List <Type>() { typeof(StateInitialize), typeof(StateDeal), typeof(StateDraw), typeof(StateShowdown), typeof(StateConcludeGame) }; _FSM = new FiniteStateMachine(this, this, typeof(StateInitialize), tStateTypes); _FSM.Start(); }
public void TestAnyTransitionEvents() { int event1Count = 0; State?event1From = null; State?event1To = null; FiniteStateMachine <State> fsm = new FiniteStateMachine <State>(); fsm.AddTransition(State.A, State.B); fsm.AddTransition(State.B, State.C); fsm.AddEventOnAnyTransition((from, to) => { event1From = from; event1To = to; event1Count++; }); fsm.Start(State.A); Assert.IsTrue(event1Count == 0); fsm.TransitionTo(State.B); Assert.IsTrue(event1Count == 1); Assert.IsTrue(event1From == State.A); Assert.IsTrue(event1To == State.B); fsm.TransitionTo(State.C); Assert.IsTrue(event1Count == 2); Assert.IsTrue(event1From == State.B); Assert.IsTrue(event1To == State.C); }
IEnumerator Start() { _fsm.Start(); // Wait for the game to be over. yield return(null); // Wait for us to actually have enough birds to win. while (_birdSet.Count < _collectableSet.Count) { yield return(null); } // Disable the actors we know about. foreach (var item in _collectableSet.Items) { item.Disable(); } PlayerBirdController.Instance.Disable(); // Set priority arbitrarily high. _finalCamera.Priority = 100; _fsm.SetTrigger(Triggers.EndHappened); // Mark Game as complete. SaveData.CompleteGame(); }
public void TestIsRunning() { FiniteStateMachine <State> fsm = new FiniteStateMachine <State>(); Assert.IsFalse(fsm.IsRunning); fsm.Start(State.A); Assert.IsTrue(fsm.IsRunning); }
public void TestMultiStateEnterExitTransitionEvents() { int abEnterCount = 0; int abExitCount = 0; int bcEnterCount = 0; int bcExitCount = 0; FiniteStateMachine <State> fsm = new FiniteStateMachine <State>(); fsm.AddTransition(State.A, State.B, State.C); fsm.AddTransition(State.B, State.A, State.C); fsm.AddTransition(State.C, State.A, State.B); fsm.AddEventOnGroupEnter(new[] { State.A, State.B }, (from, to) => { Assert.IsTrue(from != State.A && from != State.B); Assert.IsTrue(to == State.A || to == State.B); abEnterCount++; }); fsm.AddEventOnGroupExit(new[] { State.A, State.B }, (from, to) => { Assert.IsTrue(from == State.A || from == State.B); Assert.IsTrue(to != State.A && to != State.B); abExitCount++; }); fsm.AddEventOnGroupEnter(new[] { State.B, State.C }, (from, to) => { Assert.IsTrue(from != State.B && from != State.C); Assert.IsTrue(to == State.B || to == State.C); bcEnterCount++; }); fsm.AddEventOnGroupExit(new[] { State.B, State.C }, (from, to) => { Assert.IsTrue(from == State.B || from == State.C); Assert.IsTrue(to != State.B && to != State.C); bcExitCount++; }); fsm.Start(State.A); Assert.AreEqual(0, abEnterCount); Assert.AreEqual(0, abExitCount); Assert.AreEqual(0, bcEnterCount); Assert.AreEqual(0, bcExitCount); fsm.TransitionTo(State.B); Assert.AreEqual(0, abEnterCount); Assert.AreEqual(0, abExitCount); Assert.AreEqual(1, bcEnterCount); Assert.AreEqual(0, bcExitCount); fsm.TransitionTo(State.C); Assert.AreEqual(0, abEnterCount); Assert.AreEqual(1, abExitCount); Assert.AreEqual(1, bcEnterCount); Assert.AreEqual(0, bcExitCount); fsm.TransitionTo(State.A); Assert.AreEqual(1, abEnterCount); Assert.AreEqual(1, abExitCount); Assert.AreEqual(1, bcEnterCount); Assert.AreEqual(1, bcExitCount); }
public void Init() { fsm = new FiniteStateMachine <GameState>(); fsm.AddTransition(GameState.INIT, GameState.START, onStart); onStart += StartHandler; fsm.AddTransition(GameState.START, GameState.RUNNING, RunningHandler); // onRunning += RunningHandler; fsm.AddTransition(GameState.PAUSED, GameState.RUNNING, RunningHandler); // onRunning += RunningHandler; fsm.AddTransition(GameState.RUNNING, GameState.PAUSED, PausedHandler); // onPaused += PausedHandler; fsm.AddTransition(GameState.PAUSED, GameState.EXIT, ExitHandler); // onExit += ExitHandler; fsm.Start(GameState.INIT); fsm.ChangeState(GameState.START); }
public void TestStart() { FiniteStateMachine fsm = new FiniteStateMachine("Turnstile"); FiniteState gettingWorkItem = new FiniteState("GettingWorkItem") { OnEnterAction = () => { } }; FiniteState movingToProcessed = new FiniteState("MovingToProcessed") { OnEnterAction = () => { } }; FiniteState addedState = fsm.AddState(gettingWorkItem); fsm.Start(gettingWorkItem); //fsm.Stop(); }
public override void Start() { base.Start(); Drone = Entity.Get <Drone>(); stateMachine = new FiniteStateMachine("DroneAnimation"); stateMachine.RegisterState(new State(IdleState) { EnterMethod = StartIdle }); stateMachine.RegisterState(new State(MoveState) { EnterMethod = StartMove }); stateMachine.Start(Script, IdleState); }
void Awake() { if (instance == null) { instance = this; //dungeonTurn = CharacterFaction.PLAYER; Players = new List <DungeonCharacter> (); Friendlies = new List <DungeonCharacter> (); Enemies = new List <DungeonCharacter> (); dungeonStateMachine = new FiniteStateMachine(); dungeonStateMachine.Add(new FiniteStateMachineNode(PlayerTurn, PlayerTurnEnter, PlayerTurnExit), (int)CharacterFaction.PLAYER); dungeonStateMachine.Add(new FiniteStateMachineNode(FriendlyTurn, FriendlyTurnEnter, FriendlyTurnExit), (int)CharacterFaction.FRIENDLY); dungeonStateMachine.Add(new FiniteStateMachineNode(EnemyTurn, EnemyTurnEnter, EnemyTurnExit), (int)CharacterFaction.ENEMY); dungeonStateMachine.Start((int)CharacterFaction.PLAYER); } }
void InitFSM() { fsm = new FiniteStateMachine(); State wanderingState = fsm.AddState(true); wanderingState.OnUpdate += wander.Execute; wanderingState.OnUpdate += CheckWordBound; State detectingState = fsm.AddState(); detectingState.OnEnter += () => { isDetecting = false; }; detectingState.OnEnter += () => { animator.SetBool(detectingHashCode, true); }; detectingState.OnExit += () => { animator.SetBool(detectingHashCode, false); }; detectingState.OnUpdate += Detecting; State targetingState = fsm.AddState(); targetingState.OnUpdate += flowFieldPathfinder.Execute; //targetingState.OnUpdate += seek.Execute; targetingState.OnUpdate += flock.Execute; State returnState = fsm.AddState(); returnState.OnUpdate += flowFieldPathfinder.Execute; returnState.OnUpdate += flock.Execute; State scaredState = fsm.AddState(); scaredState.OnUpdate += flee.Execute; fsm.AddTransition(wanderingState, detectingState, () => { return(isDetecting); }); fsm.AddTransition(detectingState, targetingState, () => { return(detectionTimer >= detectionDuration); }); fsm.AddTransition(wanderingState, scaredState, () => { return(isScare); }); fsm.AddTransition(returnState, scaredState, () => { return(isScare); }); fsm.AddTransition(targetingState, scaredState, () => { return(isScare); }); fsm.AddTransition(targetingState, returnState, () => { return(state == States.Return); }); fsm.AddTransition(returnState, wanderingState, () => { return((brain.target.transform.position - transform.position).magnitude <= 0.5f); }); fsm.AddTransition(scaredState, wanderingState, () => { return(!isScare && state == States.Wandering); }); fsm.AddTransition(scaredState, targetingState, () => { return(!isScare && state == States.Targeting); }); fsm.AddTransition(scaredState, returnState, () => { return(!isScare && state == States.Return); }); fsm.Start(); }
private void InitializeUpperStateMachine() { upperStateMachine = new FiniteStateMachine("SoldierAnimationLower"); var lowState = new State(UpperLowState); var aimState = new State(UpperAimState) { EnterMethod = async from => { // Display the draw animation only if we come from the low state if (!HasAdditiveAnimation && @from.Name == UpperLowState) { var animation = NewAnimation(AnimKeys.DrawWeapon, true); AnimationComponent.PlayingAnimations.Add(animation); await AnimationComponent.Ended(animation); } }, ExitMethod = async to => { // Display the draw animation only if we're going to the low state if (!HasAdditiveAnimation && to.Name == UpperLowState) { var animation = NewAnimation(AnimKeys.HolsterWeapon, true); AnimationComponent.PlayingAnimations.Add(animation); await AnimationComponent.Ended(animation); } } }; var fireState = new State(UpperFireState) { EnterMethod = State.ToTask(from => { fireAnimation = NewAnimation(AnimKeys.FireWeapon, true); AnimationComponent.PlayingAnimations.Add(fireAnimation); }), ExitMethod = State.ToTask(to => AnimationComponent.PlayingAnimations.Remove(fireAnimation)) }; upperStateMachine.RegisterState(lowState); upperStateMachine.RegisterState(aimState); upperStateMachine.RegisterState(fireState); upperStateMachine.Start(Script, UpperLowState); }
public void TestTransitionEvents() { int eventCount = 0; FiniteStateMachine <State> fsm = new FiniteStateMachine <State>(); fsm.AddTransition(State.A, State.B); fsm.AddTransition(State.B, State.C); fsm.AddEventOnTransition(State.A, State.B, (from, to) => { Assert.IsTrue(from == State.A); Assert.IsTrue(to == State.B); eventCount++; }); fsm.Start(State.A); Assert.IsTrue(eventCount == 0); fsm.TransitionTo(State.B); Assert.IsTrue(eventCount == 1); fsm.TransitionTo(State.C); Assert.IsTrue(eventCount == 1); }
public void TestEnterExitTransitionEvents() { int aEnterCount = 0; int aExitCount = 0; State?aExitToState = null; int bEnterCount = 0; State?bEnterFromState = null; FiniteStateMachine <State> fsm = new FiniteStateMachine <State>(); fsm.AddTransition(State.A, State.B); fsm.AddTransition(State.B, State.C); fsm.AddEventOnEnter(State.A, (from, to) => { Assert.IsTrue(to == State.A); aEnterCount++; }); fsm.AddEventOnExit(State.A, (from, to) => { Assert.IsTrue(from == State.A); aExitToState = to; aExitCount++; }); fsm.AddEventOnEnter(State.B, (from, to) => { bEnterFromState = from; Assert.IsTrue(to == State.B); bEnterCount++; }); // Start does not trigger enter event fsm.Start(State.A); Assert.IsTrue(aEnterCount == 0); Assert.IsTrue(aExitCount == 0); Assert.IsTrue(bEnterCount == 0); fsm.TransitionTo(State.B); Assert.IsTrue(aEnterCount == 0); Assert.IsTrue(aExitCount == 1); Assert.IsTrue(aExitToState == State.B); Assert.IsTrue(bEnterCount == 1); Assert.IsTrue(bEnterFromState == State.A); }
public override void Start() { base.Start(); if (Entity.Transform.Parent != null) { throw new ArgumentException("DroneRocketController must be root"); } alertZoneTrigger = AlertZoneTriggerEntity.Get <RigidbodyComponent>(); stateMachine = new FiniteStateMachine("Drone"); stateMachine.RegisterState(new State(StartState) { EnterMethod = StartEnter, UpdateMethod = () => { stateMachine.SwitchTo(PatrolState.Name); } }); stateMachine.RegisterState(new PatrolState() { EnterMethod = PatrolEnter, UpdateMethod = PatrolUpdate }); stateMachine.RegisterState(new ChaseState() { EnterMethod = ChaseStart, UpdateMethod = ChaseUpdate }); stateMachine.Start(Script, StartState); // Exit state machine when drone died Drone.Died += drone => stateMachine.Exit(); Drone.AlertedChanged += DroneOnAlertedChanged; UpdateAlertZoneRadius(); spawnLocation = Entity.Transform.WorldMatrix.TranslationVector; spawnOrientation = new Tuple <float, float>(Drone.BodyRotation, Drone.HeadRotation); }
void Start() { _fsm.Start(); _collectableSet.Add(this); }
void Start() => _fsm.Start();
private void InitializeLowerStateMachine() { lowerStateMachine = new FiniteStateMachine("SoldierAnimationLower"); Action <State, string, string> startIdleOrRun = (from, newAnimNameLow, newAnimNameAim) => { // Select the proper animation to play lowerAnimation = NewAnimation(HasAdditiveAnimation ? newAnimNameAim : newAnimNameLow); if (from != null) { // Blend in if needed lowerAnimation.Weight = 0.0f; lowerAnimation.WeightTarget = 1.0f; lowerAnimation.CrossfadeRemainingTime = TimeSpan.FromSeconds(0.2); } // Add the animation InsertLowerAnimation(0, lowerAnimation); }; Action updateLowerIdleOrRun = () => { // Swap between the low animation (used if no additive animation) and the aim animation (used only for additive animation) if (HasAdditiveAnimation) { SwapLowerAnimation(AnimKeys.IdleLow, AnimKeys.IdleAim, false); SwapLowerAnimation(AnimKeys.RunLow, AnimKeys.RunAim, false); } else { SwapLowerAnimation(AnimKeys.IdleAim, AnimKeys.IdleLow, true); SwapLowerAnimation(AnimKeys.RunAim, AnimKeys.RunLow, true); } }; Action <State> exitLowerIdleOrRun = to => { // Blend away the remaining animation lowerAnimation.WeightTarget = 0.0f; lowerAnimation.CrossfadeRemainingTime = TimeSpan.FromSeconds(0.2); }; Action <State> startLowerWalk = from => { // Add all the animations used for walking, enable only Idle at start AddLowerAnimation(NewAnimation(AnimKeys.IdleAim)); AddLowerAnimation(NewAnimation(AnimKeys.WalkForward, 0.0f)); AddLowerAnimation(NewAnimation(AnimKeys.WalkBackward, 0.0f)); AddLowerAnimation(NewAnimation(AnimKeys.WalkLeft, 0.0f)); AddLowerAnimation(NewAnimation(AnimKeys.WalkRight, 0.0f)); }; Action updateLowerWalk = () => { var dot = Vector3.Dot(moveDirection, aimDirection); string lowerAnimToPlay; var forwardThreshold = Math.Cos(MathUtil.DegreesToRadians(WalkAngleThreshold.X)); var backwardThreshold = Math.Cos(MathUtil.DegreesToRadians(WalkAngleThreshold.Y)); var crossfadeFactor = Game.UpdateTime.Elapsed.TotalSeconds / WalkCrossfadeDuration; // Find out which anim should be played if (moveDirection == Vector3.Zero) { lowerAnimToPlay = AnimKeys.IdleAim; } else if (dot > forwardThreshold) { lowerAnimToPlay = AnimKeys.WalkForward; } else if (dot < backwardThreshold) { lowerAnimToPlay = AnimKeys.WalkBackward; } else { var cross = Vector3.Cross(aimDirection, moveDirection); lowerAnimToPlay = cross.Y <= 0 ? AnimKeys.WalkLeft : AnimKeys.WalkRight; } foreach (var anim in AnimationComponent.PlayingAnimations.Take(LowerAnimCount)) { if (anim.Name == lowerAnimToPlay) { anim.Weight = (float)Math.Min(anim.Weight + crossfadeFactor, 1.0f); } else { anim.Weight = (float)Math.Max(anim.Weight - crossfadeFactor, 0.0f); } } }; Action <State> exitWalk = to => { PlayingAnimation crossfadeAnim = null; var maxWeight = float.MinValue; foreach (var anim in AnimationComponent.PlayingAnimations.Take(LowerAnimCount).ToList()) { if (anim.Weight > maxWeight) { crossfadeAnim = anim; maxWeight = anim.Weight; } // Remove all walking animation... AnimationComponent.PlayingAnimations.Remove(anim); } // ... except the current one that will be blended away if (crossfadeAnim != null) { InsertLowerAnimation(0, crossfadeAnim); crossfadeAnim.WeightTarget = 0.0f; crossfadeAnim.CrossfadeRemainingTime = TimeSpan.FromSeconds(0.2); } }; var idleState = new State(LowerIdleState) { EnterMethod = State.ToTask(from => startIdleOrRun(from, AnimKeys.IdleLow, AnimKeys.IdleAim)), UpdateMethod = updateLowerIdleOrRun, ExitMethod = State.ToTask(to => exitLowerIdleOrRun(to)) }; var runState = new State(LowerRunState) { EnterMethod = State.ToTask(from => startIdleOrRun(from, AnimKeys.RunLow, AnimKeys.RunAim)), UpdateMethod = updateLowerIdleOrRun, ExitMethod = State.ToTask(to => exitLowerIdleOrRun(to)) }; var walkState = new State(LowerWalkState) { EnterMethod = State.ToTask(startLowerWalk), UpdateMethod = updateLowerWalk, ExitMethod = State.ToTask(to => exitWalk(to)) }; lowerStateMachine.RegisterState(idleState); lowerStateMachine.RegisterState(runState); lowerStateMachine.RegisterState(walkState); lowerStateMachine.Start(Script, LowerIdleState); }
public override void Start() { if (Entity.Transform.Parent != null) { throw new ArgumentException("SoldierController must be root"); } if (Camera == null) { throw new ArgumentException("Camera is not set"); } if (AnimationComponent == null) { throw new ArgumentException("AnimationComponent is not set"); } if (CameraController == null) { throw new ArgumentException("CameraController is not set"); } if (DroneCrosshair == null) { throw new ArgumentException("DroneCrosshair is not set"); } if (RotationSpeed < 0 || RotationSpeed > 1) { throw new ArgumentException("Rotation Speed must be between 0 and 1"); } //this.GetSimulation().ColliderShapesRendering = true; // Enable UI IStarbreach ipbrGame = Game as IStarbreach; //Entity statusBarEntity = ipbrGame.PlayerUiEntity.FindChild("StatusBar"); //uiComponent = statusBarEntity.Get<UIComponent>(); //uiComponent.Enabled = true; stateMachine = new FiniteStateMachine("SoldierController"); stateMachine.RegisterState(new State(IdleState) { UpdateMethod = UpdateIdle }); stateMachine.RegisterState(new State(RunState) { UpdateMethod = UpdateRun }); stateMachine.RegisterState(new State(WalkState) { EnterMethod = StartWalk, UpdateMethod = UpdateWalk }); stateMachine.Start(Script, IdleState); Instance = this; character = Entity.Get <CharacterComponent>(); sphereCastOrigin = Entity.FindChild("SphereCastOrigin"); AudioEmitterComponent emitter = Entity.Get <AudioEmitterComponent>(); // Load 3 different being hit sounds hitSounds = new AudioEmitterSoundController[3]; for (int i = 0; i < 3; i++) { hitSounds[i] = emitter["Hit" + i]; hitSounds[i].IsLooping = false; } deathSound = emitter["Death"]; }