BowCamera.cs 4.3 KB

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