GameAssistUI.cs 14 KB

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