GameAssistUIDoublePlayer.cs 16 KB

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