Exaction timelinetion
This commit is contained in:
1 parent
921c4125b7
commit
0288afb00a
722 files changed
+144715
-152451
No files matched your search
@@ -23,14 +23,14 @@ namespace Code.OfficeGame.BattleMode.Timeline
|
||||
public ReadOnlyReactiveProperty<int> CurrentBattleFrame => _currentBattleFrame;
|
||||
|
||||
private readonly BattleTimelinePanel _timelinePanel;
|
||||
private List<EntityProperties> _entities;
|
||||
private DisposableBag _battleSubscriptions;
|
||||
|
||||
private Dictionary<BattleActionType, BattleAction> _battleActions;
|
||||
private Dictionary<DeciderType, BattleActionDecider> _battleActionDeciders;
|
||||
|
||||
private Dictionary<EntityId, BattleActionDecisionResult> _actionDecisionResults = new();
|
||||
private Dictionary<EntityId, EntityProperties> _entities;
|
||||
private Dictionary<EntityId, BattleActionData> _battleActionDatas = new();
|
||||
private Dictionary<EntityId, BattleActionDecisionResult> _actionDecisionResults = new();
|
||||
|
||||
private BattleActionDecisionPanel _battleActionDecisionPanel;
|
||||
private CameraStateMachine _cameraStateMachine;
|
||||
@@ -52,13 +52,13 @@ namespace Code.OfficeGame.BattleMode.Timeline
|
||||
battleAction.SetBattleTimeline(this);
|
||||
_battleActions.Add(battleAction.ActionType, battleAction);
|
||||
}
|
||||
|
||||
|
||||
_battleActionDeciders = battleActionDeciders.ToDictionary(x => x.DeciderType);
|
||||
}
|
||||
|
||||
public async UniTask RunBattleLoopAsync(List<EntityProperties> entities, CancellationToken ctx)
|
||||
{
|
||||
_entities = entities;
|
||||
_entities = entities.ToDictionary(x => x.GetEntityId());
|
||||
_currentBattleFrame.Value = 0;
|
||||
|
||||
_battleSubscriptions = new DisposableBag();
|
||||
@@ -69,20 +69,11 @@ namespace Code.OfficeGame.BattleMode.Timeline
|
||||
.Subscribe(_timelinePanel.OnFrameTick)
|
||||
.AddTo(ref _battleSubscriptions);
|
||||
|
||||
// foreach (var entity in entities)
|
||||
// entity.OnActionSet
|
||||
// .Subscribe(action =>
|
||||
// {
|
||||
// action.Frame += _currentBattleFrame.Value;
|
||||
// _timelinePanel.SetAction(action);
|
||||
// })
|
||||
// .AddTo(ref _battleSubscriptions);
|
||||
|
||||
await DecideActions(_entities, ctx);
|
||||
await DecideActions(_entities.Keys.ToArray(), ctx);
|
||||
|
||||
while (!ctx.IsCancellationRequested)
|
||||
{
|
||||
List<EntityProperties> entitiesNeedOrders = await PlayTimelineUntilPause(ctx);
|
||||
EntityId[] entitiesNeedOrders = await PlayTimelineUntilPause(ctx);
|
||||
|
||||
await DecideActions(entitiesNeedOrders, ctx);
|
||||
}
|
||||
@@ -93,12 +84,11 @@ namespace Code.OfficeGame.BattleMode.Timeline
|
||||
}
|
||||
}
|
||||
|
||||
private async UniTask<List<EntityProperties>> PlayTimelineUntilPause(CancellationToken ctx)
|
||||
private async UniTask<EntityId[]> PlayTimelineUntilPause(CancellationToken ctx)
|
||||
{
|
||||
float accumulator = 0f;
|
||||
var readyEntities = new List<EntityProperties>();
|
||||
|
||||
while (readyEntities.Count == 0 && !ctx.IsCancellationRequested)
|
||||
while (!ctx.IsCancellationRequested)
|
||||
{
|
||||
await UniTask.Yield(PlayerLoopTiming.Update, ctx);
|
||||
|
||||
@@ -110,80 +100,86 @@ namespace Code.OfficeGame.BattleMode.Timeline
|
||||
|
||||
_currentBattleFrame.Value++;
|
||||
|
||||
SimulateSingleFrame(readyEntities, _entities);
|
||||
var readyEntities = SimulateSingleFrame(_entities);
|
||||
|
||||
if (readyEntities.Count > 0) return readyEntities;
|
||||
if (readyEntities.Length > 0) return readyEntities;
|
||||
}
|
||||
}
|
||||
|
||||
return readyEntities;
|
||||
return null;
|
||||
}
|
||||
|
||||
public async UniTask PreviewBattleLoopAsync(int frameCount,
|
||||
List<EntityProperties> entities,
|
||||
private async UniTask PreviewBattleLoopAsync(int frameCount,
|
||||
Dictionary<EntityId, EntityProperties> entities,
|
||||
CancellationToken ctx)
|
||||
{
|
||||
while (!ctx.IsCancellationRequested)
|
||||
Dictionary<EntityId, BattleActionData> actionDataSnapshots = new();
|
||||
Dictionary<EntityId, BattleActionDecisionResult> resultsSnapshots = new();
|
||||
Dictionary<EntityId, EntitySnapshot> entitySnapshots = new();
|
||||
|
||||
foreach (var (entityId, entity) in entities)
|
||||
{
|
||||
Dictionary<EntityId, BattleActionData> actionDataSnapshots = new();
|
||||
Dictionary<EntityId, BattleActionDecisionResult> resultsSnapshots = new();
|
||||
Dictionary<EntityId, EntitySnapshot> entitySnapshots = new();
|
||||
if (!_actionDecisionResults.TryGetValue(entityId, out var actionDecisionResult)
|
||||
|| !_battleActionDatas.TryGetValue(entityId, out var battleActionData))
|
||||
continue;
|
||||
|
||||
foreach (var entity in entities)
|
||||
{
|
||||
var entityId = entity.GetEntityId();
|
||||
resultsSnapshots[entityId] = actionDecisionResult.GetSnapshot();
|
||||
actionDataSnapshots[entityId] = battleActionData.GetSnapshot();
|
||||
entitySnapshots[entityId] = entity.GetSnapshot();
|
||||
}
|
||||
|
||||
if (!_actionDecisionResults.TryGetValue(entityId, out var actionDecisionResult)
|
||||
|| !_battleActionDatas.TryGetValue(entityId, out var battleActionData))
|
||||
continue;
|
||||
|
||||
resultsSnapshots[entityId] = actionDecisionResult.GetSnapshot();
|
||||
actionDataSnapshots[entityId] = battleActionData.GetSnapshot();
|
||||
entitySnapshots[entityId] = entity.GetSnapshot();
|
||||
}
|
||||
|
||||
try
|
||||
try
|
||||
{
|
||||
while (!ctx.IsCancellationRequested)
|
||||
{
|
||||
RestoreFromSnapshots(entities, resultsSnapshots, actionDataSnapshots, entitySnapshots);
|
||||
|
||||
await PlayTimelineForCertainFrames(frameCount, ctx).AttachExternalCancellation(ctx);
|
||||
}
|
||||
finally
|
||||
}
|
||||
finally
|
||||
{
|
||||
RestoreFromSnapshots(entities, resultsSnapshots, actionDataSnapshots, entitySnapshots);
|
||||
}
|
||||
}
|
||||
|
||||
private void RestoreFromSnapshots(Dictionary<EntityId, EntityProperties> entities,
|
||||
Dictionary<EntityId, BattleActionDecisionResult> resultsSnapshots,
|
||||
Dictionary<EntityId, BattleActionData> actionDataSnapshots,
|
||||
Dictionary<EntityId, EntitySnapshot> entitySnapshots)
|
||||
{
|
||||
foreach (var (entityId, entity) in entities)
|
||||
{
|
||||
if (!resultsSnapshots.TryGetValue(entityId, out var actionDecisionResult)
|
||||
|| !actionDataSnapshots.TryGetValue(entityId, out var battleActionData))
|
||||
continue;
|
||||
|
||||
bool actionChanged = false;
|
||||
|
||||
if (!_actionDecisionResults.TryAdd(entityId, actionDecisionResult))
|
||||
{
|
||||
foreach (var entity in entities)
|
||||
{
|
||||
var entityId = entity.GetEntityId();
|
||||
actionChanged = actionDecisionResult.ActionType
|
||||
!= _actionDecisionResults[entityId].ActionType;
|
||||
|
||||
if (!resultsSnapshots.TryGetValue(entityId, out var actionDecisionResult)
|
||||
|| !actionDataSnapshots.TryGetValue(entityId, out var battleActionData))
|
||||
continue;
|
||||
|
||||
bool actionChanged = false;
|
||||
|
||||
if (!_actionDecisionResults.TryAdd(entityId, actionDecisionResult))
|
||||
{
|
||||
actionChanged = actionDecisionResult != _actionDecisionResults[entityId];
|
||||
|
||||
if(actionChanged)
|
||||
_actionDecisionResults[entityId] = actionDecisionResult;
|
||||
else
|
||||
_actionDecisionResults[entityId]?.RestoreSnapshot(actionDecisionResult);
|
||||
}
|
||||
|
||||
if (!_battleActionDatas.TryAdd(entityId, battleActionData))
|
||||
{
|
||||
if(actionChanged) _battleActionDatas[entityId] = battleActionData;
|
||||
else _battleActionDatas[entityId]?.RestoreSnapshot(battleActionData);
|
||||
}
|
||||
|
||||
entity.RestoreSnapshot(entitySnapshots[entityId]);
|
||||
}
|
||||
if (actionChanged)
|
||||
_actionDecisionResults[entityId] = actionDecisionResult;
|
||||
else
|
||||
_actionDecisionResults[entityId]?.RestoreSnapshot(actionDecisionResult);
|
||||
}
|
||||
|
||||
if (!_battleActionDatas.TryAdd(entityId, battleActionData))
|
||||
{
|
||||
if (actionChanged) _battleActionDatas[entityId] = battleActionData;
|
||||
else _battleActionDatas[entityId]?.RestoreSnapshot(battleActionData);
|
||||
}
|
||||
|
||||
entity.RestoreSnapshot(entitySnapshots[entityId]);
|
||||
}
|
||||
}
|
||||
|
||||
private async UniTask PlayTimelineForCertainFrames(int frameCount, CancellationToken ctx)
|
||||
{
|
||||
float accumulator = 0f;
|
||||
var readyEntities = new List<EntityProperties>();
|
||||
|
||||
while (frameCount > 0 && !ctx.IsCancellationRequested)
|
||||
{
|
||||
@@ -197,14 +193,12 @@ namespace Code.OfficeGame.BattleMode.Timeline
|
||||
|
||||
frameCount--;
|
||||
|
||||
SimulateSingleFrame(readyEntities, _entities);
|
||||
var readyEntities = SimulateSingleFrame(_entities);
|
||||
|
||||
if (readyEntities.Count > 0)
|
||||
if (readyEntities.Length > 0)
|
||||
{
|
||||
foreach (var entity in readyEntities)
|
||||
foreach (var entityID in readyEntities)
|
||||
{
|
||||
var entityID = entity.GetEntityId();
|
||||
|
||||
_battleActionDatas.Remove(entityID);
|
||||
|
||||
_actionDecisionResults.Remove(entityID);
|
||||
@@ -214,44 +208,42 @@ namespace Code.OfficeGame.BattleMode.Timeline
|
||||
}
|
||||
}
|
||||
|
||||
private void SimulateSingleFrame(List<EntityProperties> outReadyEntities, List<EntityProperties> inEntities)
|
||||
private EntityId[] SimulateSingleFrame(Dictionary<EntityId, EntityProperties> inEntities)
|
||||
{
|
||||
foreach (var entity in inEntities)
|
||||
{
|
||||
var entityId = entity.GetEntityId();
|
||||
var entityId = entity.Key;
|
||||
|
||||
if (!_actionDecisionResults.TryGetValue(entityId, out var actionDecisionResult)
|
||||
|| !_battleActionDatas.TryGetValue(entityId, out var battleActionData))
|
||||
continue;
|
||||
|
||||
var finished = _battleActions[actionDecisionResult.ActionType]
|
||||
_battleActions[actionDecisionResult.ActionType]
|
||||
.Tick(
|
||||
TickDelta,
|
||||
entity,
|
||||
entity.Value,
|
||||
actionDecisionResult,
|
||||
battleActionData);
|
||||
|
||||
entity.animator.Update(TickDelta);
|
||||
|
||||
if (finished)
|
||||
{
|
||||
outReadyEntities.Add(entity);
|
||||
}
|
||||
entity.Value.animator.Update(TickDelta);
|
||||
}
|
||||
|
||||
Physics.Simulate(TickDelta);
|
||||
Physics.SyncTransforms();
|
||||
|
||||
var readyEntities = _battleActionDatas
|
||||
.Where(x => x.Value.Finished)
|
||||
.Select(x => x.Key)
|
||||
.ToArray();
|
||||
|
||||
return readyEntities;
|
||||
}
|
||||
|
||||
private async UniTask DecideActions(List<EntityProperties> entities, CancellationToken ctx)
|
||||
private async UniTask DecideActions(EntityId[] entities, CancellationToken ctx)
|
||||
{
|
||||
foreach (var entity in entities)
|
||||
foreach (var entityId in entities)
|
||||
{
|
||||
Debug.Log($"Deciding action for {entity.name}");
|
||||
|
||||
var entityId = entity.GetEntityId();
|
||||
|
||||
var (decisionResult, battleActionData) = await DecideAction(ctx, entity);
|
||||
var (decisionResult, battleActionData) = await DecideAction(ctx, _entities[entityId]);
|
||||
|
||||
_actionDecisionResults[entityId] = decisionResult;
|
||||
_battleActionDatas[entityId] = battleActionData;
|
||||
@@ -278,7 +270,10 @@ namespace Code.OfficeGame.BattleMode.Timeline
|
||||
entityActionDataSnapshot = battleActionData.GetSnapshot();
|
||||
|
||||
if (_actionDecisionResults.TryGetValue(entityId, out var actionDecisionResult))
|
||||
{
|
||||
entityResultSnapshot = actionDecisionResult.GetSnapshot();
|
||||
_timelinePanel.SetPreviousActionText(entityResultSnapshot.ActionType.ToString());
|
||||
}
|
||||
|
||||
while (decidedActionType == BattleActionType.None || ctx.IsCancellationRequested)
|
||||
{
|
||||
@@ -294,7 +289,7 @@ namespace Code.OfficeGame.BattleMode.Timeline
|
||||
|
||||
var deciderType = _battleActions[decidedActionType].DeciderType;
|
||||
|
||||
_battleActionDeciders[deciderType].MountDecider();
|
||||
_battleActionDeciders[deciderType].MountDecider(entity);
|
||||
|
||||
CancellationTokenSource previewCancellationToken = new();
|
||||
|
||||
@@ -330,7 +325,6 @@ namespace Code.OfficeGame.BattleMode.Timeline
|
||||
break;
|
||||
}
|
||||
else break;
|
||||
|
||||
}
|
||||
|
||||
_battleActionDeciders[deciderType].UnmountDecider();
|
||||
@@ -351,13 +345,12 @@ namespace Code.OfficeGame.BattleMode.Timeline
|
||||
|
||||
public void ForceDecideAction(
|
||||
EntityId entityId,
|
||||
BattleActionData forcedBattleActionData,
|
||||
BattleActionData forcedBattleActionData,
|
||||
BattleActionDecisionResult forcedBattleActionDataResult)
|
||||
{
|
||||
|
||||
_battleActionDatas[entityId] = forcedBattleActionData;
|
||||
_actionDecisionResults[entityId] = forcedBattleActionDataResult;
|
||||
|
||||
|
||||
Debug.Log($"Задано действие {forcedBattleActionDataResult.ActionType} для {entityId}");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user