BowCamera.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. RecordDefaultCameraFieldOfView();
  53. if (UserSettings.ins.bowCameraFixed && !CommonConfig.isReleaseVersion) {
  54. bowCameraFixed = new BowCameraFixed(this);
  55. }
  56. }
  57. void Start() {
  58. touchChecker.onMoved += delegate(Touch t, bool isOnUI) {
  59. if (banLogic) return;
  60. if (isOnUI) return;
  61. //触摸控制镜头和拉弓射箭
  62. this.localEulerAngles.x = Mathf.Clamp(this.localEulerAngles.x - t.deltaPosition.y * Time.deltaTime * 5, -80, 80);
  63. this.localEulerAngles.y = Mathf.Clamp(this.localEulerAngles.y + t.deltaPosition.x * Time.deltaTime * 5, -90, 90);
  64. this.transform.localEulerAngles = this.localEulerAngles;
  65. };
  66. touchChecker.onEnded += delegate(Touch t, bool isOnUI) {
  67. if (banLogic) return;
  68. if (!isOnUI) armBow.ADS_fire();
  69. };
  70. }
  71. void Update()
  72. {
  73. //更新相机视野
  74. if (cameraComp && !banCameraFieldOfView) {
  75. cameraComp.fieldOfView = cameraFieldOfView;
  76. }
  77. if (banLogic) return;
  78. //满足以下条件则阻止控制输入
  79. if (GameMgr.ins.gameOver) {
  80. return;
  81. }
  82. if (GameMgr.debugInEditor) {
  83. //鼠标控制镜头和拉弓射箭
  84. this.localEulerAngles.x = Mathf.Clamp(this.localEulerAngles.x - 2f * Input.GetAxis("Mouse Y"), -80, 80);
  85. this.localEulerAngles.y = Mathf.Clamp(this.localEulerAngles.y + 2f * Input.GetAxis("Mouse X"), -90, 90);
  86. this.transform.localEulerAngles = this.localEulerAngles;
  87. if (EventSystem.current.IsPointerOverGameObject()) return;
  88. }
  89. else if (isTouchMode) {
  90. touchChecker.Update();
  91. } else {
  92. if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked) return;
  93. //需要-镜头看向九轴姿态虚拟节点
  94. needLookAtPoint = true;
  95. }
  96. }
  97. //需要-镜头看向九轴姿态虚拟节点?
  98. bool needLookAtPoint = false;
  99. //镜头旋转转换比值
  100. float[] _bowRotateConvertRate = null;
  101. float bowRotateConvertRate {
  102. get {
  103. if (_bowRotateConvertRate == null) {
  104. _bowRotateConvertRate = new float[]{ UserSettings.ins.bowRotateConvert.GetRate() };
  105. }
  106. return _bowRotateConvertRate[0];
  107. }
  108. }
  109. void LateUpdate() {
  110. if (needLookAtPoint) {
  111. needLookAtPoint = false;
  112. //镜头看向九轴姿态虚拟节点
  113. this.transform.LookAt(CameraToLook.ins.point);
  114. if (!CommonConfig.isReleaseVersion) {
  115. //镜头旋转比值转换
  116. Vector3 localEulerAngles = this.transform.localEulerAngles;
  117. float angleY = localEulerAngles.y;
  118. if (angleY < 180) {
  119. angleY = Mathf.Clamp(angleY * bowRotateConvertRate, 0, 90);
  120. } else {
  121. angleY = Mathf.Clamp((angleY - 360) * bowRotateConvertRate, -90, 0);
  122. }
  123. localEulerAngles.y = angleY;
  124. this.transform.localEulerAngles = localEulerAngles;
  125. }
  126. }
  127. onAfterLateUpdate?.Invoke();
  128. }
  129. Action onAfterLateUpdate;
  130. //---------------相机视野的相关操作---------------------
  131. //视野值记录
  132. float cameraFieldOfView = 60;
  133. [NonSerialized] public float defaultCameraFieldOfView = 60;
  134. private void RecordDefaultCameraFieldOfView() {
  135. defaultCameraFieldOfView = cameraComp.fieldOfView;
  136. cameraFieldOfView = defaultCameraFieldOfView;
  137. }
  138. //禁止相机视野改变
  139. [NonSerialized] public bool banCameraFieldOfView = false;
  140. public void SetCameraFieldOfView(float value) {
  141. cameraComp.fieldOfView = value;
  142. }
  143. public void SetCameraFieldOfViewRecord(float value) {
  144. cameraFieldOfView = value;
  145. }
  146. //拉弓时的相机视野值变化
  147. public void updateFollowPullBow() {
  148. // if (cameraFieldOfView > 40) {
  149. // cameraFieldOfView -= 20 * Time.deltaTime;
  150. // } else {
  151. // cameraFieldOfView = 40;
  152. // }
  153. }
  154. //松开拉弓时的相机视野值变化
  155. public void updateGiveUpPullBow() {
  156. // if (cameraFieldOfView < 60) {
  157. // cameraFieldOfView += 20 * Time.deltaTime;
  158. // } else {
  159. // cameraFieldOfView = 60;
  160. // }
  161. }
  162. // 2022-04-28
  163. // ------ 添加固定镜头选项后,新增的API ------
  164. bool isArrowFollowing = false;
  165. public void SetArrowFollowing(bool value) {
  166. isArrowFollowing = value;
  167. cameraComp.enabled = !isArrowFollowing;
  168. AutoSwitchCamera();
  169. }
  170. bool isScaleAimDisplaying = false;
  171. public void SetScaleAimDisplaying(bool value) {
  172. isScaleAimDisplaying = value;
  173. AutoSwitchCamera();
  174. }
  175. void AutoSwitchCamera() {
  176. if (bowCameraFixed == null) {
  177. cameraComp.enabled = !isArrowFollowing;
  178. } else {
  179. bowCameraFixed.gameObject.SetActive(!isScaleAimDisplaying && !isArrowFollowing);
  180. cameraComp.enabled = isScaleAimDisplaying && !isArrowFollowing;
  181. }
  182. }
  183. public Camera GetRenderCamera() {
  184. if (bowCameraFixed == null) {
  185. return cameraComp;
  186. } else {
  187. if (bowCameraFixed.gameObject.activeSelf) {
  188. return bowCameraFixed.camera;
  189. } else {
  190. return cameraComp;
  191. }
  192. }
  193. }
  194. BowCameraFixed bowCameraFixed = null;
  195. public class BowCameraFixed {
  196. public GameObject gameObject;
  197. public Transform transform;
  198. public Camera camera;
  199. private UnityStandardAssets.ImageEffects.Blur blur;
  200. //bowCameraComponent
  201. BowCamera bowCamera;
  202. private Camera targetCamera;
  203. private UnityStandardAssets.ImageEffects.Blur targetBlur;
  204. public BowCameraFixed(BowCamera bowCamera) {
  205. this.bowCamera = bowCamera;
  206. gameObject = new GameObject("BowCameraFixed");
  207. transform = gameObject.transform;
  208. //复制Camera组件
  209. targetCamera = bowCamera.cameraComp;
  210. camera = gameObject.AddComponent<Camera>();
  211. camera.tag = "MainCamera";
  212. camera.clearFlags = targetCamera.clearFlags;
  213. camera.backgroundColor = targetCamera.backgroundColor;
  214. camera.cullingMask = targetCamera.cullingMask;
  215. camera.fieldOfView = targetCamera.fieldOfView;
  216. camera.nearClipPlane = targetCamera.nearClipPlane;
  217. camera.depth = targetCamera.depth + 0.1f;
  218. camera.renderingPath = targetCamera.renderingPath;
  219. //复制Blur组件
  220. targetBlur = bowCamera.GetComponent<UnityStandardAssets.ImageEffects.Blur>();
  221. if (targetBlur) {
  222. blur = gameObject.AddComponent<UnityStandardAssets.ImageEffects.Blur>();
  223. blur.enabled = targetBlur.enabled;
  224. blur.blurShader = targetBlur.blurShader;
  225. blur.blurSpread = targetBlur.blurSpread;
  226. blur.iterations = targetBlur.iterations;
  227. }
  228. //设置Transform属性
  229. transform.parent = bowCamera.transform.parent;
  230. transform.localPosition = bowCamera.transform.localPosition;
  231. transform.localScale = bowCamera.transform.localScale;
  232. transform.localRotation = Quaternion.identity;
  233. //监听和驱动LateUpdate
  234. bowCamera.onAfterLateUpdate += LateUpdate;
  235. //切换镜头
  236. bowCamera.AutoSwitchCamera();
  237. }
  238. void LateUpdate() {
  239. if (gameObject.activeSelf) {
  240. CrossHair.ins.UpdatePostionWhenFixedCamera();
  241. }
  242. if (blur) {
  243. blur.enabled = targetBlur.enabled;
  244. if (blur.enabled) {
  245. blur.blurShader = targetBlur.blurShader;
  246. blur.blurSpread = targetBlur.blurSpread;
  247. blur.iterations = targetBlur.iterations;
  248. }
  249. }
  250. }
  251. }
  252. }