GameAssistUI.cs 13 KB

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