refactor game run and fix game flow

This commit is contained in:
NguyenVanDat
2025-11-21 11:48:39 +07:00
parent a35cd6d0e7
commit 8f5673c68e
35 changed files with 256 additions and 97 deletions
+52 -61
View File
@@ -1,34 +1,58 @@
using BrewMonster;
using BrewMonster.Network;
using BrewMonster.Scripts;
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
public partial class CECGameRun
{
private static CECGameRun instance;
[SerializeField] private GameObject characterPrefab;
[SerializeField] private CECMonster monsterPrefab;
[SerializeField] private CECNPCServer npcServerPrefab;
[SerializeField] private CinemachineCamera cinemachineCamera;
[SerializeField] private GameObject _testVfxPrefab;
private GameObject _playerPrefab;
private GameObject _monsterPrefab;//CECMonster
private GameObject _npcServerPrefab;//CECNPCServer
private GameObject _testVfxPrefab;
// private GameRunConfig _gameRunConfig;
//[SerializeField] private Transform ground;
CECHostPlayer hostPlayer;
public CinemachineFreeLook freeLookCam;
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 void Init()
{
instance = this;
// _gameRunConfig = Resources.Load<GameRunConfig>("GameRunConfig");
// _playerPrefab = _gameRunConfig.PlayerPrefab;
// _monsterPrefab = _gameRunConfig.MonsterPrefab;
// _npcServerPrefab = _gameRunConfig.NpcServerPrefab;
// _testVfxPrefab = _gameRunConfig.TestVfxPrefab;
LoadPrefabs();
// LoadPrefabs();
EC_ManMessage.RegisterHandler(this);
}
public static void Dispose()
{
instance = null;
AddressableManager.Instance.UnloadAsset(AddressResourceConfig.PlayerPrefab);
AddressableManager.Instance.UnloadAsset(AddressResourceConfig.MonsterPrefab);
AddressableManager.Instance.UnloadAsset(AddressResourceConfig.NpcServerPrefab);
AddressableManager.Instance.UnloadAsset(AddressResourceConfig.TestVfxPrefab);
}
private async void LoadPrefabs()
{
_playerPrefab = await AddressableManager.Instance.LoadPrefabAsync(AddressResourceConfig.PlayerPrefab);
_monsterPrefab = await AddressableManager.Instance.LoadPrefabAsync(AddressResourceConfig.MonsterPrefab);
_npcServerPrefab = await AddressableManager.Instance.LoadPrefabAsync(AddressResourceConfig.NpcServerPrefab);
_testVfxPrefab = await AddressableManager.Instance.LoadPrefabAsync(AddressResourceConfig.TestVfxPrefab);
}
public bool StartGame(int idInst, Vector3 vHostPos)
{
if (!JumpToInstance(idInst, vHostPos))
@@ -50,90 +74,57 @@ public partial class CECGameRun : MonoBehaviour, IMsgHandler
{
if (instance == null)
{
instance = FindAnyObjectByType<CECGameRun>();
instance = new CECGameRun();
instance.Init();
}
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)
if (_playerPrefab == null)
{
Debug.LogError("null prefab");
BMLogger.LogError("null _playerPrefab");
return;
}
CECPlayer.InitStaticRes();
hostPlayer = Instantiate(characterPrefab, transform).AddComponent<CECHostPlayer>();
hostPlayer = ObjectSpawner.Instance.InstantiateObject(_playerPrefab, setThisAsParent:true).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);
return ObjectSpawner.Instance.InstantiateObject(_monsterPrefab, setThisAsParent: true)
.GetComponent<CECMonster>();
}
public CECNPCServer GetNPCServer()
{
return Instantiate(npcServerPrefab, transform);
return ObjectSpawner.Instance.InstantiateObject(_npcServerPrefab, setThisAsParent:true).GetComponent<CECNPCServer>();
}
public GameObject GetTestVfx()
{
return Instantiate(_testVfxPrefab, transform);
return ObjectSpawner.Instance.InstantiateObject(_testVfxPrefab, setThisAsParent: true);
}
public void DestroyTestVfx(GameObject go, float delayTime)
{
Destroy(go,delayTime);
ObjectSpawner.Instance.DestroyGameObject(go, delayTime);
}
public GameObject InitCharacter(info_player_1 info)
{
if (characterPrefab == null)
if (_playerPrefab == null)
{
Debug.LogError("null prefab");
return null;
}
GameObject character = Instantiate(characterPrefab, transform);
GameObject character = ObjectSpawner.Instance.InstantiateObject(_playerPrefab, setThisAsParent: true);
return character.gameObject;
}
private void OnDestroy()
{
instance = null;
}
public GameObject InstantiateObject(GameObject prefab)
{
return Instantiate(prefab, transform);
}
//private void Update()
//{