GameAssistUI.cs 13 KB

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