Files
XMen/Assets/Scripts/Manager/GameManager.cs
2025-07-02 17:56:55 +08:00

536 lines
16 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections.Generic;
using BestHTTP;
using DragonLi.Core;
using Unity.Mathematics;
using Unity.XR.PXR;
using UnityEngine;
using XPlugin.Data.JsonLiteDB;
using static XPlugin.Data.JsonLiteDB.JsonLiteDB;
using Random = UnityEngine.Random;
/// <summary>
/// 游戏场地
/// </summary>
public enum GamePlace
{
Test=-1,
Company1Floor=0,
LiaoningAnShan=1,
HangZhouLongHuTianJie=2,
Nanjing_Yuhua_Wanxiang=3,
Nanjing_Xianlin_WanDaMao=4,
Yangzhou_Hanjiang_Tansuozhongxin=5,
Yangzhou_Hanjiang_TansuoZhongxin_wai=-5,
Zhejiang_Jinhua_KeJiGuan=6,
Guangzhou_Panyv_Zhanting=7,
Anhui_Wuhu_Guanwei=8,
}
public class GameManager : MonoBehaviour
{
public static GameManager Ins { get; private set; }
/// <summary>
/// 是否时间到了
/// </summary>
public bool isTimeEnd = true;
// json数据库
private JsonLiteDB DB;
// Enemy子弹数据
public Dictionary<int, BulletData> EnemyBulletDataDic = new Dictionary<int, BulletData>();
// Player子弹数据
public Dictionary<int, BulletData> PlayerBulletDataDic = new Dictionary<int, BulletData>();
public Dictionary<int, PropData> PropDataDic = new Dictionary<int, PropData>();
public Dictionary<int,BossPosData> BossPosDataDic = new Dictionary<int, BossPosData>();
public List<Enemy> curEnemyList=new List<Enemy>();
public Dictionary<int, EnemyData> EnemyDataDic = new Dictionary<int, EnemyData>();
[Header("道具")]
public GameObject EnergyPumpPre;
public GameObject gameStartPointPre;
public GameObject enemyDoorPre;
[Header("敌人")]
public GameObject[] enemyPre;
public GameObject callEffectPre;//召唤特效
[Header("玩家")]
public GameObject playerPre;
public GameObject player;
/// <summary>
/// 血包
/// </summary>
public GameObject BloodBagPre;
/// <summary>
/// 武器道具
/// </summary>
public GameObject weaponItemPre;
/// <summary>
/// 攻击BUFF
/// </summary>
public GameObject AtkBuffPre;
public GameObject deffBuffPre;
public bool GameStart = false;
public bool isGameEnd = false;
public int EnergyPumpTag = 0;
public float EnergyPumpFillAmount = 0;
public float curTime;
public float buffAtk = 0f;
public float buffDef = 0f;
public LoginInfo loginInfo=new LoginInfo();
public Enemy curBoss;
public int curLevel = 0;
public void Start()
{
Ins = this;
CreatePlayer();
LoginPanel.Show();
#if !UNITY_EDITOR
PXR_Enterprise.InitEnterpriseService();
PXR_Enterprise.BindEnterpriseService();
#endif
CoroutineTaskManager.Instance.WaitSecondTodo(ShowPlayerUI, 1.5f);
}
/// <summary>
/// 登录
/// </summary>
public void Request(Action<HTTPRequest, HTTPResponse> cb = null)
{
string url = "https://startcount-bgovpxyjzl.cn-hangzhou.fcapp.run";
HTTPRequest request = new HTTPRequest(new Uri(url), HTTPMethods.Post, (req, response) =>
{
if (response != null)
{
Debug.Log("收到数据 ->" + response.DataAsText);
}
cb?.Invoke(req, response);
});
loginInfo.deviceSn = GetPicoSn();
loginInfo.startAt = ConvertTimestampToDateTime(GetTimestamp()) + "";
loginInfo.shop = 0;
#if !UNITY_EDITOR && UNITY_ANDROID && PICO
loginInfo.shop = (int)GameInit.Ins.gamePlace;
if(GameInit.Ins.gamePlace== GamePlace.Yangzhou_Hanjiang_TansuoZhongxin_wai)
loginInfo.shop = 5;
#endif
loginInfo.gameId = 2;
string authJson = JsonUtility.ToJson(loginInfo);
Debug.Log("发送数据 -> " + authJson);
request.RawData = System.Text.Encoding.UTF8.GetBytes(authJson);
request.AddHeader("Content-Type", "application/json");
request.Send();
}
/// <summary>
/// 更新配置表
/// </summary>
public void InitData()
{
string text = "";
string text0 = Resources.Load<TextAsset>(string.Format("zh/{0}Data",GameInit.Ins.gamePlace)).text;
GetReaderJson(text0);
}
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["EnemyData"].GetReader();
EnemyDataDic.Clear();
while (infoReader.Read())
{
EnemyData info = new EnemyData(infoReader);
EnemyDataDic.Add(info.Id, info);
}
// 敌人子弹数据
TableReader EnemyBulletReader = DB["EnemyBulletData"].GetReader();
EnemyBulletDataDic.Clear();
while (EnemyBulletReader.Read())
{
BulletData data = new BulletData(EnemyBulletReader);
EnemyBulletDataDic.Add(data.ID,data);
}
// 玩家子弹数据
TableReader PlayerBulletReader = DB["PlayerBulletData"].GetReader();
PlayerBulletDataDic.Clear();
while (PlayerBulletReader.Read())
{
BulletData data = new BulletData(PlayerBulletReader);
PlayerBulletDataDic.Add(data.ID,data);
}
// 道具表
TableReader PropReader = DB["PropData"].GetReader();
PropDataDic.Clear();
while (PropReader.Read())
{
PropData data = new PropData(PropReader);
PropDataDic.Add(data.PropId,data);
}
// Boss位置
TableReader BossPosReader = DB["BossPosData"].GetReader();
BossPosDataDic.Clear();
while (BossPosReader.Read())
{
BossPosData data = new BossPosData(BossPosReader);
BossPosDataDic.Add(data.ID,data);
}
}
/// <summary>
/// Get sn
/// </summary>
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;
// 游戏开始
public void GamePlay()
{
isGamePlay = true;
CreateBoss();
PlayerUIMessage(0);
CoroutineTaskManager.Instance.WaitSecondTodo(() =>
AudioManager.Ins?.SoundPlay("bgm2", true), 10f);
CoroutineTaskManager.Instance.WaitSecondTodo(() =>
AudioManager.Ins?.SoundPlay("2.39BGM", true), 30f);
}
public void ShowPlayerUI()
{
OverlayUI.Ins.Cover("UI/PlayerUI", false);
}
public void ShowEndGameUI(bool isWin)
{
CurEnemyDie();
GameEndPanel.Show(isWin);
}
public GameObject CreateEnemy(int id,Vector3 pos,Vector3 eulerAngles,bool isBoss)
{
GameObject go=Instantiate(enemyPre[id],pos,Quaternion.identity);
go.transform.localEulerAngles = eulerAngles;
Enemy enemy = go.GetComponent<Enemy>();
enemy.Init();
if (isBoss)
{
curBoss = enemy;
}
else
{
curEnemyList.Add(enemy);
}
return go;
}
public GameObject CreateCallEnemyEffect(Vector3 pos)
{
GameObject go=Instantiate(callEffectPre,pos,quaternion.identity);
MonoSingleton<CoroutineTaskManager>.Instance.WaitSecondTodo(() =>
{
Destroy(go);
},3f);
return go;
}
void Destroy()
{
#if !UNITY_EDITOR
PXR_Enterprise.UnBindEnterpriseService();
#endif
}
public void CreateEnergyPump()
{
isGamePlay = false;
GameObject EnergyPump = Instantiate(EnergyPumpPre);
//EnergyPump.transform.position = pos;
EnergyPump.transform.position = new Vector3(2,0,2);
if (GameInit.Ins.gamePlace == GamePlace.Nanjing_Xianlin_WanDaMao)
{
EnergyPump.transform.position = new Vector3(-1.2f,0,1.5f);
}
if (GameInit.Ins.gamePlace == GamePlace.Nanjing_Yuhua_Wanxiang)
{
EnergyPump.transform.position = new Vector3(-3,0,2);
}
if (GameInit.Ins.gamePlace == GamePlace.Yangzhou_Hanjiang_Tansuozhongxin)
{
EnergyPump.transform.position = new Vector3(-1.2f,0,0f);
}
if (GameInit.Ins.gamePlace == GamePlace.Yangzhou_Hanjiang_TansuoZhongxin_wai)
{
EnergyPump.transform.position = new Vector3(0,0,2f);
}
if (GameInit.Ins.gamePlace == GamePlace.Zhejiang_Jinhua_KeJiGuan)
{
EnergyPump.transform.position = new Vector3(0,0,2f);
}
if (GameInit.Ins.gamePlace == GamePlace.Guangzhou_Panyv_Zhanting)
{
EnergyPump.transform.position = new Vector3(0,0,2f);
}
EnergyPumpTag++;
EnergyPump.GetComponent<EnergyPump>().Init(EnergyPumpTag);
}
public void PlayerUIMessage(int index)
{
EventDispatcher.TriggerEvent("PromptMessage", index);
}
public void CreateGameStartPoint()
{
GameStart = true;
isGamePlay = false;
GameObject point = Instantiate(gameStartPointPre);
if (GameInit.Ins.gamePlace == GamePlace.LiaoningAnShan)
{
point.transform.position = new Vector3(-2, 0, 2);
}
else
{
point.transform.position = Vector3.zero;
}
CoroutineTaskManager.Instance.WaitSecondTodo(() =>
{
if(isGamePlay)
return;
Destroy(point.gameObject);
GamePlay();
}, 5f);
}
public GameObject CreateEnemyDoor(Vector3 pos, Vector3 angle,Vector3 scale)
{
GameObject crack = Instantiate(enemyDoorPre);
crack.transform.position = new Vector3(pos.x, pos.y, pos.z);
crack.transform.localEulerAngles = angle;
crack.transform.localScale = scale;
return crack;
}
/// <summary>
/// 创建攻击BUFF
/// </summary>
/// <param name="pos"></param>
public void CreateAtkBuff(Vector3 pos)
{
GameObject atkBuff = Instantiate(AtkBuffPre);
atkBuff.transform.position = new Vector3(pos.x, 0, pos.z);
}
/// <summary>
/// 创建防御BUFF
/// </summary>
/// <param name="pos"></param>
public void CreateDeeBuff(Vector3 pos)
{
GameObject deeBuff = Instantiate(deffBuffPre);
deeBuff.transform.position = new Vector3(pos.x, 0, pos.z);
}
/// <summary>
/// 创建血包
/// </summary>
public void CreateBloodBag(Vector3 pos)
{
GameObject bloodBag = Instantiate(BloodBagPre);
bloodBag.transform.position = new Vector3(pos.x, 0, pos.z);
}
/// <summary>
/// 创建武器道具
/// </summary>
public void CreatePlayerWeapon(PlayerWeaponType type, Vector3 pos)
{
GameObject prop = Instantiate(weaponItemPre);
prop.GetComponent<WeaponProp>().Init(type);
prop.transform.position = new Vector3(pos.x,1f,pos.z);
}
public bool isFirstGetWeapon;
public void EnemyDisGetWeapon(Vector3 pos)
{
bool isGet = Random.Range(0, 100) > 70;
if (!isGet) return;
int index = Random.Range(0, 100);
int id = 2;
if (index <= 10)
id = Random.Range(8, 10);
if(index is > 10 and <= 50)
id= Random.Range(5, 8);
if(index is > 50)
id= Random.Range(2, 5);
CreatePlayerWeapon((PlayerWeaponType)id, pos);
if (!isFirstGetWeapon)
{
GameInit.Ins.PlayAudio("1.5",GameInit.Ins.self.transform,true);
isFirstGetWeapon = true;
}
}
public void CurEnemyDie()
{
foreach (var enemy in curEnemyList)
{
if(enemy!=null)
enemy.Dead();
}
curEnemyList.Clear();
}
public void CreatePlayer()
{
player=Instantiate(playerPre);
}
public bool IsAllBossPetDie()
{
foreach (var enemy in curEnemyList)
{
if (enemy != null && enemy.health > 0)
return false;
}
return true;
}
public void CurLevelWin()
{
curLevel++;
CurEnemyDie();
CreateEnergyPump();
}
public void CreateBoss()
{
BossPosData data = BossPosDataDic[curLevel];
GameObject door = null;
if(curLevel!=0)
door=CreateEnemyDoor(data.DoorPos, data.DoorAng,Vector3.one*data.DoorScale);
switch (curLevel)
{
case 0:
CreateEnemy(0, data.BossPos, Vector3.zero,true);
break;
case 1:
//舰载机
MonoSingleton<CoroutineTaskManager>.Instance.WaitSecondTodo(() =>
{
door.GetComponent<EnemyDoor>().desTime = 6;
door.GetComponent<EnemyDoor>().InitData();
},1f);
MonoSingleton<CoroutineTaskManager>.Instance.WaitSecondTodo(() =>
{
CreateEnemy(2, data.BossPos,data.BossAng,true);
},3f);
break;
case 2:
MonoSingleton<CoroutineTaskManager>.Instance.WaitSecondTodo(() =>
{
door.GetComponent<EnemyDoor>().desTime = 10;
door.GetComponent<EnemyDoor>().InitData();
},1f);
MonoSingleton<CoroutineTaskManager>.Instance.WaitSecondTodo(() =>
{
CreateEnemy(3, data.BossPos,data.BossAng,true);
},3f);
break;
case 3:
MonoSingleton<CoroutineTaskManager>.Instance.WaitSecondTodo(() =>
{
door.GetComponent<EnemyDoor>().desTime = 10;
door.GetComponent<EnemyDoor>().InitData();
},1f);
MonoSingleton<CoroutineTaskManager>.Instance.WaitSecondTodo(() =>
{
CreateEnemy(5, data.BossPos,data.BossAng,true);
},3f);
break;
case 4:
MonoSingleton<CoroutineTaskManager>.Instance.WaitSecondTodo(() =>
{
door.GetComponent<EnemyDoor>().desTime = 6;
door.GetComponent<EnemyDoor>().InitData();
},1f);
MonoSingleton<CoroutineTaskManager>.Instance.WaitSecondTodo(() =>
{
CreateEnemy(7, data.BossPos,data.BossAng,true);
},1f);
break;
}
}
public void WinEndGame()
{
isGameEnd = true;
ShowEndGameUI(true);
GameInit.Ins.PlayAudio("1.17",GameInit.Ins.self.transform,true);
AudioManager.Ins?.SoundPlay("bgm4", true);
}
public void LoseEndGame()
{
ShowEndGameUI(false);
if (curBoss != null)
{
Destroy(curBoss.gameObject);
}
GameInit.Ins.self.IsAlive = false;
GameInit.Ins.self.End();
isGameEnd = true;
CurEnemyDie();
AudioManager.Ins?.SoundPlay("bgm4", true);
}
}