Files
SXDMystery/Assets/_SXDMystery/Scripts/Base/GameManager.cs
2025-12-16 11:15:33 +08:00

328 lines
7.9 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;
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;
/// <summary>
/// 游戏状态
/// </summary>
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<CoroutineTaskManager>.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<AIController>();
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);
}
/// <summary>
/// 更新配置表
/// </summary>
public void UpdateConf()
{
// string text = Resources.Load<TextAsset>("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<CoroutineTaskManager>.Instance.WaitSecondTodo(() =>
{
if (!isStartGame)
{
if (door.gameObject==null)
return;
door.gameObject.GetComponent<Collider>().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<Enemy>().Init();
enemy = curEnemy.GetComponent<Enemy>();
}
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
}