void inputCallBack(InputControl.KeyBoard key) { switch (key) { // 跳跃、进入爬梯子状态 case InputControl.KeyBoard.Down_W: { if (PlayerStateChangeEntity.getInstance().checkIsCanChange(playerState, Consts.PlayerState.climb)) { Transform ladder = RoadScript.s_instance.checkLadder(transform.localPosition); if (ladder) { if (playerState == Consts.PlayerState.idle) { if ((ladder.position.y + 50) > transform.position.y) { // transform.position = new Vector3(ladder.position.x, transform.position.y, 0); setState(Consts.PlayerState.climb); break; } } } } if (PlayerStateChangeEntity.getInstance().checkIsCanChange(playerState, Consts.PlayerState.jump)) { if (playerState == Consts.PlayerState.crouch) { setState(Consts.PlayerState.idle); } else { if (curJumpCount < jumpComboCount) { setState(Consts.PlayerState.jump); } } } break; } // 向上爬梯子 case InputControl.KeyBoard.Keep_W: { if (playerState == Consts.PlayerState.climb) { changePlayerPos(0, climbSpeed); Transform ladder = RoadScript.s_instance.checkLadder(transform.localPosition); if (!ladder) { setState(Consts.PlayerState.idle); } } break; } // 暂停向上爬梯子 case InputControl.KeyBoard.Up_W: { if (playerState == Consts.PlayerState.climb) { FrameAnimationUtil.getInstance().stopAnimation(self_img); } break; } // 左右移动 case InputControl.KeyBoard.Keep_A: case InputControl.KeyBoard.Keep_D: { Consts.MoveDirection direction = key == InputControl.KeyBoard.Keep_A ? Consts.MoveDirection.left : Consts.MoveDirection.right; Consts.PlayerState _playerState = key == InputControl.KeyBoard.Keep_A ? Consts.PlayerState.run_left : Consts.PlayerState.run_right; if (PlayerStateChangeEntity.getInstance().checkIsCanChange(playerState, _playerState)) { if (playerState == Consts.PlayerState.jump) { setMoveDirection(direction); move(false); } else if (playerState == Consts.PlayerState.drop) { setMoveDirection(direction); move(false); } else if (playerState == Consts.PlayerState.crouch) { setMoveDirection(direction); //move(false); } else { setState(_playerState); } } break; } // 停止左右移动 case InputControl.KeyBoard.Up_A: case InputControl.KeyBoard.Up_D: { Consts.PlayerState _playerState = key == InputControl.KeyBoard.Up_A ? Consts.PlayerState.run_left : Consts.PlayerState.run_right; if (PlayerStateChangeEntity.getInstance().checkIsCanChange(playerState, Consts.PlayerState.idle)) { if (playerState == _playerState) { setState(Consts.PlayerState.idle); } } break; } // 蹲下、进入爬梯子状态 case InputControl.KeyBoard.Down_S: { if (PlayerStateChangeEntity.getInstance().checkIsCanChange(playerState, Consts.PlayerState.climb)) { Transform ladder = RoadScript.s_instance.checkLadder(transform.localPosition); if (ladder) { if (playerState == Consts.PlayerState.idle) { if ((ladder.position.y + 50) < transform.position.y) { // transform.position = new Vector3(ladder.position.x, transform.position.y, 0); setState(Consts.PlayerState.climb); break; } } } } if (PlayerStateChangeEntity.getInstance().checkIsCanChange(playerState, Consts.PlayerState.crouch)) { setState(Consts.PlayerState.crouch); } break; } // 往下爬梯子 case InputControl.KeyBoard.Keep_S: { if (playerState == Consts.PlayerState.climb) { changePlayerPos(0, -climbSpeed); Transform ladder = RoadScript.s_instance.checkLadder(transform.localPosition); if (!ladder) { setState(Consts.PlayerState.idle); } } break; } // 暂停向下爬梯子 case InputControl.KeyBoard.Up_S: { if (playerState == Consts.PlayerState.climb) { FrameAnimationUtil.getInstance().stopAnimation(self_img); } break; } // 闪现 case InputControl.KeyBoard.Down_N: { if (PlayerStateChangeEntity.getInstance().checkIsCanChange(playerState, Consts.PlayerState.sprint)) { setState(Consts.PlayerState.sprint); } break; } // 射击 case InputControl.KeyBoard.Down_M: { if (PlayerStateChangeEntity.getInstance().checkIsCanChange(playerState, Consts.PlayerState.shoot)) { switch (playerState) { case Consts.PlayerState.jump: case Consts.PlayerState.drop: case Consts.PlayerState.crouch: { BulletScript.Create(transform.parent, moveDirection); break; } default: { setState(Consts.PlayerState.shoot); break; } } } break; } } }
public void setState(Consts.PlayerState _playerState) { playerState = _playerState; switch (playerState) { case Consts.PlayerState.crouch: { collider2D_stand.enabled = false; collider2D_crouch.enabled = true; collider2D_jump.enabled = false; break; } case Consts.PlayerState.jump: case Consts.PlayerState.drop: { collider2D_stand.enabled = false; collider2D_crouch.enabled = false; collider2D_jump.enabled = true; break; } default: { collider2D_stand.enabled = true; collider2D_crouch.enabled = false; collider2D_jump.enabled = false; break; } } switch (_playerState) { // 站立 case Consts.PlayerState.idle: { curJumpCount = 0; FrameAnimationUtil.getInstance().startAnimation(self_img, "Sprites/player/idle-", FrameAnimationUtil.FrameAnimationSpeed.low); break; } // 左跑 case Consts.PlayerState.run_left: { setMoveDirection(Consts.MoveDirection.left); transform.localScale = new Vector3(-playerScale, playerScale, playerScale); FrameAnimationUtil.getInstance().startAnimation(self_img, "Sprites/player/run-", FrameAnimationUtil.FrameAnimationSpeed.quick); break; } // 右跑 case Consts.PlayerState.run_right: { setMoveDirection(Consts.MoveDirection.right); transform.localScale = new Vector3(playerScale, playerScale, playerScale); FrameAnimationUtil.getInstance().startAnimation(self_img, "Sprites/player/run-", FrameAnimationUtil.FrameAnimationSpeed.quick); break; } // 跳跃 case Consts.PlayerState.jump: { ++curJumpCount; curJumpPower = jumpPower; FrameAnimationUtil.getInstance().startAnimation(self_img, "Sprites/player/jump-", FrameAnimationUtil.FrameAnimationSpeed.normal, false); break; } // 爬楼梯 case Consts.PlayerState.climb: { FrameAnimationUtil.getInstance().startAnimation(self_img, "Sprites/player/climb-", FrameAnimationUtil.FrameAnimationSpeed.low); break; } // 蹲下 case Consts.PlayerState.crouch: { FrameAnimationUtil.getInstance().startAnimation(self_img, "Sprites/player/crouch-", FrameAnimationUtil.FrameAnimationSpeed.low); break; } // 被攻击 case Consts.PlayerState.hurt: { FrameAnimationUtil.getInstance().startAnimation(self_img, "Sprites/player/hurt-", FrameAnimationUtil.FrameAnimationSpeed.low, false, () => { setState(Consts.PlayerState.idle); }); break; } // 射击 case Consts.PlayerState.shoot: { BulletScript.Create(transform.parent, moveDirection); FrameAnimationUtil.getInstance().startAnimation(self_img, "Sprites/player/shoot-", FrameAnimationUtil.FrameAnimationSpeed.low, false, () => { setState(Consts.PlayerState.idle); }); break; } // 闪现 case Consts.PlayerState.sprint: { FrameAnimationUtil.getInstance().startAnimation(self_img, "Sprites/player/sprint-", FrameAnimationUtil.FrameAnimationSpeed.low); // 位移 { self_img.color = new Color(1, 1, 1, 0.3f); float targetX = transform.position.x; if (moveDirection == Consts.MoveDirection.left) { targetX = transform.position.x - 200; } else if (moveDirection == Consts.MoveDirection.right) { targetX = transform.position.x + 200; } transform.GetComponent <RectTransform>().DOMoveX(targetX, 0.3f, false).OnComplete(() => { self_img.color = new Color(1, 1, 1, 1); setState(Consts.PlayerState.idle); // 如果闪现后不在屏幕内,则把坐标强制改到屏幕内 if (!checkIsInScreen()) { if (moveDirection == Consts.MoveDirection.left) { transform.position = new Vector3(width / 2, transform.position.y, 0); } else if (moveDirection == Consts.MoveDirection.right) { transform.position = new Vector3((Screen.width - width / 2), transform.position.y, 0); } } }); } break; } // 降落 case Consts.PlayerState.drop: { curJumpPower = 0; playerState = Consts.PlayerState.drop; FrameAnimationUtil.getInstance().startAnimation(self_img, "Sprites/player/drop-", FrameAnimationUtil.FrameAnimationSpeed.low); break; } } }
void inputCallBack(Consts.PlayerState _playerState) { if (PlayerStateChangeEntity.getInstance().checkIsCanChange(playerState, _playerState)) { switch (_playerState) { // 静止 case Consts.PlayerState.idle: { setState(Consts.PlayerState.idle); break; } // 跳跃 case Consts.PlayerState.jump: { if (playerState == Consts.PlayerState.crouch) { setState(Consts.PlayerState.idle); } else { setState(Consts.PlayerState.jump); } break; } // 左移、右移 case Consts.PlayerState.run_left: case Consts.PlayerState.run_right: { Consts.MoveDirection direction = _playerState == Consts.PlayerState.run_left ? Consts.MoveDirection.left : Consts.MoveDirection.right; if (playerState == Consts.PlayerState.jump) { setMoveDirection(direction); move(false); } else if (playerState == Consts.PlayerState.drop) { setMoveDirection(direction); move(false); } else if (playerState == Consts.PlayerState.crouch) { // setMoveDirection(direction); // 蹲下时只移动方向,不进行位移 // move(false); setState(_playerState); } else { setState(_playerState); } break; } // 蹲下 case Consts.PlayerState.crouch: { setState(Consts.PlayerState.crouch); break; } // 开枪 case Consts.PlayerState.shoot: { switch (playerState) { case Consts.PlayerState.jump: case Consts.PlayerState.drop: case Consts.PlayerState.crouch: { BulletScript.Create(transform.parent, moveDirection); break; } default: { setState(Consts.PlayerState.shoot); break; } } break; } // 闪现 case Consts.PlayerState.sprint: { setState(Consts.PlayerState.sprint); break; } // 降落 case Consts.PlayerState.drop: { setState(Consts.PlayerState.drop); break; } // 停止左移 case Consts.PlayerState.stop_run_left: { if (playerState == Consts.PlayerState.run_left) { setState(Consts.PlayerState.idle); } break; } // 停止右移 case Consts.PlayerState.stop_run_right: { if (playerState == Consts.PlayerState.run_right) { setState(Consts.PlayerState.idle); } break; } } } }