GameAssistUI.cs 15 KB

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