BowCamera.cs 14 KB

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