GameAssistUIDoublePlayer.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. using AppUI.Bluetooth;
  2. using DG.Tweening;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. using UnityEngine.SceneManagement;
  8. using UnityEngine.UI;
  9. /* 游戏帮助界面(右侧方的辅助功能) */
  10. public class GameAssistUIDoublePlayer : MonoBehaviour
  11. {
  12. [SerializeField] Material outlight;
  13. Button btnScaleAim;
  14. Button btnScaleShoot;
  15. public Action action_OnClickBtnIdentity;
  16. public static GameAssistUIDoublePlayer ins;
  17. [Header("公共相关参数")]
  18. [SerializeField]
  19. Button btnBack;
  20. //当前操作的用户
  21. PlayerType currentPlayerType = PlayerType.FirstPlayer;
  22. [Header("1P相关参数")]
  23. [SerializeField]
  24. Button btnScaleAim1P;
  25. [SerializeField]
  26. Button btnScaleShoot1P;
  27. [SerializeField]
  28. Button btnCrosshair1P;
  29. [SerializeField]
  30. Button btnDoIdentity1P;
  31. [Header("2P相关参数")]
  32. [SerializeField]
  33. Button btnScaleAim2P;
  34. [SerializeField]
  35. Button btnScaleShoot2P;
  36. [SerializeField]
  37. Button btnCrosshair2P;
  38. [SerializeField]
  39. Button btnDoIdentity2P;
  40. void Awake()
  41. {
  42. ins = this;
  43. }
  44. public void onBtnBack()
  45. {
  46. AudioMgr.ins.PlayBtn();
  47. SceneManager.LoadScene("Home", LoadSceneMode.Single);
  48. //GameMgr.ins.userGameAnalyse.showResultView(() => {
  49. // SceneManager.LoadScene("Home", LoadSceneMode.Single);
  50. //});
  51. }
  52. void Start()
  53. {
  54. //返回主页
  55. btnBack.onClick.AddListener(onBtnBack);
  56. //控制瞄准镜
  57. btnScaleAim1P.onClick.AddListener(delegate ()
  58. {
  59. AudioMgr.ins.PlayBtn();
  60. currentPlayerType = PlayerType.FirstPlayer;
  61. if (btnScaleAim1P.GetComponentInChildren<Image>().material == outlight)
  62. {
  63. closeScaleAim();
  64. }
  65. else
  66. {
  67. openScaleAim();
  68. }
  69. });
  70. btnScaleAim2P.onClick.AddListener(delegate ()
  71. {
  72. AudioMgr.ins.PlayBtn();
  73. currentPlayerType = PlayerType.SecondPlayer;
  74. if (btnScaleAim2P.GetComponentInChildren<Image>().material == outlight)
  75. {
  76. closeScaleAim();
  77. }
  78. else
  79. {
  80. openScaleAim();
  81. }
  82. });
  83. //控制加速
  84. btnScaleShoot1P.onClick.AddListener(delegate ()
  85. {
  86. AudioMgr.ins.PlayBtn();
  87. currentPlayerType = PlayerType.FirstPlayer;
  88. if (btnScaleShoot1P.GetComponentInChildren<Image>().material == outlight)
  89. {
  90. closeScaleShoot();
  91. }
  92. else
  93. {
  94. openScaleShoot();
  95. }
  96. });
  97. btnScaleShoot2P.onClick.AddListener(delegate ()
  98. {
  99. AudioMgr.ins.PlayBtn();
  100. currentPlayerType = PlayerType.SecondPlayer;
  101. if (btnScaleShoot2P.GetComponentInChildren<Image>().material == outlight)
  102. {
  103. closeScaleShoot();
  104. }
  105. else
  106. {
  107. openScaleShoot();
  108. }
  109. });
  110. defaultOpenScaleShoot();
  111. //重置中心点
  112. btnDoIdentity1P.onClick.AddListener(delegate ()
  113. {
  114. if (btnDoIdentity1P.GetComponent<LongPressMonitor>().isLongPress) return;
  115. AudioMgr.ins.PlayBtn();
  116. if (GameObject.Find("AutoResetViewNewLeft")) return;
  117. GameObject resetView = Instantiate(Resources.Load<GameObject>("AutoResetViewNew"));
  118. resetView.name = "AutoResetViewNewLeft";
  119. AutoResetViewNew autoResetViewNewScript = resetView.GetComponent<AutoResetViewNew>();
  120. autoResetViewNewScript.setTextKey("new-user-guider_tip_视角归位-瞄准-infraredD");
  121. autoResetViewNewScript.setPosLeft();
  122. autoResetViewNewScript.action_OnDestroy += () =>
  123. {
  124. if (SceneManager.GetActiveScene().name == "InfraredGameDouble")
  125. {
  126. InfraredDemo._ins?.SetAdjustPointsOffset(PlayerType.FirstPlayer);
  127. }
  128. else
  129. {
  130. //使用旧模式重置1p
  131. SmartBowDeviceHub.ins.Aim?.DoIdentity();
  132. }
  133. };
  134. //action_OnClickBtnIdentity?.Invoke();
  135. });
  136. btnDoIdentity1P.gameObject.AddComponent<LongPressMonitor>().onLongPress += () =>
  137. {
  138. AudioMgr.ins.PlayBtn();
  139. if (SB_EventSystem.ins) SB_EventSystem.ins.AwakenSimulateMouse();
  140. };
  141. //重置中心点
  142. btnDoIdentity2P.onClick.AddListener(delegate ()
  143. {
  144. if (btnDoIdentity2P.GetComponent<LongPressMonitor>().isLongPress) return;
  145. AudioMgr.ins.PlayBtn();
  146. if (GameObject.Find("AutoResetViewNewRight")) return;
  147. GameObject resetView = Instantiate(Resources.Load<GameObject>("AutoResetViewNew"));
  148. resetView.name = "AutoResetViewNewRight";
  149. AutoResetViewNew autoResetViewNewScript = resetView.GetComponent<AutoResetViewNew>();
  150. autoResetViewNewScript.setTextKey("new-user-guider_tip_视角归位-瞄准-infraredD");
  151. autoResetViewNewScript.setPosRight();
  152. autoResetViewNewScript.action_OnDestroy += () =>
  153. {
  154. if (SceneManager.GetActiveScene().name == "InfraredGameDouble")
  155. {
  156. InfraredDemo._ins?.SetAdjustPointsOffset(PlayerType.SecondPlayer);
  157. }
  158. else {
  159. //使用新模式重置2p
  160. if (SmartBowDeviceHub.ins != null)
  161. {
  162. SmartBowDeviceHub.ins.ResetAim(BluetoothPlayer.SECOND_PLAYER);
  163. }
  164. }
  165. };
  166. //action_OnClickBtnIdentity?.Invoke();
  167. });
  168. btnDoIdentity2P.gameObject.AddComponent<LongPressMonitor>().onLongPress += () =>
  169. {
  170. AudioMgr.ins.PlayBtn();
  171. if (SB_EventSystem.ins) SB_EventSystem.ins.AwakenSimulateMouse();
  172. };
  173. if(btnCrosshair1P != null) InitCrosshair(PlayerType.FirstPlayer, btnCrosshair1P);
  174. if(btnCrosshair2P != null) InitCrosshair(PlayerType.SecondPlayer, btnCrosshair2P);
  175. // ------ 查看靶子 ------
  176. //Transform targetView = this.transform.Find("TargetView");
  177. //Button btnViewTarget = this.transform.Find("Button10").GetComponent<Button>();
  178. //btnViewTarget.onClick.AddListener(delegate(){
  179. // AudioMgr.ins.PlayBtn();
  180. // TargetView.ins.ReverseActive();
  181. //});
  182. //if (GameMgr.gameType == 2 || GameMgr.gameType == 9) {
  183. // targetView.transform.GetComponent<RectTransform>().anchoredPosition = new Vector2(45, 30);
  184. // btnViewTarget.transform.GetComponent<RectTransform>().anchoredPosition = new Vector2(45, 195);
  185. //}
  186. LoadMyRecord();
  187. applyPlayerRecordsWhenGameTryAgain();
  188. }
  189. /// <summary>
  190. /// 准心显示
  191. /// </summary>
  192. /// <param name="playerType"></param>
  193. public void InitCrosshair(PlayerType playerType, Button btnCrosshair)
  194. {
  195. if (InfraredDemo._ins == null) return;
  196. AimCrossHair aimCrossHair = GameController.ins.GetAimCrossHair(playerType);
  197. //设置准心是否显示
  198. aimCrossHair.SetOnlyShow(InfraredDemo._ins.getDoubleCrosshairValue(playerType) == 1);
  199. aimCrossHair.SyncVisiable();
  200. bool onInitOpen = aimCrossHair.GetOnlyShow();
  201. Image crossHairImage = btnCrosshair.GetComponentInChildren<Image>();
  202. crossHairImage.material = onInitOpen ? outlight : null;
  203. btnCrosshair.onClick.AddListener(delegate () {
  204. AudioMgr.ins.PlayBtn();
  205. bool onlyShow = !aimCrossHair.GetOnlyShow();
  206. aimCrossHair.SetOnlyShow(onlyShow);
  207. //保存准心状态
  208. if (InfraredDemo._ins) InfraredDemo._ins.setDoubleCrosshairValue(playerType, onlyShow);
  209. if (onlyShow)
  210. {
  211. crossHairImage.material = outlight;
  212. }
  213. else
  214. {
  215. crossHairImage.material = null;
  216. }
  217. });
  218. }
  219. void OnDestroy()
  220. {
  221. if (ins == null) ins = null;
  222. SaveMyRecord();
  223. }
  224. void Update()
  225. {
  226. }
  227. [System.NonSerialized] public int playerScaleAimValue_OnlinePK = 0;
  228. bool[] playerScaleAimRecords = { false, false };
  229. bool[] playerScaleShootRecords = { false, false };
  230. int GetMyPlayerIndex()
  231. {
  232. if (GlobalData.pkMatchType == PKMatchType.OnlinePK)
  233. {
  234. // return GlobalData.playerIndexInRoom;
  235. return 0;
  236. }
  237. return 0;
  238. }
  239. int GetCurPlayerIndex()
  240. {
  241. if (GlobalData.pkMatchType == PKMatchType.OnlinePK)
  242. {
  243. // GameMode gm = GameMgr.ins.gameMode;
  244. // if (GameMgr.gameType == 9) return ((PKGameMode_OnlinePK) gm).gameLogic.currentPlayerIndex;
  245. return 0;
  246. }
  247. else if (GlobalData.pkMatchType == PKMatchType.LocalPK)
  248. {
  249. GameMode gm = GameMgr.ins.gameMode;
  250. if (GameMgr.gameType == 2) return ((PKGameMode)gm).currentPlayerIndex;
  251. if (GameMgr.gameType == 6) return ((RabbitHuntGameMode_LocalPK)gm).currentPlayerIndex;
  252. if (GameMgr.gameType == 7) return ((YejiHuntGameMode_LocalPK)gm).currentPlayerIndex;
  253. if (GameMgr.gameType == 8) return ((WolfHuntGameMode_LocalPK)gm).currentPlayerIndex;
  254. }
  255. return 0;
  256. }
  257. void SaveMyRecord()
  258. {
  259. int gameType = 0; //多种模式共用记录
  260. int myPlayerIndex = GetMyPlayerIndex();
  261. bool isScaleAimOpen = playerScaleAimRecords[myPlayerIndex];
  262. bool isScaleShootOpen = playerScaleShootRecords[myPlayerIndex];
  263. PlayerPrefs.SetInt("ScaleAim," + gameType + "," + LoginMgr.myUserInfo.id, isScaleAimOpen ? 1 : 0);
  264. PlayerPrefs.SetInt("ScaleShoot," + gameType + "," + LoginMgr.myUserInfo.id, isScaleShootOpen ? 1 : 0);
  265. }
  266. void LoadMyRecord()
  267. {
  268. int gameType = 0; //多种模式共用记录
  269. if (CommonConfig.SpecialVersion1)
  270. {
  271. if (PlayerPrefs.GetInt("sv1_ScaleAimShoot," + LoginMgr.myUserInfo.id, 0) == 0)
  272. {
  273. PlayerPrefs.SetInt("sv1_ScaleAimShoot," + LoginMgr.myUserInfo.id, 1);
  274. PlayerPrefs.SetInt("ScaleAim," + gameType + "," + LoginMgr.myUserInfo.id, 0);
  275. PlayerPrefs.SetInt("ScaleShoot," + gameType + "," + LoginMgr.myUserInfo.id, 1);
  276. }
  277. }
  278. bool isScaleAimOpen = PlayerPrefs.GetInt("ScaleAim," + gameType + "," + LoginMgr.myUserInfo.id, 0) == 1 ? true : false;
  279. bool isScaleShootOpen = PlayerPrefs.GetInt("ScaleShoot," + gameType + "," + LoginMgr.myUserInfo.id, 0) == 1 ? true : false;
  280. if (isScaleAimOpen && GetPropScaleAimValue() == 0)
  281. {
  282. isScaleAimOpen = false;
  283. }
  284. if (isScaleShootOpen && getScaleShootProp() == null)
  285. {
  286. isScaleShootOpen = false;
  287. }
  288. int myPlayerIndex = GetMyPlayerIndex();
  289. playerScaleAimRecords[myPlayerIndex] = isScaleAimOpen;
  290. playerScaleShootRecords[myPlayerIndex] = isScaleShootOpen;
  291. if (isLocalPK())
  292. {
  293. playerScaleAimRecords[(myPlayerIndex + 1) % 2] = isScaleAimOpen;
  294. playerScaleShootRecords[(myPlayerIndex + 1) % 2] = isScaleShootOpen;
  295. }
  296. }
  297. #region 本地双人PK,再来时保存记录
  298. private static bool[] playerRecords = null;
  299. private bool isLocalPK()
  300. {
  301. int[] gameTypes = { 2, 6, 7, 8 };
  302. if (System.Array.IndexOf(gameTypes, GameMgr.gameType) > -1)
  303. {
  304. return true;
  305. }
  306. return false;
  307. }
  308. public void recordPlayerRecordsWhenGameTryAgain()
  309. {
  310. if (!isLocalPK()) return;
  311. playerRecords = new bool[] {
  312. playerScaleAimRecords[0], playerScaleAimRecords[1],
  313. playerScaleShootRecords[0], playerScaleShootRecords[1]
  314. };
  315. }
  316. private void applyPlayerRecordsWhenGameTryAgain()
  317. {
  318. if (playerRecords != null)
  319. {
  320. playerScaleAimRecords[0] = playerRecords[0];
  321. playerScaleAimRecords[1] = playerRecords[1];
  322. playerScaleShootRecords[0] = playerRecords[2];
  323. playerScaleShootRecords[1] = playerRecords[3];
  324. playerRecords = null;
  325. }
  326. }
  327. #endregion
  328. #region 道具开关监听
  329. private void onOpenScaleAimSuccess()
  330. {
  331. playerScaleAimRecords[GetCurPlayerIndex()] = true;
  332. }
  333. private void onCloseScaleAimSuccess()
  334. {
  335. playerScaleAimRecords[GetCurPlayerIndex()] = false;
  336. }
  337. private void onOpenScaleShootSuccess()
  338. {
  339. playerScaleShootRecords[GetCurPlayerIndex()] = true;
  340. }
  341. private void onCloseScaleShootSuccess()
  342. {
  343. playerScaleShootRecords[GetCurPlayerIndex()] = false;
  344. }
  345. #endregion
  346. // ------ 开镜瞄准功能 ------
  347. [System.NonSerialized] public int aimScaleValue = 1;
  348. float[] scaleAimFieldOfViews = { 30, 20, 12, 6, 3 };
  349. float[] scaleAimScopeScales = { 150, 98, 58, 29, 14.5f };
  350. Sequence seq1 = null;
  351. [System.NonSerialized] public bool scaleAimOn = false; //该功能是否处于打开状态
  352. bool openScaleAim()
  353. {
  354. int scaleValue = GetPropScaleAimValue();
  355. if (scaleValue > 0)
  356. {
  357. if (currentPlayerType == PlayerType.FirstPlayer)
  358. {
  359. btnScaleAim1P.GetComponentInChildren<Image>().material = outlight;
  360. }
  361. else
  362. {
  363. btnScaleAim2P.GetComponentInChildren<Image>().material = outlight;
  364. }
  365. aimScaleValue = scaleValue;
  366. DisplayScaleAim(true, aimScaleValue);
  367. //scaleAimOn = true;
  368. // onOpenScaleAimSuccess();
  369. return true;
  370. }
  371. return false;
  372. }
  373. int aimScaleDisplayValue = -1;
  374. void DisplayScaleAim(bool open, int scaleValue)
  375. { //渲染
  376. aimScaleDisplayValue = scaleValue;
  377. Debug.Log("DisplayScaleAim:" + currentPlayerType);
  378. BowCameraDoublePlayer bowCamera = GameController.ins.GetBowCameraDoublePlayer(currentPlayerType);
  379. Transform scope = bowCamera.transform.Find("Scope");
  380. GameController.ins.GetAimCrossHair(currentPlayerType).gameObject.GetComponent<RectTransform>().sizeDelta =
  381. open ? new Vector2(500, 500) : new Vector2(260, 260);
  382. if (open) GameController.ins.GetAimCrossHair(currentPlayerType).openDisplayToCenter();// //Vector3.zero;
  383. bowCamera.banCameraFieldOfView = open;
  384. bowCamera.SetScaleAimDisplaying(open);
  385. if (open)
  386. {
  387. bowCamera.SetCameraFieldOfView(scaleAimFieldOfViews[scaleValue - 1]);
  388. }
  389. ArmBowDoublePlayer armbowDoublePlayer = GameController.ins.GetArmBowDoublePlayer(currentPlayerType);
  390. Vector3 localPosition = armbowDoublePlayer.transform.localPosition;
  391. localPosition.z = open ? -2 : ArmBowDoublePlayer.localPosZ;
  392. armbowDoublePlayer.transform.localPosition = localPosition;
  393. if (open)
  394. {
  395. float scopeScale = scaleAimScopeScales[scaleValue - 1];
  396. scope.localScale = new Vector3(scopeScale, scopeScale, scopeScale);
  397. }
  398. else
  399. {
  400. scope.localScale = new Vector3(0, 0, 0);
  401. }
  402. }
  403. void closeScaleAim()
  404. {
  405. if (currentPlayerType == PlayerType.FirstPlayer)
  406. {
  407. btnScaleAim1P.GetComponentInChildren<Image>().material = null;
  408. }
  409. else
  410. {
  411. btnScaleAim2P.GetComponentInChildren<Image>().material = null;
  412. }
  413. DisplayScaleAim(false, aimScaleValue);
  414. //onCloseScaleAimSuccess();
  415. }
  416. int GetPropScaleAimValue()
  417. {
  418. List<PropInfo> props = PropMgr.ins.ListForEquipped();
  419. foreach (var prop in props)
  420. {
  421. if (prop.config.type == 1)
  422. {
  423. PropScaleAim config = prop.config as PropScaleAim;
  424. return config.scaleValue;
  425. }
  426. }
  427. return 0;
  428. }
  429. // ------ 发射加速功能 ------
  430. [System.NonSerialized] public int shootScaleValue = 1;
  431. [System.NonSerialized] public int shootScaleValue2P = 1;
  432. Sequence seq2 = null;
  433. bool scaleShootOn = false; //该功能是否处于打开状态
  434. //默认开启两个加速
  435. void defaultOpenScaleShoot() {
  436. PropInfo prop = getScaleShootProp();
  437. if (prop != null)
  438. {
  439. PropScaleShoot config = prop.config as PropScaleShoot;
  440. btnScaleShoot1P.GetComponentInChildren<Image>().material = outlight;
  441. shootScaleValue = config.scaleValue;
  442. btnScaleShoot2P.GetComponentInChildren<Image>().material = outlight;
  443. shootScaleValue2P = config.scaleValue;
  444. }
  445. }
  446. bool openScaleShoot()
  447. {
  448. PropInfo prop = getScaleShootProp();
  449. if (prop != null)
  450. {
  451. PropScaleShoot config = prop.config as PropScaleShoot;
  452. if (currentPlayerType == PlayerType.FirstPlayer)
  453. {
  454. btnScaleShoot1P.GetComponentInChildren<Image>().material = outlight;
  455. shootScaleValue = config.scaleValue;
  456. }
  457. else
  458. {
  459. btnScaleShoot2P.GetComponentInChildren<Image>().material = outlight;
  460. shootScaleValue2P = config.scaleValue;
  461. }
  462. //scaleShootOn = true;
  463. //onOpenScaleShootSuccess();
  464. return true;
  465. }
  466. return false;
  467. }
  468. void closeScaleShoot()
  469. {
  470. if (currentPlayerType == PlayerType.FirstPlayer)
  471. {
  472. btnScaleShoot1P.GetComponentInChildren<Image>().material = null;
  473. shootScaleValue = 1;
  474. }
  475. else
  476. {
  477. btnScaleShoot2P.GetComponentInChildren<Image>().material = null;
  478. shootScaleValue2P = 1;
  479. }
  480. // scaleShootOn = false;
  481. // onCloseScaleShootSuccess();
  482. }
  483. PropInfo getScaleShootProp()
  484. {
  485. List<PropInfo> props = PropMgr.ins.ListForEquipped();
  486. foreach (var prop in props)
  487. {
  488. if (prop.config.type == 2)
  489. {
  490. return prop;
  491. }
  492. }
  493. return null;
  494. }
  495. }