LoginMgr.cs 8.6 KB

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