BowCamera.cs 13 KB

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