GameAssistUI.cs 13 KB

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