GameAssistUI.cs 13 KB

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