GameAssistUIDoublePlayer.cs 18 KB

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