423 lines
14 KiB
C#
423 lines
14 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Net.Http;
|
||
using System.Net.NetworkInformation;
|
||
using System.Net.Sockets;
|
||
using System.Text;
|
||
using BestHTTP;
|
||
using DarkTonic.MasterAudio;
|
||
using DragonLi.Core;
|
||
using FluffyUnderware.Curvy;
|
||
using FluffyUnderware.Curvy.ImportExport;
|
||
using Mirror;
|
||
using Pathfinding.RVO;
|
||
using Unity.XR.PXR;
|
||
using UnityAsyncAwaitUtil;
|
||
using UnityEngine;
|
||
using UnityEngine.Rendering;
|
||
using XPlugin.Data.Json;
|
||
using XPlugin.Data.JsonLiteDB;
|
||
using static XPlugin.Data.JsonLiteDB.JsonLiteDB;
|
||
using Random = UnityEngine.Random;
|
||
|
||
namespace DinosaurAge
|
||
{
|
||
public enum Place
|
||
{
|
||
Company1Floor = 0,
|
||
LiaoningAnShan = 1,
|
||
HangZhouLongHuTianJie = 2,
|
||
NanJing_YvHua_Wanxiang = 3,
|
||
Nanjing_Xianlin_WanDaMao = 4,
|
||
Yangzhou_Hanjiang_TansuoZhongxin = 5,
|
||
Yangzhou_Hanjiang_TansuoZhongxin_Wai = -5,
|
||
Zhejiang_Jinhua_KeJiGuan = 6,
|
||
GuangZhou_PanYu_ZhanTing = 7,
|
||
Anhui_Wuhu_Guanwei = 8,
|
||
Shandong_Jining_Shangchang = 9,
|
||
Shandong_Jining_Shangchang_nei = -9,
|
||
Shandong_Langfang_QingzhouTaihuacheng = 10,
|
||
Hubei_Xiangyang_Kejiguan = 11,
|
||
Zhejiang_Shaoxing_Shengzhou_WuyueGuangchang = 12,
|
||
Hunan_Jishou_Qianzhou_Tianhong = 13,
|
||
Jilin_Tonghua_Liuhe = 14,
|
||
Shandong_Jinan_Huaiyin_ShengfutongShangmao = 15,
|
||
Shandong_Jinan_Huaiyin_ShengfutongShangmao_wai = -15,
|
||
Henan_Xinzheng_Shuanghudadao_Longhujinyicheng = 16,
|
||
Nanjing_Qixia_Yaohuamen_Jindiguangchang = 17,
|
||
Nanjing_Qixia_Yaohuamen_Jindiguangchang_nei = -17
|
||
}
|
||
|
||
public class GameManager : MonoBehaviour
|
||
{
|
||
public static GameManager Ins { get; private set; }
|
||
public Camera[] Cameras;
|
||
public Camera MRCamera;
|
||
public GameObject ModelScene;
|
||
public GameObject Rain;
|
||
|
||
public GameObject dinosaurScene;//恐龙场景
|
||
public GameObject museumScene;//博物馆场景
|
||
public GameObject goObj;
|
||
public AudioSource bgm;
|
||
|
||
public AudioClip dinosaurClip;
|
||
public AudioClip goClip;
|
||
public AudioClip museumClip;
|
||
|
||
public Transform doorPos;
|
||
public Transform aiPos;
|
||
|
||
[NonSerialized]
|
||
public Player self;
|
||
|
||
|
||
public bool IsShowModel = false;
|
||
public DinosaurSpawner dinosaurSpawner;
|
||
|
||
// 场地
|
||
public Place place;
|
||
// 版本号
|
||
public string Version = "1.0.1";
|
||
private string HttpServerUrl = "";
|
||
// http验证
|
||
//public readonly string Author = "Bearer eyJhbGciOiJIUzUxMiJ9.eyJqdGkiOiJhMGE4OTE0ZjRjODE0NTNiYWZmMTdiODFmYjBjNmU5YSIsInVzZXIiOiJkZXZpY2UtMDAxIiwic3ViIjoiZGV2aWNlLTAwMSJ9.krKxq1NQYUTlqAMcb0FwHlsI17TLos08OjqUIi_5zmK8sh-LA6hL2awyXZpblrE-LYuWh0g8qA8DjRcjP74hOQ";
|
||
private readonly string Author = "x.";
|
||
// 验证信息
|
||
private AuthInfo authInfo = new AuthInfo();
|
||
|
||
|
||
// json数据库
|
||
private JsonLiteDB DB;
|
||
// 恐龙详情
|
||
public Dictionary<int, DinosaurDescInfo> DinosaurDescInfos = new Dictionary<int, DinosaurDescInfo>();
|
||
// 恐龙地图
|
||
public List<DinosaurMapInfo> DinosaurMapInfos = new List<DinosaurMapInfo>();
|
||
|
||
public CurvySpline[] DinosaursSplines;
|
||
|
||
/// <summary>
|
||
/// 游戏物体信息
|
||
/// </summary>
|
||
public List<GameObjInfo> GameObjInfos = new List<GameObjInfo>();
|
||
|
||
void Awake()
|
||
{
|
||
Ins = this;
|
||
}
|
||
|
||
void Start()
|
||
{
|
||
#if !UNITY_EDITOR && UNITY_ANDROID && PICO
|
||
MRCamera = Cameras[1];
|
||
#endif
|
||
UpdateConf();
|
||
Rain.SetActive(false);
|
||
bgm.clip = museumClip;
|
||
bgm.Play();
|
||
dinosaurScene.SetActive(false);
|
||
museumScene.SetActive(true);
|
||
goObj.SetActive(false);
|
||
|
||
if (ModelScene != null)
|
||
{
|
||
ModelScene.SetActive(IsShowModel);
|
||
}
|
||
Application.targetFrameRate = 60;
|
||
//ConPanel.Show();
|
||
MRNetworkManager.Ins.CreateAndJoinRoom();
|
||
MRNetworkManager.Ins.networkDiscovery.AdvertiseServer();
|
||
}
|
||
|
||
void Update()
|
||
{
|
||
|
||
}
|
||
|
||
void Destroy()
|
||
{
|
||
#if !UNITY_EDITOR
|
||
PXR_Enterprise.UnBindEnterpriseService();
|
||
#endif
|
||
}
|
||
|
||
/// <summary>
|
||
/// 请求配置
|
||
/// </summary>
|
||
public void RequestConf(Action<HTTPRequest, HTTPResponse> cb = null)
|
||
{
|
||
string url = HttpServerUrl + "/api/config/info?deviceSn={0}&gameType=1&version={1}";
|
||
url = string.Format(url, GetSn(), Version);
|
||
Debug.Log("发送数据 -> " + url);
|
||
HTTPRequest request = new HTTPRequest(new Uri(url), HTTPMethods.Get, (req, response) =>
|
||
{
|
||
cb?.Invoke(req, response);
|
||
});
|
||
request.AddHeader("Authorization", Author);
|
||
request.Send();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 鉴权
|
||
/// </summary>
|
||
public void RequestAuth(Action<HTTPRequest, HTTPResponse> cb = null)
|
||
{
|
||
string url = "http://www.pineappletech.cn/startcount";
|
||
HTTPRequest request = new HTTPRequest(new Uri(url), HTTPMethods.Post, (req, response) =>
|
||
{
|
||
if (response != null)
|
||
{
|
||
Debug.Log("收到数据 ->" + response.DataAsText);
|
||
}
|
||
cb?.Invoke(req, response);
|
||
});
|
||
//request.AddHeader("Authorization", Author);
|
||
authInfo.deviceSn = GetSn();
|
||
authInfo.startAt = ConvertTimestampToDateTime(GetTimestamp()) + "";
|
||
|
||
//authInfo.type = 1;
|
||
authInfo.shop = 0;
|
||
#if !UNITY_EDITOR && UNITY_ANDROID && PICO
|
||
authInfo.shop = (int)place;
|
||
if(place== Place.Yangzhou_Hanjiang_TansuoZhongxin_Wai)
|
||
authInfo.shop = 5;
|
||
if(place== Place.Shandong_Jining_Shangchang_nei)
|
||
authInfo.shop = 9;
|
||
if(place== Place.Shandong_Jinan_Huaiyin_ShengfutongShangmao_wai)
|
||
authInfo.shop = 15;
|
||
if(place== Place.Nanjing_Qixia_Yaohuamen_Jindiguangchang_nei)
|
||
authInfo.shop = 17;
|
||
#endif
|
||
authInfo.gameId = 0;
|
||
string authJson = JsonUtility.ToJson(authInfo);
|
||
Debug.Log("发送数据 -> " + authJson);
|
||
request.RawData = System.Text.Encoding.UTF8.GetBytes(authJson);
|
||
request.AddHeader("Content-Type", "application/json");
|
||
request.Send();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 通知服务器已开始游戏
|
||
/// </summary>
|
||
public void RequestNotifyStart(Action<HTTPRequest, HTTPResponse> cb = null)
|
||
{
|
||
string url = HttpServerUrl + "/api/record";
|
||
HTTPRequest request = new HTTPRequest(new Uri(url), HTTPMethods.Put, (req, response) =>
|
||
{
|
||
if (response != null)
|
||
{
|
||
Debug.Log("收到数据 ->" + response.DataAsText);
|
||
cb?.Invoke(req, response);
|
||
}
|
||
});
|
||
request.AddHeader("Authorization", Author);
|
||
//authInfo.paid = 1;
|
||
string authJson = JsonUtility.ToJson(authInfo);
|
||
Debug.Log("发送数据 -> " + authJson);
|
||
request.RawData = System.Text.Encoding.UTF8.GetBytes(authJson);
|
||
request.AddHeader("Content-Type", "application/json");
|
||
request.Send();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 通知服务器游戏结束
|
||
/// </summary>
|
||
public void RequestNotifyEnd(Action<HTTPRequest, HTTPResponse> cb = null)
|
||
{
|
||
string url = HttpServerUrl + "/api/record";
|
||
HTTPRequest request = new HTTPRequest(new Uri(url), HTTPMethods.Put, (req, response) =>
|
||
{
|
||
if (response != null)
|
||
{
|
||
Debug.Log("收到数据 ->" + response.DataAsText);
|
||
cb?.Invoke(req, response);
|
||
}
|
||
});
|
||
request.AddHeader("Authorization", Author);
|
||
// authInfo.endAt = ConvertTimestampToDateTime(GetTimestamp()) + "";
|
||
//authInfo.dur = (int)(((long)(DateTime.Now.Subtract(new DateTime(1970, 1, 1))).TotalSeconds) -
|
||
//(GameInit.Ins.vistEnd - GameInit.Ins.vistAllTime));
|
||
string authJson = JsonUtility.ToJson(authInfo);
|
||
Debug.Log("发送数据 -> " + authJson);
|
||
request.RawData = System.Text.Encoding.UTF8.GetBytes(authJson);
|
||
request.AddHeader("Content-Type", "application/json");
|
||
request.Send();
|
||
}
|
||
|
||
public void ParseGameSplines(string text)
|
||
{
|
||
DinosaursSplines = SplineJsonConverter.JsonToSplines(text);
|
||
Debug.Log("游戏路径更新 -> complete");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 更新配置表
|
||
/// </summary>
|
||
public void UpdateConf()
|
||
{
|
||
string text = "";
|
||
// switch (place)
|
||
// {
|
||
// case Place.LiaoningAnShan:
|
||
// text = Resources.Load<TextAsset>("Data").text;
|
||
// break;
|
||
// case Place.Company1Floor:
|
||
// text = Resources.Load<TextAsset>("Data").text;
|
||
// break;
|
||
// }
|
||
text = Resources.Load<TextAsset>("Data").text;
|
||
if (text != null)
|
||
{
|
||
Debug.Log("读取的数据:" + place);
|
||
ParseGameJson(text);
|
||
UpdateSplines();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 更新Splines配置表
|
||
/// </summary>
|
||
public void UpdateSplines(Action cb = null)
|
||
{
|
||
string splineText = Resources.Load<TextAsset>("Splines_" + place).text;
|
||
if (splineText != null)
|
||
{
|
||
ParseGameSplines(splineText);
|
||
}
|
||
cb?.Invoke();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 解析游戏使用的json
|
||
/// </summary>
|
||
public void ParseGameJson(string text)
|
||
{
|
||
DB = new JsonLiteDB();
|
||
DB.Load(text);
|
||
|
||
// 读取Info表数据
|
||
TableReader infoReader = DB["Info"].GetReader();
|
||
Dictionary<int, DinosaurDescInfo> DinosaurDescInfosC = new Dictionary<int, DinosaurDescInfo>();
|
||
while (infoReader.Read())
|
||
{
|
||
DinosaurDescInfo info = new DinosaurDescInfo(infoReader);
|
||
DinosaurDescInfosC.Add(info.Id, info);
|
||
}
|
||
DinosaurDescInfos = DinosaurDescInfosC;
|
||
|
||
// 读取Map表数据
|
||
TableReader mapReader = DB["Map"].GetReader();
|
||
List<DinosaurMapInfo> DinosaurMapInfosC = new List<DinosaurMapInfo>();
|
||
while (mapReader.Read())
|
||
{
|
||
DinosaurMapInfo info = new DinosaurMapInfo(mapReader);
|
||
//Debug.Log("恐龙数据:"+info.Id);
|
||
DinosaurMapInfosC.Add(info);
|
||
}
|
||
DinosaurMapInfos = DinosaurMapInfosC;
|
||
|
||
List<GameObjInfo> gameObjInfos = new List<GameObjInfo>();
|
||
TableReader gameReader = DB["GameInfo"].GetReader();
|
||
while (gameReader.Read())
|
||
{
|
||
GameObjInfo info = new GameObjInfo(gameReader);
|
||
gameObjInfos.Add(info);
|
||
}
|
||
|
||
GameObjInfos = gameObjInfos;
|
||
Debug.Log("游戏数值更新 -> complete");
|
||
}
|
||
|
||
public void LogPosition(Vector3 pos)
|
||
{
|
||
Debug.Log("x:" + pos.x + " y:" + pos.y + " z:" + pos.z);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取sn号
|
||
/// </summary>
|
||
public string GetSn()
|
||
{
|
||
string res = "UnityEditor";
|
||
// string res = "PA8E10MGH7210380D";
|
||
#if !UNITY_EDITOR && UNITY_ANDROID && PICO
|
||
res = PXR_Enterprise.StateGetDeviceInfo(SystemInfoEnum.EQUIPMENT_SN);
|
||
#endif
|
||
return res;
|
||
}
|
||
|
||
public long GetTimestamp()
|
||
{
|
||
return (long)DateTime.Now.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
|
||
}
|
||
|
||
public static string ConvertTimestampToDateTime(long timestamp)
|
||
{
|
||
// Unix时间戳是从1970年1月1日00:00:00开始的秒数或毫秒数
|
||
// 这里以秒为单位,如果时间戳是毫秒则除以1000
|
||
DateTime dateTime = DateTimeOffset.FromUnixTimeSeconds(timestamp).DateTime;
|
||
return dateTime.ToString("yyyy-MM-dd HH:mm:ss");
|
||
}
|
||
|
||
public void Show3DLogo()
|
||
{
|
||
//Logo.SetActive(true);
|
||
DinosaurLogoPanel.Welcome();
|
||
MonoSingleton<CoroutineTaskManager>.Instance.WaitSecondTodo(() =>
|
||
{
|
||
//Logo.SetActive(false);
|
||
dinosaurScene.SetActive(false);
|
||
museumScene.SetActive(true);
|
||
goObj.SetActive(false);
|
||
}, 3.0f, this);
|
||
}
|
||
|
||
public void AllChildren()
|
||
{
|
||
// 获取当前游戏对象的所有子物体数量
|
||
int childCount = dinosaurSpawner.dinosaurs.transform.childCount;
|
||
|
||
for (int i = childCount - 1; i >= 0; i--)
|
||
{
|
||
// 从后往前删除可以避免在删除过程中改变索引的问题
|
||
Destroy(dinosaurSpawner.dinosaurs.transform.GetChild(i).gameObject);
|
||
}
|
||
}
|
||
|
||
public void ResetMuseum()
|
||
{
|
||
dinosaurScene.SetActive(false);
|
||
goObj.SetActive(true);
|
||
goObj.transform.position = MRCamera.transform.position += new Vector3(0, 1, 20);
|
||
if (place == Place.Yangzhou_Hanjiang_TansuoZhongxin)
|
||
goObj.transform.position = MRCamera.transform.position += new Vector3(0, 1, -30);
|
||
bgm.clip = goClip;
|
||
bgm.Play();
|
||
AllChildren();
|
||
dinosaurSpawner.gameObject.SetActive(true);
|
||
dinosaurSpawner.Play();
|
||
MonoSingleton<CoroutineTaskManager>.Instance.WaitSecondTodo(() =>
|
||
{
|
||
goObj.SetActive(false);
|
||
museumScene.SetActive(true);
|
||
bgm.clip = museumClip;
|
||
bgm.Play();
|
||
// 结束面板
|
||
DinosaurLogoPanel.Byebye();
|
||
dinosaurSpawner.Stop();
|
||
dinosaurSpawner.enabled = false;
|
||
GameEnd();
|
||
}, 10f, this);
|
||
}
|
||
|
||
public void GameEnd()
|
||
{
|
||
//播放回到博物馆语音
|
||
MasterAudio.PlaySound3DAtTransform("博物馆结束", self.transform);
|
||
MonoSingleton<CoroutineTaskManager>.Instance.WaitSecondTodo(() =>
|
||
{
|
||
Application.Quit();
|
||
}, 10f, this);
|
||
}
|
||
}
|
||
} |