LoginMgr.cs 8.8 KB

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