GameAssistUI.cs 16 KB

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