LoginMgr.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 static UserInfo LoadLocal(int id)
  76. {
  77. UserInfo result = null;
  78. if (CommonConfig.StandaloneMode)
  79. {
  80. try
  81. {
  82. result = JsonConvert.DeserializeObject<UserInfo>(PlayerPrefs.GetString("UserInfo_" + id));
  83. if (result == null) throw new Exception("UserInfo.LoadLocal Null");
  84. }
  85. catch (Exception e)
  86. {
  87. Debug.LogError(e);
  88. }
  89. }
  90. return result;
  91. }
  92. public void Save()
  93. {
  94. if (CommonConfig.StandaloneMode)
  95. {
  96. PlayerPrefs.SetString("UserInfo_" + id, JsonConvert.SerializeObject(this));
  97. return;
  98. }
  99. try { UserComp.Instance.saveUserInfo(this); } catch (System.Exception e) { Debug.LogError(e.Message); }
  100. }
  101. public void SetChallengeLevelPass(int gameType, int level)
  102. {
  103. if (gameType != 3 && gameType != 4 && gameType != 5) return;
  104. if (challengeLevels.ContainsKey(gameType))
  105. {
  106. if (level <= challengeLevels[gameType])
  107. {
  108. return;
  109. }
  110. }
  111. challengeLevels.Remove(gameType);
  112. challengeLevels.Add(gameType, level);
  113. }
  114. public int GetChallengeLevelPass(int gameType)
  115. {
  116. if (challengeLevels.ContainsKey(gameType))
  117. {
  118. return challengeLevels[gameType];
  119. }
  120. return 0;
  121. }
  122. //判断引导是否完成(服务端保存)
  123. //index0: 新手引导NewUserGuide
  124. public bool IsGuideFinish(int index)
  125. {
  126. if (index == 0)
  127. {
  128. return PlayerPrefs.GetInt("NewUserGuideFinish_" + LoginMgr.myUserInfo.id, 0) == 1;
  129. }
  130. char[] chars = guideRecord.ToCharArray();
  131. if (index < chars.Length)
  132. {
  133. return chars[index] == '1';
  134. }
  135. return false;
  136. }
  137. public void SaveGuideFinish(int index)
  138. {
  139. if (index == 0)
  140. {
  141. PlayerPrefs.SetInt("NewUserGuideFinish_" + LoginMgr.myUserInfo.id, 1);
  142. return;
  143. }
  144. char[] chars = guideRecord.ToCharArray();
  145. if (index < chars.Length)
  146. {
  147. if (chars[index] == '1') return;
  148. chars[index] = '1';
  149. }
  150. else
  151. {
  152. int newLen = index + 1;
  153. char[] newChars = new char[newLen];
  154. for (int i = 0; i < newLen; i++)
  155. {
  156. newChars[i] = i < chars.Length ? chars[i] : '0';
  157. }
  158. newChars[index] = '1';
  159. chars = newChars;
  160. }
  161. this.guideRecord = string.Join("", chars);
  162. UserPlayer.ins?.call("userComp.saveGuideRecord", this.guideRecord);
  163. }
  164. }
  165. public class UserSettings
  166. {
  167. //打开BGM
  168. public bool openBGM = true;
  169. //打开音效
  170. public bool openEffect = true;
  171. //是否打开准心
  172. public bool openCrossHair = true;
  173. //射击难度
  174. public int shootLevel = 0;
  175. //游戏里的箭重,单位克
  176. public float actualArrowWeight = 20;
  177. //弓箭旋转转化
  178. public BowRotateConvert bowRotateConvert = new BowRotateConvert();
  179. //游戏中是否固定镜头
  180. public bool bowCameraFixed = true;
  181. //训练模式
  182. public bool trainMode = false;
  183. //游戏里面视角重置时间(3秒改成默认5秒)
  184. public float calibrationTime = 5f;
  185. //游戏里面的弓箭手臂(在所有游戏中,游戏设置里的‘手臂弓箭’这个选项,默认为关;即在游戏中,不出现手臂和弓箭的模型)
  186. public bool openBowAndArrow = false;
  187. //选择的红外连接设备
  188. public string selectDevicesName = "";
  189. //激光设备开启状态 默认关闭
  190. public bool lightState = false;
  191. //b端 每一局的设置
  192. public int PerRoundCoin = 2; //投币
  193. public int PerRoundSeconds = 1200;//时间
  194. public int TotalCoinsNum = 0;//总投币数
  195. public int ChangeTicket = 0;//多少分兑换一张票
  196. public int TotalTicketNum = 0;//总出票数量
  197. public int NeedCoin = 1;//是否需要投币
  198. public int GiveTicket = 0;//是否出票
  199. //设备校准引导-是否已经完成
  200. public bool deviceCalibrateGuideFinish = false;
  201. //游戏规则引导-是否已经完成(完成则保存对应的GameType)
  202. public HashSet<int> gameRuleGuideFinish = new HashSet<int>();
  203. private static UserSettings _ins;
  204. public static UserSettings ins
  205. {
  206. get
  207. {
  208. if (_ins == null)
  209. {
  210. string dataStr = PlayerPrefs.GetString("UserSettings", "{}");
  211. try
  212. {
  213. _ins = JsonConvert.DeserializeObject<UserSettings>(dataStr);
  214. }
  215. catch (System.Exception) { }
  216. if (_ins == null)
  217. {
  218. _ins = new UserSettings();
  219. }
  220. if (CommonConfig.SpecialVersion1)
  221. {
  222. if (PlayerPrefs.GetInt("sv1_UserSettings_2", 0) == 0)
  223. {
  224. PlayerPrefs.SetInt("sv1_UserSettings_2", 1);
  225. UserSettings us = _ins;
  226. us.bowRotateConvert.screenSize = 60;
  227. us.bowRotateConvert.screenDistance = 1.5f;
  228. us.Save();
  229. }
  230. }
  231. }
  232. return _ins;
  233. }
  234. }
  235. public void Save()
  236. {
  237. PlayerPrefs.SetString("UserSettings", JsonConvert.SerializeObject(this));
  238. }
  239. }
  240. /*
  241. 描述1
  242. 已知:
  243. 屏幕宽高比例w:h=16:9,屏幕尺寸s=60.11英寸,屏幕距离d=2.50米,实际指向右边树的角度为e=5.56°。
  244. 可知:
  245. 屏幕尺寸s1 = (s * 0.0254)米
  246. 屏幕单位大小为unit,关系式(w*unit)^2 + (h*unit)^2 = s1^2
  247. 结果:
  248. 向量(玩家->右边树)在屏幕上投影的长度比例q = tan(e) * d / unit
  249. */
  250. /*
  251. 描述2(适配各种尺寸的屏幕)
  252. 已知:
  253. 屏幕宽高比例w:h=16:9,屏幕尺寸s=(任意)英寸,屏幕距离d=(任意)米,游戏指向右边树的角度为r=27.30°。
  254. 借用描述1的结果q
  255. 可知:
  256. 屏幕尺寸s1 = (s * 0.0254)米
  257. 屏幕单位大小为unit,关系式(w*unit)^2 + (h*unit)^2 = s1^2
  258. 实际指向右边树的角度为e = atan(q * unit / d)
  259. 结果:
  260. 游戏转动角度:实际转动角度 = r / e
  261. */
  262. public class BowRotateConvert
  263. {
  264. public float screenSize = 60; //屏幕尺寸(英寸)
  265. public float screenDistance = 2.5f; //玩家距离屏幕多远(米)
  266. [NonSerialized] public float fieldOfView = 25;
  267. //获取建议的屏幕距离
  268. public float GetAdviseScreenDistance()
  269. {
  270. float w = 16;
  271. float h = 9;
  272. float s1 = screenSize * 0.0254f;
  273. float unit = s1 / Mathf.Sqrt(w * w + h * h);
  274. float screenHeight = 9 * unit;
  275. return screenHeight * 0.5f / Mathf.Tan(fieldOfView / 2 / 180 * Mathf.PI);
  276. }
  277. // 游戏旋转角度 : 实际旋转角度 (这个版本丢弃掉这个功能-所以直接返回1)
  278. public float GetRate()
  279. {
  280. return 1;
  281. }
  282. // 游戏旋转角度 : 实际旋转角度
  283. // public float GetRate() {
  284. // double w = 16;
  285. // double h = 9;
  286. // double s = Convert.ToDouble(screenSize);
  287. // double d = Convert.ToDouble(screenDistance);
  288. // double r = 27.3 / 180 * Math.PI;
  289. // double q = get_q();
  290. // double s1 = s * 0.0254;
  291. // double unit = Math.Sqrt(Math.Pow(s1, 2) / (Math.Pow(w, 2) + Math.Pow(h, 2)));
  292. // double e = Math.Atan(q * unit / d);
  293. // return (float) (r / e);
  294. // }
  295. // private double get_q() {
  296. // double w = 16;
  297. // double h = 9;
  298. // double s = 60.11;
  299. // double d = 2.5;
  300. // double e = 5.56 / 180 * Math.PI;
  301. // double s1 = s * 0.0254;
  302. // double unit = Math.Sqrt(Math.Pow(s1, 2) / (Math.Pow(w, 2) + Math.Pow(h, 2)));
  303. // double q = Math.Tan(e) * d / unit;
  304. // return q;
  305. // }
  306. }