using System;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
using JCUnityLib;
/* 登录管理者,用户数据定义和存储 */
public class LoginMgr : MonoBehaviour
{
[SerializeField] GameObject loginView;
[SerializeField] GameObject registerView;
public static UserInfo myUserInfo = new UserInfo();
public void showRegisterView()
{
loginView.SetActive(false);
registerView.SetActive(true);
}
public void showLoginView()
{
loginView.SetActive(true);
registerView.SetActive(false);
}
public void showForgetPWD_View()
{
Instantiate(SceneResourceManager.Instance.GetPrefab("RetrievePasswordView"));
}
public const string LoginTokenKey = "LoginToken";
public static bool HasToken()
{
string loginToken = PlayerPrefs.GetString(LoginMgr.LoginTokenKey, "");
return string.IsNullOrEmpty(loginToken) ? false : true;
}
void Awake()
{
transform.Find("AgreementPopup").gameObject.SetActive(true);
ViewMgr.Instance.DestroyAllViews();
}
void Start()
{
//退出到登录界面,也要把蓝牙断开
if (BluetoothAim.ins) {
if (BluetoothAim.ins.status == BluetoothStatusEnum.ConnectSuccess) BluetoothAim.ins.DoConnect();
else BluetoothAim.ins.connectCanceled = true;
}
}
}
public class UserInfo
{
public int id;
public int avatarID = 0;
public string avatarUrl = "";
public string nickname = "超级射手";
public int gender = 1;
public string phone = "";
public string email = "";
public string birthday = "";
public string country = "";
public string state = "";
public string city = "";
public int integral = 0;
public int coin = 0;
public int diamond = 1000;
public string mac = "";
///
/// 显示游戏最高分(不同距离分数独立)
/// 2023-7-16兼容WildAttack最高分纪录
///
public Dictionary timeLimitGameScores = new Dictionary();
//闯关记录(gameType:通关数)(野兔、野鸡、野狼的通关数)
public Dictionary challengeLevels = new Dictionary();
public string guideRecord = "";
public static UserInfo LoadLocal(int id)
{
UserInfo result = null;
if (CommonConfig.StandaloneMode)
{
try
{
result = JsonConvert.DeserializeObject(PlayerPrefs.GetString("UserInfo_" + id));
if (result == null) throw new Exception("UserInfo.LoadLocal Null");
}
catch (Exception e)
{
Debug.LogError(e);
}
}
return result;
}
public void Save()
{
if (CommonConfig.StandaloneMode)
{
PlayerPrefs.SetString("UserInfo_" + id, JsonConvert.SerializeObject(this));
return;
}
}
public void SetChallengeLevelPass(int gameType, int level)
{
if (gameType != 3 && gameType != 4 && gameType != 5) return;
if (challengeLevels.ContainsKey(gameType))
{
if (level <= challengeLevels[gameType])
{
return;
}
}
challengeLevels.Remove(gameType);
challengeLevels.Add(gameType, level);
}
public int GetChallengeLevelPass(int gameType)
{
if (challengeLevels.ContainsKey(gameType))
{
return challengeLevels[gameType];
}
return 0;
}
//判断引导是否完成(服务端保存)
//index0: 新手引导NewUserGuide
public bool IsGuideFinish(int index)
{
if (index == 0)
{
return PlayerPrefs.GetInt("NewUserGuideFinish_" + LoginMgr.myUserInfo.id, 0) == 1;
}
char[] chars = guideRecord.ToCharArray();
if (index < chars.Length)
{
return chars[index] == '1';
}
return false;
}
public void SaveGuideFinish(int index)
{
if (index == 0)
{
PlayerPrefs.SetInt("NewUserGuideFinish_" + LoginMgr.myUserInfo.id, 1);
return;
}
char[] chars = guideRecord.ToCharArray();
if (index < chars.Length)
{
if (chars[index] == '1') return;
chars[index] = '1';
}
else
{
int newLen = index + 1;
char[] newChars = new char[newLen];
for (int i = 0; i < newLen; i++)
{
newChars[i] = i < chars.Length ? chars[i] : '0';
}
newChars[index] = '1';
chars = newChars;
}
this.guideRecord = string.Join("", chars);
}
}
public class UserSettings
{
//打开BGM
public bool openBGM = true;
//打开音效
public bool openEffect = true;
//是否打开准心
public bool openCrossHair = true;
//射击难度
public int shootLevel = 0;
//游戏里的箭重,单位克
public float actualArrowWeight = 20;
//弓箭旋转转化
public BowRotateConvert bowRotateConvert = new BowRotateConvert();
//游戏中是否固定镜头
public bool bowCameraFixed = true;
//训练模式
public bool trainMode = false;
//设备校准引导-是否已经完成
public bool deviceCalibrateGuideFinish = false;
//游戏规则引导-是否已经完成(完成则保存对应的GameType)
public HashSet gameRuleGuideFinish = new HashSet();
private static UserSettings _ins;
public static UserSettings ins
{
get
{
if (_ins == null)
{
string dataStr = PlayerPrefs.GetString("UserSettings", "{}");
try
{
_ins = JsonConvert.DeserializeObject(dataStr);
}
catch (System.Exception) { }
if (_ins == null)
{
_ins = new UserSettings();
}
if (CommonConfig.SpecialVersion1)
{
if (PlayerPrefs.GetInt("sv1_UserSettings_2", 0) == 0)
{
PlayerPrefs.SetInt("sv1_UserSettings_2", 1);
UserSettings us = _ins;
us.bowRotateConvert.screenSize = 60;
us.bowRotateConvert.screenDistance = 1.5f;
us.Save();
}
}
}
return _ins;
}
}
public void Save()
{
PlayerPrefs.SetString("UserSettings", JsonConvert.SerializeObject(this));
}
}
/*
描述1
已知:
屏幕宽高比例w:h=16:9,屏幕尺寸s=60.11英寸,屏幕距离d=2.50米,实际指向右边树的角度为e=5.56°。
可知:
屏幕尺寸s1 = (s * 0.0254)米
屏幕单位大小为unit,关系式(w*unit)^2 + (h*unit)^2 = s1^2
结果:
向量(玩家->右边树)在屏幕上投影的长度比例q = tan(e) * d / unit
*/
/*
描述2(适配各种尺寸的屏幕)
已知:
屏幕宽高比例w:h=16:9,屏幕尺寸s=(任意)英寸,屏幕距离d=(任意)米,游戏指向右边树的角度为r=27.30°。
借用描述1的结果q
可知:
屏幕尺寸s1 = (s * 0.0254)米
屏幕单位大小为unit,关系式(w*unit)^2 + (h*unit)^2 = s1^2
实际指向右边树的角度为e = atan(q * unit / d)
结果:
游戏转动角度:实际转动角度 = r / e
*/
public class BowRotateConvert
{
public float screenSize = 60; //屏幕尺寸(英寸)
public float screenDistance = 2.5f; //玩家距离屏幕多远(米)
[NonSerialized] public float fieldOfView = 25;
//获取建议的屏幕距离
public float GetAdviseScreenDistance()
{
float w = 16;
float h = 9;
float s1 = screenSize * 0.0254f;
float unit = s1 / Mathf.Sqrt(w * w + h * h);
float screenHeight = 9 * unit;
return screenHeight * 0.5f / Mathf.Tan(fieldOfView / 2 / 180 * Mathf.PI);
}
// 游戏旋转角度 : 实际旋转角度 (这个版本丢弃掉这个功能-所以直接返回1)
public float GetRate()
{
return 1;
}
// 游戏旋转角度 : 实际旋转角度
// public float GetRate() {
// double w = 16;
// double h = 9;
// double s = Convert.ToDouble(screenSize);
// double d = Convert.ToDouble(screenDistance);
// double r = 27.3 / 180 * Math.PI;
// double q = get_q();
// double s1 = s * 0.0254;
// double unit = Math.Sqrt(Math.Pow(s1, 2) / (Math.Pow(w, 2) + Math.Pow(h, 2)));
// double e = Math.Atan(q * unit / d);
// return (float) (r / e);
// }
// private double get_q() {
// double w = 16;
// double h = 9;
// double s = 60.11;
// double d = 2.5;
// double e = 5.56 / 180 * Math.PI;
// double s1 = s * 0.0254;
// double unit = Math.Sqrt(Math.Pow(s1, 2) / (Math.Pow(w, 2) + Math.Pow(h, 2)));
// double q = Math.Tan(e) * d / unit;
// return q;
// }
}