using BestHTTP;
using DragonLi.Core;
using System;
using System.Collections;
using System.Collections.Generic;
using DarkTonic.MasterAudio;
using Pathfinding;
using Unity.Mathematics;
using Unity.XR.PXR;
using UnityEngine;
using XPlugin.Data.JsonLiteDB;
using static XPlugin.Data.JsonLiteDB.JsonLiteDB;
using Random = UnityEngine.Random;
///
/// 游戏场地
///
public enum GamePlace
{
// Test = -1,
Company1Floor = 0,
Company1FloorShiwai=-999,
LiaoningAnShan = 1,//否
Liaoning_AnShan_Lishan_Dayuecheng =-1,
HangZhouLongHuTianJie = 2,
Nanjing_Yuhua_Wanxiang = 3,
Nanjing_Xianlin_WanDaMao = 4,
Yangzhou_Hanjiang_Tansuozhongxin = 5,
Yangzhou_Hanjiang_TansuoZhongxin_wai = -5,
Zhejiang_Jinhua_KeJiGuan = 6,
Anhui_Wuhu_Guanwei = 8,
Shandong_Jining_Wanhuicheng=9,
Shandong_Jining_Wanhuicheng_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,
Anhui_Suzhou_Yueshan_Guchengshangyejie = 18,
Anhui_Suzhou_Yueshan_Guchengshangyejie_2=-18,//--
Gansu_Longnan_Shicheng_Dongsheng = 19,
Shandong_Heze_Yuncheng_Gefuli = 20,
Wulanhaote_Ouya_Shangchang = 21,
Wulanhaote_Wanda_Shangchang = -21,
Yunnan_Mile_Jinchen_Shidaiguangchang = 22,
Xinjiang_Yili_Yining_Wanrong = 23,
Hunan_Hengyang_Zhuhui_Dongzhoudao = 24,
Hunan_Hengyang_Zhuhui_Dongzhoudao_nei = -24,
Yunnan_Lincang_Linxiang_Hengji = 25,
Yunnan_Lincang_Linxiang_Hengji_Dixia = -25,
Guangxi_Guilin_Gongcheng_Shijixincheng = 26,
Guangdong_shenzheng_Guangming_Shijiguangchang=27,
Gansu_Jinchang_Jinchuan_Shijiguangchang = 28,
Gansu_Jinchang_Jinchuan_Shijiguangchang_Shiwai=-28,
Jiangsu_Xvzhou_Fengxian_Wuyueguangchang = 29,//--
Hebei_Tangshan_Qianan_Tianyuangu = 30,
Guangdong_Guangzhou_Yanghaiyan = 31,
Guangdong_Guangzhou_Yanghaiyan_Lihu=-31,
Zhejiang_Hangzhou_Linping_Yintaicheng = 32,
Zhejiang_Hangzhou_Linping_Yintaicheng_Shinei = -32,
Henan_Xinxiang_Wandaguangchang = 33,
Henan_Xinxiang_Wandaguangchang_Shinei = -33,
Yangzhou_Hanjiang_TansuoZhongxin_Waidai =34,
Nanjing_Pukou_Longhutianjie=35,
Jiangsu_Xvzhou_Guolou_Oulebao=36,
Jiangsu_Xvzhou_Guolou_Oulebao_2=-36,
Jiangsu_Xvzhou_Suning_Guangchang =37,
Jiangsu_Xvzhou_Suning_Guangchang_1 = -37,//--
Shanxi_Baoji_Meixian_TianlongShangcheng = 38,
Hunan_Changde_Lixian_WandaGuangchang=39,
Jilin_Changchun_Beihu_WyueGuangchang=41,
Chongqing_Yuzhong_Hongyadong_Xiakexing=42,
Chongqing_Yuzhong_Hongyadong_Xiakexing_shiwan=-42,
Chengdu_Shuangliu_ShengfeiXuexiao =43,
Hebei_Hengshui_Xinji_WandaGuangchang=44,
Hebei_Hengshui_Taocheng_WandaGuangchang = -44,
Ningxia_Yinchuan_Jinfeng_XinhualianGuangchang = 45,
Hunan_Zhuzhou_Wanda_Shennongcheng_Chaowanshe = 46,
Anhui_Suzhou_Yueshan_Guchengshangyejie_Shinei=47,
Zhejiang_Wenzhou_Cangnan_Yintaicheng = 48,
Shandong_Jining_Shangchang_3=49,
Anhui_Manshan_XingyueGuangchang = 50,
Shandong_Weifang_Linqu_WandaGuangchang = 51,
Guangdong_Foushan_Haiyangguang = 52,
Jilin_Changchun_Chaoyang_OuyaMaichang=53,
Shandong_Weifang_Linqu_WandaGuangchang_Shinei = 54,
Liaoning_Panjin_Shuangtaizi_Shuangtaicheng = 55,
Liaoning_Dalian_Pulandian_WandaGuangchang = 56,
Jiangxi_Ganzhou_Longnan_WandaGuangchang=57,
Jiangxi_Ganzhou_Longnan_WandaGuangchang_Shinei=58,
Jiangxi_Ganzhou_Zhanggong_GanzhouShucheng = 59,
Henan_Shangqiu_Juyang_WuyueGuangchang = 60,
Guangdong_Meizhou_Meixian_TianhongShangchang = 61,
Beijing_Tongzhou_Luyijie_XiangrikuiGuangchang = 62,
Ningxia_Yinchuang_Shizuishan_WandaGuangchang = 63,
Jilin_Jilin_Chuangying_Zhongdongxinshenghuo = 64,
Jiangxi_Nanchang_Xinjian_XvhuiGuangchang=65,
Shandong_Weihai_Jingqu_W37=66,
Hunan_Xiangxi_Yongshun_XintiandiShangchang=67,
Shanxi_Yuncheng_Yanhu_WandaGuangchang = 68,//
Bejing_Miyun_RuijiaShangchang = 69,//
Henan_Kaifeng_Longting_XihuJingqu = 70,//
Henan_Kaifeng_Longting_XihuJingqu_Shiwai = 71,//
Jiangxi_Nanchang_Xihu_NanchangDongwuyuan = 72,//否
Liaoning_Panjin_Xinglongtai_Shuiyoucheng = 73,//
Shanxi_Yulin_Shenmu_Liuta_Yuyuecheng = 74,//
Sichuan_Liangshan_Mianning_GaoyangJiedao = 75,//
Jiangxi_Nanchang_Xihu_NanchangDongwuyuan_Shinei = 76,//否
Ningxia_Yinchuan_Xingqing_XinhuaBaihuo = 77,//
Liaoning_Huludao_Jianchang_XinglongDajiating = 78,//
Ningxia_Yinchuan_Xingqing_XinhuaBaihuo_Shiwai = 79,//
Ningxia_Wuzhong_Litong_YiwuShangmaocheng = 80,//
}
public enum GameKey
{
DinosaurPark2=0,//重返侏罗纪
SpongeBob=1,//深海冒险
XMen=2,//银河守护者
KOF=3,//幻影交锋
Valheim=4,//小小幻宠
FutureMen=5,//未来战警
AliceBall=6,//爱丽丝的舞会
Zombie=7,//僵尸来了
DefendNJ=8,//保卫金陵
Loong=9, //巨龙猎人
MRCS=10,//火力对决
SXDMystery=11,//三星堆之谜
XMen2=12,//银河守护者2异形
MRFishingMaster=13,//MR捕鱼达人
}
public class GameManager : MonoBehaviour
{
public static GameManager Ins { get; private set; }
[NonSerialized] public float Taxation = 0.5f;
// json数据库
private JsonLiteDB DB;
// Player子弹数据
public Dictionary PlayerBulletDataDic = new Dictionary();
public Dictionary FishDataDic = new Dictionary();
public Dictionary ShopDataDic = new Dictionary();
[Header("道具")]
public GameObject gameStartPointPre;
public GameObject fireworksPre;
public GameObject redEnvelopePre;//红包
public GameObject ingotPre;//元宝
public GameObject shopPre;
public GameObject coinPilePre;
public GameObject fishTidePre;
[Header("敌人")]
public GameObject[] fishPres;
[Header("玩家")]
public GameObject playerPre;
public GameObject player;
public GameObject rightHndPre;
[NonSerialized]
public RightHand rightHand;
[NonReorderable]
public Shop Shop;
public FishSpawner fishSpawner;
public DriftBottleManager driftBottleManager;
public bool isGameEnd = false;
public LoginInfo authInfo = new LoginInfo();
public List itemList = new List();
//添加
[Header("AI角色")]
public GameObject aiCharacterPre;//AI角色预制体
private GameObject aiCharacter;//AI角色实例
private void Awake()
{
Ins = this;
}
public void Start()
{
CreatePlayer();
CreateRightHand();
LoginPanel.Show();
#if !UNITY_EDITOR
PXR_Enterprise.InitEnterpriseService();
PXR_Enterprise.BindEnterpriseService();
#endif
CoroutineTaskManager.Instance.WaitSecondTodo(ShowPlayerUI, 1.5f);
}
///
/// 登录
///
public void Request(Action 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);
});
authInfo.deviceSn = GetPicoSn();
authInfo.startAt = ConvertTimestampToDateTime(GetTimestamp()) + "";
authInfo.shop = 0;
#if !UNITY_EDITOR && UNITY_ANDROID && PICO
authInfo.shop = (int)GameInit.Ins.gamePlace;
if(GameInit.Ins.gamePlace== GamePlace.Liaoning_AnShan_Lishan_Dayuecheng)
authInfo.shop = 1;
if(GameInit.Ins.gamePlace== GamePlace.Yangzhou_Hanjiang_TansuoZhongxin_wai)
authInfo.shop = 5;
if (GameInit.Ins.gamePlace == GamePlace.Shandong_Jining_Wanhuicheng_nei)
authInfo.shop = 9;
if (GameInit.Ins.gamePlace == GamePlace.Shandong_Jinan_Huaiyin_ShengfutongShangmao_wai)
authInfo.shop = 15;
if (GameInit.Ins.gamePlace == GamePlace.Nanjing_Qixia_Yaohuamen_Jindiguangchang_nei)
authInfo.shop = 17;
if (GameInit.Ins.gamePlace == GamePlace.Anhui_Suzhou_Yueshan_Guchengshangyejie_2)
authInfo.shop = 18;
if (GameInit.Ins.gamePlace == GamePlace.Wulanhaote_Wanda_Shangchang)
authInfo.shop = 21;
if (GameInit.Ins.gamePlace == GamePlace.Hunan_Hengyang_Zhuhui_Dongzhoudao_nei)
authInfo.shop = 24;
if (GameInit.Ins.gamePlace == GamePlace.Yunnan_Lincang_Linxiang_Hengji_Dixia)
authInfo.shop = 25;
if (GameInit.Ins.gamePlace == GamePlace.Gansu_Jinchang_Jinchuan_Shijiguangchang_Shiwai)
authInfo.shop = 28;
if (GameInit.Ins.gamePlace == GamePlace.Guangdong_Guangzhou_Yanghaiyan_Lihu)
authInfo.shop = 31;
if (GameInit.Ins.gamePlace == GamePlace.Zhejiang_Hangzhou_Linping_Yintaicheng_Shinei)
authInfo.shop = 32;
if (GameInit.Ins.gamePlace == GamePlace.Henan_Xinxiang_Wandaguangchang_Shinei)
authInfo.shop = 33;
if (GameInit.Ins.gamePlace == GamePlace.Jiangsu_Xvzhou_Guolou_Oulebao_2)
authInfo.shop = 36;
if (GameInit.Ins.gamePlace == GamePlace.Jiangsu_Xvzhou_Suning_Guangchang_1)
authInfo.shop = 37;
if (GameInit.Ins.gamePlace == GamePlace.Chongqing_Yuzhong_Hongyadong_Xiakexing_shiwan)
authInfo.shop = 42;
if (GameInit.Ins.gamePlace == GamePlace. Hebei_Hengshui_Taocheng_WandaGuangchang )
authInfo.shop = 44;
if (GameInit.Ins.gamePlace == GamePlace.Company1FloorShiwai)
authInfo.shop = 0;
#endif
authInfo.gameId = (int)GameInit.Ins.gameId;
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 InitData()
{
string text = Resources.Load("Data").text;
GetReaderJson(text);
}
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 GetReaderJson(string text)
{
DB = new JsonLiteDB();
DB.Load(text);
// 读取Info表数据
TableReader infoReader = DB["FishData"].GetReader();
FishDataDic.Clear();
while (infoReader.Read())
{
FishData info = new FishData(infoReader);
FishDataDic.Add(info.FishId, info);
}
ShopDataDic.Clear();
TableReader shopReader = DB["ShopItemData"].GetReader();
while (shopReader.Read())
{
ShopData info = new ShopData(shopReader);
ShopDataDic.Add(info.Id, info);
}
// 玩家子弹数据
TableReader PlayerBulletReader = DB["PlayerBulletData"].GetReader();
PlayerBulletDataDic.Clear();
while (PlayerBulletReader.Read())
{
BulletData data = new BulletData(PlayerBulletReader);
PlayerBulletDataDic.Add(data.ID, data);
}
}
///
/// Get sn
///
public string GetPicoSn()
{
string res = "UnityEditor";
#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 bool isGamePlay;
//AI介绍是否完成
private bool isAIIntroductionComplete = false;
// 游戏开始
public void GamePlay()
{
//修改处:创建AI角色
//CreateAICharacter();
// PlayerUIMessage(0);
// CoroutineTaskManager.Instance.WaitSecondTodo(() =>
// AudioManager.Ins?.SoundPlay("bgm2", true), 10f);
// CoroutineTaskManager.Instance.WaitSecondTodo(() =>
// AudioManager.Ins?.SoundPlay("2.39BGM", true), 30f);
isGamePlay = true;
CreateShopPre();
StartDriftBottleEvent();
}
//修改处:添加AI介绍完成后的游戏开始方法
public void StartGameAfterIntroduction()
{
isAIIntroductionComplete = true;
isGamePlay = true;
Debug.Log("AI介绍完成,开始游戏正常流程");
CreateGameStartPoint();
//创建Boss
PlayerUIMessage(0);
}
//修改处:添加创建AI角色的方法
public void CreateAICharacter()
{
//检查是否已经存在AI角色
if (aiCharacter != null)
{
Debug.Log("AI角色已经存在,不再创建新的");
return;
}
if (aiCharacterPre != null)
{
Debug.Log("创建AI角色");
//在玩家前方创建AI角色
Vector3 spawnPosition =GameInit .Ins . startDoorPos.transform.position ;
aiCharacter = Instantiate(aiCharacterPre, spawnPosition, Quaternion.identity);
//获取AIController并启动开场白
AIController aiController = aiCharacter.GetComponent();
if (aiController != null)
{
//注册AI介绍完成回调
aiController.OnIntroductionComplete += StartGameAfterIntroduction;
//添加延迟,确保所有组件已经完成初始化
StartCoroutine(DelayedStartIntroduction(aiController));
}
else
{
Debug.LogError("AI预制体中的AIController组件丢失");
}
}
else
{
Debug.LogError("AI预制体没有在GameManager分配!");
}
}
private IEnumerator DelayedStartIntroduction(AIController aiController)
{
yield return new WaitForSeconds(0.1f); // 短暂延迟
aiController.StartIntroduction();
}
public void ShowPlayerUI()
{
OverlayUI.Ins.Cover("UI/PlayerUI", false);
}
public void ShowEndGameUI(bool isWin)
{
GameEndPanel.Show(isWin);
}
void Destroy()
{
#if !UNITY_EDITOR
PXR_Enterprise.UnBindEnterpriseService();
#endif
}
public void PlayerUIMessage(int index)
{
EventDispatcher.TriggerEvent("PromptMessage", index);
}
public void CreateGameStartPoint()
{
isGamePlay = false;
GameObject point = Instantiate(gameStartPointPre);
point.transform.position = GameInit.Ins.startDoorPos.position;
fishSpawner.LoadFishData();
AudioManager.Ins.SoundPlay(0,true);
CoroutineTaskManager.Instance.WaitSecondTodo(() =>
{
if (isGamePlay)
return;
Destroy(point.gameObject);
GamePlay();
}, 5f);
}
public void CreateFireworks(Vector3 pos)
{
GameObject point = Instantiate(fireworksPre);
point.transform.position = pos;
}
public void CreatePlayer()
{
player = Instantiate(playerPre);
}
public void CreateRightHand()
{
var hand= Instantiate(rightHndPre);
rightHand = hand.GetComponent();
}
public void CreateShopPre()
{
GameObject point = Instantiate(shopPre);
point.transform.position = GameInit.Ins.showPos.position;
point.transform.rotation = GameInit.Ins.showPos.rotation;
point.transform.localScale = GameInit.Ins.showPos.localScale;
Shop=point.GetComponent();
}
public void CreateItemPre()
{
if (itemList.Count >= 10)
{
Destroy(itemList[0]);
itemList.RemoveAt(0);
}
int randIndex=Random.Range(0,100);
List walkableNodes = GetAllWalkableNodes();
GraphNode randomNode = walkableNodes[Random.Range(0, walkableNodes.Count)];
Vector3 spawnPosition = (Vector3)randomNode.position;
if (randIndex >= 10)
{
var obj= Instantiate(redEnvelopePre,spawnPosition,Quaternion.identity);
itemList.Add(obj);
}
else
{
var obj=Instantiate(ingotPre,spawnPosition,Quaternion.identity);
itemList.Add(obj);
}
}
List GetAllWalkableNodes()
{
List walkableNodes = new List();
foreach (NavGraph graph in AstarPath.active.graphs)
{
graph.GetNodes(node =>
{
if (node != null && node.Walkable)
{
walkableNodes.Add(node);
}
return true;
});
}
Debug.Log($"找到 {walkableNodes.Count} 个可行走节点");
return walkableNodes;
}
public void WinEndGame()
{
isGameEnd = true;
fishSpawner.DestroyAllFish();
driftBottleManager.ClearAllStatuesCompleted();
EventDispatcher.TriggerEvent("HideShop",1);
ShowEndGameUI(true);
AudioManager.Ins.SoundPlay(1, true);
PlaySound2D("金币结算");
////修改处:销毁AI角色
if (aiCharacter != null)
{
//取消注册事件
AIController aiController = aiCharacter.GetComponent();
if (aiController != null)
{
aiController.OnIntroductionComplete -= StartGameAfterIntroduction;
}
Destroy(aiCharacter);
}
}
private float _createItemInterval=5;
private float curCreateItemTime;
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
//GameInit.Ins.self.AddCoin(5000);
GameInit.Ins.self.AddScore(10000);
WinEndGame();
//StartFishTideEvent();
//StartDriftBottleEvent();
}
if (fishSpawner.GetFishCount(FishType.AngelHorse) >= 1)
{
curCreateItemTime+=Time.deltaTime;
if (curCreateItemTime >= _createItemInterval)
{
CreateItemPre();
curCreateItemTime=0;
}
}
}
public void ClearItem()
{
foreach (var item in itemList)
{
Destroy(item);
}
itemList.Clear();
}
public void PlaySound2D(string soundName)
{
MasterAudio.PlaySound(soundName);
}
public void PlaySound3D(string soundName, Transform tran, bool isStop=false)
{
if(isStop)
MasterAudio.StopAllSoundsOfTransform(tran);
MasterAudio.PlaySound3DAtTransform(soundName, tran);
}
#region 鱼
///
/// 创建鱼群
///
public GameObject GetFishPre(int fishIndex)
{
if (fishIndex >= fishPres.Length)
return null;
return fishPres[fishIndex];
}
public void CreateFishTide()
{
// 计算鱼潮初始位置(与FishTideController一致)
Vector3 startOffset = new Vector3(8f, 0f, 8f);
Transform playerCam = GameInit.Ins.playerCam.transform;
Vector3 playerPos = playerCam.position;
Vector3 playerForward = playerCam.forward;
Vector3 playerRight = playerCam.right;
// 计算起始位置(玩家右前方,高度为0)
Vector3 spawnPosition = playerPos + playerForward * startOffset.z + playerRight * startOffset.x;
spawnPosition.y = 0f; // 高度设为0
// 面朝右边(玩家右侧方向)
Quaternion spawnRotation = Quaternion.LookRotation(-playerRight);
Instantiate(fishTidePre, spawnPosition, spawnRotation);
}
public void StartFishTideEvent()
{
CreateFishTide();
PlaySound2D("鱼潮事件NPC语音");
fishSpawner.StartFishTide();
Shop.ShowFishFideTip();
}
#endregion
#region 雕像事件
public void StartDriftBottleEvent()
{
driftBottleManager.StartStatueEvent();
}
#endregion
}