GameAssistUI.cs 14 KB

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