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
@@ -20,16 +20,36 @@ MonoBehaviour:
m_ReadOnly: 0
m_SerializedLabels: []
FlaggedDuringContentUpdateRestriction: 0
- m_GUID: 1f80fef119a826b4a826baf52a8f92d7
m_Address: Prefabs/Player/PlayerPrefab
m_ReadOnly: 0
m_SerializedLabels: []
FlaggedDuringContentUpdateRestriction: 0
- m_GUID: 23c524cd6f7fb27449cee2b10668fdd7
m_Address: "\u7A0B\u5E8F\u8054\u5165/\u9F20\u6807\u60AC\u6D6E.gfx"
m_ReadOnly: 0
m_SerializedLabels: []
FlaggedDuringContentUpdateRestriction: 0
- m_GUID: 244942d3fb9382846b82581ce24fbf4e
m_Address: Prefabs/Monster/MonsterPrefab
m_ReadOnly: 0
m_SerializedLabels: []
FlaggedDuringContentUpdateRestriction: 0
- m_GUID: 4b6364e18e35c47aab298e1d0a1c0def
m_Address: "models/npcs/\u602A\u7269/\u91CE\u517D/\u72FC/\u70BD\u7130\u72FC\u738B.ecm"
m_ReadOnly: 0
m_SerializedLabels: []
FlaggedDuringContentUpdateRestriction: 0
- m_GUID: 88e840e5dac12ed4383d994c7fadfef1
m_Address: Prefabs/Vfx/TestVfx
m_ReadOnly: 0
m_SerializedLabels: []
FlaggedDuringContentUpdateRestriction: 0
- m_GUID: 9bd42cdc18b3d624db35707155fc50de
m_Address: Prefabs/NPC/NPCServer
m_ReadOnly: 0
m_SerializedLabels: []
FlaggedDuringContentUpdateRestriction: 0
m_ReadOnly: 0
m_Settings: {fileID: 11400000, guid: 070edb522e6e54c14a99055900003890, type: 2}
m_SchemaSet:
+8
View File
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: f0e041e8264a83545bf6e555757c9851
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 11e93f1f86aabfa4cb76a0762f688f59
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,18 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 913f99a0553e9ec438c859c044d5c04b, type: 3}
m_Name: GameRunConfig
m_EditorClassIdentifier:
playerPrefab: {fileID: 6513559496054861882, guid: 1f80fef119a826b4a826baf52a8f92d7, type: 3}
monsterPrefab: {fileID: 6975799234359536760, guid: 244942d3fb9382846b82581ce24fbf4e, type: 3}
npcServerPrefab: {fileID: 2426933851766496167, guid: 9bd42cdc18b3d624db35707155fc50de, type: 3}
testVfxPrefab: {fileID: 2282869967233380084, guid: 88e840e5dac12ed4383d994c7fadfef1, type: 3}
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 883220837a320e5469c2cbab056f86c4
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:
@@ -111,7 +111,6 @@ GameObject:
m_Component:
- component: {fileID: 2780428059708698453}
- component: {fileID: 1890210201668811196}
- component: {fileID: 6919949516443544071}
m_Layer: 0
m_Name: GameController
m_TagString: Untagged
@@ -153,23 +152,3 @@ MonoBehaviour:
currentTargetNPCID: 0
dialogResouce: {fileID: 11400000, guid: 540bc8e61556ba4479407a2d68e17580, type: 2}
canvasDlg: {fileID: 7894129013412138377}
--- !u!114 &6919949516443544071
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6950113420985123515}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 5de219a5b9756ae4ebf01e2919b92cde, type: 3}
m_Name:
m_EditorClassIdentifier:
characterPrefab: {fileID: 6513559496054861882, guid: 1f80fef119a826b4a826baf52a8f92d7, type: 3}
monsterPrefab: {fileID: 2542060226037108388, guid: 244942d3fb9382846b82581ce24fbf4e, type: 3}
npcServerPrefab: {fileID: -5899287755522118344, guid: 9bd42cdc18b3d624db35707155fc50de, type: 3}
cinemachineCamera: {fileID: 0}
_testVfxPrefab: {fileID: 2282869967233380084, guid: 88e840e5dac12ed4383d994c7fadfef1, type: 3}
freeLookCam: {fileID: 0}
rotateSpeedX: 300
rotateSpeedY: 2
+2 -2
View File
@@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:f2f84021795e4a19e8f6abeb4f2329aff81db3f188790486d5f6df4735ea760d
size 68126
oid sha256:313596d9b61bddad09dbe2897de5659555f564855ddb10bbddb9daf59a56de7a
size 69371
+8
View File
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 246ab49d6d6e35e4f8c8945ba84ede9f
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,19 @@
using Unity.Cinemachine;
using UnityEngine;
namespace BrewMonster
{
public class CameraController : MonoBehaviour
{
[SerializeField]private CinemachineCamera _cinemachineCamera;
private void Update()
{
//todo: should not always update
if (_cinemachineCamera.Follow == null && CECGameRun.Instance.GetHostPlayer() != null)
{
_cinemachineCamera.Follow = CECGameRun.Instance.GetHostPlayer().transform;
_cinemachineCamera.ForceCameraPosition(CECGameRun.Instance.GetHostPlayer().transform.position, Quaternion.identity);
}
}
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: cbda204e0e5552e4692f0f5e234f062d
@@ -0,0 +1,21 @@
using UnityEngine;
using UnityEngine.Serialization;
namespace BrewMonster
{
[CreateAssetMenu(fileName = "GameRunConfig", menuName = "BrewMonster/Game Run Config")]
public class GameRunConfig : ScriptableObject
{
[FormerlySerializedAs("characterPrefab")]
[Header("Prefabs")]
[SerializeField] private GameObject playerPrefab;
[SerializeField] private GameObject monsterPrefab;
[SerializeField] private GameObject npcServerPrefab;
[SerializeField] private GameObject testVfxPrefab;
public GameObject PlayerPrefab => playerPrefab;
public GameObject MonsterPrefab => monsterPrefab;
public GameObject NpcServerPrefab => npcServerPrefab.gameObject;
public GameObject TestVfxPrefab => testVfxPrefab;
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 913f99a0553e9ec438c859c044d5c04b
@@ -102,13 +102,6 @@ namespace BrewMonster.Network
return false;
}
m_pGameRun = CECGameRun.Instance;
if (m_pGameRun == null)
{
BMLogger.LogError("CECGame::Init");
return false;
}
// Create GFX caster
if (m_pGFXCaster == null)
{
@@ -118,9 +111,14 @@ namespace BrewMonster.Network
// Initialize string tables
InitializeStringTables();
m_pGameRun = new CECGameRun();
m_pGameRun.Init();
return true;
}
//todo release?
/// <summary>
/// Initialize all string tables with their respective data files
@@ -127,7 +127,7 @@ namespace PerfectWorld.Scripts.Managers
{
info_player_1 info_Player_1 = GPDataTypeHelper.FromBytes<info_player_1>((byte[])Msg.dwParam1);
ElsePlayerEnter(info_Player_1, commandID);
CECGameRun.Instance.Log("ElsePlayer has join");
BMLogger.Log("ElsePlayer has join");
}
break;
}
@@ -46,6 +46,7 @@ namespace BrewMonster.Managers
private void OnDestroy()
{
EC_ManMessage.Dispose();
CECGameRun.Dispose();
}
private void Update()
+8
View File
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 98a0fa350772f4d4dbb54fd605d07d91
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,21 @@
using UnityEngine;
namespace BrewMonster
{
public class ObjectSpawner : MonoSingleton<ObjectSpawner>
{
public GameObject InstantiateObject(GameObject prefab, Transform parent =null, bool setThisAsParent = true)
{
if (setThisAsParent)
{
return Instantiate(prefab, transform);
}
return Instantiate(prefab, parent);
}
public void DestroyGameObject(GameObject go, float delayTime=0)
{
Destroy(go,delayTime);
}
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 93d437f7912a87a4994ed21237f81e5e
@@ -39,6 +39,8 @@ namespace BrewMonster.UI
_usernameInputField.text = PlayerPrefs.GetString("username", "");
_passwordInputField.text = PlayerPrefs.GetString("password", "");
EC_Game.Init();
}
// Update is called once per frame
@@ -152,7 +154,6 @@ namespace BrewMonster.UI
private async void OnEnterWorldComplete()
{
EC_Game.Init();
await Task.Delay(2000);
// Request all known packages: 0=Inventory,1=Equipment,2=Task
UnityGameSession.RequestAllInventoriesAsync(() => { /*BMLogger.Log("Sent Inventory Detail Requests (all packs)");*/ }, 0, 1, 2);
+8
View File
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: aaf2b924949332e48877645b985caa17
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,11 @@
namespace BrewMonster
{
public static class AddressResourceConfig
{
//prefab
public static string PlayerPrefab = "Prefabs/Player/PlayerPrefab";
public static string MonsterPrefab = "Prefabs/Monster/MonsterPrefab";
public static string NpcServerPrefab = "Prefabs/NPC/NPCServer";
public static string TestVfxPrefab = "Prefabs/Vfx/TestVfx";
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: f96a6cffc35ef594fa639136541a9539
@@ -26,7 +26,7 @@ namespace BrewMonster
//GameController.Instance.SelectingVfxPrefab
// todo: make pool
BaseVfxObject pGFX = CECGameRun.Instance.InstantiateObject(loadObj).GetComponent<BaseVfxObject>();
BaseVfxObject pGFX = ObjectSpawner.Instance.InstantiateObject(loadObj, setThisAsParent:true).GetComponent<BaseVfxObject>();
if (!pGFX)
{
return null;
+8
View File
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 152c005a211ff46478903112b64687b9
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
+8
View File
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 5ecd9a3e05e1c5149bc20361d3b78a14
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
+8
View File
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 5c966bf47fcdf2a4a905418477fb4623
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
+2 -2
View File
@@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:9f45d4e79cbf04b71e1e1bf360f444d945bf41a61fe66c90737387c48b61cf6c
size 530349871
oid sha256:cd5bea3232193d14785540c3bc153e23efb8a9edd42745e61b52cd6dd8875bf9
size 530350301
+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()
//{
-1
View File
@@ -12,7 +12,6 @@ namespace BrewMonster.Managers
[SerializeField] private ScreenLogin screenLogin;
[SerializeField] private SelecScreenCharacter screenCharacter;
[SerializeField] private GameObject bgr;
[SerializeField] private CECGameRun gameController;
GameObject screenLoginOb;
GameObject screenCharacterOb;