GameAssistUI.cs 12 KB

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