GameAssistUIDoublePlayer.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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.setPosLeft();
  120. autoResetViewNewScript.action_OnDestroy += () =>
  121. {
  122. if (SceneManager.GetActiveScene().name == "InfraredGameDouble")
  123. {
  124. InfraredDemo._ins?.SetAdjustPointsOffset(PlayerType.FirstPlayer);
  125. }
  126. else
  127. {
  128. //使用旧模式重置1p
  129. AimHandler.ins?.DoIdentity();
  130. }
  131. };
  132. //action_OnClickBtnIdentity?.Invoke();
  133. });
  134. btnDoIdentity1P.gameObject.AddComponent<LongPressMonitor>().onLongPress += () =>
  135. {
  136. AudioMgr.ins.PlayBtn();
  137. if (SB_EventSystem.ins) SB_EventSystem.ins.AwakenSimulateMouse();
  138. };
  139. //重置中心点
  140. btnDoIdentity2P.onClick.AddListener(delegate ()
  141. {
  142. if (btnDoIdentity2P.GetComponent<LongPressMonitor>().isLongPress) return;
  143. AudioMgr.ins.PlayBtn();
  144. if (GameObject.Find("AutoResetViewNewRight")) return;
  145. GameObject resetView = Instantiate(Resources.Load<GameObject>("AutoResetViewNew"));
  146. resetView.name = "AutoResetViewNewRight";
  147. AutoResetViewNew autoResetViewNewScript = resetView.GetComponent<AutoResetViewNew>();
  148. autoResetViewNewScript.setPosRight();
  149. autoResetViewNewScript.action_OnDestroy += () =>
  150. {
  151. if (SceneManager.GetActiveScene().name == "InfraredGameDouble")
  152. {
  153. InfraredDemo._ins?.SetAdjustPointsOffset(PlayerType.SecondPlayer);
  154. }
  155. else {
  156. //使用新模式重置2p
  157. if (BluetoothAim.ins.getSmartBowHelper2P() != null)
  158. {
  159. BluetoothAim.ins.getSmartBowHelper2P().ResetAim();
  160. }
  161. }
  162. };
  163. //action_OnClickBtnIdentity?.Invoke();
  164. });
  165. btnDoIdentity2P.gameObject.AddComponent<LongPressMonitor>().onLongPress += () =>
  166. {
  167. AudioMgr.ins.PlayBtn();
  168. if (SB_EventSystem.ins) SB_EventSystem.ins.AwakenSimulateMouse();
  169. };
  170. if(btnCrosshair1P != null) InitCrosshair(PlayerType.FirstPlayer, btnCrosshair1P);
  171. if(btnCrosshair2P != null) InitCrosshair(PlayerType.SecondPlayer, btnCrosshair2P);
  172. // ------ 查看靶子 ------
  173. //Transform targetView = this.transform.Find("TargetView");
  174. //Button btnViewTarget = this.transform.Find("Button10").GetComponent<Button>();
  175. //btnViewTarget.onClick.AddListener(delegate(){
  176. // AudioMgr.ins.PlayBtn();
  177. // TargetView.ins.ReverseActive();
  178. //});
  179. //if (GameMgr.gameType == 2 || GameMgr.gameType == 9) {
  180. // targetView.transform.GetComponent<RectTransform>().anchoredPosition = new Vector2(45, 30);
  181. // btnViewTarget.transform.GetComponent<RectTransform>().anchoredPosition = new Vector2(45, 195);
  182. //}
  183. LoadMyRecord();
  184. applyPlayerRecordsWhenGameTryAgain();
  185. }
  186. /// <summary>
  187. /// 准心显示
  188. /// </summary>
  189. /// <param name="playerType"></param>
  190. public void InitCrosshair(PlayerType playerType, Button btnCrosshair)
  191. {
  192. if (InfraredDemo._ins == null) return;
  193. AimCrossHair aimCrossHair = GameController.ins.GetAimCrossHair(playerType);
  194. //设置准心是否显示
  195. aimCrossHair.SetOnlyShow(InfraredDemo._ins.getDoubleCrosshairValue(playerType) == 1);
  196. aimCrossHair.SyncVisiable();
  197. bool onInitOpen = aimCrossHair.GetOnlyShow();
  198. Image crossHairImage = btnCrosshair.GetComponentInChildren<Image>();
  199. crossHairImage.material = onInitOpen ? outlight : null;
  200. btnCrosshair.onClick.AddListener(delegate () {
  201. AudioMgr.ins.PlayBtn();
  202. bool onlyShow = !aimCrossHair.GetOnlyShow();
  203. aimCrossHair.SetOnlyShow(onlyShow);
  204. //保存准心状态
  205. if (InfraredDemo._ins) InfraredDemo._ins.setDoubleCrosshairValue(playerType, onlyShow);
  206. if (onlyShow)
  207. {
  208. crossHairImage.material = outlight;
  209. }
  210. else
  211. {
  212. crossHairImage.material = null;
  213. }
  214. });
  215. }
  216. void OnDestroy()
  217. {
  218. if (ins == null) ins = null;
  219. SaveMyRecord();
  220. }
  221. void Update()
  222. {
  223. }
  224. [System.NonSerialized] public int playerScaleAimValue_OnlinePK = 0;
  225. bool[] playerScaleAimRecords = { false, false };
  226. bool[] playerScaleShootRecords = { false, false };
  227. int GetMyPlayerIndex()
  228. {
  229. if (GlobalData.pkMatchType == PKMatchType.OnlinePK)
  230. {
  231. // return GlobalData.playerIndexInRoom;
  232. return 0;
  233. }
  234. return 0;
  235. }
  236. int GetCurPlayerIndex()
  237. {
  238. if (GlobalData.pkMatchType == PKMatchType.OnlinePK)
  239. {
  240. // GameMode gm = GameMgr.ins.gameMode;
  241. // if (GameMgr.gameType == 9) return ((PKGameMode_OnlinePK) gm).gameLogic.currentPlayerIndex;
  242. return 0;
  243. }
  244. else if (GlobalData.pkMatchType == PKMatchType.LocalPK)
  245. {
  246. GameMode gm = GameMgr.ins.gameMode;
  247. if (GameMgr.gameType == 2) return ((PKGameMode)gm).currentPlayerIndex;
  248. if (GameMgr.gameType == 6) return ((RabbitHuntGameMode_LocalPK)gm).currentPlayerIndex;
  249. if (GameMgr.gameType == 7) return ((YejiHuntGameMode_LocalPK)gm).currentPlayerIndex;
  250. if (GameMgr.gameType == 8) return ((WolfHuntGameMode_LocalPK)gm).currentPlayerIndex;
  251. }
  252. return 0;
  253. }
  254. void SaveMyRecord()
  255. {
  256. int gameType = 0; //多种模式共用记录
  257. int myPlayerIndex = GetMyPlayerIndex();
  258. bool isScaleAimOpen = playerScaleAimRecords[myPlayerIndex];
  259. bool isScaleShootOpen = playerScaleShootRecords[myPlayerIndex];
  260. PlayerPrefs.SetInt("ScaleAim," + gameType + "," + LoginMgr.myUserInfo.id, isScaleAimOpen ? 1 : 0);
  261. PlayerPrefs.SetInt("ScaleShoot," + gameType + "," + LoginMgr.myUserInfo.id, isScaleShootOpen ? 1 : 0);
  262. }
  263. void LoadMyRecord()
  264. {
  265. int gameType = 0; //多种模式共用记录
  266. if (CommonConfig.SpecialVersion1)
  267. {
  268. if (PlayerPrefs.GetInt("sv1_ScaleAimShoot," + LoginMgr.myUserInfo.id, 0) == 0)
  269. {
  270. PlayerPrefs.SetInt("sv1_ScaleAimShoot," + LoginMgr.myUserInfo.id, 1);
  271. PlayerPrefs.SetInt("ScaleAim," + gameType + "," + LoginMgr.myUserInfo.id, 0);
  272. PlayerPrefs.SetInt("ScaleShoot," + gameType + "," + LoginMgr.myUserInfo.id, 1);
  273. }
  274. }
  275. bool isScaleAimOpen = PlayerPrefs.GetInt("ScaleAim," + gameType + "," + LoginMgr.myUserInfo.id, 0) == 1 ? true : false;
  276. bool isScaleShootOpen = PlayerPrefs.GetInt("ScaleShoot," + gameType + "," + LoginMgr.myUserInfo.id, 0) == 1 ? true : false;
  277. if (isScaleAimOpen && GetPropScaleAimValue() == 0)
  278. {
  279. isScaleAimOpen = false;
  280. }
  281. if (isScaleShootOpen && getScaleShootProp() == null)
  282. {
  283. isScaleShootOpen = false;
  284. }
  285. int myPlayerIndex = GetMyPlayerIndex();
  286. playerScaleAimRecords[myPlayerIndex] = isScaleAimOpen;
  287. playerScaleShootRecords[myPlayerIndex] = isScaleShootOpen;
  288. if (isLocalPK())
  289. {
  290. playerScaleAimRecords[(myPlayerIndex + 1) % 2] = isScaleAimOpen;
  291. playerScaleShootRecords[(myPlayerIndex + 1) % 2] = isScaleShootOpen;
  292. }
  293. }
  294. #region 本地双人PK,再来时保存记录
  295. private static bool[] playerRecords = null;
  296. private bool isLocalPK()
  297. {
  298. int[] gameTypes = { 2, 6, 7, 8 };
  299. if (System.Array.IndexOf(gameTypes, GameMgr.gameType) > -1)
  300. {
  301. return true;
  302. }
  303. return false;
  304. }
  305. public void recordPlayerRecordsWhenGameTryAgain()
  306. {
  307. if (!isLocalPK()) return;
  308. playerRecords = new bool[] {
  309. playerScaleAimRecords[0], playerScaleAimRecords[1],
  310. playerScaleShootRecords[0], playerScaleShootRecords[1]
  311. };
  312. }
  313. private void applyPlayerRecordsWhenGameTryAgain()
  314. {
  315. if (playerRecords != null)
  316. {
  317. playerScaleAimRecords[0] = playerRecords[0];
  318. playerScaleAimRecords[1] = playerRecords[1];
  319. playerScaleShootRecords[0] = playerRecords[2];
  320. playerScaleShootRecords[1] = playerRecords[3];
  321. playerRecords = null;
  322. }
  323. }
  324. #endregion
  325. #region 道具开关监听
  326. private void onOpenScaleAimSuccess()
  327. {
  328. playerScaleAimRecords[GetCurPlayerIndex()] = true;
  329. }
  330. private void onCloseScaleAimSuccess()
  331. {
  332. playerScaleAimRecords[GetCurPlayerIndex()] = false;
  333. }
  334. private void onOpenScaleShootSuccess()
  335. {
  336. playerScaleShootRecords[GetCurPlayerIndex()] = true;
  337. }
  338. private void onCloseScaleShootSuccess()
  339. {
  340. playerScaleShootRecords[GetCurPlayerIndex()] = false;
  341. }
  342. #endregion
  343. // ------ 开镜瞄准功能 ------
  344. [System.NonSerialized] public int aimScaleValue = 1;
  345. float[] scaleAimFieldOfViews = { 30, 20, 12, 6, 3 };
  346. float[] scaleAimScopeScales = { 150, 98, 58, 29, 14.5f };
  347. Sequence seq1 = null;
  348. [System.NonSerialized] public bool scaleAimOn = false; //该功能是否处于打开状态
  349. bool openScaleAim()
  350. {
  351. int scaleValue = GetPropScaleAimValue();
  352. if (scaleValue > 0)
  353. {
  354. if (currentPlayerType == PlayerType.FirstPlayer)
  355. {
  356. btnScaleAim1P.GetComponentInChildren<Image>().material = outlight;
  357. }
  358. else
  359. {
  360. btnScaleAim2P.GetComponentInChildren<Image>().material = outlight;
  361. }
  362. aimScaleValue = scaleValue;
  363. DisplayScaleAim(true, aimScaleValue);
  364. //scaleAimOn = true;
  365. // onOpenScaleAimSuccess();
  366. return true;
  367. }
  368. return false;
  369. }
  370. int aimScaleDisplayValue = -1;
  371. void DisplayScaleAim(bool open, int scaleValue)
  372. { //渲染
  373. aimScaleDisplayValue = scaleValue;
  374. Debug.Log("DisplayScaleAim:" + currentPlayerType);
  375. BowCameraDoublePlayer bowCamera = GameController.ins.GetBowCameraDoublePlayer(currentPlayerType);
  376. Transform scope = bowCamera.transform.Find("Scope");
  377. GameController.ins.GetAimCrossHair(currentPlayerType).gameObject.GetComponent<RectTransform>().sizeDelta =
  378. open ? new Vector2(500, 500) : new Vector2(260, 260);
  379. if (open) GameController.ins.GetAimCrossHair(currentPlayerType).openDisplayToCenter();// //Vector3.zero;
  380. bowCamera.banCameraFieldOfView = open;
  381. bowCamera.SetScaleAimDisplaying(open);
  382. if (open)
  383. {
  384. bowCamera.SetCameraFieldOfView(scaleAimFieldOfViews[scaleValue - 1]);
  385. }
  386. ArmBowDoublePlayer armbowDoublePlayer = GameController.ins.GetArmBowDoublePlayer(currentPlayerType);
  387. Vector3 localPosition = armbowDoublePlayer.transform.localPosition;
  388. localPosition.z = open ? -2 : ArmBowDoublePlayer.localPosZ;
  389. armbowDoublePlayer.transform.localPosition = localPosition;
  390. if (open)
  391. {
  392. float scopeScale = scaleAimScopeScales[scaleValue - 1];
  393. scope.localScale = new Vector3(scopeScale, scopeScale, scopeScale);
  394. }
  395. else
  396. {
  397. scope.localScale = new Vector3(0, 0, 0);
  398. }
  399. }
  400. void closeScaleAim()
  401. {
  402. if (currentPlayerType == PlayerType.FirstPlayer)
  403. {
  404. btnScaleAim1P.GetComponentInChildren<Image>().material = null;
  405. }
  406. else
  407. {
  408. btnScaleAim2P.GetComponentInChildren<Image>().material = null;
  409. }
  410. DisplayScaleAim(false, aimScaleValue);
  411. //onCloseScaleAimSuccess();
  412. }
  413. int GetPropScaleAimValue()
  414. {
  415. List<PropInfo> props = PropMgr.ins.ListForEquipped();
  416. foreach (var prop in props)
  417. {
  418. if (prop.config.type == 1)
  419. {
  420. PropScaleAim config = prop.config as PropScaleAim;
  421. return config.scaleValue;
  422. }
  423. }
  424. return 0;
  425. }
  426. // ------ 发射加速功能 ------
  427. [System.NonSerialized] public int shootScaleValue = 1;
  428. [System.NonSerialized] public int shootScaleValue2P = 1;
  429. Sequence seq2 = null;
  430. bool scaleShootOn = false; //该功能是否处于打开状态
  431. //默认开启两个加速
  432. void defaultOpenScaleShoot() {
  433. PropInfo prop = getScaleShootProp();
  434. if (prop != null)
  435. {
  436. PropScaleShoot config = prop.config as PropScaleShoot;
  437. btnScaleShoot1P.GetComponentInChildren<Image>().material = outlight;
  438. shootScaleValue = config.scaleValue;
  439. btnScaleShoot2P.GetComponentInChildren<Image>().material = outlight;
  440. shootScaleValue2P = config.scaleValue;
  441. }
  442. }
  443. bool openScaleShoot()
  444. {
  445. PropInfo prop = getScaleShootProp();
  446. if (prop != null)
  447. {
  448. PropScaleShoot config = prop.config as PropScaleShoot;
  449. if (currentPlayerType == PlayerType.FirstPlayer)
  450. {
  451. btnScaleShoot1P.GetComponentInChildren<Image>().material = outlight;
  452. shootScaleValue = config.scaleValue;
  453. }
  454. else
  455. {
  456. btnScaleShoot2P.GetComponentInChildren<Image>().material = outlight;
  457. shootScaleValue2P = config.scaleValue;
  458. }
  459. //scaleShootOn = true;
  460. //onOpenScaleShootSuccess();
  461. return true;
  462. }
  463. return false;
  464. }
  465. void closeScaleShoot()
  466. {
  467. if (currentPlayerType == PlayerType.FirstPlayer)
  468. {
  469. btnScaleShoot1P.GetComponentInChildren<Image>().material = null;
  470. shootScaleValue = 1;
  471. }
  472. else
  473. {
  474. btnScaleShoot2P.GetComponentInChildren<Image>().material = null;
  475. shootScaleValue2P = 1;
  476. }
  477. // scaleShootOn = false;
  478. // onCloseScaleShootSuccess();
  479. }
  480. PropInfo getScaleShootProp()
  481. {
  482. List<PropInfo> props = PropMgr.ins.ListForEquipped();
  483. foreach (var prop in props)
  484. {
  485. if (prop.config.type == 2)
  486. {
  487. return prop;
  488. }
  489. }
  490. return null;
  491. }
  492. }