Checkpoint 3 & Checkpoint 4:
- GameStateBattle 接入 Simulation 主更新入口 - SimulationWorld 增加开关 UseSimulationMovement(默认 false) - SimulationWorld.Tick(...) 现在在开关开启时执行敌人追踪/移动模拟,基本还原 MovementComponent 功能(敌人互斥) - 调整 IEnemySeparationSolver 系列方法,不再依赖 MovementComponent
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
|
||||
using System.Collections.Generic;
|
||||
using Components;
|
||||
using UnityEngine;
|
||||
|
||||
namespace CustomUtility
|
||||
@@ -9,12 +8,11 @@ namespace CustomUtility
|
||||
{
|
||||
private struct Registration
|
||||
{
|
||||
public Transform Transform;
|
||||
public float BodyRadius;
|
||||
}
|
||||
|
||||
private static IEnemySeparationSolver _current = new GridBucketEnemySeparationSolver();
|
||||
private static readonly Dictionary<MovementComponent, Registration> Registrations = new();
|
||||
private static readonly Dictionary<Transform, Registration> Registrations = new();
|
||||
|
||||
public static IEnemySeparationSolver Current => _current;
|
||||
public static string CurrentSolverName => _current.GetType().Name;
|
||||
@@ -36,42 +34,41 @@ namespace CustomUtility
|
||||
SetSolver(new NaiveEnemySeparationSolver());
|
||||
}
|
||||
|
||||
public static void Register(MovementComponent mover, Transform transform, float bodyRadius)
|
||||
public static void Register(Transform transform, float bodyRadius)
|
||||
{
|
||||
if (mover == null || transform == null) return;
|
||||
if (transform == null) return;
|
||||
|
||||
var registration = new Registration
|
||||
{
|
||||
Transform = transform,
|
||||
BodyRadius = bodyRadius
|
||||
};
|
||||
Registrations[mover] = registration;
|
||||
_current.Register(mover, transform, bodyRadius);
|
||||
Registrations[transform] = registration;
|
||||
_current.Register(transform, bodyRadius);
|
||||
}
|
||||
|
||||
public static void Unregister(MovementComponent mover)
|
||||
public static void Unregister(Transform transform)
|
||||
{
|
||||
if (mover == null) return;
|
||||
if (transform == null) return;
|
||||
|
||||
_current.Unregister(mover);
|
||||
Registrations.Remove(mover);
|
||||
_current.Unregister(transform);
|
||||
Registrations.Remove(transform);
|
||||
}
|
||||
|
||||
public static Vector3 Resolve(MovementComponent mover, Vector3 desiredPosition, Vector3 fallbackDirection,
|
||||
public static Vector3 Resolve(Transform transform, Vector3 desiredPosition, Vector3 fallbackDirection,
|
||||
int iterations)
|
||||
{
|
||||
return _current.Resolve(mover, desiredPosition, fallbackDirection, iterations);
|
||||
return _current.Resolve(transform, desiredPosition, fallbackDirection, iterations);
|
||||
}
|
||||
|
||||
private static void ReRegisterAll()
|
||||
{
|
||||
foreach (var pair in Registrations)
|
||||
{
|
||||
MovementComponent mover = pair.Key;
|
||||
Transform transform = pair.Key;
|
||||
Registration registration = pair.Value;
|
||||
if (mover == null || registration.Transform == null) continue;
|
||||
if (transform == null) continue;
|
||||
|
||||
_current.Register(mover, registration.Transform, registration.BodyRadius);
|
||||
_current.Register(transform, registration.BodyRadius);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
using Components;
|
||||
using UnityEngine;
|
||||
|
||||
namespace CustomUtility
|
||||
@@ -14,12 +13,12 @@ namespace CustomUtility
|
||||
public int CellZ;
|
||||
}
|
||||
|
||||
private readonly System.Collections.Generic.Dictionary<MovementComponent, Agent> _agents = new();
|
||||
private readonly System.Collections.Generic.Dictionary<Transform, Agent> _agents = new();
|
||||
|
||||
private readonly System.Collections.Generic.Dictionary<long, System.Collections.Generic.List<MovementComponent>>
|
||||
private readonly System.Collections.Generic.Dictionary<long, System.Collections.Generic.List<Transform>>
|
||||
_buckets = new();
|
||||
|
||||
private readonly System.Collections.Generic.List<MovementComponent> _recycle = new();
|
||||
private readonly System.Collections.Generic.List<Transform> _recycle = new();
|
||||
private readonly float _cellSize;
|
||||
|
||||
private int _snapshotFrame = -1;
|
||||
@@ -30,14 +29,14 @@ namespace CustomUtility
|
||||
_cellSize = Mathf.Max(0.1f, cellSize);
|
||||
}
|
||||
|
||||
public void Register(MovementComponent mover, Transform transform, float bodyRadius)
|
||||
public void Register(Transform transform, float bodyRadius)
|
||||
{
|
||||
if (mover == null || transform == null) return;
|
||||
if (transform == null) return;
|
||||
|
||||
if (!_agents.TryGetValue(mover, out var agent))
|
||||
if (!_agents.TryGetValue(transform, out var agent))
|
||||
{
|
||||
agent = new Agent();
|
||||
_agents.Add(mover, agent);
|
||||
_agents.Add(transform, agent);
|
||||
}
|
||||
|
||||
agent.Transform = transform;
|
||||
@@ -50,22 +49,22 @@ namespace CustomUtility
|
||||
_snapshotFrame = -1;
|
||||
}
|
||||
|
||||
public void Unregister(MovementComponent mover)
|
||||
public void Unregister(Transform transform)
|
||||
{
|
||||
if (mover == null) return;
|
||||
if (!_agents.TryGetValue(mover, out var agent)) return;
|
||||
if (transform == null) return;
|
||||
if (!_agents.TryGetValue(transform, out var agent)) return;
|
||||
|
||||
RemoveFromBucket(mover, agent.CellX, agent.CellZ);
|
||||
_agents.Remove(mover);
|
||||
RemoveFromBucket(transform, agent.CellX, agent.CellZ);
|
||||
_agents.Remove(transform);
|
||||
RecalculateMaxRadius();
|
||||
_snapshotFrame = -1;
|
||||
}
|
||||
|
||||
public Vector3 Resolve(MovementComponent mover, Vector3 desiredPosition, Vector3 fallbackDirection,
|
||||
public Vector3 Resolve(Transform transform, Vector3 desiredPosition, Vector3 fallbackDirection,
|
||||
int iterations)
|
||||
{
|
||||
if (mover == null) return desiredPosition;
|
||||
if (!_agents.TryGetValue(mover, out var self)) return desiredPosition;
|
||||
if (transform == null) return desiredPosition;
|
||||
if (!_agents.TryGetValue(transform, out var self)) return desiredPosition;
|
||||
|
||||
EnsureSnapshot();
|
||||
|
||||
@@ -90,9 +89,9 @@ namespace CustomUtility
|
||||
|
||||
for (int i = 0; i < bucket.Count; i++)
|
||||
{
|
||||
MovementComponent otherMover = bucket[i];
|
||||
if (otherMover == mover) continue;
|
||||
if (!_agents.TryGetValue(otherMover, out var other)) continue;
|
||||
Transform otherTransform = bucket[i];
|
||||
if (otherTransform == transform) continue;
|
||||
if (!_agents.TryGetValue(otherTransform, out var other)) continue;
|
||||
|
||||
Vector3 toSelf = candidate - other.Position;
|
||||
float minDistance = self.Radius + other.Radius;
|
||||
@@ -115,7 +114,7 @@ namespace CustomUtility
|
||||
}
|
||||
}
|
||||
|
||||
SyncAgentPosition(mover, self, candidate);
|
||||
SyncAgentPosition(transform, self, candidate);
|
||||
|
||||
candidate.y = desiredPosition.y;
|
||||
return candidate;
|
||||
@@ -132,11 +131,11 @@ namespace CustomUtility
|
||||
|
||||
foreach (var pair in _agents)
|
||||
{
|
||||
MovementComponent mover = pair.Key;
|
||||
Transform transform = pair.Key;
|
||||
Agent agent = pair.Value;
|
||||
if (mover == null || agent.Transform == null)
|
||||
if (transform == null || agent.Transform == null)
|
||||
{
|
||||
_recycle.Add(mover);
|
||||
_recycle.Add(transform);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -146,7 +145,7 @@ namespace CustomUtility
|
||||
|
||||
agent.CellX = ToCell(position.x);
|
||||
agent.CellZ = ToCell(position.z);
|
||||
AddToBucket(mover, agent.CellX, agent.CellZ);
|
||||
AddToBucket(transform, agent.CellX, agent.CellZ);
|
||||
}
|
||||
|
||||
for (int i = 0; i < _recycle.Count; i++)
|
||||
@@ -155,15 +154,15 @@ namespace CustomUtility
|
||||
}
|
||||
}
|
||||
|
||||
private void SyncAgentPosition(MovementComponent mover, Agent agent, Vector3 position)
|
||||
private void SyncAgentPosition(Transform transform, Agent agent, Vector3 position)
|
||||
{
|
||||
int newCellX = ToCell(position.x);
|
||||
int newCellZ = ToCell(position.z);
|
||||
|
||||
if (agent.CellX != newCellX || agent.CellZ != newCellZ)
|
||||
{
|
||||
RemoveFromBucket(mover, agent.CellX, agent.CellZ);
|
||||
AddToBucket(mover, newCellX, newCellZ);
|
||||
RemoveFromBucket(transform, agent.CellX, agent.CellZ);
|
||||
AddToBucket(transform, newCellX, newCellZ);
|
||||
agent.CellX = newCellX;
|
||||
agent.CellZ = newCellZ;
|
||||
}
|
||||
@@ -171,24 +170,24 @@ namespace CustomUtility
|
||||
agent.Position = position;
|
||||
}
|
||||
|
||||
private void AddToBucket(MovementComponent mover, int cellX, int cellZ)
|
||||
private void AddToBucket(Transform transform, int cellX, int cellZ)
|
||||
{
|
||||
long key = CellKey(cellX, cellZ);
|
||||
if (!_buckets.TryGetValue(key, out var list))
|
||||
{
|
||||
list = new System.Collections.Generic.List<MovementComponent>(8);
|
||||
list = new System.Collections.Generic.List<Transform>(8);
|
||||
_buckets.Add(key, list);
|
||||
}
|
||||
|
||||
list.Add(mover);
|
||||
list.Add(transform);
|
||||
}
|
||||
|
||||
private void RemoveFromBucket(MovementComponent mover, int cellX, int cellZ)
|
||||
private void RemoveFromBucket(Transform transform, int cellX, int cellZ)
|
||||
{
|
||||
long key = CellKey(cellX, cellZ);
|
||||
if (!_buckets.TryGetValue(key, out var list)) return;
|
||||
|
||||
list.Remove(mover);
|
||||
list.Remove(transform);
|
||||
if (list.Count == 0)
|
||||
{
|
||||
_buckets.Remove(key);
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
using Components;
|
||||
using UnityEngine;
|
||||
|
||||
namespace CustomUtility
|
||||
{
|
||||
public interface IEnemySeparationSolver
|
||||
{
|
||||
void Register(MovementComponent mover, Transform transform, float bodyRadius);
|
||||
void Unregister(MovementComponent mover);
|
||||
Vector3 Resolve(MovementComponent mover, Vector3 desiredPosition, Vector3 fallbackDirection, int iterations);
|
||||
void Register(Transform transform, float bodyRadius);
|
||||
void Unregister(Transform transform);
|
||||
Vector3 Resolve(Transform transform, Vector3 desiredPosition, Vector3 fallbackDirection, int iterations);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
using Components;
|
||||
using UnityEngine;
|
||||
|
||||
namespace CustomUtility
|
||||
@@ -11,33 +10,33 @@ namespace CustomUtility
|
||||
public float Radius;
|
||||
}
|
||||
|
||||
private readonly System.Collections.Generic.Dictionary<MovementComponent, Agent> _agents = new();
|
||||
private readonly System.Collections.Generic.List<MovementComponent> _agentKeys = new();
|
||||
private readonly System.Collections.Generic.Dictionary<Transform, Agent> _agents = new();
|
||||
private readonly System.Collections.Generic.List<Transform> _agentKeys = new();
|
||||
|
||||
public void Register(MovementComponent mover, Transform transform, float bodyRadius)
|
||||
public void Register(Transform transform, float bodyRadius)
|
||||
{
|
||||
if (mover == null || transform == null) return;
|
||||
if (transform == null) return;
|
||||
|
||||
if (!_agents.TryGetValue(mover, out var agent))
|
||||
if (!_agents.TryGetValue(transform, out var agent))
|
||||
{
|
||||
agent = new Agent();
|
||||
_agents.Add(mover, agent);
|
||||
_agents.Add(transform, agent);
|
||||
}
|
||||
|
||||
agent.Transform = transform;
|
||||
agent.Radius = Mathf.Max(0.01f, bodyRadius);
|
||||
}
|
||||
|
||||
public void Unregister(MovementComponent mover)
|
||||
public void Unregister(Transform transform)
|
||||
{
|
||||
if (mover == null) return;
|
||||
_agents.Remove(mover);
|
||||
if (transform == null) return;
|
||||
_agents.Remove(transform);
|
||||
}
|
||||
|
||||
public Vector3 Resolve(MovementComponent mover, Vector3 desiredPosition, Vector3 fallbackDirection, int iterations)
|
||||
public Vector3 Resolve(Transform transform, Vector3 desiredPosition, Vector3 fallbackDirection, int iterations)
|
||||
{
|
||||
if (mover == null) return desiredPosition;
|
||||
if (!_agents.TryGetValue(mover, out var self)) return desiredPosition;
|
||||
if (transform == null) return desiredPosition;
|
||||
if (!_agents.TryGetValue(transform, out var self)) return desiredPosition;
|
||||
|
||||
Vector3 candidate = desiredPosition;
|
||||
candidate.y = 0f;
|
||||
@@ -56,9 +55,9 @@ namespace CustomUtility
|
||||
{
|
||||
for (int i = 0; i < _agentKeys.Count; i++)
|
||||
{
|
||||
MovementComponent otherMover = _agentKeys[i];
|
||||
if (otherMover == mover) continue;
|
||||
if (!_agents.TryGetValue(otherMover, out var other)) continue;
|
||||
Transform otherTransform = _agentKeys[i];
|
||||
if (otherTransform == transform) continue;
|
||||
if (!_agents.TryGetValue(otherTransform, out var other)) continue;
|
||||
if (other.Transform == null) continue;
|
||||
|
||||
Vector3 otherPosition = other.Transform.position;
|
||||
|
||||
Reference in New Issue
Block a user