//https://gist.github.com/jbroadway/b94b971d224332f9158988a66f35f22d //https://answers.unity.com/questions/546668/how-to-create-coroutine-delegates.html void Start() { if (PRINT_METHOD_CALL) { Debug.Log("Start"); } CachePlatforms(); StateMachine = new StateMachine(this); Logger = new Logger(); LineRender = GetComponent <LineRenderer>(); LineRender.enabled = false; // Since our update methods are all of type void, pass them as delegates // Pass coroutines as illustrated in the second link UDelegateFn dNormalUpdate = NormalUpdate, dNormalBegin = NormalBegin, dNormalEnd = NormalEnd; System.Action l_normalCoroutine = () => StartCoroutine(NormalCoroutine()); StateMachine.SetCallbacks(StNormal, dNormalUpdate, l_normalCoroutine, dNormalBegin, dNormalEnd); UDelegateFn dClimbUpdate = ClimbUpdate, dClimbBegin = ClimbBegin, dClimbEnd = ClimbEnd; System.Action l_climbCoroutine = () => StartCoroutine(ClimbCoroutine()); StateMachine.SetCallbacks(StClimb, dClimbUpdate, l_climbCoroutine, dClimbBegin, dClimbEnd); UDelegateFn dDashUpdate = DashUpdate, dDashBegin = DashBegin, dDashEnd = DashEnd; System.Action l_dashCoroutine = () => StartCoroutine(DashCoroutine()); StateMachine.SetCallbacks(StDash, DashUpdate, l_dashCoroutine, DashBegin, DashEnd); UDelegateFn dSwingUpdate = SwingUpdate, dSwingBegin = SwingBegin, dSwingEnd = SwingEnd; System.Action l_swingCoroutine = () => StartCoroutine(SwingCoroutine()); StateMachine.SetCallbacks(StSwing, SwingUpdate, l_swingCoroutine, SwingBegin, SwingEnd); UDelegateFn dSlideUpdate = SlideUpdate, dSlideBegin = SlideBegin, dSlideEnd = SlideEnd; System.Action l_slideCoroutine = () => StartCoroutine(SlideCoroutine()); StateMachine.SetCallbacks(StSlide, dSlideUpdate, l_slideCoroutine, dSlideBegin, dSlideEnd); // Set up our input class instance Input_ = new Input_(); // Freeze rotation on the player rb.freezeRotation = true; StateMachine.State = StNormal; Animator = GetComponent <Animator>(); }
public void Update() { // If we've changed state, then end the previous state and begin the next state if (State != PrevState) { // End the previous state if (callbacks[PrevState].end != null) { callbacks[PrevState].end(); } // Update the state PrevState = State; // Start the new state UDelegateFn begin = callbacks[State].begin; if (begin != null) { begin(); } // If we have a coroutine, start it System.Action coroutine = callbacks[State].coroutine; if (coroutine != null) { coroutine(); } } // Execute the update function of the current state UDelegateFn update = callbacks[State].update; if (update != null) { update(); } }
// Add the callback functions for a new state to our callbacks dictioanry public void SetCallbacks(int i, UDelegateFn update, System.Action coroutine, UDelegateFn begin, UDelegateFn end) { callbacks[i] = new StateStruct(update, coroutine, begin, end); }
public StateStruct(UDelegateFn a, System.Action b, UDelegateFn c, UDelegateFn d) { this.update = a; this.coroutine = b; this.begin = c; this.end = d; }