GameAssistUI.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. openScaleAim();
  36. }
  37. });
  38. btnScaleShoot = this.transform.Find("Button3").GetComponent<Button>();
  39. btnScaleShoot.onClick.AddListener(delegate(){
  40. AudioMgr.ins.PlayBtn();
  41. if (btnScaleShoot.GetComponentInChildren<Image>().material == outlight) {
  42. closeScaleShoot();
  43. } else {
  44. openScaleShoot();
  45. }
  46. });
  47. Button btnIdentity = this.transform.Find("Button4").GetComponent<Button>();
  48. btnIdentity.onClick.AddListener(delegate(){
  49. AudioMgr.ins.PlayBtn();
  50. AimHandler.ins.DoIdentity();
  51. });
  52. // ------ 查看靶子 ------
  53. Transform targetView = this.transform.Find("TargetView");
  54. Button btnViewTarget = this.transform.Find("Button10").GetComponent<Button>();
  55. btnViewTarget.onClick.AddListener(delegate(){
  56. AudioMgr.ins.PlayBtn();
  57. TargetView.ins.ReverseActive();
  58. });
  59. if (GameMgr.gameType == 2) {
  60. targetView.transform.GetComponent<RectTransform>().anchoredPosition = new Vector2(45, 30);
  61. btnViewTarget.transform.GetComponent<RectTransform>().anchoredPosition = new Vector2(45, 195);
  62. }
  63. //看看是不是双人游戏的再次对战,从静态内存中读取功能的开关记录
  64. applyPlayerRecordsWhenGameTryAgain();
  65. //看看是不是其它模式,从本地数据库读取开关记录
  66. applyScaleAimFromRecord();
  67. applyScaleShootFromRecord();
  68. }
  69. // ------ 开镜瞄准功能 ------
  70. Transform scope = null;
  71. float[] scaleAimFieldOfViews = {30, 20, 12, 6, 3};
  72. float[] scaleAimScopeScales = {150, 98, 58, 29, 14.5f};
  73. Sequence seq1 = null;
  74. bool scaleAimOn = false; //该功能是否处于打开状态
  75. bool openScaleAim()
  76. {
  77. int scaleValue = GetPropScaleAimValue();
  78. if (scaleValue > 0)
  79. {
  80. BowCamera bowCamera = GameObject.FindObjectOfType<BowCamera>();
  81. bowCamera.banCameraFieldOfView = true;
  82. CrossHair.ins.gameObject.GetComponent<RectTransform>().sizeDelta = new Vector2(500, 500);
  83. bowCamera.SetCameraFieldOfView(scaleAimFieldOfViews[scaleValue - 1]);
  84. Vector3 localPosition = ArmBow.ins.transform.localPosition;
  85. localPosition.z = -2;
  86. ArmBow.ins.transform.localPosition = localPosition;
  87. scope = bowCamera.transform.Find("Scope");
  88. float scopeScale = scaleAimScopeScales[scaleValue - 1];
  89. scope.localScale = new Vector3(scopeScale, scopeScale, scopeScale);
  90. scaleAimOn = true;
  91. btnScaleAim.GetComponentInChildren<Image>().material = outlight;
  92. onOpenScaleAimSuccess();
  93. return true;
  94. }
  95. if (seq1 != null && !seq1.IsComplete()) {
  96. seq1.Complete();
  97. }
  98. seq1 = DOTween.Sequence();
  99. seq1.Append(text1.DOFade(1, 0.5f));
  100. seq1.AppendInterval(2);
  101. seq1.Append(text1.DOFade(0, 0.5f));
  102. return false;
  103. }
  104. void closeScaleAim()
  105. {
  106. if (!scope) return;
  107. btnScaleAim.GetComponentInChildren<Image>().material = null;
  108. BowCamera bowCamera = GameObject.FindObjectOfType<BowCamera>();
  109. bowCamera.banCameraFieldOfView = false;
  110. CrossHair.ins.gameObject.GetComponent<RectTransform>().sizeDelta = new Vector2(260, 260);
  111. Vector3 localPosition = ArmBow.ins.transform.localPosition;
  112. localPosition.z = -0.1f;
  113. ArmBow.ins.transform.localPosition = localPosition;
  114. scope.localScale = new Vector3(0, 0, 0);
  115. scope = null;
  116. scaleAimOn = false;
  117. onCloseScaleAimSuccess();
  118. }
  119. int GetPropScaleAimValue()
  120. {
  121. List<PropInfo> props = PropMgr.ins.ListForEquipped();
  122. foreach (var prop in props)
  123. {
  124. if (prop.config.type == 1) {
  125. PropScaleAim config = prop.config as PropScaleAim;
  126. return config.scaleValue;
  127. }
  128. }
  129. return 0;
  130. }
  131. // ------ 发射加速功能 ------
  132. [System.NonSerialized] public int shootScaleValue = 1;
  133. Sequence seq2 = null;
  134. bool scaleShootOn = false; //该功能是否处于打开状态
  135. bool openScaleShoot()
  136. {
  137. PropInfo prop = getScaleShootProp();
  138. if (prop != null) {
  139. PropScaleShoot config = prop.config as PropScaleShoot;
  140. shootScaleValue = config.scaleValue;
  141. if (GameDebug.ins) GameDebug.ins.caluculateAbsoluteAngle();
  142. scaleShootOn = true;
  143. btnScaleShoot.GetComponentInChildren<Image>().material = outlight;
  144. onOpenScaleShootSuccess();
  145. return true;
  146. }
  147. if (seq2 != null && !seq2.IsComplete()) {
  148. seq2.Complete();
  149. }
  150. seq2 = DOTween.Sequence();
  151. seq2.Append(text2.DOFade(1, 0.5f));
  152. seq2.AppendInterval(2);
  153. seq2.Append(text2.DOFade(0, 0.5f));
  154. return false;
  155. }
  156. void closeScaleShoot()
  157. {
  158. btnScaleShoot.GetComponentInChildren<Image>().material = null;
  159. shootScaleValue = 1;
  160. if (GameDebug.ins) GameDebug.ins.caluculateAbsoluteAngle();
  161. scaleShootOn = false;
  162. onCloseScaleShootSuccess();
  163. }
  164. PropInfo getScaleShootProp() {
  165. List<PropInfo> props = PropMgr.ins.ListForEquipped();
  166. foreach (var prop in props)
  167. {
  168. if (prop.config.type == 2) {
  169. return prop;
  170. }
  171. }
  172. return null;
  173. }
  174. //---道具功能开关监听---
  175. private void onOpenScaleAimSuccess() {
  176. if (GameMgr.gameType == 2) {
  177. playerScaleAimRecords[getPlayerIndex()] = true;
  178. } else {
  179. saveScaleAim(true);
  180. }
  181. }
  182. private void onCloseScaleAimSuccess() {
  183. if (GameMgr.gameType == 2) {
  184. playerScaleAimRecords[getPlayerIndex()] = false;
  185. } else {
  186. saveScaleAim(false);
  187. }
  188. }
  189. private void onOpenScaleShootSuccess() {
  190. if (GameMgr.gameType == 2) {
  191. playerScaleShootRecords[getPlayerIndex()] = true;
  192. } else {
  193. saveScaleShoot(true);
  194. }
  195. }
  196. private void onCloseScaleShootSuccess() {
  197. if (GameMgr.gameType == 2) {
  198. playerScaleShootRecords[getPlayerIndex()] = false;
  199. } else {
  200. saveScaleShoot(false);
  201. }
  202. }
  203. //---以下为其它模式提供的道具开关记录和应用接口
  204. private void saveScaleAim(bool isOpen) {
  205. if (GameMgr.gameType == 2) return;
  206. int gameType = 0; //多种模式共用记录
  207. PlayerPrefs.SetInt("ScaleAim," + gameType + "," + LoginMgr.myUserInfo.user, isOpen ? 1 : 0);
  208. }
  209. private void applyScaleAimFromRecord() {
  210. if (GameMgr.gameType == 2) return;
  211. int gameType = 0; //多种模式共用记录
  212. int saveValue = PlayerPrefs.GetInt("ScaleAim," + gameType + "," + LoginMgr.myUserInfo.user, 0);
  213. if (saveValue == 1) {
  214. if (GetPropScaleAimValue() == 0) {
  215. saveScaleAim(false);
  216. } else {
  217. openScaleAim();
  218. }
  219. } else {
  220. closeScaleAim();
  221. }
  222. }
  223. private void saveScaleShoot(bool isOpen) {
  224. if (GameMgr.gameType == 2) return;
  225. int gameType = 0; //多种模式共用记录
  226. PlayerPrefs.SetInt("ScaleShoot," + gameType + "," + LoginMgr.myUserInfo.user, isOpen ? 1 : 0);
  227. }
  228. private void applyScaleShootFromRecord() {
  229. if (GameMgr.gameType == 2) return;
  230. int gameType = 0; //多种模式共用记录
  231. int saveValue = PlayerPrefs.GetInt("ScaleShoot," + gameType + "," + LoginMgr.myUserInfo.user, 0);
  232. if (saveValue == 1) {
  233. if (getScaleShootProp() == null) {
  234. saveScaleShoot(false);
  235. } else {
  236. openScaleShoot();
  237. }
  238. } else {
  239. closeScaleShoot();
  240. }
  241. }
  242. //------ 以下给双人模式分别记录提供的接口 ------
  243. //两位玩家的开镜情况
  244. bool[] playerScaleAimRecords = {false, false};
  245. //两位玩家的加速情况
  246. bool[] playerScaleShootRecords = {false, false};
  247. private int getPlayerIndex() {
  248. return ((PKGameMode) GameMgr.ins.gameMode).currentPlayerIndex;
  249. }
  250. private void updateFunctionByPlayerRecords() {
  251. if (GameMgr.gameType != 2) return;
  252. int playerIndex = getPlayerIndex();
  253. if (scaleAimOn != playerScaleAimRecords[playerIndex]) {
  254. if (playerScaleAimRecords[playerIndex]) {
  255. openScaleAim();
  256. } else {
  257. closeScaleAim();
  258. }
  259. }
  260. if (scaleShootOn != playerScaleShootRecords[playerIndex]) {
  261. if (playerScaleShootRecords[playerIndex]) {
  262. openScaleShoot();
  263. } else {
  264. closeScaleShoot();
  265. }
  266. }
  267. }
  268. private static bool[] playerRecords = null;
  269. public void recordPlayerRecordsWhenGameTryAgain() {
  270. if (GameMgr.gameType != 2) return;
  271. playerRecords = new bool[] {
  272. playerScaleAimRecords[0], playerScaleAimRecords[1],
  273. playerScaleShootRecords[0], playerScaleShootRecords[1]
  274. };
  275. }
  276. private void applyPlayerRecordsWhenGameTryAgain() {
  277. if (playerRecords != null) {
  278. playerScaleAimRecords[0] = playerRecords[0];
  279. playerScaleAimRecords[1] = playerRecords[1];
  280. playerScaleShootRecords[0] = playerRecords[2];
  281. playerScaleShootRecords[1] = playerRecords[3];
  282. playerRecords = null;
  283. }
  284. }
  285. void Update() {
  286. updateFunctionByPlayerRecords();
  287. }
  288. }