PlayerController.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using Cinemachine;
  2. using DG.Tweening;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using UnityEngine;
  7. public class PlayerController : EntityBase {
  8. internal override bool isPressingCrouch() => InputM.GetKeyEvent(InputM.KeyType.Crouch);
  9. internal override bool isPressDownJump() => InputM.GetKeyEvent(InputM.KeyType.Jump);
  10. internal override bool isPressingLeft() => InputM.GetKeyEvent(InputM.KeyType.Left);
  11. internal override bool isPressingRight() => InputM.GetKeyEvent(InputM.KeyType.Right);
  12. internal override void OnHitBorder() {
  13. Hurt();
  14. transform.position = new Vector3(0, 8, 0);
  15. }
  16. public float knockbackForceH = 30f, knockbackForceV = 15f;
  17. internal override void OnHitEnemy(Collision2D collision) {
  18. Hurt();
  19. int face = collision.transform.position.x < transform.position.x ? 1 : -1;
  20. rb.velocity = new Vector2(face * knockbackForceH, knockbackForceV);
  21. }
  22. public void Hurt() {
  23. // 播放音效
  24. AudioM.Play("hurt");
  25. rb.velocity = Vector2.zero;
  26. Camera.current.DOShakePosition(0.3f, 0.3f, 20);
  27. }
  28. // 测试方法:按 Ctrl+R 生成骰子
  29. public GameObject dice;
  30. private void LateUpdate() {
  31. if (!Input.GetKey(KeyCode.LeftControl) || !Input.GetKeyUp(KeyCode.R)) return;
  32. // 生成测试骰子
  33. InputM.DropDice(Module.RandomOne(InputM.keyTypes.ToList()));
  34. }
  35. }