BowCameraDoublePlayer.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. using AppUI.Bluetooth;
  2. using BestHTTP.SignalR.Hubs;
  3. using System;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. /* 弓的相机 */
  7. public class BowCameraDoublePlayer : MonoBehaviour
  8. {
  9. //[Header("当前脚本属于的玩家是?")]
  10. [HideInInspector]
  11. public PlayerType playerType = PlayerType.FirstPlayer;
  12. AimCrossHair aimCrossHair;
  13. //相机组件
  14. Camera _cameraComp;
  15. Camera cameraComp
  16. {
  17. get
  18. {
  19. if (!_cameraComp) _cameraComp = GetComponent<Camera>();
  20. return _cameraComp;
  21. }
  22. }
  23. //控制的手臂弓
  24. ArmBowDoublePlayer _armBow;
  25. ArmBowDoublePlayer armBow
  26. {
  27. get
  28. {
  29. if (!_armBow) _armBow = GetComponentInChildren<ArmBowDoublePlayer>();
  30. return _armBow;
  31. }
  32. }
  33. //本地欧拉角记录值
  34. Vector3 localEulerAngles;
  35. //触摸检测器
  36. JCUnityLib.TouchChecker touchChecker = new JCUnityLib.TouchChecker();
  37. //触摸模式开关
  38. private bool _isTouchMode = true;
  39. public bool isTouchMode
  40. {
  41. get
  42. {
  43. if (CommonConfig.isReleaseVersion) return false;
  44. return _isTouchMode;
  45. }
  46. set
  47. {
  48. _isTouchMode = value;
  49. }
  50. }
  51. //左右转动范围限制
  52. float[] limitRangeRotateY = { -80, 80 };
  53. //上下转动范围限制
  54. float[] limitRangeRotateX = { -80, 80 };
  55. //禁止逻辑,只用于同步状态和渲染,目前用于联机
  56. [NonSerialized] public bool banLogic = false;
  57. void Awake()
  58. {
  59. localEulerAngles = transform.localEulerAngles;
  60. if (CommonConfig.SpecialVersion1)
  61. {
  62. if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name.Equals("GameChallenge"))
  63. {
  64. this.cameraComp.fieldOfView = UserSettings.ins.bowRotateConvert.fieldOfView;
  65. }
  66. }
  67. RecordDefaultCameraFieldOfView();
  68. }
  69. void Start()
  70. {
  71. touchChecker.onMoved += delegate (Touch t, bool isOnUI)
  72. {
  73. if (banLogic) return;
  74. if (isOnUI) return;
  75. //触摸控制镜头和拉弓射箭
  76. this.localEulerAngles.x = Mathf.Clamp(this.localEulerAngles.x - t.deltaPosition.y * Time.deltaTime * 5, limitRangeRotateX[0], limitRangeRotateX[1]);
  77. this.localEulerAngles.y = Mathf.Clamp(this.localEulerAngles.y + t.deltaPosition.x * Time.deltaTime * 5, limitRangeRotateY[0], limitRangeRotateY[1]);
  78. this.transform.localEulerAngles = this.localEulerAngles;
  79. };
  80. touchChecker.onEnded += delegate (Touch t, bool isOnUI)
  81. {
  82. if (banLogic) return;
  83. if (!isOnUI) armBow.ADS_fire();
  84. };
  85. //直接使用固定相机
  86. bowCameraFixed = new BowCameraFixed(this);
  87. //获取当前的playerType
  88. Debug.Log("BowCameraDoublePlayer PlayerType:" + playerType);
  89. aimCrossHair = GameController.ins.GetAimCrossHair(playerType);//GameController.ins.aimCrossHairs[playerType == PlayerType.FirstPlayer ? 0 : 1];
  90. //开始时候更新一次2P isTouchMode状态
  91. if (playerType== PlayerType.SecondPlayer && SmartBowDeviceHub.ins != null) {
  92. SmartBowSDK.BluetoothStatusEnum newStatus = SmartBowDeviceHub.ins.GetSdkStatus(BluetoothPlayer.SECOND_PLAYER);
  93. if (newStatus == SmartBowSDK.BluetoothStatusEnum.None)
  94. {
  95. isTouchMode = true;
  96. }else {
  97. isTouchMode = false;
  98. }
  99. Debug.Log("2P Start isTouchMode:"+ isTouchMode);
  100. }
  101. }
  102. void OnDestroy()
  103. {
  104. }
  105. void Update()
  106. {
  107. //1P,同步使用 BowCamera.isTouchMode
  108. if (playerType == PlayerType.FirstPlayer)
  109. {
  110. isTouchMode = BowCamera.isTouchMode;
  111. }
  112. //更新相机视野
  113. if (cameraComp && !banCameraFieldOfView)
  114. {
  115. cameraComp.fieldOfView = cameraFieldOfView;
  116. }
  117. //if (banLogic) return;
  118. // Debug.Log(GameController.ins.gameStart + " == " + GameController.ins.gameOver + " == " + (!GameController.ins.gameStart || GameController.ins.gameOver));
  119. //满足以下条件则阻止控制输入
  120. if (GameController.ins.gameOver)
  121. {
  122. return;
  123. }
  124. if (GameController.ins.debugInEditor)
  125. {
  126. //// 制作一条射线
  127. //Ray ray = bowCameraFixed.camera.ScreenPointToRay(aimCrossHair.transform.position); // 将屏幕点转换为射线
  128. //Debug.DrawRay(ray.origin, ray.direction * 1000, Color.red);
  129. //// 人物旋转
  130. //transform.LookAt(ray.GetPoint(1000));
  131. //return;
  132. //鼠标控制镜头和拉弓射箭
  133. this.localEulerAngles.x = Mathf.Clamp(this.localEulerAngles.x - 2f * Input.GetAxis("Mouse Y"), limitRangeRotateX[0], limitRangeRotateX[1]);
  134. this.localEulerAngles.y = Mathf.Clamp(this.localEulerAngles.y + 2f * Input.GetAxis("Mouse X"), limitRangeRotateY[0], limitRangeRotateY[1]);
  135. this.transform.localEulerAngles = this.localEulerAngles;
  136. if (EventSystem.current.IsPointerOverGameObject()) return;
  137. }
  138. else if (isTouchMode)
  139. {
  140. //Debug.Log("Start2 isTouchMode:" + isTouchMode);
  141. touchChecker.Update();
  142. }
  143. else
  144. {
  145. if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked) return;
  146. //需要-镜头看向九轴姿态虚拟节点
  147. needLookAtPoint = true;
  148. }
  149. }
  150. //需要-镜头看向九轴姿态虚拟节点?
  151. bool needLookAtPoint = false;
  152. //镜头旋转转换比值
  153. float[] _bowRotateConvertRate = null;
  154. float bowRotateConvertRate
  155. {
  156. get
  157. {
  158. if (_bowRotateConvertRate == null)
  159. {
  160. _bowRotateConvertRate = new float[] { UserSettings.ins.bowRotateConvert.GetRate() };
  161. }
  162. return _bowRotateConvertRate[0];
  163. }
  164. }
  165. void LateUpdate()
  166. {
  167. if (needLookAtPoint)
  168. {
  169. needLookAtPoint = false;
  170. //镜头看向九轴姿态虚拟节点
  171. if (playerType == PlayerType.FirstPlayer) {
  172. this.transform.LookAt(CameraToLook.ins.point);
  173. } else {
  174. this.transform.LookAt(CameraToLookNew.ins.point2P);
  175. }
  176. // if (BowQuatDebug.ins) BowQuatDebug.ins.ShowRealBowQuat(this.transform.localEulerAngles);
  177. if (!CommonConfig.isReleaseVersion)
  178. {
  179. //镜头旋转比值转换
  180. Vector3 localAngles = this.transform.localEulerAngles;
  181. localAngles.x = Mathf.Clamp((localAngles.x > 180 ? localAngles.x - 360 : localAngles.x) * bowRotateConvertRate,
  182. limitRangeRotateX[0], limitRangeRotateX[1]);
  183. localAngles.y = Mathf.Clamp((localAngles.y > 180 ? localAngles.y - 360 : localAngles.y) * bowRotateConvertRate,
  184. limitRangeRotateY[0], limitRangeRotateY[1]);
  185. if (bowCameraFixed != null)
  186. {
  187. localAngles = bowCameraFixed.LimitBowAngle(localAngles);
  188. }
  189. this.transform.localEulerAngles = localAngles;
  190. // if (BowQuatDebug.ins) BowQuatDebug.ins.ShowGameBowQuat(this.transform.localEulerAngles);
  191. }
  192. }
  193. onAfterLateUpdate?.Invoke();
  194. }
  195. [NonSerialized] public Action onAfterLateUpdate;
  196. //---------------相机视野的相关操作---------------------
  197. //视野值记录
  198. float cameraFieldOfView = 60;
  199. [NonSerialized] public float defaultCameraFieldOfView = 60;
  200. private void RecordDefaultCameraFieldOfView()
  201. {
  202. defaultCameraFieldOfView = cameraComp.fieldOfView;
  203. cameraFieldOfView = defaultCameraFieldOfView;
  204. }
  205. //禁止相机视野改变
  206. [NonSerialized] public bool banCameraFieldOfView = false;
  207. public void SetCameraFieldOfView(float value)
  208. {
  209. cameraComp.fieldOfView = value;
  210. }
  211. public void SetCameraFieldOfViewRecord(float value)
  212. {
  213. cameraFieldOfView = value;
  214. }
  215. //拉弓时的相机视野值变化
  216. public void updateFollowPullBow()
  217. {
  218. // if (cameraFieldOfView > 40) {
  219. // cameraFieldOfView -= 20 * Time.deltaTime;
  220. // } else {
  221. // cameraFieldOfView = 40;
  222. // }
  223. }
  224. //松开拉弓时的相机视野值变化
  225. public void updateGiveUpPullBow()
  226. {
  227. // if (cameraFieldOfView < 60) {
  228. // cameraFieldOfView += 20 * Time.deltaTime;
  229. // } else {
  230. // cameraFieldOfView = 60;
  231. // }
  232. }
  233. // 2022-04-28
  234. // ------ 添加固定镜头选项后,新增的API ------
  235. bool isArrowFollowing = false;
  236. public void SetArrowFollowing(bool value)
  237. {
  238. isArrowFollowing = value;
  239. cameraComp.enabled = !isArrowFollowing;
  240. AutoSwitchCamera();
  241. }
  242. public bool GetArrowFollowing() {
  243. return isArrowFollowing;
  244. }
  245. bool isScaleAimDisplaying = false;
  246. public void SetScaleAimDisplaying(bool value)
  247. {
  248. isScaleAimDisplaying = value;
  249. AutoSwitchCamera();
  250. }
  251. void AutoSwitchCamera()
  252. {
  253. if (bowCameraFixed == null)
  254. {
  255. cameraComp.enabled = !isArrowFollowing;
  256. Debug.Log(playerType + "_1cameraComp.enabled:" + cameraComp.enabled);
  257. }
  258. else
  259. {
  260. bowCameraFixed.gameObject.SetActive(!isScaleAimDisplaying && !isArrowFollowing);
  261. cameraComp.enabled = isScaleAimDisplaying && !isArrowFollowing;
  262. Debug.Log(playerType + "_2cameraComp.enabled:" + cameraComp.enabled);
  263. }
  264. }
  265. public Camera GetRenderCamera()
  266. {
  267. if (bowCameraFixed == null)
  268. {
  269. return cameraComp;
  270. }
  271. else
  272. {
  273. if (bowCameraFixed.gameObject.activeSelf)
  274. {
  275. return bowCameraFixed.camera;
  276. }
  277. else
  278. {
  279. return cameraComp;
  280. }
  281. }
  282. }
  283. public BowCameraFixed bowCameraFixed = null;
  284. public class BowCameraFixed
  285. {
  286. public GameObject gameObject;
  287. public Transform transform;
  288. public Camera camera;
  289. private UnityStandardAssets.ImageEffects.Blur blur;
  290. //bowCameraComponent
  291. BowCameraDoublePlayer bowCamera;
  292. private Camera targetCamera;
  293. private UnityStandardAssets.ImageEffects.Blur targetBlur;
  294. public BowCameraFixed(BowCameraDoublePlayer _bowCamera)
  295. {
  296. bowCamera = _bowCamera;
  297. if (bowCamera.playerType == PlayerType.FirstPlayer)
  298. {
  299. gameObject = new GameObject("BowCameraFixedFirst");
  300. transform = gameObject.transform;
  301. //复制Camera组件
  302. targetCamera = bowCamera.cameraComp;
  303. camera = gameObject.AddComponent<Camera>();
  304. camera.tag = "MainCamera";
  305. }
  306. else
  307. {
  308. gameObject = new GameObject("BowCameraFixedSecond");
  309. transform = gameObject.transform;
  310. //复制Camera组件
  311. targetCamera = bowCamera.cameraComp;
  312. camera = gameObject.AddComponent<Camera>();
  313. camera.tag = "SecondCamera";
  314. }
  315. GameController.ins.GetAimCrossHair(bowCamera.playerType).mainCamera = camera;
  316. camera.clearFlags = targetCamera.clearFlags;
  317. camera.backgroundColor = targetCamera.backgroundColor;
  318. camera.cullingMask = targetCamera.cullingMask;
  319. camera.fieldOfView = targetCamera.fieldOfView;
  320. camera.nearClipPlane = targetCamera.nearClipPlane;
  321. camera.depth = targetCamera.depth + 0.1f;
  322. camera.renderingPath = targetCamera.renderingPath;
  323. camera.rect = targetCamera.rect;
  324. camera.farClipPlane = targetCamera.farClipPlane;
  325. camera.nearClipPlane = targetCamera.nearClipPlane;
  326. //复制Blur组件
  327. targetBlur = bowCamera.GetComponent<UnityStandardAssets.ImageEffects.Blur>();
  328. if (targetBlur)
  329. {
  330. blur = gameObject.AddComponent<UnityStandardAssets.ImageEffects.Blur>();
  331. blur.enabled = targetBlur.enabled;
  332. blur.blurShader = targetBlur.blurShader;
  333. blur.blurSpread = targetBlur.blurSpread;
  334. blur.iterations = targetBlur.iterations;
  335. }
  336. //设置Transform属性
  337. transform.parent = bowCamera.transform.parent;
  338. transform.localPosition = bowCamera.transform.localPosition;
  339. transform.localScale = bowCamera.transform.localScale;
  340. transform.localRotation = Quaternion.identity;
  341. //监听和驱动LateUpdate
  342. bowCamera.onAfterLateUpdate += LateUpdate;
  343. //切换镜头
  344. bowCamera.cameraComp.enabled = false;
  345. InitForLimitBound();
  346. }
  347. void LateUpdate()
  348. {
  349. if (gameObject.activeSelf)
  350. {
  351. bowCamera.aimCrossHair.UpdatePostionWhenFixedCamera(bowCamera);
  352. }
  353. if (blur)
  354. {
  355. blur.enabled = targetBlur.enabled;
  356. if (blur.enabled)
  357. {
  358. blur.blurShader = targetBlur.blurShader;
  359. blur.blurSpread = targetBlur.blurSpread;
  360. blur.iterations = targetBlur.iterations;
  361. }
  362. }
  363. }
  364. //边界限制
  365. float[] rangeRotateY = { -80, 80 };
  366. float rangeRotateX = 25;
  367. Vector3 vecF;
  368. Vector3 vecU;
  369. public int outBoundIndex = -1; //-1为未出界
  370. void InitForLimitBound()
  371. {
  372. for (int i = (int)rangeRotateY[0]; i < 0; i++)
  373. {
  374. Vector3 pos = transform.position + Quaternion.AngleAxis(i, Vector3.up) * transform.forward;
  375. pos = camera.WorldToViewportPoint(pos);
  376. if (pos.x >= 0)
  377. {
  378. rangeRotateY[0] = i;
  379. rangeRotateY[1] = -i - 4;
  380. break;
  381. }
  382. }
  383. rangeRotateX = camera.fieldOfView / 2;
  384. vecF = Quaternion.AngleAxis(rangeRotateX, Vector3.right) * Vector3.forward;
  385. vecU = Quaternion.AngleAxis(rangeRotateX, Vector3.right) * Vector3.up;
  386. }
  387. public Vector3 LimitBowAngle(Vector3 outAngle)
  388. {
  389. float angleY = outAngle.y;
  390. float angleX = outAngle.x;
  391. outAngle.y = Mathf.Clamp(angleY, rangeRotateY[0], rangeRotateY[1]);
  392. Vector3 vec = Quaternion.AngleAxis(outAngle.y, vecU) * vecF;
  393. float rx = (float)(Math.Asin(vec.y) / Math.PI * 180) * (angleX < 0 ? 1 : -1);
  394. if (angleY < rangeRotateY[0]) outBoundIndex = 0;
  395. else if (angleY > rangeRotateY[1]) outBoundIndex = 1;
  396. else if (angleX < -rangeRotateX) outBoundIndex = 2;
  397. else if (angleX > rangeRotateX) outBoundIndex = 3;
  398. else outBoundIndex = -1;
  399. if (Mathf.Abs(angleX) > Mathf.Abs(rx))
  400. {
  401. outAngle.x = rx;
  402. }
  403. //因为Vector3是struct,传递给函数是值传递而非引用传递
  404. return outAngle;
  405. }
  406. }
  407. }