GameAssistUI.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. using SmartBowSDK;
  9. /* 游戏帮助界面(右侧方的辅助功能) */
  10. public class GameAssistUI : MonoBehaviour
  11. {
  12. [SerializeField] Material outlight;
  13. [SerializeField] Text text1;
  14. [SerializeField] Text text2;
  15. Button btnScaleAim;
  16. Button btnScaleShoot;
  17. public Action action_OnClickBtnIdentity;
  18. public static GameAssistUI ins;
  19. void Awake() {
  20. ins = this;
  21. }
  22. int num = 0;
  23. GameResultView gameResultView;
  24. public void onBtnBack() {
  25. AudioMgr.ins.PlayBtn();
  26. //如果是从地磁计跳进来的,直接返回到主页
  27. Debug.Log("步骤1:点击次数" + num);
  28. //num++;
  29. if (num == 0)
  30. {
  31. if (GlobalData.pkMatchType == PKMatchType.None && GameMgr.gameType == 1 && GameMgr.bNavBack)
  32. {
  33. GameMgr.bNavBack = false;
  34. HomeView.bOpenOtherView = true;
  35. HomeView.openName = "DeviceView";
  36. SceneManager.LoadScene("Home", LoadSceneMode.Single);
  37. }
  38. else
  39. {
  40. gameResultView = GameMgr.ins.userGameAnalyse.showResultView(() =>
  41. {
  42. SceneManager.LoadScene("Home", LoadSceneMode.Single);
  43. //Debug.Log("退出当前场景");
  44. });
  45. }
  46. num += 1;
  47. return;
  48. }
  49. if (num == 1)
  50. {
  51. gameResultView.OnClick_Back();
  52. SceneManager.LoadScene("Home", LoadSceneMode.Single);
  53. Debug.Log("退出当前场景");
  54. }
  55. }
  56. void Start()
  57. {
  58. this.transform.Find("Button0").GetComponent<Button>().onClick.AddListener(onBtnBack);
  59. Button guideBtn = this.transform.Find("Button1").GetComponent<Button>();
  60. btnScaleAim = this.transform.Find("Button2").GetComponent<Button>();
  61. guideBtn.gameObject.SetActive(!CommonConfig.StandaloneModeOrPlatformB);
  62. btnScaleAim.gameObject.SetActive(!CommonConfig.StandaloneModeOrPlatformB);
  63. btnScaleShoot = this.transform.Find("Button3").GetComponent<Button>();
  64. //枪类型下隐藏
  65. if (GlobalData.MyDeviceMode == DeviceMode.Gun || CommonConfig.StandaloneModeOrPlatformB)
  66. {
  67. btnScaleShoot.gameObject.SetActive(false);
  68. }
  69. if (!CommonConfig.StandaloneModeOrPlatformB)
  70. {
  71. guideBtn.onClick.AddListener(delegate () {
  72. AudioMgr.ins.PlayBtn();
  73. GameRuleView.Create();
  74. });
  75. btnScaleAim.onClick.AddListener(delegate () {
  76. AudioMgr.ins.PlayBtn();
  77. if (btnScaleAim.GetComponentInChildren<Image>().material == outlight)
  78. {
  79. closeScaleAim();
  80. }
  81. else
  82. {
  83. bool success = openScaleAim();
  84. if (!success) ShowOpenScaleAimFailTip();
  85. }
  86. });
  87. btnScaleShoot.onClick.AddListener(delegate () {
  88. AudioMgr.ins.PlayBtn();
  89. if (btnScaleShoot.GetComponentInChildren<Image>().material == outlight)
  90. {
  91. closeScaleShoot();
  92. }
  93. else
  94. {
  95. bool success = openScaleShoot();
  96. if (!success) ShowOpenScaleShootFailTip();
  97. }
  98. });
  99. }
  100. Button btnIdentity = this.transform.Find("Button4").GetComponent<Button>();
  101. if (!CommonConfig.StandaloneModeOrPlatformB)
  102. {
  103. //手枪不显示视角归位,连接接红外也不显示视角归位
  104. if (GlobalData.MyDeviceMode == DeviceMode.Gun || BluetoothAim.ins && BluetoothAim.ins.isMainConnectToInfraredDevice())
  105. {
  106. btnIdentity.gameObject.SetActive(false);
  107. //显示控制准心按钮
  108. Button crossHairBtn = transform.Find("Button5").GetComponent<Button>();
  109. crossHairBtn.gameObject.SetActive(true);
  110. //设置准心是否显示
  111. if (InfraredDemo._ins) CrossHair.ins.SetOnlyShow(InfraredDemo._ins.getCrosshairValue() == 1);
  112. bool onInitOpen = CrossHair.ins.GetOpen() && CrossHair.ins.GetOnlyShow();
  113. Image crossHairImage = crossHairBtn.GetComponentInChildren<Image>();
  114. crossHairImage.material = onInitOpen ? outlight : null;
  115. crossHairBtn.onClick.AddListener(delegate () {
  116. AudioMgr.ins.PlayBtn();
  117. bool onlyShow = !CrossHair.ins.GetOnlyShow();
  118. CrossHair.ins.SetOnlyShow(onlyShow);
  119. //保存准心状态
  120. if (InfraredDemo._ins) InfraredDemo._ins.setCrosshairValue(onlyShow);
  121. if (onlyShow)
  122. {
  123. crossHairImage.material = outlight;
  124. }
  125. else
  126. {
  127. crossHairImage.material = null;
  128. }
  129. });
  130. //校准
  131. Button offsetBtn = transform.Find("Button6").GetComponent<Button>();
  132. offsetBtn.gameObject.SetActive(true);
  133. offsetBtn.onClick.AddListener(delegate () {
  134. AudioMgr.ins.PlayBtn();
  135. AutoResetView.DoIdentity();
  136. });
  137. }
  138. else
  139. {
  140. btnIdentity.gameObject.SetActive(true);
  141. btnIdentity.onClick.AddListener(delegate () {
  142. if (btnIdentity.GetComponent<LongPressMonitor>().isLongPress) return;
  143. AudioMgr.ins.PlayBtn();
  144. AutoResetView.DoIdentity();
  145. action_OnClickBtnIdentity?.Invoke();
  146. });
  147. btnIdentity.gameObject.AddComponent<LongPressMonitor>().onLongPress += () => {
  148. AudioMgr.ins.PlayBtn();
  149. if (SB_EventSystem.ins) SB_EventSystem.ins.AwakenSimulateMouse();
  150. };
  151. }
  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. if (ins == null) ins = null;
  169. SaveMyRecord();
  170. }
  171. void Update() {
  172. bool autoSwitch = true;
  173. int myPlayerIndex = GetMyPlayerIndex();
  174. int curPlayerIndex = GetCurPlayerIndex();
  175. // if (GlobalData.pkMatchType == PKMatchType.OnlinePK) {
  176. // if (myPlayerIndex == curPlayerIndex) {
  177. // autoSwitch = true;
  178. // } else {
  179. // autoSwitch = false;
  180. // //渲染别人的瞄准镜
  181. // DisplayScaleAim(playerScaleAimValue_OnlinePK > 0, playerScaleAimValue_OnlinePK);
  182. // }
  183. // }
  184. if (autoSwitch) {
  185. if (scaleAimOn != playerScaleAimRecords[curPlayerIndex]) {
  186. if (playerScaleAimRecords[curPlayerIndex]) {
  187. openScaleAim();
  188. } else {
  189. closeScaleAim();
  190. }
  191. }
  192. if (scaleShootOn != playerScaleShootRecords[curPlayerIndex]) {
  193. if (playerScaleShootRecords[curPlayerIndex]) {
  194. openScaleShoot();
  195. } else {
  196. closeScaleShoot();
  197. }
  198. }
  199. DisplayScaleAim(scaleAimOn, aimScaleValue);
  200. }
  201. }
  202. // bool IsOnLinePkAndNotMyTime() {
  203. // if (GlobalData.pkMatchType != PKMatchType.OnlinePK) return false;
  204. // int myPlayerIndex = GetMyPlayerIndex();
  205. // int curPlayerIndex = GetCurPlayerIndex();
  206. // return myPlayerIndex != curPlayerIndex;
  207. // }
  208. [System.NonSerialized] public int playerScaleAimValue_OnlinePK = 0;
  209. bool[] playerScaleAimRecords = {false, false};
  210. bool[] playerScaleShootRecords = {false, false};
  211. int GetMyPlayerIndex() {
  212. if (GlobalData.pkMatchType == PKMatchType.OnlinePK) {
  213. // return GlobalData.playerIndexInRoom;
  214. return 0;
  215. }
  216. return 0;
  217. }
  218. int GetCurPlayerIndex() {
  219. if (GlobalData.pkMatchType == PKMatchType.OnlinePK) {
  220. // GameMode gm = GameMgr.ins.gameMode;
  221. // if (GameMgr.gameType == 9) return ((PKGameMode_OnlinePK) gm).gameLogic.currentPlayerIndex;
  222. return 0;
  223. } else if (GlobalData.pkMatchType == PKMatchType.LocalPK) {
  224. GameMode gm = GameMgr.ins.gameMode;
  225. if (GameMgr.gameType == 2) return ((PKGameMode) gm).currentPlayerIndex;
  226. if (GameMgr.gameType == 6) return ((RabbitHuntGameMode_LocalPK) gm).currentPlayerIndex;
  227. if (GameMgr.gameType == 7) return ((YejiHuntGameMode_LocalPK) gm).currentPlayerIndex;
  228. if (GameMgr.gameType == 8) return ((WolfHuntGameMode_LocalPK) gm).currentPlayerIndex;
  229. }
  230. return 0;
  231. }
  232. void SaveMyRecord() {
  233. int gameType = 0; //多种模式共用记录
  234. int myPlayerIndex = GetMyPlayerIndex();
  235. bool isScaleAimOpen = playerScaleAimRecords[myPlayerIndex];
  236. bool isScaleShootOpen = playerScaleShootRecords[myPlayerIndex];
  237. PlayerPrefs.SetInt("ScaleAim," + gameType + "," + LoginMgr.myUserInfo.id, isScaleAimOpen ? 1 : 0);
  238. PlayerPrefs.SetInt("ScaleShoot," + gameType + "," + LoginMgr.myUserInfo.id, isScaleShootOpen ? 1 : 0);
  239. }
  240. void LoadMyRecord() {
  241. int gameType = 0; //多种模式共用记录
  242. if (CommonConfig.SpecialVersion1) {
  243. if (PlayerPrefs.GetInt("sv1_ScaleAimShoot," + LoginMgr.myUserInfo.id, 0) == 0) {
  244. PlayerPrefs.SetInt("sv1_ScaleAimShoot," + LoginMgr.myUserInfo.id, 1);
  245. PlayerPrefs.SetInt("ScaleAim," + gameType + "," + LoginMgr.myUserInfo.id, 0);
  246. PlayerPrefs.SetInt("ScaleShoot," + gameType + "," + LoginMgr.myUserInfo.id, 1);
  247. }
  248. }
  249. bool isScaleAimOpen = PlayerPrefs.GetInt("ScaleAim," + gameType + "," + LoginMgr.myUserInfo.id, 0) == 1 ? true : false;
  250. bool isScaleShootOpen = PlayerPrefs.GetInt("ScaleShoot," + gameType + "," + LoginMgr.myUserInfo.id, 0) == 1 ? true : false;
  251. if (isScaleAimOpen && GetPropScaleAimValue() == 0) {
  252. isScaleAimOpen = false;
  253. }
  254. if (isScaleShootOpen && getScaleShootProp() == null) {
  255. isScaleShootOpen = false;
  256. }
  257. int myPlayerIndex = GetMyPlayerIndex();
  258. playerScaleAimRecords[myPlayerIndex] = isScaleAimOpen;
  259. playerScaleShootRecords[myPlayerIndex] = isScaleShootOpen;
  260. if (isLocalPK()) {
  261. playerScaleAimRecords[(myPlayerIndex + 1) % 2] = isScaleAimOpen;
  262. playerScaleShootRecords[(myPlayerIndex + 1) % 2] = isScaleShootOpen;
  263. }
  264. }
  265. #region 本地双人PK,再来时保存记录
  266. private static bool[] playerRecords = null;
  267. private bool isLocalPK() {
  268. int[] gameTypes = {2, 6, 7, 8};
  269. if (System.Array.IndexOf(gameTypes, GameMgr.gameType) > -1) {
  270. return true;
  271. }
  272. return false;
  273. }
  274. public void recordPlayerRecordsWhenGameTryAgain() {
  275. if (!isLocalPK()) return;
  276. playerRecords = new bool[] {
  277. playerScaleAimRecords[0], playerScaleAimRecords[1],
  278. playerScaleShootRecords[0], playerScaleShootRecords[1]
  279. };
  280. }
  281. private void applyPlayerRecordsWhenGameTryAgain() {
  282. if (playerRecords != null) {
  283. playerScaleAimRecords[0] = playerRecords[0];
  284. playerScaleAimRecords[1] = playerRecords[1];
  285. playerScaleShootRecords[0] = playerRecords[2];
  286. playerScaleShootRecords[1] = playerRecords[3];
  287. playerRecords = null;
  288. }
  289. }
  290. #endregion
  291. #region 道具开关监听
  292. private void onOpenScaleAimSuccess() {
  293. playerScaleAimRecords[GetCurPlayerIndex()] = true;
  294. }
  295. private void onCloseScaleAimSuccess() {
  296. playerScaleAimRecords[GetCurPlayerIndex()] = false;
  297. }
  298. private void onOpenScaleShootSuccess() {
  299. playerScaleShootRecords[GetCurPlayerIndex()] = true;
  300. }
  301. private void onCloseScaleShootSuccess() {
  302. playerScaleShootRecords[GetCurPlayerIndex()] = false;
  303. }
  304. #endregion
  305. // ------ 开镜瞄准功能 ------
  306. [System.NonSerialized] public int aimScaleValue = 1;
  307. float[] scaleAimFieldOfViews = {30, 20, 12, 6, 3};
  308. float[] scaleAimScopeScales = {150, 98, 58, 29, 14.5f};
  309. Sequence seq1 = null;
  310. [System.NonSerialized] public bool scaleAimOn = false; //该功能是否处于打开状态
  311. bool openScaleAim()
  312. {
  313. int scaleValue = GetPropScaleAimValue();
  314. if (scaleValue > 0)
  315. {
  316. btnScaleAim.GetComponentInChildren<Image>().material = outlight;
  317. aimScaleValue = scaleValue;
  318. scaleAimOn = true;
  319. onOpenScaleAimSuccess();
  320. return true;
  321. }
  322. return false;
  323. }
  324. bool isScaleAimDisplaying = false;
  325. int aimScaleDisplayValue = -1;
  326. void DisplayScaleAim(bool open, int scaleValue) { //渲染
  327. if (isScaleAimDisplaying == open && aimScaleDisplayValue == scaleValue) return;
  328. isScaleAimDisplaying = open;
  329. aimScaleDisplayValue = scaleValue;
  330. BowCamera bowCamera = BowCamera.ins;
  331. Transform scope = bowCamera.transform.Find("Scope");
  332. if (open)
  333. {
  334. if (GlobalData.MyDeviceMode == DeviceMode.Gun)
  335. CrossHair.ins.gameObject.GetComponent<SwitchCrossHair>().ShowOldImage(true);
  336. CrossHair.ins.gameObject.GetComponent<RectTransform>().sizeDelta = new Vector2(500, 500);
  337. }
  338. else {
  339. if (GlobalData.MyDeviceMode == DeviceMode.Gun)
  340. {
  341. SwitchCrossHair switchCrossHair = CrossHair.ins.gameObject.GetComponent<SwitchCrossHair>();
  342. switchCrossHair.ShowOldImage(false);
  343. CrossHair.ins.gameObject.GetComponent<RectTransform>().sizeDelta = switchCrossHair.targetImageSize;
  344. }
  345. else {
  346. CrossHair.ins.gameObject.GetComponent<RectTransform>().sizeDelta = new Vector2(260, 260);
  347. }
  348. }
  349. if (open) CrossHair.ins.transform.localPosition = Vector3.zero;
  350. bowCamera.banCameraFieldOfView = open;
  351. bowCamera.SetScaleAimDisplaying(isScaleAimDisplaying);
  352. if (open) {
  353. bowCamera.SetCameraFieldOfView(scaleAimFieldOfViews[scaleValue - 1]);
  354. }
  355. Vector3 localPosition = ArmBow.ins.transform.localPosition;
  356. localPosition.z = open ? -2 : ArmBow.localPosZ;
  357. ArmBow.ins.transform.localPosition = localPosition;
  358. if (open) {
  359. float scopeScale = scaleAimScopeScales[scaleValue - 1];
  360. scope.localScale = new Vector3(scopeScale, scopeScale, scopeScale);
  361. } else {
  362. scope.localScale = new Vector3(0, 0, 0);
  363. }
  364. }
  365. void ShowOpenScaleAimFailTip() {
  366. if (seq1 != null && !seq1.IsComplete()) {
  367. seq1.Complete();
  368. }
  369. seq1 = DOTween.Sequence();
  370. seq1.Append(text1.DOFade(1, 0.5f));
  371. seq1.AppendInterval(2);
  372. seq1.Append(text1.DOFade(0, 0.5f));
  373. }
  374. void closeScaleAim()
  375. {
  376. btnScaleAim.GetComponentInChildren<Image>().material = null;
  377. scaleAimOn = false;
  378. onCloseScaleAimSuccess();
  379. }
  380. int GetPropScaleAimValue()
  381. {
  382. List<PropInfo> props = PropMgr.ins.ListForEquipped();
  383. foreach (var prop in props)
  384. {
  385. if (prop.config.type == 1) {
  386. PropScaleAim config = prop.config as PropScaleAim;
  387. return config.scaleValue;
  388. }
  389. }
  390. return 0;
  391. }
  392. // ------ 发射加速功能 ------
  393. [System.NonSerialized] public int shootScaleValue = 1;
  394. Sequence seq2 = null;
  395. bool scaleShootOn = false; //该功能是否处于打开状态
  396. bool openScaleShoot()
  397. {
  398. PropInfo prop = getScaleShootProp();
  399. if (prop != null) {
  400. btnScaleShoot.GetComponentInChildren<Image>().material = outlight;
  401. PropScaleShoot config = prop.config as PropScaleShoot;
  402. shootScaleValue = config.scaleValue;
  403. scaleShootOn = true;
  404. onOpenScaleShootSuccess();
  405. return true;
  406. }
  407. return false;
  408. }
  409. void ShowOpenScaleShootFailTip() {
  410. if (seq2 != null && !seq2.IsComplete()) {
  411. seq2.Complete();
  412. }
  413. seq2 = DOTween.Sequence();
  414. seq2.Append(text2.DOFade(1, 0.5f));
  415. seq2.AppendInterval(2);
  416. seq2.Append(text2.DOFade(0, 0.5f));
  417. }
  418. void closeScaleShoot()
  419. {
  420. btnScaleShoot.GetComponentInChildren<Image>().material = null;
  421. shootScaleValue = 1;
  422. scaleShootOn = false;
  423. onCloseScaleShootSuccess();
  424. }
  425. PropInfo getScaleShootProp() {
  426. List<PropInfo> props = PropMgr.ins.ListForEquipped();
  427. foreach (var prop in props)
  428. {
  429. if (prop.config.type == 2) {
  430. return prop;
  431. }
  432. }
  433. return null;
  434. }
  435. }