BowCamera.cs 14 KB

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