BowCamera.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. //需要-镜头看向九轴姿态虚拟节点?
  94. bool needLookAtPoint = false;
  95. //镜头旋转转换比值
  96. float[] _bowRotateConvertRate = null;
  97. float bowRotateConvertRate {
  98. get {
  99. if (_bowRotateConvertRate == null) {
  100. _bowRotateConvertRate = new float[]{ UserSettings.ins.bowRotateConvert.GetRate() };
  101. }
  102. return _bowRotateConvertRate[0];
  103. }
  104. }
  105. void LateUpdate() {
  106. if (needLookAtPoint) {
  107. needLookAtPoint = false;
  108. //镜头看向九轴姿态虚拟节点
  109. this.transform.LookAt(CameraToLook.ins.point);
  110. if (!CommonConfig.isReleaseVersion) {
  111. //镜头旋转比值转换
  112. Vector3 localEulerAngles = this.transform.localEulerAngles;
  113. float angleY = localEulerAngles.y;
  114. if (angleY < 180) {
  115. angleY = Mathf.Clamp(angleY * bowRotateConvertRate, 0, 180);
  116. } else {
  117. angleY = Mathf.Clamp((angleY - 360) * bowRotateConvertRate, -180, 0);
  118. }
  119. localEulerAngles.y = angleY;
  120. this.transform.localEulerAngles = localEulerAngles;
  121. }
  122. }
  123. }
  124. //---------------相机视野的相关操作---------------------
  125. //视野值记录
  126. float cameraFieldOfView = 60;
  127. //禁止相机视野改变
  128. [NonSerialized] public bool banCameraFieldOfView = false;
  129. public void SetCameraFieldOfView(float value) {
  130. cameraComp.fieldOfView = value;
  131. }
  132. public void SetCameraFieldOfViewRecord(float value) {
  133. cameraFieldOfView = value;
  134. }
  135. //拉弓时的相机视野值变化
  136. public void updateFollowPullBow() {
  137. if (cameraFieldOfView > 40) {
  138. cameraFieldOfView -= 20 * Time.deltaTime;
  139. } else {
  140. cameraFieldOfView = 40;
  141. }
  142. }
  143. //松开拉弓时的相机视野值变化
  144. public void updateGiveUpPullBow() {
  145. if (cameraFieldOfView < 60) {
  146. cameraFieldOfView += 20 * Time.deltaTime;
  147. } else {
  148. cameraFieldOfView = 60;
  149. }
  150. }
  151. }