public void Test1() { var walkState = new WalkState(3); var idleState = new IdleState(); var sm = new StateMachine <PlayerState, InputEvent>(idleState, new StateComparer()); sm.AddState(walkState); sm.AddState(idleState); sm.Start(); sm.HandleEvent(InputEvent.Move); for (int i = 0; i < 4; i++) { sm.Update(); } // Output: // Enter IdleState // Pause IdleState // Enter WalkState // Tick WalkState // Tick WalkState // Tick WalkState // Exit WalkState // Resume IdleState }
void Awake() { hungryRate += defaultHungryDrain; energyRate = 0f; EatState eat = new EatState(this); SleepState sleep = new SleepState(this); WorkState work = new WorkState(this); IdleState idle = new IdleState(this); WalkState walk = new WalkState(this); fsm = new StateMachine(); fsm.AddTransition(new Transition(idle, eat, idle.IsTranslateToEat)); fsm.AddTransition(new Transition(idle, sleep, idle.IsTranslateToSleep)); fsm.AddTransition(new Transition(idle, work, idle.IsTranslateToWork)); fsm.AddTransition(new Transition(walk, idle, walk.IsTranslateToIdle)); fsm.AddTransition(new Transition(eat, idle, eat.IsTranslateToIdle)); fsm.AddTransition(new Transition(eat, walk, IsTranslateToWalk)); fsm.AddTransition(new Transition(work, idle, work.IsTranslateToIdle)); fsm.AddTransition(new Transition(work, walk, IsTranslateToWalk)); fsm.AddTransition(new Transition(sleep, idle, sleep.IsTranslateToIdle)); fsm.AddTransition(new Transition(sleep, walk, IsTranslateToWalk)); // set default fsm.SetState(idle); }