BowCamera.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 banLogic = false;
  31. private static BowCamera _ins;
  32. public static BowCamera ins {
  33. get {
  34. if (!_ins) {
  35. _ins = GameObject.FindObjectOfType<BowCamera>();
  36. }
  37. return _ins;
  38. }
  39. }
  40. void Awake() {
  41. _ins = this;
  42. localEulerAngles = transform.localEulerAngles;
  43. }
  44. void Start() {
  45. touchChecker.onMoved += delegate(Touch t, bool isOnUI) {
  46. if (banLogic) return;
  47. if (isOnUI) return;
  48. //触摸控制镜头和拉弓射箭
  49. this.localEulerAngles.x = Mathf.Clamp(this.localEulerAngles.x - t.deltaPosition.y * Time.deltaTime * 5, -36, 36);
  50. this.localEulerAngles.y = Mathf.Clamp(this.localEulerAngles.y + t.deltaPosition.x * Time.deltaTime * 5, -180, 180);
  51. this.transform.localEulerAngles = this.localEulerAngles;
  52. };
  53. touchChecker.onEnded += delegate(Touch t, bool isOnUI) {
  54. if (banLogic) return;
  55. if (!isOnUI) armBow.ADS_fire();
  56. };
  57. }
  58. void Update()
  59. {
  60. //更新相机视野
  61. if (cameraComp && !banCameraFieldOfView) {
  62. cameraComp.fieldOfView = cameraFieldOfView;
  63. }
  64. if (banLogic) return;
  65. //满足以下条件则阻止控制输入
  66. if (GameMgr.ins.gameOver) {
  67. return;
  68. }
  69. if (GameMgr.debugInEditor) {
  70. //鼠标控制镜头和拉弓射箭
  71. this.localEulerAngles.x = Mathf.Clamp(this.localEulerAngles.x - 2f * Input.GetAxis("Mouse Y"), -36, 36);
  72. this.localEulerAngles.y = Mathf.Clamp(this.localEulerAngles.y + 2f * Input.GetAxis("Mouse X"), -180, 180);
  73. this.transform.localEulerAngles = this.localEulerAngles;
  74. if (EventSystem.current.IsPointerOverGameObject()) return;
  75. }
  76. else if (isTouchMode) {
  77. touchChecker.Update();
  78. } else {
  79. //镜头看向九轴姿态虚拟节点
  80. this.transform.LookAt(CameraToLook.ins.point);
  81. }
  82. }
  83. //---------------相机视野的相关操作---------------------
  84. //视野值记录
  85. float cameraFieldOfView = 60;
  86. //禁止相机视野改变
  87. [NonSerialized] public bool banCameraFieldOfView = false;
  88. public void SetCameraFieldOfView(float value) {
  89. cameraComp.fieldOfView = value;
  90. }
  91. public void SetCameraFieldOfViewRecord(float value) {
  92. cameraFieldOfView = value;
  93. }
  94. //拉弓时的相机视野值变化
  95. public void updateFollowPullBow() {
  96. if (cameraFieldOfView > 40) {
  97. cameraFieldOfView -= 20 * Time.deltaTime;
  98. } else {
  99. cameraFieldOfView = 40;
  100. }
  101. }
  102. //松开拉弓时的相机视野值变化
  103. public void updateGiveUpPullBow() {
  104. if (cameraFieldOfView < 60) {
  105. cameraFieldOfView += 20 * Time.deltaTime;
  106. } else {
  107. cameraFieldOfView = 60;
  108. }
  109. }
  110. }