GameAssistUI.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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("EnterTheInfraredScene", 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. defaultOpenScaleShoot();
  53. Button btnIdentity = this.transform.Find("Button4").GetComponent<Button>();
  54. btnIdentity.onClick.AddListener(delegate(){
  55. if (btnIdentity.GetComponent<LongPressMonitor>().isLongPress) return;
  56. AudioMgr.ins.PlayBtn();
  57. AutoResetView.DoIdentity();
  58. action_OnClickBtnIdentity?.Invoke();
  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. }
  138. return 0;
  139. }
  140. void SaveMyRecord() {
  141. int gameType = 0; //多种模式共用记录
  142. int myPlayerIndex = GetMyPlayerIndex();
  143. bool isScaleAimOpen = playerScaleAimRecords[myPlayerIndex];
  144. bool isScaleShootOpen = playerScaleShootRecords[myPlayerIndex];
  145. }
  146. void LoadMyRecord() {
  147. int gameType = 0; //多种模式共用记录
  148. bool isScaleAimOpen = true;
  149. bool isScaleShootOpen = true;
  150. if (isScaleAimOpen && GetPropScaleAimValue() == 0) {
  151. isScaleAimOpen = false;
  152. }
  153. if (isScaleShootOpen) {
  154. isScaleShootOpen = false;
  155. }
  156. int myPlayerIndex = GetMyPlayerIndex();
  157. playerScaleAimRecords[myPlayerIndex] = isScaleAimOpen;
  158. playerScaleShootRecords[myPlayerIndex] = isScaleShootOpen;
  159. if (isLocalPK()) {
  160. playerScaleAimRecords[(myPlayerIndex + 1) % 2] = isScaleAimOpen;
  161. playerScaleShootRecords[(myPlayerIndex + 1) % 2] = isScaleShootOpen;
  162. }
  163. }
  164. #region 本地双人PK,再来时保存记录
  165. private static bool[] playerRecords = null;
  166. private bool isLocalPK() {
  167. int[] gameTypes = {2, 6, 7, 8};
  168. if (System.Array.IndexOf(gameTypes, GameMgr.gameType) > -1) {
  169. return true;
  170. }
  171. return false;
  172. }
  173. public void recordPlayerRecordsWhenGameTryAgain() {
  174. if (!isLocalPK()) return;
  175. playerRecords = new bool[] {
  176. playerScaleAimRecords[0], playerScaleAimRecords[1],
  177. playerScaleShootRecords[0], playerScaleShootRecords[1]
  178. };
  179. }
  180. private void applyPlayerRecordsWhenGameTryAgain() {
  181. if (playerRecords != null) {
  182. playerScaleAimRecords[0] = playerRecords[0];
  183. playerScaleAimRecords[1] = playerRecords[1];
  184. playerScaleShootRecords[0] = playerRecords[2];
  185. playerScaleShootRecords[1] = playerRecords[3];
  186. playerRecords = null;
  187. }
  188. }
  189. #endregion
  190. #region 道具开关监听
  191. private void onOpenScaleAimSuccess() {
  192. playerScaleAimRecords[GetCurPlayerIndex()] = true;
  193. }
  194. private void onCloseScaleAimSuccess() {
  195. playerScaleAimRecords[GetCurPlayerIndex()] = false;
  196. }
  197. private void onOpenScaleShootSuccess() {
  198. playerScaleShootRecords[GetCurPlayerIndex()] = true;
  199. }
  200. private void onCloseScaleShootSuccess() {
  201. playerScaleShootRecords[GetCurPlayerIndex()] = false;
  202. }
  203. #endregion
  204. // ------ 开镜瞄准功能 ------
  205. [System.NonSerialized] public int aimScaleValue = 1;
  206. float[] scaleAimFieldOfViews = {30, 20, 12, 6, 3};
  207. float[] scaleAimScopeScales = {150, 98, 58, 29, 14.5f};
  208. Sequence seq1 = null;
  209. [System.NonSerialized] public bool scaleAimOn = false; //该功能是否处于打开状态
  210. bool openScaleAim()
  211. {
  212. int scaleValue = GetPropScaleAimValue();
  213. if (scaleValue > 0)
  214. {
  215. btnScaleAim.GetComponentInChildren<Image>().material = outlight;
  216. aimScaleValue = scaleValue;
  217. scaleAimOn = false;
  218. onOpenScaleAimSuccess();
  219. return true;
  220. }
  221. return false;
  222. }
  223. bool isScaleAimDisplaying = false;
  224. int aimScaleDisplayValue = -1;
  225. void DisplayScaleAim(bool open, int scaleValue) { //渲染
  226. if (isScaleAimDisplaying == open && aimScaleDisplayValue == scaleValue) return;
  227. isScaleAimDisplaying = open;
  228. aimScaleDisplayValue = scaleValue;
  229. BowCamera bowCamera = BowCamera.ins;
  230. Transform scope = bowCamera.transform.Find("Scope");
  231. CrossHair.ins.gameObject.GetComponent<RectTransform>().sizeDelta =
  232. open ? new Vector2(500, 500) : new Vector2(260, 260);
  233. if (open) CrossHair.ins.transform.localPosition = Vector3.zero;
  234. bowCamera.banCameraFieldOfView = open;
  235. bowCamera.SetScaleAimDisplaying(isScaleAimDisplaying);
  236. if (open) {
  237. bowCamera.SetCameraFieldOfView(scaleAimFieldOfViews[scaleValue - 1]);
  238. }
  239. Vector3 localPosition = ArmBow.ins.transform.localPosition;
  240. localPosition.z = open ? -2 : ArmBow.localPosZ;
  241. ArmBow.ins.transform.localPosition = localPosition;
  242. if (open) {
  243. float scopeScale = scaleAimScopeScales[scaleValue - 1];
  244. scope.localScale = new Vector3(scopeScale, scopeScale, scopeScale);
  245. } else {
  246. scope.localScale = new Vector3(0, 0, 0);
  247. }
  248. }
  249. void ShowOpenScaleAimFailTip() {
  250. if (seq1 != null && !seq1.IsComplete()) {
  251. seq1.Complete();
  252. }
  253. seq1 = DOTween.Sequence();
  254. seq1.Append(text1.DOFade(1, 0.5f));
  255. seq1.AppendInterval(2);
  256. seq1.Append(text1.DOFade(0, 0.5f));
  257. }
  258. void closeScaleAim()
  259. {
  260. btnScaleAim.GetComponentInChildren<Image>().material = null;
  261. scaleAimOn = false;
  262. onCloseScaleAimSuccess();
  263. }
  264. int GetPropScaleAimValue()
  265. {
  266. return 1;
  267. }
  268. // ------ 发射加速功能 ------
  269. [System.NonSerialized] public int shootScaleValue = 1;
  270. Sequence seq2 = null;
  271. bool scaleShootOn = false; //该功能是否处于打开状态
  272. void defaultOpenScaleShoot()
  273. {
  274. btnScaleShoot.GetComponentInChildren<Image>().material = outlight;
  275. shootScaleValue = 5;
  276. }
  277. bool openScaleShoot()
  278. {
  279. btnScaleShoot.GetComponentInChildren<Image>().material = outlight;
  280. shootScaleValue = 5;
  281. return true;
  282. }
  283. void ShowOpenScaleShootFailTip() {
  284. if (seq2 != null && !seq2.IsComplete()) {
  285. seq2.Complete();
  286. }
  287. seq2 = DOTween.Sequence();
  288. seq2.Append(text2.DOFade(1, 0.5f));
  289. seq2.AppendInterval(2);
  290. seq2.Append(text2.DOFade(0, 0.5f));
  291. }
  292. void closeScaleShoot()
  293. {
  294. btnScaleShoot.GetComponentInChildren<Image>().material = null;
  295. shootScaleValue = 1;
  296. scaleShootOn = false;
  297. onCloseScaleShootSuccess();
  298. }
  299. }