BowCamera.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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 (InfraredDemo.DebugInEditor) needLookAtPoint = true;
  150. #endif
  151. if (needLookAtPoint)
  152. {
  153. needLookAtPoint = false;
  154. //镜头看向九轴姿态虚拟节点
  155. this.transform.LookAt(CameraToLook.ins.point);
  156. // if (BowQuatDebug.ins) BowQuatDebug.ins.ShowRealBowQuat(this.transform.localEulerAngles);
  157. if (!CommonConfig.isReleaseVersion)
  158. {
  159. //镜头旋转比值转换
  160. Vector3 localAngles = this.transform.localEulerAngles;
  161. localAngles.x = Mathf.Clamp((localAngles.x > 180 ? localAngles.x - 360 : localAngles.x) * bowRotateConvertRate,
  162. limitRangeRotateX[0], limitRangeRotateX[1]);
  163. localAngles.y = Mathf.Clamp((localAngles.y > 180 ? localAngles.y - 360 : localAngles.y) * bowRotateConvertRate,
  164. limitRangeRotateY[0], limitRangeRotateY[1]);
  165. if (bowCameraFixed != null)
  166. {
  167. localAngles = bowCameraFixed.LimitBowAngle(localAngles);
  168. }
  169. this.transform.localEulerAngles = localAngles;
  170. // if (BowQuatDebug.ins) BowQuatDebug.ins.ShowGameBowQuat(this.transform.localEulerAngles);
  171. }
  172. }
  173. onAfterLateUpdate?.Invoke();
  174. }
  175. [NonSerialized] public Action onAfterLateUpdate;
  176. //---------------相机视野的相关操作---------------------
  177. //视野值记录
  178. float cameraFieldOfView = 60;
  179. [NonSerialized] public float defaultCameraFieldOfView = 60;
  180. private void RecordDefaultCameraFieldOfView()
  181. {
  182. defaultCameraFieldOfView = cameraComp.fieldOfView;
  183. cameraFieldOfView = defaultCameraFieldOfView;
  184. }
  185. //禁止相机视野改变
  186. [NonSerialized] public bool banCameraFieldOfView = false;
  187. public void SetCameraFieldOfView(float value)
  188. {
  189. cameraComp.fieldOfView = value;
  190. }
  191. public void SetCameraFieldOfViewRecord(float value)
  192. {
  193. cameraFieldOfView = value;
  194. }
  195. //拉弓时的相机视野值变化
  196. public void updateFollowPullBow()
  197. {
  198. // if (cameraFieldOfView > 40) {
  199. // cameraFieldOfView -= 20 * Time.deltaTime;
  200. // } else {
  201. // cameraFieldOfView = 40;
  202. // }
  203. }
  204. //松开拉弓时的相机视野值变化
  205. public void updateGiveUpPullBow()
  206. {
  207. // if (cameraFieldOfView < 60) {
  208. // cameraFieldOfView += 20 * Time.deltaTime;
  209. // } else {
  210. // cameraFieldOfView = 60;
  211. // }
  212. }
  213. // 2022-04-28
  214. // ------ 添加固定镜头选项后,新增的API ------
  215. bool isArrowFollowing = false;
  216. public void SetArrowFollowing(bool value)
  217. {
  218. isArrowFollowing = value;
  219. cameraComp.enabled = !isArrowFollowing;
  220. AutoSwitchCamera();
  221. }
  222. bool isScaleAimDisplaying = false;
  223. public void SetScaleAimDisplaying(bool value)
  224. {
  225. isScaleAimDisplaying = value;
  226. AutoSwitchCamera();
  227. }
  228. void AutoSwitchCamera()
  229. {
  230. if (bowCameraFixed == null)
  231. {
  232. cameraComp.enabled = !isArrowFollowing;
  233. }
  234. else
  235. {
  236. bowCameraFixed.gameObject.SetActive(!isScaleAimDisplaying && !isArrowFollowing);
  237. cameraComp.enabled = isScaleAimDisplaying && !isArrowFollowing;
  238. }
  239. }
  240. public Camera GetRenderCamera()
  241. {
  242. if (bowCameraFixed == null)
  243. {
  244. return cameraComp;
  245. }
  246. else
  247. {
  248. if (bowCameraFixed.gameObject.activeSelf)
  249. {
  250. return bowCameraFixed.camera;
  251. }
  252. else
  253. {
  254. return cameraComp;
  255. }
  256. }
  257. }
  258. public BowCameraFixed bowCameraFixed = null;
  259. public class BowCameraFixed
  260. {
  261. public GameObject gameObject;
  262. public Transform transform;
  263. public Camera camera;
  264. private UnityStandardAssets.ImageEffects.Blur blur;
  265. //bowCameraComponent
  266. BowCamera bowCamera;
  267. private Camera targetCamera;
  268. private UnityStandardAssets.ImageEffects.Blur targetBlur;
  269. public BowCameraFixed(BowCamera bowCamera)
  270. {
  271. this.bowCamera = bowCamera;
  272. gameObject = new GameObject("BowCameraFixed");
  273. transform = gameObject.transform;
  274. //复制Camera组件
  275. targetCamera = bowCamera.cameraComp;
  276. camera = gameObject.AddComponent<Camera>();
  277. camera.tag = "MainCamera";
  278. camera.clearFlags = targetCamera.clearFlags;
  279. camera.backgroundColor = targetCamera.backgroundColor;
  280. camera.cullingMask = targetCamera.cullingMask;
  281. camera.fieldOfView = targetCamera.fieldOfView;
  282. camera.nearClipPlane = targetCamera.nearClipPlane;
  283. camera.depth = targetCamera.depth + 0.1f;
  284. camera.renderingPath = targetCamera.renderingPath;
  285. //复制Blur组件
  286. targetBlur = bowCamera.GetComponent<UnityStandardAssets.ImageEffects.Blur>();
  287. if (targetBlur)
  288. {
  289. blur = gameObject.AddComponent<UnityStandardAssets.ImageEffects.Blur>();
  290. blur.enabled = targetBlur.enabled;
  291. blur.blurShader = targetBlur.blurShader;
  292. blur.blurSpread = targetBlur.blurSpread;
  293. blur.iterations = targetBlur.iterations;
  294. }
  295. //设置Transform属性
  296. transform.parent = bowCamera.transform.parent;
  297. transform.localPosition = bowCamera.transform.localPosition;
  298. transform.localScale = bowCamera.transform.localScale;
  299. transform.localRotation = Quaternion.identity;
  300. //监听和驱动LateUpdate
  301. bowCamera.onAfterLateUpdate += LateUpdate;
  302. //切换镜头
  303. bowCamera.AutoSwitchCamera();
  304. InitForLimitBound();
  305. }
  306. void LateUpdate()
  307. {
  308. if (gameObject.activeSelf)
  309. {
  310. CrossHair.ins.UpdatePostionWhenFixedCamera();
  311. }
  312. if (blur)
  313. {
  314. blur.enabled = targetBlur.enabled;
  315. if (blur.enabled)
  316. {
  317. blur.blurShader = targetBlur.blurShader;
  318. blur.blurSpread = targetBlur.blurSpread;
  319. blur.iterations = targetBlur.iterations;
  320. }
  321. }
  322. }
  323. //边界限制
  324. float[] rangeRotateY = { -80, 80 };
  325. float rangeRotateX = 25;
  326. Vector3 vecF;
  327. Vector3 vecU;
  328. public int outBoundIndex = -1; //-1为未出界
  329. void InitForLimitBound()
  330. {
  331. int _leftNum = 4;//以前的设置
  332. //如果是红外设备。不限制旋转
  333. if (BluetoothAim.ins && (BluetoothAim.ins.isMainConnectToInfraredDevice() || BluetoothAim.ins.isMainConnectToGun()))
  334. {
  335. //// 如果不启用边界限制,则直接设置默认的范围
  336. //rangeRotateY = new float[] { -180, 180 }; // 设置默认的无边界限制值(根据需求调整)
  337. //rangeRotateX = camera.fieldOfView / 2;
  338. //vecF = Quaternion.AngleAxis(rangeRotateX, Vector3.right) * Vector3.forward;
  339. //vecU = Quaternion.AngleAxis(rangeRotateX, Vector3.right) * Vector3.up;
  340. _leftNum = 0;
  341. }
  342. for (int i = (int)rangeRotateY[0]; i < 0; i++)
  343. {
  344. Vector3 pos = transform.position + Quaternion.AngleAxis(i, Vector3.up) * transform.forward;
  345. pos = camera.WorldToViewportPoint(pos);
  346. if (pos.x >= 0)
  347. {
  348. rangeRotateY[0] = i;
  349. rangeRotateY[1] = -i - _leftNum;
  350. break;
  351. }
  352. }
  353. rangeRotateX = camera.fieldOfView / 2;
  354. vecF = Quaternion.AngleAxis(rangeRotateX, Vector3.right) * Vector3.forward;
  355. vecU = Quaternion.AngleAxis(rangeRotateX, Vector3.right) * Vector3.up;
  356. }
  357. public Vector3 LimitBowAngle(Vector3 outAngle)
  358. {
  359. float angleY = outAngle.y;
  360. float angleX = outAngle.x;
  361. outAngle.y = Mathf.Clamp(angleY, rangeRotateY[0], rangeRotateY[1]);
  362. Vector3 vec = Quaternion.AngleAxis(outAngle.y, vecU) * vecF;
  363. float rx = (float)(Math.Asin(vec.y) / Math.PI * 180) * (angleX < 0 ? 1 : -1);
  364. if (angleY < rangeRotateY[0]) outBoundIndex = 0;
  365. else if (angleY > rangeRotateY[1]) outBoundIndex = 1;
  366. else if (angleX < -rangeRotateX) outBoundIndex = 2;
  367. else if (angleX > rangeRotateX) outBoundIndex = 3;
  368. else outBoundIndex = -1;
  369. if (Mathf.Abs(angleX) > Mathf.Abs(rx))
  370. {
  371. outAngle.x = rx;
  372. }
  373. //因为Vector3是struct,传递给函数是值传递而非引用传递
  374. return outAngle;
  375. }
  376. }
  377. }