Cleanup 1
This commit is contained in:
@@ -36,11 +36,6 @@ namespace Simulation
|
||||
in Vector3 center, float radius, int maxTargets, int shapeType, in Vector3 direction, float halfAngleDeg,
|
||||
float halfWidth, float halfLength)
|
||||
{
|
||||
if (!_useSimulationMovement)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (sourceEntityId == 0 || radius <= 0f || maxTargets <= 0)
|
||||
{
|
||||
return false;
|
||||
|
||||
@@ -16,7 +16,7 @@ namespace Simulation
|
||||
|
||||
public void OnLateUpdate()
|
||||
{
|
||||
if (_world == null || !_world.UseSimulationMovement)
|
||||
if (_world == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@@ -104,4 +104,4 @@ namespace Simulation
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Simulation
|
||||
{
|
||||
public sealed partial class SimulationWorld
|
||||
{
|
||||
private Transform _playerMovementTransform;
|
||||
private Vector3 _playerMovementPosition;
|
||||
private Vector3 _playerMovementDirection = Vector3.zero;
|
||||
private float _playerMovementSpeed;
|
||||
private bool _playerMovementActive;
|
||||
|
||||
public void SyncPlayerMovementInput(Transform playerTransform, bool isMoving, in Vector3 direction, float speed)
|
||||
{
|
||||
if (playerTransform == null)
|
||||
{
|
||||
ClearPlayerMovementState();
|
||||
return;
|
||||
}
|
||||
|
||||
if (_playerMovementTransform != playerTransform)
|
||||
{
|
||||
_playerMovementTransform = playerTransform;
|
||||
_playerMovementPosition = playerTransform.position;
|
||||
}
|
||||
|
||||
Vector3 planarDirection = direction;
|
||||
planarDirection.y = 0f;
|
||||
bool hasDirection = planarDirection.sqrMagnitude > Mathf.Epsilon;
|
||||
if (hasDirection)
|
||||
{
|
||||
_playerMovementDirection = planarDirection.normalized;
|
||||
}
|
||||
else
|
||||
{
|
||||
_playerMovementDirection = Vector3.zero;
|
||||
}
|
||||
|
||||
_playerMovementActive = isMoving && hasDirection;
|
||||
_playerMovementSpeed = _playerMovementActive ? Mathf.Max(0f, speed) : 0f;
|
||||
}
|
||||
|
||||
public void UnregisterPlayerMovement(Transform playerTransform)
|
||||
{
|
||||
if (playerTransform == null || _playerMovementTransform != playerTransform)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ClearPlayerMovementState();
|
||||
}
|
||||
|
||||
private Vector3 ResolvePlayerPositionForTick(in SimulationTickContext context)
|
||||
{
|
||||
if (_playerMovementTransform == null)
|
||||
{
|
||||
return context.PlayerPosition;
|
||||
}
|
||||
|
||||
if (!_playerMovementActive || _playerMovementSpeed <= 0f ||
|
||||
_playerMovementDirection.sqrMagnitude <= Mathf.Epsilon || context.DeltaTime <= 0f)
|
||||
{
|
||||
_playerMovementPosition = _playerMovementTransform.position;
|
||||
return _playerMovementPosition;
|
||||
}
|
||||
|
||||
_playerMovementPosition += _playerMovementDirection * (_playerMovementSpeed * context.DeltaTime);
|
||||
_playerMovementPosition.y = _playerMovementTransform.position.y;
|
||||
_playerMovementTransform.position = _playerMovementPosition;
|
||||
return _playerMovementPosition;
|
||||
}
|
||||
|
||||
private void ClearPlayerMovementState()
|
||||
{
|
||||
_playerMovementTransform = null;
|
||||
_playerMovementPosition = Vector3.zero;
|
||||
_playerMovementDirection = Vector3.zero;
|
||||
_playerMovementSpeed = 0f;
|
||||
_playerMovementActive = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 417eac38d86047d096eb9f74647b6cf7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -22,6 +22,7 @@ namespace Simulation
|
||||
EnemyBinding.Clear();
|
||||
ProjectileBinding.Clear();
|
||||
PickupBinding.Clear();
|
||||
ClearPlayerMovementState();
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -101,6 +102,31 @@ namespace Simulation
|
||||
return true;
|
||||
}
|
||||
|
||||
public void SyncEnemyMovementInput(int entityId, bool isMoving, in UnityEngine.Vector3 direction, float speed,
|
||||
bool avoidEnemyOverlap, float enemyBodyRadius, int separationIterations)
|
||||
{
|
||||
if (!EnemyBinding.TryGetSimulationIndex(entityId, out int simulationIndex) || simulationIndex < 0 ||
|
||||
simulationIndex >= _enemies.Count)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
EnemySimData enemyData = _enemies[simulationIndex];
|
||||
enemyData.Speed = isMoving ? UnityEngine.Mathf.Max(0f, speed) : 0f;
|
||||
enemyData.AvoidEnemyOverlap = avoidEnemyOverlap;
|
||||
enemyData.EnemyBodyRadius = UnityEngine.Mathf.Max(0.01f, enemyBodyRadius);
|
||||
enemyData.SeparationIterations = UnityEngine.Mathf.Max(1, separationIterations);
|
||||
|
||||
UnityEngine.Vector3 planarDirection = direction;
|
||||
planarDirection.y = 0f;
|
||||
if (planarDirection.sqrMagnitude > UnityEngine.Mathf.Epsilon)
|
||||
{
|
||||
enemyData.Forward = planarDirection.normalized;
|
||||
}
|
||||
|
||||
_enemies[simulationIndex] = enemyData;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Projectile Simulation State
|
||||
@@ -219,4 +245,4 @@ namespace Simulation
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,11 +14,6 @@ namespace Simulation
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!_useSimulationMovement)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
BuildEnemyTargetSpatialIndexIfNeeded();
|
||||
|
||||
float cellSize = GetTargetSelectionCellSize();
|
||||
|
||||
@@ -37,9 +37,6 @@ namespace Simulation
|
||||
private const int ProjectileStateActive = 0;
|
||||
private const int ProjectileStateExpired = 1;
|
||||
|
||||
[Header("模拟世界全局设置")] [Tooltip("是否启用世界模拟")] [SerializeField]
|
||||
private bool _useSimulationMovement = true;
|
||||
|
||||
private EntitySync _entitySync;
|
||||
private TransformSync _transformSync;
|
||||
private HitPresentation _hitPresentation;
|
||||
@@ -47,7 +44,7 @@ namespace Simulation
|
||||
public IReadOnlyList<EnemySimData> Enemies => _enemies;
|
||||
public IReadOnlyList<ProjectileSimData> Projectiles => _projectiles;
|
||||
public IReadOnlyList<PickupSimData> Pickups => _pickups;
|
||||
public bool UseSimulationMovement => _useSimulationMovement;
|
||||
public bool UseSimulationMovement => true;
|
||||
|
||||
#region Lifecycle
|
||||
|
||||
@@ -68,14 +65,12 @@ namespace Simulation
|
||||
|
||||
public void Tick(in SimulationTickContext context)
|
||||
{
|
||||
if (!_useSimulationMovement)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Vector3 playerPosition = ResolvePlayerPositionForTick(in context);
|
||||
SimulationTickContext resolvedContext =
|
||||
new SimulationTickContext(context.DeltaTime, context.RealDeltaTime, playerPosition);
|
||||
using (CustomProfilerMarker.TickEnemies.Auto())
|
||||
{
|
||||
TickSimulationPipeline(in context);
|
||||
TickSimulationPipeline(in resolvedContext);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user