Files
test/Assets/Scripts/CECGameRun.cs
T
VDH a30427e3f5 Merge remote-tracking branch 'origin/bugfix/fix-character-animation' into feature/skill-data
# Conflicts:
#	Assets/PerfectWorld/Scripts/Move/CECPlayer.cs
#	Assets/PerfectWorld/Scripts/Network/CSNetwork/GameSession.cs
#	Assets/Scenes/NPCRender.unity
#	Assets/Scripts/CECGameRun.cs
#	Assets/Scripts/CECHostPlayer.cs
#	Assets/Scripts/CECHostPlayer_Inventory.cs
2025-11-19 10:35:54 +07:00

292 lines
9.3 KiB
C#

using BrewMonster;
using BrewMonster.Network;
using BrewMonster.Scripts.World;
using BrewMonster.UI;
using CSNetwork;
using CSNetwork.GPDataType;
using CSNetwork.GPDataType;
using CSNetwork.Protocols.RPCData;
using System.Data;
using Unity.Cinemachine;
using UnityEngine;
public partial class CECGameRun : MonoBehaviour, IMsgHandler
{
private static CECGameRun instance;
[SerializeField] private GameObject characterPrefab;
[SerializeField] private CECMonster monsterPrefab;
[SerializeField] private CECNPCServer npcServerPrefab;
[SerializeField] private CinemachineCamera cinemachineCamera;
[SerializeField] private GameObject _testVfxPrefab;
//[SerializeField] private Transform ground;
CECHostPlayer hostPlayer;
public float rotateSpeedX = 300f; // tốc độ xoay ngang
public float rotateSpeedY = 2f; // tốc độ xoay dọc
private CECWorld m_pWorld;
protected CECUIManager m_pUIManager; // UI manager
public CECWorld GetWorld() { return m_pWorld; }
public bool StartGame(int idInst, Vector3 vHostPos)
{
if (!JumpToInstance(idInst, vHostPos))
{
BMLogger.LogError("CECGameRun::StartGame, Failed to create game world.");
return false;
}
return true;
}
private bool JumpToInstance(int idInst, Vector3 vHostPos, int iParallelWorldID = 0)
{
return true;
}
public static CECGameRun Instance
{
get
{
if (instance == null)
{
instance = FindAnyObjectByType<CECGameRun>();
}
return instance;
}
}
private void Awake()
{
if (instance == null)
{
instance = this;
}
EC_ManMessage.RegisterHandler(this);
}
public void Log(string s)
{
Debug.LogError(s);
}
public CECHostPlayer GetHostPlayer()
{
if (hostPlayer == null)
{
hostPlayer = FindAnyObjectByType<CECHostPlayer>();
}
return hostPlayer;
}
public void InitCharacter(cmd_self_info_1 info)
{
if (characterPrefab == null)
{
Debug.LogError("null prefab");
return;
}
CECPlayer.InitStaticRes();
hostPlayer = Instantiate(characterPrefab, transform).AddComponent<CECHostPlayer>();
hostPlayer.InitCharacter(info);
cinemachineCamera.Follow = hostPlayer.transform;
cinemachineCamera.ForceCameraPosition(hostPlayer.transform.position, Quaternion.identity);
//Vector3 pos = new Vector3(info.pos.x, info.pos.y, info.pos.z);
//Vector3 posCam = pos;
//posCam.z -= 10f;
//camera.transform.position = posCam;
//Vector3 posGround = pos;
//posGround.y -= 2f;
//ground.transform.position = posGround;
}
public CECMonster GetMonster()
{
return Instantiate(monsterPrefab, transform);
}
public CECNPCServer GetNPCServer()
{
return Instantiate(npcServerPrefab, transform);
}
public GameObject GetTestVfx()
{
return Instantiate(_testVfxPrefab, transform);
}
public void DestroyTestVfx(GameObject go, float delayTime)
{
Destroy(go,delayTime);
}
public GameObject InitCharacter(info_player_1 info)
{
if (characterPrefab == null)
{
Debug.LogError("null prefab");
return null;
}
GameObject character = Instantiate(characterPrefab, transform);
return character.gameObject;
}
private void OnDestroy()
{
instance = null;
}
public GameObject InstantiateObject(GameObject prefab)
{
return Instantiate(prefab, transform);
}
//private void Update()
//{
// if (hostPlayer == null || hostPlayer.transform == null)
// return;
// bool rightClick = Input.GetMouseButton(1);
// if (rightClick)
// {
// // Bật khả năng xoay khi giữ chuột phải
// freeLookCam.m_XAxis.m_MaxSpeed = rotateSpeedX;
// freeLookCam.m_YAxis.m_MaxSpeed = rotateSpeedY;
// // Ghi giá trị chuột thủ công (nếu muốn override Input System cũ)
// float mouseX = Input.GetAxis("Mouse X");
// float mouseY = Input.GetAxis("Mouse Y");
// // Xoay camera theo hướng chuột
// freeLookCam.m_XAxis.Value += mouseX * Time.deltaTime * rotateSpeedX;
// freeLookCam.m_YAxis.Value -= mouseY * Time.deltaTime * (rotateSpeedY / 100f);
// }
// else
// {
// // Khi thả chuột thì khoá xoay
// freeLookCam.m_XAxis.m_MaxSpeed = 0;
// freeLookCam.m_YAxis.m_MaxSpeed = 0;
// }
//}
/// <summary>
/// Load necessary user configs (UI, shortcut, accelerate keys) from server
/// 从服务器加载必要的用户配置(UI、快捷键、加速键)
/// </summary>
/// <param name="pDataBuf">Config data buffer / 配置数据缓冲区</param>
/// <param name="iDataSize">Data size / 数据大小</param>
/// <returns>True if loaded successfully / 加载成功返回true</returns>
public bool LoadConfigsFromServer(byte[] pDataBuf, int iDataSize)
{
const uint USERCFG_VERSION = 3;
if (pDataBuf == null || iDataSize == 0)
{
BMLogger.LogError("CECGameRun::LoadConfigsFromServer, configs data is empty");
return false;
}
// TODO: Uncomment when these objects are available
// Debug.Assert(m_pWorld != null && m_pWorld.GetHostPlayer() != null && m_pUIManager.GetInGameUIMan() != null);
int offset = 0;
// Read version / 读取版本号
uint dwVer = System.BitConverter.ToUInt32(pDataBuf, offset);
offset += sizeof(uint);
if (dwVer > USERCFG_VERSION)
{
Debug.LogError($"CECGameRun::LoadConfigsFromServer, version {dwVer} > {USERCFG_VERSION}");
return false;
}
byte[] pUncompBuf = null;
uint dwRealLen = (uint)(iDataSize - sizeof(uint));
byte[] pData = pDataBuf;
int dataOffset = offset;
if (dwVer >= 3)
{
// Uncompress config data / 解压配置数据
dwRealLen = 4096;
pUncompBuf = new byte[dwRealLen];
// Extract compressed data / 提取压缩数据
byte[] compressedData = new byte[iDataSize - sizeof(uint)];
System.Array.Copy(pDataBuf, offset, compressedData, 0, compressedData.Length);
// TODO: Implement AFilePackage::Uncompress equivalent
int iRes = EC_Utility.UncompressData(compressedData, compressedData.Length, pUncompBuf, ref dwRealLen);
if (iRes != 0)
{
// 解压失败,过程出错 / Decompression failed, process error
BMLogger.LogError($"CECGameRun::LoadConfigsFromServer, Failed to uncompress configs data ({iRes}:{iDataSize})");
return false;
}
pData = pUncompBuf;
dataOffset = 0;
}
try
{
// Create data reader / 创建数据读取器
CECDataReader dr = new CECDataReader(pData, dataOffset, (int)dwRealLen);
// Load host configs / 加载主机配置
CECHostPlayer pHost = GetHostPlayer();
if (pHost != null)
{
int iSize = dr.ReadInt();
byte[] hostConfigData = dr.ReadData(iSize);
if (!pHost.LoadConfigData(hostConfigData))
{
BMLogger.LogError("CECGameRun::LoadConfigsFromServer, Failed to load host configs");
return false;
}
}
// TODO: Uncomment when UI manager is available
// Load UI configs / 加载UI配置
// CECGameUIMan pGameUI = m_pUIManager.GetInGameUIMan();
// if (pGameUI != null)
// {
// int iSize = dr.ReadInt();
// byte[] uiConfigData = dr.ReadData(iSize);
// if (!pGameUI.SetUserLayout(uiConfigData, iSize))
// {
// BMLogger.LogError("CECGameRun::LoadConfigsFromServer, Failed to set user layout");
// return false;
// }
// }
// Load user settings / 加载用户设置
if (dwVer >= 2)
{
// TODO: Uncomment when game configs are available
// int iSize = dr.ReadInt();
// byte[] settingsData = dr.ReadData(iSize);
// if (!g_pGame.GetConfigs().LoadUserConfigData(settingsData, iSize))
// {
// BMLogger.LogError("CECGameRun::LoadConfigsFromServer, Failed to load user config data");
// return false;
// }
}
}
catch (System.Exception e)
{
BMLogger.LogError($"CECGameRun::LoadConfigsFromServer, data read error: {e.Message}");
return false;
}
return true;
}
// Get UI manager
public CECUIManager GetUIManager()
{
if(m_pUIManager == null)
{
m_pUIManager = CECUIManager.Instance;
}
return m_pUIManager;
}
}