GameAssistUI.cs 17 KB

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