BowCamera.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.EventSystems;
  4. /* 弓的相机 */
  5. public class BowCamera : MonoBehaviour
  6. {
  7. //相机组件
  8. Camera _cameraComp;
  9. Camera cameraComp {
  10. get {
  11. if (!_cameraComp) _cameraComp = GetComponent<Camera>();
  12. return _cameraComp;
  13. }
  14. }
  15. //控制的手臂弓
  16. ArmBow _armBow;
  17. ArmBow armBow {
  18. get {
  19. if (!_armBow) _armBow = GetComponentInChildren<ArmBow>();
  20. return _armBow;
  21. }
  22. }
  23. //本地欧拉角记录值
  24. Vector3 localEulerAngles;
  25. //触摸检测器
  26. JC.Unity.TouchChecker touchChecker = new JC.Unity.TouchChecker();
  27. //触摸模式开关
  28. public static bool isTouchMode = true;
  29. //禁止弓镜头被用户输入控制
  30. [NonSerialized] public bool banUserControl = false;
  31. public static BowCamera ins;
  32. void Awake() {
  33. ins = this;
  34. localEulerAngles = transform.localEulerAngles;
  35. }
  36. void Start() {
  37. touchChecker.onMoved += delegate(Touch t, bool isOnUI) {
  38. if (banUserControl) return;
  39. if (isOnUI) return;
  40. //触摸控制镜头和拉弓射箭
  41. this.localEulerAngles.x = Mathf.Clamp(this.localEulerAngles.x - t.deltaPosition.y * Time.deltaTime * 5, -36, 36);
  42. this.localEulerAngles.y = Mathf.Clamp(this.localEulerAngles.y + t.deltaPosition.x * Time.deltaTime * 5, -180, 180);
  43. this.transform.localEulerAngles = this.localEulerAngles;
  44. };
  45. touchChecker.onEnded += delegate(Touch t, bool isOnUI) {
  46. if (banUserControl) return;
  47. if (!isOnUI) armBow.ADS_fire();
  48. };
  49. }
  50. void Update()
  51. {
  52. //更新相机视野
  53. if (cameraComp && !banCameraFieldOfView) {
  54. cameraComp.fieldOfView = cameraFieldOfView;
  55. }
  56. if (banUserControl) return;
  57. //满足以下条件则阻止控制输入
  58. if (GameMgr.ins.gameOver) {
  59. return;
  60. }
  61. if (GameMgr.debugInEditor) {
  62. //鼠标控制镜头和拉弓射箭
  63. this.localEulerAngles.x = Mathf.Clamp(this.localEulerAngles.x - 2f * Input.GetAxis("Mouse Y"), -36, 36);
  64. this.localEulerAngles.y = Mathf.Clamp(this.localEulerAngles.y + 2f * Input.GetAxis("Mouse X"), -180, 180);
  65. this.transform.localEulerAngles = this.localEulerAngles;
  66. if (EventSystem.current.IsPointerOverGameObject()) return;
  67. }
  68. else if (isTouchMode) {
  69. touchChecker.Update();
  70. } else {
  71. //镜头看向九轴姿态虚拟节点
  72. this.transform.LookAt(CameraToLook.ins.point);
  73. }
  74. }
  75. //---------------相机视野的相关操作---------------------
  76. //视野值记录
  77. float cameraFieldOfView = 60;
  78. //禁止相机视野改变
  79. [NonSerialized] public bool banCameraFieldOfView = false;
  80. public void SetCameraFieldOfView(float value) {
  81. cameraComp.fieldOfView = value;
  82. }
  83. public void SetCameraFieldOfViewRecord(float value) {
  84. cameraFieldOfView = value;
  85. }
  86. //拉弓时的相机视野值变化
  87. public void updateFollowPullBow() {
  88. if (cameraFieldOfView > 40) {
  89. cameraFieldOfView -= 20 * Time.deltaTime;
  90. } else {
  91. cameraFieldOfView = 40;
  92. }
  93. }
  94. //松开拉弓时的相机视野值变化
  95. public void updateGiveUpPullBow() {
  96. if (cameraFieldOfView < 60) {
  97. cameraFieldOfView += 20 * Time.deltaTime;
  98. } else {
  99. cameraFieldOfView = 60;
  100. }
  101. }
  102. }