GameAssistUI.cs 12 KB

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