GameAssistUI.cs 11 KB

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