LoginMgr.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Newtonsoft.Json;
  5. using JCUnityLib;
  6. /* 登录管理者,用户数据定义和存储 */
  7. public class LoginMgr : MonoBehaviour
  8. {
  9. [SerializeField] GameObject loginView;
  10. [SerializeField] GameObject registerView;
  11. public static UserInfo myUserInfo = new UserInfo();
  12. public void showRegisterView()
  13. {
  14. loginView.SetActive(false);
  15. registerView.SetActive(true);
  16. }
  17. public void showLoginView()
  18. {
  19. loginView.SetActive(true);
  20. registerView.SetActive(false);
  21. }
  22. public void showForgetPWD_View()
  23. {
  24. Instantiate(SceneResourceManager.Instance.GetPrefab("RetrievePasswordView"));
  25. }
  26. public const string LoginTokenKey = "LoginToken";
  27. public static bool HasToken()
  28. {
  29. string loginToken = PlayerPrefs.GetString(LoginMgr.LoginTokenKey, "");
  30. return string.IsNullOrEmpty(loginToken) ? false : true;
  31. }
  32. void Awake()
  33. {
  34. transform.Find("AgreementPopup").gameObject.SetActive(true);
  35. ViewMgr.Instance.DestroyAllViews();
  36. }
  37. void Start()
  38. {
  39. //退出到登录界面,也要把蓝牙断开
  40. if (BluetoothAim.ins) {
  41. if (BluetoothAim.ins.status == BluetoothStatusEnum.ConnectSuccess) BluetoothAim.ins.DoConnect();
  42. else BluetoothAim.ins.connectCanceled = true;
  43. }
  44. }
  45. }
  46. public class UserInfo
  47. {
  48. public int id;
  49. public int avatarID = 0;
  50. public string avatarUrl = "";
  51. public string nickname = "超级射手";
  52. public int gender = 1;
  53. public string phone = "";
  54. public string email = "";
  55. public string birthday = "";
  56. public string country = "";
  57. public string state = "";
  58. public string city = "";
  59. public int integral = 0;
  60. public int coin = 0;
  61. public int diamond = 1000;
  62. public string mac = "";
  63. /// <summary>
  64. /// 显示游戏最高分(不同距离分数独立)
  65. /// 2023-7-16兼容WildAttack最高分纪录
  66. /// </summary>
  67. public Dictionary<string, float> timeLimitGameScores = new Dictionary<string, float>();
  68. //闯关记录(gameType:通关数)(野兔、野鸡、野狼的通关数)
  69. public Dictionary<int, int> challengeLevels = new Dictionary<int, int>();
  70. public string guideRecord = "";
  71. public static UserInfo LoadLocal(int id)
  72. {
  73. UserInfo result = null;
  74. if (CommonConfig.StandaloneMode)
  75. {
  76. try
  77. {
  78. result = JsonConvert.DeserializeObject<UserInfo>(PlayerPrefs.GetString("UserInfo_" + id));
  79. if (result == null) throw new Exception("UserInfo.LoadLocal Null");
  80. }
  81. catch (Exception e)
  82. {
  83. Debug.LogError(e);
  84. }
  85. }
  86. return result;
  87. }
  88. public void Save()
  89. {
  90. if (CommonConfig.StandaloneMode)
  91. {
  92. PlayerPrefs.SetString("UserInfo_" + id, JsonConvert.SerializeObject(this));
  93. return;
  94. }
  95. }
  96. public void SetChallengeLevelPass(int gameType, int level)
  97. {
  98. if (gameType != 3 && gameType != 4 && gameType != 5) return;
  99. if (challengeLevels.ContainsKey(gameType))
  100. {
  101. if (level <= challengeLevels[gameType])
  102. {
  103. return;
  104. }
  105. }
  106. challengeLevels.Remove(gameType);
  107. challengeLevels.Add(gameType, level);
  108. }
  109. public int GetChallengeLevelPass(int gameType)
  110. {
  111. if (challengeLevels.ContainsKey(gameType))
  112. {
  113. return challengeLevels[gameType];
  114. }
  115. return 0;
  116. }
  117. //判断引导是否完成(服务端保存)
  118. //index0: 新手引导NewUserGuide
  119. public bool IsGuideFinish(int index)
  120. {
  121. if (index == 0)
  122. {
  123. return PlayerPrefs.GetInt("NewUserGuideFinish_" + LoginMgr.myUserInfo.id, 0) == 1;
  124. }
  125. char[] chars = guideRecord.ToCharArray();
  126. if (index < chars.Length)
  127. {
  128. return chars[index] == '1';
  129. }
  130. return false;
  131. }
  132. public void SaveGuideFinish(int index)
  133. {
  134. if (index == 0)
  135. {
  136. PlayerPrefs.SetInt("NewUserGuideFinish_" + LoginMgr.myUserInfo.id, 1);
  137. return;
  138. }
  139. char[] chars = guideRecord.ToCharArray();
  140. if (index < chars.Length)
  141. {
  142. if (chars[index] == '1') return;
  143. chars[index] = '1';
  144. }
  145. else
  146. {
  147. int newLen = index + 1;
  148. char[] newChars = new char[newLen];
  149. for (int i = 0; i < newLen; i++)
  150. {
  151. newChars[i] = i < chars.Length ? chars[i] : '0';
  152. }
  153. newChars[index] = '1';
  154. chars = newChars;
  155. }
  156. this.guideRecord = string.Join("", chars);
  157. }
  158. }
  159. public class UserSettings
  160. {
  161. //打开BGM
  162. public bool openBGM = true;
  163. //打开音效
  164. public bool openEffect = true;
  165. //是否打开准心
  166. public bool openCrossHair = true;
  167. //射击难度
  168. public int shootLevel = 0;
  169. //游戏里的箭重,单位克
  170. public float actualArrowWeight = 20;
  171. //弓箭旋转转化
  172. public BowRotateConvert bowRotateConvert = new BowRotateConvert();
  173. //游戏中是否固定镜头
  174. public bool bowCameraFixed = true;
  175. //训练模式
  176. public bool trainMode = false;
  177. //设备校准引导-是否已经完成
  178. public bool deviceCalibrateGuideFinish = false;
  179. //游戏规则引导-是否已经完成(完成则保存对应的GameType)
  180. public HashSet<int> gameRuleGuideFinish = new HashSet<int>();
  181. private static UserSettings _ins;
  182. public static UserSettings ins
  183. {
  184. get
  185. {
  186. if (_ins == null)
  187. {
  188. string dataStr = PlayerPrefs.GetString("UserSettings", "{}");
  189. try
  190. {
  191. _ins = JsonConvert.DeserializeObject<UserSettings>(dataStr);
  192. }
  193. catch (System.Exception) { }
  194. if (_ins == null)
  195. {
  196. _ins = new UserSettings();
  197. }
  198. if (CommonConfig.SpecialVersion1)
  199. {
  200. if (PlayerPrefs.GetInt("sv1_UserSettings_2", 0) == 0)
  201. {
  202. PlayerPrefs.SetInt("sv1_UserSettings_2", 1);
  203. UserSettings us = _ins;
  204. us.bowRotateConvert.screenSize = 60;
  205. us.bowRotateConvert.screenDistance = 1.5f;
  206. us.Save();
  207. }
  208. }
  209. }
  210. return _ins;
  211. }
  212. }
  213. public void Save()
  214. {
  215. PlayerPrefs.SetString("UserSettings", JsonConvert.SerializeObject(this));
  216. }
  217. }
  218. /*
  219. 描述1
  220. 已知:
  221. 屏幕宽高比例w:h=16:9,屏幕尺寸s=60.11英寸,屏幕距离d=2.50米,实际指向右边树的角度为e=5.56°。
  222. 可知:
  223. 屏幕尺寸s1 = (s * 0.0254)米
  224. 屏幕单位大小为unit,关系式(w*unit)^2 + (h*unit)^2 = s1^2
  225. 结果:
  226. 向量(玩家->右边树)在屏幕上投影的长度比例q = tan(e) * d / unit
  227. */
  228. /*
  229. 描述2(适配各种尺寸的屏幕)
  230. 已知:
  231. 屏幕宽高比例w:h=16:9,屏幕尺寸s=(任意)英寸,屏幕距离d=(任意)米,游戏指向右边树的角度为r=27.30°。
  232. 借用描述1的结果q
  233. 可知:
  234. 屏幕尺寸s1 = (s * 0.0254)米
  235. 屏幕单位大小为unit,关系式(w*unit)^2 + (h*unit)^2 = s1^2
  236. 实际指向右边树的角度为e = atan(q * unit / d)
  237. 结果:
  238. 游戏转动角度:实际转动角度 = r / e
  239. */
  240. public class BowRotateConvert
  241. {
  242. public float screenSize = 60; //屏幕尺寸(英寸)
  243. public float screenDistance = 2.5f; //玩家距离屏幕多远(米)
  244. [NonSerialized] public float fieldOfView = 25;
  245. //获取建议的屏幕距离
  246. public float GetAdviseScreenDistance()
  247. {
  248. float w = 16;
  249. float h = 9;
  250. float s1 = screenSize * 0.0254f;
  251. float unit = s1 / Mathf.Sqrt(w * w + h * h);
  252. float screenHeight = 9 * unit;
  253. return screenHeight * 0.5f / Mathf.Tan(fieldOfView / 2 / 180 * Mathf.PI);
  254. }
  255. // 游戏旋转角度 : 实际旋转角度 (这个版本丢弃掉这个功能-所以直接返回1)
  256. public float GetRate()
  257. {
  258. return 1;
  259. }
  260. // 游戏旋转角度 : 实际旋转角度
  261. // public float GetRate() {
  262. // double w = 16;
  263. // double h = 9;
  264. // double s = Convert.ToDouble(screenSize);
  265. // double d = Convert.ToDouble(screenDistance);
  266. // double r = 27.3 / 180 * Math.PI;
  267. // double q = get_q();
  268. // double s1 = s * 0.0254;
  269. // double unit = Math.Sqrt(Math.Pow(s1, 2) / (Math.Pow(w, 2) + Math.Pow(h, 2)));
  270. // double e = Math.Atan(q * unit / d);
  271. // return (float) (r / e);
  272. // }
  273. // private double get_q() {
  274. // double w = 16;
  275. // double h = 9;
  276. // double s = 60.11;
  277. // double d = 2.5;
  278. // double e = 5.56 / 180 * Math.PI;
  279. // double s1 = s * 0.0254;
  280. // double unit = Math.Sqrt(Math.Pow(s1, 2) / (Math.Pow(w, 2) + Math.Pow(h, 2)));
  281. // double q = Math.Tan(e) * d / unit;
  282. // return q;
  283. // }
  284. }