Checkpoint 2:
- 调整 SimulationWorld 作为 GameFrameworkComponent 组件 - ProcedureGame 接入 SimulationWorld 生命周期(创建/清理)并在敌人 Show/Hide 时同步注册/反注册 - 增加 EnemySimData 构建与缓存去重移除,避免重复注册和悬空映射 - SimulationWorld 增加 UpsertEnemy/UpsertProjectile/UpsertPickup,支持幂等注册
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
//------------------------------------------------------------
|
||||
|
||||
using CustomComponent;
|
||||
using Simulation;
|
||||
using StarForce;
|
||||
using UI;
|
||||
using UnityEngine;
|
||||
@@ -20,12 +21,15 @@ public partial class GameEntry : MonoBehaviour
|
||||
public static HPBarComponent HPBar { get; private set; }
|
||||
|
||||
public static DamageTextComponent DamageText { get; private set; }
|
||||
|
||||
#if UNITY_EDITOR || DEVELOPMENT_BUILD
|
||||
public static RuntimeDebugPanelComponent RuntimeDebugPanel { get; private set; }
|
||||
#endif
|
||||
|
||||
|
||||
public static EnemyManagerComponent EnemyManager { get; private set; }
|
||||
|
||||
|
||||
public static SimulationWorld SimulationWorld { get; private set; }
|
||||
|
||||
public static SpriteCacheComponent SpriteCache { get; private set; }
|
||||
|
||||
public static UIRouterComponent UIRouter { get; private set; }
|
||||
@@ -35,19 +39,14 @@ public partial class GameEntry : MonoBehaviour
|
||||
BuiltinData = UnityGameFramework.Runtime.GameEntry.GetComponent<BuiltinDataComponent>();
|
||||
HPBar = UnityGameFramework.Runtime.GameEntry.GetComponent<HPBarComponent>();
|
||||
DamageText = UnityGameFramework.Runtime.GameEntry.GetComponent<DamageTextComponent>();
|
||||
if (DamageText == null && Base != null)
|
||||
{
|
||||
DamageText = Base.gameObject.AddComponent<DamageTextComponent>();
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR || DEVELOPMENT_BUILD
|
||||
RuntimeDebugPanel = UnityGameFramework.Runtime.GameEntry.GetComponent<RuntimeDebugPanelComponent>();
|
||||
if (RuntimeDebugPanel == null && Base != null)
|
||||
{
|
||||
RuntimeDebugPanel = Base.gameObject.AddComponent<RuntimeDebugPanelComponent>();
|
||||
}
|
||||
#endif
|
||||
|
||||
EnemyManager = UnityGameFramework.Runtime.GameEntry.GetComponent<EnemyManagerComponent>();
|
||||
SimulationWorld = UnityGameFramework.Runtime.GameEntry.GetComponent<SimulationWorld>();
|
||||
SpriteCache = UnityGameFramework.Runtime.GameEntry.GetComponent<SpriteCacheComponent>();
|
||||
UIRouter = UnityGameFramework.Runtime.GameEntry.GetComponent<UIRouterComponent>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ using Entity;
|
||||
using Entity.EntityData;
|
||||
using GameFramework.Event;
|
||||
using Procedure;
|
||||
using Simulation;
|
||||
using StarForce;
|
||||
using UnityEngine;
|
||||
using UnityGameFramework.Runtime;
|
||||
@@ -213,7 +214,17 @@ namespace CustomComponent
|
||||
{
|
||||
_currentEnemyCount++;
|
||||
enemy.SetTarget(_player);
|
||||
RemoveEnemyFromCache(enemy.Id);
|
||||
_enemies.Add(enemy);
|
||||
|
||||
if (ne.UserData is EnemyData enemyData)
|
||||
{
|
||||
GameEntry.SimulationWorld?.UpsertEnemy(CreateEnemySimData(enemy, enemyData));
|
||||
}
|
||||
else
|
||||
{
|
||||
GameEntry.SimulationWorld?.UpsertEnemy(CreateEnemySimData(enemy, null));
|
||||
}
|
||||
}
|
||||
|
||||
if (ne.EntityLogicType == typeof(Player))
|
||||
@@ -228,11 +239,43 @@ namespace CustomComponent
|
||||
{
|
||||
if (ne.EntityGroup.Name == "Enemy")
|
||||
{
|
||||
_currentEnemyCount--;
|
||||
if (_currentEnemyCount > 0)
|
||||
{
|
||||
_currentEnemyCount--;
|
||||
}
|
||||
|
||||
RemoveEnemyFromCache(ne.EntityId);
|
||||
GameEntry.SimulationWorld?.RemoveEnemyByEntityId(ne.EntityId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static EnemySimData CreateEnemySimData(EnemyBase enemy, EnemyData enemyData)
|
||||
{
|
||||
return new EnemySimData
|
||||
{
|
||||
EntityId = enemy.Id,
|
||||
Position = enemy.CachedTransform.position,
|
||||
Forward = enemy.CachedTransform.forward,
|
||||
Speed = enemyData != null ? enemyData.SpeedBase : 0f,
|
||||
AttackRange = 1f,
|
||||
TargetType = 0,
|
||||
State = 0
|
||||
};
|
||||
}
|
||||
|
||||
private void RemoveEnemyFromCache(int entityId)
|
||||
{
|
||||
for (int i = _enemies.Count - 1; i >= 0; i--)
|
||||
{
|
||||
EntityBase cachedEnemy = _enemies[i];
|
||||
if (cachedEnemy == null || cachedEnemy.Id == entityId)
|
||||
{
|
||||
_enemies.RemoveAt(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using CustomEvent;
|
||||
using DataTable;
|
||||
using Definition.Enum;
|
||||
using Entity;
|
||||
@@ -24,11 +23,11 @@ namespace Procedure
|
||||
{
|
||||
public override bool UseNativeDialog => false;
|
||||
|
||||
private HudForm _hudForm = null;
|
||||
private bool _hudInitialized = false;
|
||||
private HudForm _hudForm;
|
||||
private bool _hudInitialized;
|
||||
|
||||
private IFsm<IProcedureManager> _procedureOwner = null;
|
||||
private PlayerData _currentPlayerData = null;
|
||||
private IFsm<IProcedureManager> _procedureOwner;
|
||||
private PlayerData _currentPlayerData;
|
||||
public int CurrentLevel = 1;
|
||||
|
||||
private GameStateType _currentGameState = GameStateType.None;
|
||||
@@ -88,6 +87,7 @@ namespace Procedure
|
||||
base.OnEnter(procedureOwner);
|
||||
|
||||
_procedureOwner = procedureOwner;
|
||||
GameEntry.SimulationWorld?.Clear();
|
||||
|
||||
GameEntry.Event.Subscribe(OpenUIFormSuccessEventArgs.EventId, OpenUIFormSuccess);
|
||||
GameEntry.Event.Subscribe(ShowEntitySuccessEventArgs.EventId, ShowEntitySuccess);
|
||||
@@ -135,6 +135,7 @@ namespace Procedure
|
||||
Player = null;
|
||||
|
||||
_procedureOwner = null;
|
||||
GameEntry.SimulationWorld?.Clear();
|
||||
|
||||
GameEntry.Event.Unsubscribe(OpenUIFormSuccessEventArgs.EventId, OpenUIFormSuccess);
|
||||
GameEntry.Event.Unsubscribe(ShowEntitySuccessEventArgs.EventId, ShowEntitySuccess);
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityGameFramework.Runtime;
|
||||
|
||||
namespace Simulation
|
||||
{
|
||||
public sealed class SimulationWorld
|
||||
public sealed class SimulationWorld : GameFrameworkComponent
|
||||
{
|
||||
private readonly List<EnemySimData> _enemies = new List<EnemySimData>();
|
||||
private readonly List<ProjectileSimData> _projectiles = new List<ProjectileSimData>();
|
||||
@@ -24,6 +25,17 @@ namespace Simulation
|
||||
return simulationIndex;
|
||||
}
|
||||
|
||||
public int UpsertEnemy(in EnemySimData simData)
|
||||
{
|
||||
if (EnemyBinding.TryGetSimulationIndex(simData.EntityId, out int simulationIndex))
|
||||
{
|
||||
_enemies[simulationIndex] = simData;
|
||||
return simulationIndex;
|
||||
}
|
||||
|
||||
return AddEnemy(simData);
|
||||
}
|
||||
|
||||
public bool RemoveEnemyByEntityId(int entityId)
|
||||
{
|
||||
if (!EnemyBinding.TryGetSimulationIndex(entityId, out int simulationIndex))
|
||||
@@ -52,6 +64,17 @@ namespace Simulation
|
||||
return simulationIndex;
|
||||
}
|
||||
|
||||
public int UpsertProjectile(in ProjectileSimData simData)
|
||||
{
|
||||
if (ProjectileBinding.TryGetSimulationIndex(simData.EntityId, out int simulationIndex))
|
||||
{
|
||||
_projectiles[simulationIndex] = simData;
|
||||
return simulationIndex;
|
||||
}
|
||||
|
||||
return AddProjectile(simData);
|
||||
}
|
||||
|
||||
public bool RemoveProjectileByEntityId(int entityId)
|
||||
{
|
||||
if (!ProjectileBinding.TryGetSimulationIndex(entityId, out int simulationIndex))
|
||||
@@ -80,6 +103,17 @@ namespace Simulation
|
||||
return simulationIndex;
|
||||
}
|
||||
|
||||
public int UpsertPickup(in PickupSimData simData)
|
||||
{
|
||||
if (PickupBinding.TryGetSimulationIndex(simData.EntityId, out int simulationIndex))
|
||||
{
|
||||
_pickups[simulationIndex] = simData;
|
||||
return simulationIndex;
|
||||
}
|
||||
|
||||
return AddPickup(simData);
|
||||
}
|
||||
|
||||
public bool RemovePickupByEntityId(int entityId)
|
||||
{
|
||||
if (!PickupBinding.TryGetSimulationIndex(entityId, out int simulationIndex))
|
||||
|
||||
Reference in New Issue
Block a user