public ICharacterState HandleInput(CharacterInstructions instructions) { if (exit || animator.GetCurrentAnimatorStateInfo(0).shortNameHash == idleHash) { return new Idle(); } else if (character.stunned) { return new Stunned(); } return null; }
public void GetInput(ref CharacterInstructions instructions) { float L_X = Input.GetAxis("L_XAxis_" + character.playerNo); float L_Y = Input.GetAxis("L_YAxis_" + character.playerNo); leftStickInput = new Vector2(L_X, L_Y); float R_X = Input.GetAxis("R_XAxis_" + character.playerNo); float R_Y = Input.GetAxis("R_YAxis_" + character.playerNo); rightStickInput = new Vector2(R_X, R_Y); //If left stick is pressed this frame if (leftStickInput.sqrMagnitude >= stickInputThreshold * stickInputThreshold) { leftStickPressed = true; leftStickDown = false; leftStickDirection = DirectionUtility.GetDirection(leftStickInput); //If left stick down this frame if (!leftStickPressedCache) { leftStickDown = true; } } else { leftStickPressed = false; //If left stick up this frame if (leftStickPressedCache) { leftStickDirection = Direction.Neutral; } } //If right stick is pressed this frame if (rightStickInput.sqrMagnitude >= stickInputThreshold * stickInputThreshold) { rightStickPressed = true; rightStickDown = false; rightStickDirection = DirectionUtility.GetDirection(rightStickInput); //If right stick down this frame if (!rightStickPressedCache) { rightStickDown = true; } } else { rightStickPressed = false; //If right stick up this frame if (rightStickPressedCache) { rightStickDirection = Direction.Neutral; } } leftStickPressedCache = leftStickPressed; rightStickPressedCache = rightStickPressed; //if the right stick is down this frame, set this frame's action instructions to attacking if(rightStickDown) { instructions.actionInstructions = CharacterInstructions.ActionInstructions.attack; instructions.direction = rightStickDirection; } else if(character.grounded) { if(leftStickInput.y > ControllerThresholds.JumpThreshold) { if (!jumpSwitch) { instructions.actionInstructions = CharacterInstructions.ActionInstructions.jump; jumpSwitch = true; } } else { jumpSwitch = false; } if(leftStickInput.x > ControllerThresholds.WalkThreshold) { instructions.moveInstructions = CharacterInstructions.MoveInstructions.right; } else if(leftStickInput.x < -ControllerThresholds.WalkThreshold) { instructions.moveInstructions = CharacterInstructions.MoveInstructions.left; } else { instructions.moveInstructions = CharacterInstructions.MoveInstructions.neutral; } } else { if (leftStickDown) { instructions.actionInstructions = CharacterInstructions.ActionInstructions.dash; instructions.direction = leftStickDirection; } } /* bool tryToJump = false; switch (leftStickDirection) { case Direction.Neutral: instructions.moveInstructions = CharacterInstructions.MoveInstructions.neutral; break; case Direction.Up: tryToJump = true; instructions.moveInstructions = CharacterInstructions.MoveInstructions.neutral; break; case Direction.RightUp: tryToJump = true; instructions.moveInstructions = CharacterInstructions.MoveInstructions.right; break; case Direction.Right: instructions.moveInstructions = CharacterInstructions.MoveInstructions.right; break; case Direction.RightDown: instructions.moveInstructions = CharacterInstructions.MoveInstructions.right; break; case Direction.Down: instructions.moveInstructions = CharacterInstructions.MoveInstructions.neutral; break; case Direction.LeftDown: instructions.moveInstructions = CharacterInstructions.MoveInstructions.left; break; case Direction.Left: instructions.moveInstructions = CharacterInstructions.MoveInstructions.left; break; case Direction.LeftUp: tryToJump = true; instructions.moveInstructions = CharacterInstructions.MoveInstructions.left; break; } if(!character.grounded && leftStickDown) { instructions.actionInstructions = CharacterInstructions.ActionInstructions.dash; instructions.direction = leftStickDirection; instructions.moveInstructions = CharacterInstructions.MoveInstructions.neutral; } //if the left stick is down this frame and the direction is up and the action this frame is not attacking else if (tryToJump) { if (!jumpSwitch && instructions.actionInstructions == CharacterInstructions.ActionInstructions.none) { jumpSwitch = true; instructions.actionInstructions = CharacterInstructions.ActionInstructions.jump; } } else { jumpSwitch = false; } */ }
public void GetInput(ref CharacterInstructions instructions) { }
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); } }
//********************************* // 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; }
public ICharacterState HandleInput(CharacterInstructions instructions) { if (!character.stunned) { return new Idle(); } return null; }
public ICharacterState HandleInput(CharacterInstructions instructions) { if (character.stunned) { return new Stunned(); } if(instructions.actionInstructions == CharacterInstructions.ActionInstructions.attack) { instructions.actedOn = true; return new Attacking(instructions.direction); } else if(instructions.actionInstructions == CharacterInstructions.ActionInstructions.jump && character.grounded) { instructions.actedOn = true; jumpThisFrame = true; } else { jumpThisFrame = false; if(instructions.actionInstructions == CharacterInstructions.ActionInstructions.dash) { instructions.actedOn = true; return new Dashing(instructions.direction); } } if (instructions.moveInstructions == CharacterInstructions.MoveInstructions.right) { character.facingRight = true; } else if (instructions.moveInstructions == CharacterInstructions.MoveInstructions.left) { character.facingRight = false; } return null; }
public ICharacterState HandleInput(CharacterInstructions instructions) { if (character.stunned) { return new Stunned(); } if(currentState == State.end) { return new Idle(); } return null; }