LoginMgr.cs 8.7 KB

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