using System;
using System.Collections;
using System.Collections.Generic;
using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Engines;
using Common;
using DragonLi.Core;
using UnityEngine;
using UnityEngine.PlayerLoop;
using XPlugin.Data.JsonLiteDB;
using static XPlugin.Data.JsonLiteDB.JsonLiteDB;
using LitJson;
using Pathfinding;
using DarkTonic.MasterAudio;
using NaughtyAttributes;
using UnityEngine.UIElements;
///
/// 游戏状态
///
public enum GameState
{
None = 0,
Playing = 1,
///
/// 胜利
///
Victory = 2,
///
/// 失败
///
Failur = 3,
Settle
}
public class GameManager : MonoBehaviour
{
public static GameManager Ins { get; private set; }
public TaskManager taskManager;
public GameState gameState;
public float vistEnd = 60 * 10;
public GameObject wuHui;
//预制体
public GameObject playerPre;
public GameObject enemyPre;
public GameObject gemKeyPre;
public GameObject eggBulletPre;
public GameObject eggBulletExPre;
public GameObject gameStartDoor;
public RightHand playerRightHand;
public Enemy enemy;
public bool isStartGame = false;
//添加
[Header("AI角色")]
public GameObject aiCharacterPre;//AI角色预制体
private GameObject aiCharacter;//AI角色实例
void Awake()
{
Ins = this;
}
private void Start()
{
isStartGame = false;
wuHui.SetActive(false);
}
public void GameStart()
{
CreateAICharacter();
//isStartGame = true;
//HUDPanel.Show();
//taskManager.bg.SetActive(true);
//CreateEnemy(GameLocal.Ins.enemyPos.position, new Vector3(0,180,0));
//宝石任务
//enemy.SetState(1);
//taskManager.curTaskId = 1;
//拔萝卜任务
// enemy.SetState(2);
// taskManager.curTaskId = 2;
//换装任务
//taskManager.curTaskId = 3;
//taskManager.StartTask();
//打鸡蛋任务
//enemy.SetState(3);
//taskManager.curTaskId = 4;
//taskManager.StartTask();
}
//修改处:添加创建AI角色的方法
private void CreateAICharacter()
{
//检查是否已经存在AI角色
if (aiCharacter != null)
{
Debug.Log("AI角色已经存在,不再创建新的");
return;
}
if (aiCharacterPre != null)
{
Debug.Log("创建AI角色");
//在玩家前方创建AI角色
Vector3 spawnPosition = playerPre.transform.position + playerPre.transform.forward * 3f;
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分配!");
}
}
public void StartGameAfterIntroduction()
{
isStartGame = true;
Debug.Log("AI介绍完成,开始游戏正常流程");
HUDPanel.Show();
taskManager.bg.SetActive(true);
CreateEnemy(GameLocal.Ins.enemyPos.position, new Vector3(0, 180, 0));
}
private IEnumerator DelayedStartIntroduction(AIController aiController)
{
yield return new WaitForSeconds(0.1f); // 短暂延迟
aiController.StartIntroduction();
}
public void LoginGame()
{
CreatePlayer();
//PlaySound2DRPC("1.25");
PlayBGM(0);
CreateGameStartDoor(GameLocal.Ins.startDoorPos.position);
}
public void PlayBGM(int id)
{
foreach (var item in GameLocal.Ins.BGM)
{
item.SetActive(false);
}
GameLocal.Ins.BGM[id].SetActive(true);
}
public void GameOver(GameState state)
{
}
///
/// 更新配置表
///
public void UpdateConf()
{
// string text = Resources.Load("Data").text;
// if (text != null)
// {
// ParseGameJson(text);
// }
}
public void ParseGameJson(string text)
{
// DB = new JsonLiteDB();
// DB.Load(text);
//
// TableReader infoReader = DB["EnemysInfo"].GetReader();
// while (infoReader.Read())
// {
// EnemyInfo info = new EnemyInfo(infoReader);
//
// }
Debug.Log("游戏数值更新 -> complete");
}
public void StopBgm()
{
}
public void PlaySound2DRPC(string sound)
{
MasterAudio.PlaySound(sound);
}
public void PlaySound3DRPC(string sound,Transform soundTran,bool isStop=false)
{
if(isStop)
MasterAudio.StopAllSoundsOfTransform(soundTran);
MasterAudio.PlaySound3DAtTransform(sound, soundTran);
}
public void CreateGameStartDoor(Vector3 pos)
{
var door= Instantiate(gameStartDoor, pos, Quaternion.identity);
if(GameLocal.Ins.place == Place.Jiangsu_Xvzhou_Suning_Guangchang
||GameLocal.Ins.place == Place.Hunan_Changde_Lixian_WandaGuangchang
)
{
door.transform.eulerAngles = new Vector3(0, 90, 0);
}
else
{
door.transform.eulerAngles = new Vector3(0, 180, 0);
}
MonoSingleton.Instance.WaitSecondTodo(() =>
{
if (!isStartGame)
{
if (door.gameObject==null)
return;
door.gameObject.GetComponent().enabled = false;
door.gameObject.SetActive(false);
Destroy(door.gameObject);
if (taskManager.curTaskId != 4)
{
GameStart();
}
else
{
StartDance();
}
}
},5);
}
public void CreatePlayer()
{
Instantiate(playerPre);
}
public void CreateEnemy(Vector3 pos,Vector3 eulerAngles)
{
var curEnemy=Instantiate(enemyPre,pos,Quaternion.identity);
curEnemy.transform.eulerAngles = eulerAngles;
curEnemy.GetComponent().Init();
enemy = curEnemy.GetComponent();
}
public GameObject CreateGemKey(Vector3 pos,Vector3 eulerAngles,Transform box)
{
var key= Instantiate(gemKeyPre, pos,Quaternion.identity,box);
key.transform.localEulerAngles = eulerAngles;
key.transform.localPosition = pos;
return key;
}
public void CreateEggBullet(Vector3 pos)
{
var egg= Instantiate(eggBulletPre, pos,Quaternion.identity);
egg.GetComponent().Init();
}
public void CreateEggEx(Vector3 pos)
{
var eggEx= Instantiate(eggBulletExPre, pos,Quaternion.identity);
PlaySound2DRPC("1.23");
MonoSingleton.Instance.WaitSecondTodo(() =>
{
Destroy(eggEx);
},5f);
}
public void PlayerHit()
{
if(enemy.isDie)
return;
//玩家UI模糊
HUDPanel.Ins.ShowEgg();
Debug.Log("击中玩家");
PlaySound2DRPC("1.50");
}
public void EnemyHit(Vector3 pos)
{
if(enemy.isDie)
return;
enemy.Hit();
PlaySound3DRPC("1.49",enemy.transform,true);
CreateEggEx(pos);
}
public void EnemyTimeHit()
{
enemy.isTimeHit = true;
}
public void StartGameEnd()
{
//任务结束
PlaySound2DRPC("1.24");
playerRightHand.ChangeHand();
taskManager.OverTask();
isStartGame = false;
CreateGameStartDoor(GameLocal.Ins.self.transform.position.ReflectVectorXOZ()+new Vector3(0,0,2));
}
public void StartDance()
{
//开启舞会
Debug.LogError("开启舞会场景");
PlayBGM(1);
wuHui.SetActive(true);
taskManager.bg.SetActive(false);
MonoSingleton.Instance.WaitSecondTodo(() =>
{
WinPanel.Show();
},10f);
MonoSingleton.Instance.WaitSecondTodo(() =>
{
wuHui.SetActive(false);
}, 15f);
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Q))
{
CreateEnemy(new Vector3(0,1,5), new Vector3(0,180,0));
enemy.SetState(3);
taskManager.curTaskId = 4;
taskManager.StartTask();
}
}
#region 工具
public string GetLessTimeStr()
{
string res = "";
if (gameState == GameState.Playing)
{
res = FormatTime((int)(vistEnd - DateTime.Now.Subtract(new DateTime(1970, 1, 1)).TotalSeconds));
}
return res;
}
public int GetLessTimeSeconds()
{
return (int)(vistEnd - DateTime.Now.Subtract(new DateTime(1970, 1, 1)).TotalSeconds);
}
// 时分秒
public string FormatTime(int totalSeconds)
{
int hours = totalSeconds / 3600;
// string hh = hours < 10 ? "0" + hours : hours.ToString();
int minutes = (totalSeconds - hours * 3600) / 60;
string mm = minutes < 10f ? "0" + minutes : minutes.ToString();
int seconds = totalSeconds - hours * 3600 - minutes * 60;
string ss = seconds < 10 ? "0" + seconds : seconds.ToString();
return string.Format("{0}:{1}", mm, ss);
}
#endregion
}