BowCamera.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. if (CommonConfig.SpecialVersion1) {
  68. if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name.Equals("GameChallenge")) {
  69. // this.cameraComp.fieldOfView = 25;
  70. this.cameraComp.fieldOfView = UserSettings.ins.bowRotateConvert.GetFieldOfView();
  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
  150. if (BleUDP.ins && BleUDP.ins.bluetoothStatusEnum == BluetoothStatusEnum.ConnectSuccess)
  151. {
  152. needLookAtPoint = true;
  153. }
  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. for (int i = (int)rangeRotateY[0]; i < 0; i++)
  336. {
  337. Vector3 pos = transform.position + Quaternion.AngleAxis(i, Vector3.up) * transform.forward;
  338. pos = camera.WorldToViewportPoint(pos);
  339. if (pos.x >= 0)
  340. {
  341. rangeRotateY[0] = i;
  342. rangeRotateY[1] = -i - 4;
  343. break;
  344. }
  345. }
  346. rangeRotateX = camera.fieldOfView / 2;
  347. vecF = Quaternion.AngleAxis(rangeRotateX, Vector3.right) * Vector3.forward;
  348. vecU = Quaternion.AngleAxis(rangeRotateX, Vector3.right) * Vector3.up;
  349. }
  350. public Vector3 LimitBowAngle(Vector3 outAngle)
  351. {
  352. float angleY = outAngle.y;
  353. float angleX = outAngle.x;
  354. outAngle.y = Mathf.Clamp(angleY, rangeRotateY[0], rangeRotateY[1]);
  355. Vector3 vec = Quaternion.AngleAxis(outAngle.y, vecU) * vecF;
  356. float rx = (float)(Math.Asin(vec.y) / Math.PI * 180) * (angleX < 0 ? 1 : -1);
  357. if (angleY < rangeRotateY[0]) outBoundIndex = 0;
  358. else if (angleY > rangeRotateY[1]) outBoundIndex = 1;
  359. else if (angleX < -rangeRotateX) outBoundIndex = 2;
  360. else if (angleX > rangeRotateX) outBoundIndex = 3;
  361. else outBoundIndex = -1;
  362. if (Mathf.Abs(angleX) > Mathf.Abs(rx))
  363. {
  364. outAngle.x = rx;
  365. }
  366. //因为Vector3是struct,传递给函数是值传递而非引用传递
  367. return outAngle;
  368. }
  369. }
  370. }