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,//游戏未开始状态
Waiting=1,//探索状态
Playing=2,//任务进行中
GameEnded=3,//游戏结束
}
public class GameManager : MonoBehaviour
{
public static GameManager Ins { get; private set; }
public TaskManager taskManager;
public GameState gameState;
public float vistEnd = 60 * 1;
//预制体
public GameObject playerPre;
public GameObject enemyPre;
public GameObject mousePre;
public GameObject gameStartDoor;
public RightHand playerRightHand;
public Enemy enemy;
public bool isStartGame = false;
public bool isTaskRunning;
//添加
[Header("AI角色")]
public GameObject aiCharacterPre;//AI角色预制体
private GameObject aiCharacter;//AI角色实例
void Awake()
{
Ins = this;
}
private void Start()
{
isStartGame = false;
}
public void GameStart()
{
CreateAICharacter();
MonoSingleton.Instance.WaitSecondTodo(() =>
{
Application.Quit();
},5f);
}
#region 创造
//修改处:添加创建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 CreateMouse()
{
}
#endregion
#region 任务
public void OnCatchMouseTaskFinish()
{
Debug.LogError("完成捉鼠任务");
GetBadge();
}
// ✅ 青铜神树支线任务完成
public void FinishTreeTask(bool success)
{
isTaskRunning = false;
if (success)
{
Debug.Log("获得【鉴伪大师】徽章");
// 存档、解锁成就、解锁下一区域
}
else
{
Debug.Log("任务失败");
}
}
public void GetBadge()
{
Debug.LogError("获得徽章");
PlaySound2DRPC("1.7");
}
public bool IsTaskRunning()
{
return isTaskRunning;
}
public void SetTaskRunning(bool running)
{
isTaskRunning = running;
}
#endregion
public void StartGameAfterIntroduction()
{
isStartGame = true;
Debug.Log("AI介绍完成,开始游戏正常流程");
HUDPanel.Show();
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 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);
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);
}
},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 void StartGameEnd()
{
//任务结束
PlaySound2DRPC("1.24");
playerRightHand.ChangeHand();
isStartGame = false;
CreateGameStartDoor(GameLocal.Ins.self.transform.position.ReflectVectorXOZ()+new Vector3(0,0,2));
}
public void SetHandState(bool isOpenLine)
{
playerRightHand._isOpenLine = isOpenLine;
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Q))
{
}
}
#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
}