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;
///
/// 游戏场地
///
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,
Shandong_Jining_Wai=9,
ShanDong_Langfang_QingzhouTaihuacheng=10,
}
public class GameManager : MonoBehaviour
{
public static GameManager Ins { get; private set; }
///
/// 是否时间到了
///
public bool isTimeEnd = true;
// json数据库
private JsonLiteDB DB;
// Enemy子弹数据
public Dictionary EnemyBulletDataDic = new Dictionary();
// Player子弹数据
public Dictionary PlayerBulletDataDic = new Dictionary();
public Dictionary PropDataDic = new Dictionary();
public Dictionary BossPosDataDic = new Dictionary();
public List curEnemyList=new List();
public Dictionary EnemyDataDic = new Dictionary();
[Header("道具")]
public GameObject EnergyPumpPre;
public GameObject gameStartPointPre;
public GameObject enemyDoorPre;
[Header("敌人")]
public GameObject[] enemyPre;
public GameObject callEffectPre;//召唤特效
[Header("玩家")]
public GameObject playerPre;
public GameObject player;
///
/// 血包
///
public GameObject BloodBagPre;
///
/// 武器道具
///
public GameObject weaponItemPre;
///
/// 攻击BUFF
///
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);
}
///
/// 登录
///
public void Request(Action 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();
}
///
/// 更新配置表
///
public void InitData()
{
string text = "";
string text0 = Resources.Load(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);
}
}
///
/// 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;
// 游戏开始
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.Init();
if (isBoss)
{
curBoss = enemy;
}
else
{
curEnemyList.Add(enemy);
}
return go;
}
public GameObject CreateCallEnemyEffect(Vector3 pos)
{
GameObject go=Instantiate(callEffectPre,pos,quaternion.identity);
MonoSingleton.Instance.WaitSecondTodo(() =>
{
Destroy(go);
},3f);
return go;
}
void Destroy()
{
#if !UNITY_EDITOR
PXR_Enterprise.UnBindEnterpriseService();
#endif
}
public void CreateEnergyPump(Vector3 pos)
{
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);
}
if (GameInit.Ins.gamePlace == GamePlace.ShanDong_Langfang_QingzhouTaihuacheng)
{
EnergyPump.transform.position = pos;
}
EnergyPumpTag++;
EnergyPump.GetComponent().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;
}
///
/// 创建攻击BUFF
///
///
public void CreateAtkBuff(Vector3 pos)
{
GameObject atkBuff = Instantiate(AtkBuffPre);
atkBuff.transform.position = new Vector3(pos.x, 0, pos.z);
}
///
/// 创建防御BUFF
///
///
public void CreateDeeBuff(Vector3 pos)
{
GameObject deeBuff = Instantiate(deffBuffPre);
deeBuff.transform.position = new Vector3(pos.x, 0, pos.z);
}
///
/// 创建血包
///
public void CreateBloodBag(Vector3 pos)
{
GameObject bloodBag = Instantiate(BloodBagPre);
bloodBag.transform.position = new Vector3(pos.x, 0, pos.z);
}
///
/// 创建武器道具
///
public void CreatePlayerWeapon(PlayerWeaponType type, Vector3 pos)
{
GameObject prop = Instantiate(weaponItemPre);
prop.GetComponent().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 int GetCurEnemyListCount()
{
int count = 0;
foreach (var enemy in curEnemyList)
{
if(enemy!=null)
count++;
}
return count;
}
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(Vector3 pos)
{
curLevel++;
CurEnemyDie();
CreateEnergyPump(pos);
}
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.Instance.WaitSecondTodo(() =>
{
door.GetComponent().desTime = 6;
door.GetComponent().InitData();
},1f);
MonoSingleton.Instance.WaitSecondTodo(() =>
{
CreateEnemy(2, data.BossPos,data.BossAng,true);
},3f);
break;
case 2:
MonoSingleton.Instance.WaitSecondTodo(() =>
{
door.GetComponent().desTime = 10;
door.GetComponent().InitData();
},1f);
MonoSingleton.Instance.WaitSecondTodo(() =>
{
CreateEnemy(3, data.BossPos,data.BossAng,true);
},3f);
break;
case 3:
MonoSingleton.Instance.WaitSecondTodo(() =>
{
door.GetComponent().desTime = 10;
door.GetComponent().InitData();
},1f);
MonoSingleton.Instance.WaitSecondTodo(() =>
{
CreateEnemy(5, data.BossPos,data.BossAng,true);
},3f);
break;
case 4:
MonoSingleton.Instance.WaitSecondTodo(() =>
{
door.GetComponent().desTime = 6;
door.GetComponent().InitData();
},1f);
MonoSingleton.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);
}
}