void Update() { //flip the character's x-scale to face left or right transform.localScale = new Vector3(facingRight ? startXScale : -startXScale, transform.localScale.y, transform.localScale.z); //fetch this frame's input from the input component //instructions.Clear(); //inputComponent.GetInput(ref instructions); CharacterInstructions thisFrameInstructions = instructionsBuffer.Last.Value; instructionsBuffer.RemoveLast(); thisFrameInstructions.Clear(); inputComponent.GetInput(ref thisFrameInstructions); instructionsBuffer.AddFirst(thisFrameInstructions); bool actionFound = false; foreach(CharacterInstructions i in instructionsBuffer) { if (i.actedOn) { break; } else { if (i.actionInstructions != CharacterInstructions.ActionInstructions.none) { instructions = i; actionFound = true; break; } } } if (!actionFound) { instructions = instructionsBuffer.First.Value; } //send this frame's input to the current state object //if this frame's input doesn't cause the character to change state, HandleInput returns null ICharacterState nextState = currentState.HandleInput(instructions); //if nextState is not null, we are switching states this frame. Call OnExit on the old state and OnEnter on the new state if (nextState != null) { currentState.OnExit(); currentState = nextState; currentState.OnEnter(this); } //now that we know for certain what state we're in this frame, call this state's Update function currentState.Update(); if (Mathf.Abs(rigidbody.velocity.x) > 0.1f) { animator.SetBool(walkingHash, true); } else { animator.SetBool(walkingHash, false); } }
public virtual void ToState(Character character, ICharacterState state) { character.STATE.OnExit(character); character.STATE = state; state.OnEnter(character); }
//********************************* // VVVVV MONOBEHAVIOUR EVENTS VVVVV //********************************* void Start() { rigidbody = GetComponent<Rigidbody2D>(); animator = GetComponent<Animator>(); upAttackTrigger = Animator.StringToHash("UpAttack"); sideUpAttackTrigger = Animator.StringToHash("SideUpAttack"); sideAttackTrigger = Animator.StringToHash("SideAttack"); sideDownAttackTrigger = Animator.StringToHash("SideDownAttack"); downAttackTrigger = Animator.StringToHash("DownAttack"); airJumpTrigger = Animator.StringToHash("AirJump"); groundedHash = Animator.StringToHash("Grounded"); walkingHash = Animator.StringToHash("Walking"); stunnedHash = Animator.StringToHash("Stunned"); startXScale = transform.localScale.x; attacks = new Dictionary<Direction, Attack>(); attacks.Add(Direction.Up, upAttack); attacks.Add(Direction.RightUp, sideUpAttack); attacks.Add(Direction.Right, sideAttack); attacks.Add(Direction.RightDown, sideDownAttack); attacks.Add(Direction.Down, downAttack); attacks.Add(Direction.LeftDown, sideDownAttack); attacks.Add(Direction.Left, sideAttack); attacks.Add(Direction.LeftUp, sideUpAttack); currentState = new Idle(); currentState.OnEnter(this); inputComponent = GetComponent(typeof(IGetInput)) as IGetInput; instructions = new CharacterInstructions(); instructionsBuffer = new LinkedList<CharacterInstructions>(); for(int i = 0; i < inputBufferLength; i++) { instructionsBuffer.AddFirst(new CharacterInstructions()); } grounded = false; stunned = false; rigidbody.gravityScale = gravityScale; }