BowCamera.cs 13 KB

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