GameAssistUI.cs 18 KB

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