BowCamera.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. {
  11. get
  12. {
  13. if (!_cameraComp) _cameraComp = GetComponent<Camera>();
  14. return _cameraComp;
  15. }
  16. }
  17. //控制的手臂弓
  18. ArmBow _armBow;
  19. ArmBow armBow
  20. {
  21. get
  22. {
  23. if (!_armBow) _armBow = GetComponentInChildren<ArmBow>();
  24. return _armBow;
  25. }
  26. }
  27. //本地欧拉角记录值
  28. Vector3 localEulerAngles;
  29. //触摸检测器
  30. JC.Unity.TouchChecker touchChecker = new JC.Unity.TouchChecker();
  31. //触摸模式开关
  32. private static bool _isTouchMode = true;
  33. public static bool isTouchMode
  34. {
  35. get
  36. {
  37. if (CommonConfig.isReleaseVersion) return false;
  38. return _isTouchMode;
  39. }
  40. set
  41. {
  42. _isTouchMode = value;
  43. }
  44. }
  45. //左右转动范围限制
  46. float[] limitRangeRotateY = { -80, 80 };
  47. //上下转动范围限制
  48. float[] limitRangeRotateX = { -80, 80 };
  49. //禁止逻辑,只用于同步状态和渲染,目前用于联机
  50. [NonSerialized] public bool banLogic = false;
  51. private static BowCamera _ins;
  52. public static BowCamera ins
  53. {
  54. get
  55. {
  56. if (!_ins)
  57. {
  58. _ins = GameObject.FindObjectOfType<BowCamera>();
  59. }
  60. return _ins;
  61. }
  62. }
  63. void Awake()
  64. {
  65. _ins = this;
  66. localEulerAngles = transform.localEulerAngles;
  67. RecordDefaultCameraFieldOfView();
  68. if (UserSettings.ins.bowCameraFixed && !CommonConfig.isReleaseVersion)
  69. {
  70. bowCameraFixed = new BowCameraFixed(this);
  71. }
  72. }
  73. void Start()
  74. {
  75. touchChecker.onMoved += delegate (Touch t, bool isOnUI)
  76. {
  77. if (banLogic) return;
  78. if (isOnUI) return;
  79. //触摸控制镜头和拉弓射箭
  80. this.localEulerAngles.x = Mathf.Clamp(this.localEulerAngles.x - t.deltaPosition.y * Time.deltaTime * 5, limitRangeRotateX[0], limitRangeRotateX[1]);
  81. this.localEulerAngles.y = Mathf.Clamp(this.localEulerAngles.y + t.deltaPosition.x * Time.deltaTime * 5, limitRangeRotateY[0], limitRangeRotateY[1]);
  82. this.transform.localEulerAngles = this.localEulerAngles;
  83. };
  84. touchChecker.onEnded += delegate (Touch t, bool isOnUI)
  85. {
  86. if (banLogic) return;
  87. if (!isOnUI) armBow.ADS_fire();
  88. };
  89. }
  90. void OnDestroy()
  91. {
  92. if (_ins == this) _ins = null;
  93. }
  94. void Update()
  95. {
  96. //更新相机视野
  97. if (cameraComp && !banCameraFieldOfView)
  98. {
  99. cameraComp.fieldOfView = cameraFieldOfView;
  100. }
  101. if (banLogic) return;
  102. //满足以下条件则阻止控制输入
  103. if (GameMgr.ins.gameOver)
  104. {
  105. return;
  106. }
  107. if (GameMgr.debugInEditor)
  108. {
  109. //鼠标控制镜头和拉弓射箭
  110. this.localEulerAngles.x = Mathf.Clamp(this.localEulerAngles.x - 2f * Input.GetAxis("Mouse Y"), limitRangeRotateX[0], limitRangeRotateX[1]);
  111. this.localEulerAngles.y = Mathf.Clamp(this.localEulerAngles.y + 2f * Input.GetAxis("Mouse X"), limitRangeRotateY[0], limitRangeRotateY[1]);
  112. this.transform.localEulerAngles = this.localEulerAngles;
  113. if (EventSystem.current.IsPointerOverGameObject()) return;
  114. }
  115. else if (isTouchMode)
  116. {
  117. touchChecker.Update();
  118. }
  119. else
  120. {
  121. if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked) return;
  122. //需要-镜头看向九轴姿态虚拟节点
  123. needLookAtPoint = true;
  124. }
  125. }
  126. //需要-镜头看向九轴姿态虚拟节点?
  127. bool needLookAtPoint = false;
  128. //镜头旋转转换比值
  129. float[] _bowRotateConvertRate = null;
  130. float bowRotateConvertRate
  131. {
  132. get
  133. {
  134. if (_bowRotateConvertRate == null)
  135. {
  136. _bowRotateConvertRate = new float[] { UserSettings.ins.bowRotateConvert.GetRate() };
  137. }
  138. return _bowRotateConvertRate[0];
  139. }
  140. }
  141. void LateUpdate()
  142. {
  143. #if UNITY_STANDALONE_WIN
  144. if (BleUDP.ins && BleUDP.ins.bluetoothStatusEnum == BluetoothStatusEnum.ConnectSuccess)
  145. {
  146. needLookAtPoint = true;
  147. }
  148. #endif
  149. if (needLookAtPoint)
  150. {
  151. needLookAtPoint = false;
  152. //镜头看向九轴姿态虚拟节点
  153. this.transform.LookAt(CameraToLook.ins.point);
  154. // if (BowQuatDebug.ins) BowQuatDebug.ins.ShowRealBowQuat(this.transform.localEulerAngles);
  155. if (!CommonConfig.isReleaseVersion)
  156. {
  157. //镜头旋转比值转换
  158. Vector3 localAngles = this.transform.localEulerAngles;
  159. localAngles.x = Mathf.Clamp((localAngles.x > 180 ? localAngles.x - 360 : localAngles.x) * bowRotateConvertRate,
  160. limitRangeRotateX[0], limitRangeRotateX[1]);
  161. localAngles.y = Mathf.Clamp((localAngles.y > 180 ? localAngles.y - 360 : localAngles.y) * bowRotateConvertRate,
  162. limitRangeRotateY[0], limitRangeRotateY[1]);
  163. if (bowCameraFixed != null)
  164. {
  165. localAngles = bowCameraFixed.LimitBowAngle(localAngles);
  166. }
  167. this.transform.localEulerAngles = localAngles;
  168. // if (BowQuatDebug.ins) BowQuatDebug.ins.ShowGameBowQuat(this.transform.localEulerAngles);
  169. }
  170. }
  171. onAfterLateUpdate?.Invoke();
  172. }
  173. [NonSerialized] public Action onAfterLateUpdate;
  174. //---------------相机视野的相关操作---------------------
  175. //视野值记录
  176. float cameraFieldOfView = 60;
  177. [NonSerialized] public float defaultCameraFieldOfView = 60;
  178. private void RecordDefaultCameraFieldOfView()
  179. {
  180. defaultCameraFieldOfView = cameraComp.fieldOfView;
  181. cameraFieldOfView = defaultCameraFieldOfView;
  182. }
  183. //禁止相机视野改变
  184. [NonSerialized] public bool banCameraFieldOfView = false;
  185. public void SetCameraFieldOfView(float value)
  186. {
  187. cameraComp.fieldOfView = value;
  188. }
  189. public void SetCameraFieldOfViewRecord(float value)
  190. {
  191. cameraFieldOfView = value;
  192. }
  193. //拉弓时的相机视野值变化
  194. public void updateFollowPullBow()
  195. {
  196. // if (cameraFieldOfView > 40) {
  197. // cameraFieldOfView -= 20 * Time.deltaTime;
  198. // } else {
  199. // cameraFieldOfView = 40;
  200. // }
  201. }
  202. //松开拉弓时的相机视野值变化
  203. public void updateGiveUpPullBow()
  204. {
  205. // if (cameraFieldOfView < 60) {
  206. // cameraFieldOfView += 20 * Time.deltaTime;
  207. // } else {
  208. // cameraFieldOfView = 60;
  209. // }
  210. }
  211. // 2022-04-28
  212. // ------ 添加固定镜头选项后,新增的API ------
  213. bool isArrowFollowing = false;
  214. public void SetArrowFollowing(bool value)
  215. {
  216. isArrowFollowing = value;
  217. cameraComp.enabled = !isArrowFollowing;
  218. AutoSwitchCamera();
  219. }
  220. bool isScaleAimDisplaying = false;
  221. public void SetScaleAimDisplaying(bool value)
  222. {
  223. isScaleAimDisplaying = value;
  224. AutoSwitchCamera();
  225. }
  226. void AutoSwitchCamera()
  227. {
  228. if (bowCameraFixed == null)
  229. {
  230. cameraComp.enabled = !isArrowFollowing;
  231. }
  232. else
  233. {
  234. bowCameraFixed.gameObject.SetActive(!isScaleAimDisplaying && !isArrowFollowing);
  235. cameraComp.enabled = isScaleAimDisplaying && !isArrowFollowing;
  236. }
  237. }
  238. public Camera GetRenderCamera()
  239. {
  240. if (bowCameraFixed == null)
  241. {
  242. return cameraComp;
  243. }
  244. else
  245. {
  246. if (bowCameraFixed.gameObject.activeSelf)
  247. {
  248. return bowCameraFixed.camera;
  249. }
  250. else
  251. {
  252. return cameraComp;
  253. }
  254. }
  255. }
  256. public BowCameraFixed bowCameraFixed = null;
  257. public class BowCameraFixed
  258. {
  259. public GameObject gameObject;
  260. public Transform transform;
  261. public Camera camera;
  262. private UnityStandardAssets.ImageEffects.Blur blur;
  263. //bowCameraComponent
  264. BowCamera bowCamera;
  265. private Camera targetCamera;
  266. private UnityStandardAssets.ImageEffects.Blur targetBlur;
  267. public BowCameraFixed(BowCamera bowCamera)
  268. {
  269. this.bowCamera = bowCamera;
  270. gameObject = new GameObject("BowCameraFixed");
  271. transform = gameObject.transform;
  272. //复制Camera组件
  273. targetCamera = bowCamera.cameraComp;
  274. camera = gameObject.AddComponent<Camera>();
  275. camera.tag = "MainCamera";
  276. camera.clearFlags = targetCamera.clearFlags;
  277. camera.backgroundColor = targetCamera.backgroundColor;
  278. camera.cullingMask = targetCamera.cullingMask;
  279. camera.fieldOfView = targetCamera.fieldOfView;
  280. camera.nearClipPlane = targetCamera.nearClipPlane;
  281. camera.depth = targetCamera.depth + 0.1f;
  282. camera.renderingPath = targetCamera.renderingPath;
  283. //复制Blur组件
  284. targetBlur = bowCamera.GetComponent<UnityStandardAssets.ImageEffects.Blur>();
  285. if (targetBlur)
  286. {
  287. blur = gameObject.AddComponent<UnityStandardAssets.ImageEffects.Blur>();
  288. blur.enabled = targetBlur.enabled;
  289. blur.blurShader = targetBlur.blurShader;
  290. blur.blurSpread = targetBlur.blurSpread;
  291. blur.iterations = targetBlur.iterations;
  292. }
  293. //设置Transform属性
  294. transform.parent = bowCamera.transform.parent;
  295. transform.localPosition = bowCamera.transform.localPosition;
  296. transform.localScale = bowCamera.transform.localScale;
  297. transform.localRotation = Quaternion.identity;
  298. //监听和驱动LateUpdate
  299. bowCamera.onAfterLateUpdate += LateUpdate;
  300. //切换镜头
  301. bowCamera.AutoSwitchCamera();
  302. InitForLimitBound();
  303. }
  304. void LateUpdate()
  305. {
  306. if (gameObject.activeSelf)
  307. {
  308. CrossHair.ins.UpdatePostionWhenFixedCamera();
  309. }
  310. if (blur)
  311. {
  312. blur.enabled = targetBlur.enabled;
  313. if (blur.enabled)
  314. {
  315. blur.blurShader = targetBlur.blurShader;
  316. blur.blurSpread = targetBlur.blurSpread;
  317. blur.iterations = targetBlur.iterations;
  318. }
  319. }
  320. }
  321. //边界限制
  322. float[] rangeRotateY = { -80, 80 };
  323. float rangeRotateX = 25;
  324. Vector3 vecF;
  325. Vector3 vecU;
  326. public int outBoundIndex = -1; //-1为未出界
  327. void InitForLimitBound()
  328. {
  329. for (int i = (int)rangeRotateY[0]; i < 0; i++)
  330. {
  331. Vector3 pos = transform.position + Quaternion.AngleAxis(i, Vector3.up) * transform.forward;
  332. pos = camera.WorldToViewportPoint(pos);
  333. if (pos.x >= 0)
  334. {
  335. rangeRotateY[0] = i;
  336. rangeRotateY[1] = -i - 4;
  337. break;
  338. }
  339. }
  340. rangeRotateX = camera.fieldOfView / 2;
  341. vecF = Quaternion.AngleAxis(rangeRotateX, Vector3.right) * Vector3.forward;
  342. vecU = Quaternion.AngleAxis(rangeRotateX, Vector3.right) * Vector3.up;
  343. }
  344. public Vector3 LimitBowAngle(Vector3 outAngle)
  345. {
  346. float angleY = outAngle.y;
  347. float angleX = outAngle.x;
  348. outAngle.y = Mathf.Clamp(angleY, rangeRotateY[0], rangeRotateY[1]);
  349. Vector3 vec = Quaternion.AngleAxis(outAngle.y, vecU) * vecF;
  350. float rx = (float)(Math.Asin(vec.y) / Math.PI * 180) * (angleX < 0 ? 1 : -1);
  351. if (angleY < rangeRotateY[0]) outBoundIndex = 0;
  352. else if (angleY > rangeRotateY[1]) outBoundIndex = 1;
  353. else if (angleX < -rangeRotateX) outBoundIndex = 2;
  354. else if (angleX > rangeRotateX) outBoundIndex = 3;
  355. else outBoundIndex = -1;
  356. if (Mathf.Abs(angleX) > Mathf.Abs(rx))
  357. {
  358. outAngle.x = rx;
  359. }
  360. //因为Vector3是struct,传递给函数是值传递而非引用传递
  361. return outAngle;
  362. }
  363. }
  364. }