迁移 Player/ProcedureGame/ShopUseCase 里的实体方法调用风格

- Player/ShopUseCase:调整了 Weapon 实体的打开方式
- ProcedureGame:调整初始化时的 Hud 和 Player 的初始化写法,流程更清楚
This commit is contained in:
SepComet
2026-06-16 17:09:19 +08:00
committed by basil
parent a721670b36
commit 0bab6ecbc5
8 changed files with 107 additions and 118 deletions
@@ -1,13 +1,13 @@
using System;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using SepCore.DataTable;
using SepCore.Definition;
using SepCore.Entity;
using GameFramework.Event;
using GameFramework.Fsm;
using GameFramework.Procedure;
using UnityGameFramework.Runtime;
using SepCore.UI;
using SepCore.AsyncTask;
namespace SepCore.Procedure
{
@@ -23,10 +23,6 @@ namespace SepCore.Procedure
{
public override bool UseNativeDialog => false;
private HudForm _hudForm;
private bool _hudInitialized;
private IFsm<IProcedureManager> _procedureOwner;
private PlayerData _currentPlayerData;
public int CurrentLevel = 1;
@@ -50,7 +46,7 @@ namespace SepCore.Procedure
_gameStates[GameStateType.Shop].OnInit(this);
_currentGameState = GameStateType.Battle;
_gameStates[_currentGameState].OnEnter(_procedureOwner);
_gameStates[_currentGameState].OnEnter();
}
public void BattleToShopOrLevelUp()
@@ -58,27 +54,27 @@ namespace SepCore.Procedure
CurrentLevel++;
if (_currentGameState == GameStateType.Shop || _currentGameState == GameStateType.LevelUp) return;
_gameStates[_currentGameState].OnLeave(_procedureOwner);
_gameStates[_currentGameState].OnLeave();
_currentGameState = Player.PendingLevelPoints > 0 ? GameStateType.LevelUp : GameStateType.Shop;
_gameStates[_currentGameState].OnEnter(_procedureOwner);
_gameStates[_currentGameState].OnEnter();
}
public void ShopToBattle()
{
if (_currentGameState == GameStateType.Battle) return;
_gameStates[_currentGameState].OnLeave(_procedureOwner);
_gameStates[_currentGameState].OnLeave();
_currentGameState = GameStateType.Battle;
_gameStates[_currentGameState].OnEnter(_procedureOwner);
_gameStates[_currentGameState].OnEnter();
}
public void LevelUpToShop()
{
if (_currentGameState == GameStateType.Shop) return;
_gameStates[_currentGameState].OnLeave(_procedureOwner);
_gameStates[_currentGameState].OnLeave();
_currentGameState = GameStateType.Shop;
_gameStates[_currentGameState].OnEnter(_procedureOwner);
_gameStates[_currentGameState].OnEnter();
}
#region FSM
@@ -87,87 +83,65 @@ namespace SepCore.Procedure
{
base.OnEnter(procedureOwner);
_procedureOwner = procedureOwner;
GameEntry.SimulationWorld?.ClearSimulationState();
GameEntry.Event.Subscribe(OpenUIFormSuccessEventArgs.EventId, OpenUIFormSuccess);
GameEntry.Event.Subscribe(ShowEntitySuccessEventArgs.EventId, ShowEntitySuccess);
CurrentLevel = 1;
_currentPlayerData = new PlayerData(-1, 1001);
GameEntry.Entity.ShowPlayer(_currentPlayerData);
GameEntry.UIRouter.OpenUIAsync(UIFormType.HudForm).Forget();
EnterAsync(procedureOwner).Forget();
}
protected override void OnUpdate(IFsm<IProcedureManager> procedureOwner, float elapseSeconds,
float realElapseSeconds)
{
base.OnUpdate(procedureOwner, elapseSeconds, realElapseSeconds);
_procedureOwner = procedureOwner;
if (!_hudInitialized && _hudForm != null && Player != null)
{
int selectedRoleId = _procedureOwner.GetData<VarInt32>("SelectedRoleId").Value;
var role = GameEntry.DataTable.GetDataTableRow<DRRole>(selectedRoleId);
Player.InitRole(role);
_hudInitialized = true;
InitGameState();
}
if (_hudInitialized)
_gameStates[_currentGameState].OnUpdate(procedureOwner, elapseSeconds, realElapseSeconds);
_gameStates?[_currentGameState].OnUpdate(elapseSeconds, realElapseSeconds);
}
protected override void OnLeave(IFsm<IProcedureManager> procedureOwner, bool isShutdown)
{
foreach (var state in _gameStates.Values)
if (_gameStates != null)
{
state.OnDestroy(procedureOwner);
}
foreach (var state in _gameStates.Values)
{
state.OnDestroy();
}
_gameStates.Clear();
_gameStates.Clear();
}
GameEntry.UIRouter.CloseUIAsync(UIFormType.HudForm).Forget();
_hudForm = null;
Player = null;
_procedureOwner = null;
GameEntry.SimulationWorld?.ClearSimulationState();
GameEntry.Event.Unsubscribe(OpenUIFormSuccessEventArgs.EventId, OpenUIFormSuccess);
GameEntry.Event.Unsubscribe(ShowEntitySuccessEventArgs.EventId, ShowEntitySuccess);
base.OnLeave(procedureOwner, isShutdown);
}
#endregion
#region Event Handler
private void OpenUIFormSuccess(object sender, GameEventArgs e)
private async UniTaskVoid EnterAsync(IFsm<IProcedureManager> procedureOwner)
{
if (!(e is OpenUIFormSuccessEventArgs args)) return;
if (args.UIForm.Logic is HudForm hudForm)
try
{
_hudForm = hudForm;
Player = await GameEntry.Entity.ShowPlayerAsync(_currentPlayerData);
await GameEntry.UIRouter.OpenUIAsync(UIFormType.HudForm);
int selectedRoleId = procedureOwner.GetData<VarInt32>("SelectedRoleId").Value;
var role = GameEntry.DataTable.GetDataTableRow<DRRole>(selectedRoleId);
if (role == null)
{
Log.Error("ProcedureGame.EnterAsync() can not load role '{0}'.", selectedRoleId.ToString());
return;
}
Player.InitRole(role);
InitGameState();
}
catch (Exception exception)
{
Log.Error("ProcedureGame.EnterAsync() failed with exception '{0}'.", exception);
}
}
private void ShowEntitySuccess(object sender, GameEventArgs e)
{
if (!(e is ShowEntitySuccessEventArgs args)) return;
if (args.Entity.Logic is Player player)
{
Player = player;
}
}
#endregion
}
}