GameAssistUI.cs 17 KB

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