GameAssistUI.cs 18 KB

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