Something fixed
This commit is contained in:
1 parent
f40a10912c
commit
7d236d14cd
72 files changed
+270482
-11409
No files matched your search
@@ -7,6 +7,7 @@ using Code.Game.OfficeGame.BattleMode;
|
||||
using Code.Infrastructure.Camera;
|
||||
using Code.OfficeGame.BattleMode.Actions;
|
||||
using Code.OfficeGame.BattleMode.Actions.Deciders;
|
||||
using Code.OfficeGame.BattleMode.Snapshots;
|
||||
using Code.OfficeGame.BattleMode.UI;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using R3;
|
||||
@@ -32,9 +33,13 @@ namespace Code.OfficeGame.BattleMode.Timeline
|
||||
private Dictionary<EntityId, BattleActionData> _battleActionDatas = new();
|
||||
private Dictionary<EntityId, BattleActionDecisionResult> _actionDecisionResults = new();
|
||||
|
||||
private SnapshotUtilityNonStatic _snapshotUtility = new();
|
||||
|
||||
private BattleActionDecisionPanel _battleActionDecisionPanel;
|
||||
private CameraStateMachine _cameraStateMachine;
|
||||
|
||||
private SemaphoreSlim _preview = new(1,1);
|
||||
|
||||
public BattleTimeline(
|
||||
BattleTimelinePanel timelinePanel,
|
||||
IEnumerable<BattleAction> battleActions,
|
||||
@@ -113,97 +118,63 @@ namespace Code.OfficeGame.BattleMode.Timeline
|
||||
private async UniTask PreviewBattleLoopAsync(int frameCount,
|
||||
Dictionary<EntityId, EntityProperties> entities,
|
||||
CancellationToken ctx)
|
||||
{
|
||||
Dictionary<EntityId, BattleActionData> actionDataSnapshots = new();
|
||||
Dictionary<EntityId, BattleActionDecisionResult> resultsSnapshots = new();
|
||||
Dictionary<EntityId, EntitySnapshot> entitySnapshots = new();
|
||||
|
||||
foreach (var (entityId, entity) in entities)
|
||||
{
|
||||
if (frameCount <= 0)
|
||||
{
|
||||
|
||||
if (!_actionDecisionResults.TryGetValue(entityId, out var actionDecisionResult)
|
||||
|| !_battleActionDatas.TryGetValue(entityId, out var battleActionData))
|
||||
{
|
||||
entity.EntityPreviewVisuals.ShowPreviewState(spawnStaticSnapshotGhost: false);
|
||||
continue;
|
||||
}
|
||||
entity.EntityPreviewVisuals.ShowPreviewState(spawnStaticSnapshotGhost: true);
|
||||
|
||||
resultsSnapshots[entityId] = actionDecisionResult.GetSnapshot();
|
||||
actionDataSnapshots[entityId] = battleActionData.GetSnapshot();
|
||||
entitySnapshots[entityId] = entity.GetSnapshot();
|
||||
_preview.Release(1);
|
||||
throw new Exception("FrameCount must be > 0");
|
||||
}
|
||||
|
||||
_snapshotUtility.GetSnapshots(
|
||||
entities,
|
||||
_actionDecisionResults,
|
||||
_battleActionDatas);
|
||||
|
||||
try
|
||||
{
|
||||
while (!ctx.IsCancellationRequested)
|
||||
{
|
||||
RestoreFromSnapshots(entities, resultsSnapshots, actionDataSnapshots, entitySnapshots);
|
||||
|
||||
_snapshotUtility.RestoreFromSnapshots(
|
||||
entities,
|
||||
_actionDecisionResults,
|
||||
_battleActionDatas);
|
||||
|
||||
await PlayTimelineForCertainFrames(frameCount, ctx).AttachExternalCancellation(ctx);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
RestoreFromSnapshots(entities, resultsSnapshots, actionDataSnapshots, entitySnapshots);
|
||||
|
||||
_snapshotUtility.RestoreFromSnapshots(
|
||||
entities,
|
||||
_actionDecisionResults,
|
||||
_battleActionDatas);
|
||||
|
||||
foreach (var (_, entity) in entities)
|
||||
{
|
||||
entity.EntityPreviewVisuals.HidePreviewState();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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))
|
||||
{
|
||||
actionChanged = actionDecisionResult.ActionType
|
||||
!= _actionDecisionResults[entityId].ActionType;
|
||||
|
||||
if (actionChanged)
|
||||
_actionDecisionResults[entityId] = actionDecisionResult.GetSnapshot();
|
||||
else
|
||||
_actionDecisionResults[entityId]?.RestoreSnapshot(actionDecisionResult);
|
||||
}
|
||||
|
||||
if (!_battleActionDatas.TryAdd(entityId, battleActionData))
|
||||
{
|
||||
if (actionChanged)
|
||||
_battleActionDatas[entityId] = battleActionData.GetSnapshot();
|
||||
else _battleActionDatas[entityId]?.RestoreSnapshot(battleActionData);
|
||||
}
|
||||
|
||||
entity.RestoreSnapshot(entitySnapshots[entityId]);
|
||||
_preview.Release(1);
|
||||
}
|
||||
}
|
||||
|
||||
private async UniTask PlayTimelineForCertainFrames(int frameCount, CancellationToken ctx)
|
||||
{
|
||||
float accumulator = 0f;
|
||||
int counter = frameCount;
|
||||
|
||||
while (frameCount > 0 && !ctx.IsCancellationRequested)
|
||||
while (counter > 0 && !ctx.IsCancellationRequested)
|
||||
{
|
||||
bool isCanceled = await UniTask.Yield(PlayerLoopTiming.Update, ctx).SuppressCancellationThrow();
|
||||
if (isCanceled) break;
|
||||
|
||||
accumulator += Mathf.Min(Time.unscaledDeltaTime, 0.1f);
|
||||
|
||||
while (accumulator >= TickDelta && frameCount > 0 && !ctx.IsCancellationRequested)
|
||||
while (accumulator >= TickDelta && counter > 0 && !ctx.IsCancellationRequested)
|
||||
{
|
||||
// Debug.Log($"Simulating {counter}/{frameCount}");
|
||||
accumulator -= TickDelta;
|
||||
frameCount--;
|
||||
counter--;
|
||||
|
||||
var readyEntities = SimulateSingleFrame(_entities);
|
||||
|
||||
@@ -211,21 +182,22 @@ namespace Code.OfficeGame.BattleMode.Timeline
|
||||
{
|
||||
foreach (var entityId in readyEntities)
|
||||
{
|
||||
// _battleActionDatas.Remove(entityID);
|
||||
// _actionDecisionResults.Remove(entityID);
|
||||
Debug.Log($"Entity {entityId} is ready");
|
||||
_battleActionDatas.Remove(entityId);
|
||||
_actionDecisionResults.Remove(entityId);
|
||||
|
||||
_battleActionDatas[entityId] = new WaitBattleActionData {
|
||||
TotalFrames = frameCount,
|
||||
WaitFrames = frameCount
|
||||
};
|
||||
_actionDecisionResults[entityId] = new FrameBattleActionDeciderResult
|
||||
{
|
||||
ActionType = BattleActionType.Wait,
|
||||
FrameCount = frameCount,
|
||||
ResultCode = BattleActionDecisionResultCode.Decided
|
||||
};
|
||||
// _battleActionDatas[entityId] = new WaitBattleActionData {
|
||||
// TotalFrames = frameCount,
|
||||
// WaitFrames = frameCount
|
||||
// };
|
||||
// _actionDecisionResults[entityId] = new FrameBattleActionDeciderResult
|
||||
// {
|
||||
// ActionType = BattleActionType.Wait,
|
||||
// FrameCount = frameCount,
|
||||
// ResultCode = BattleActionDecisionResultCode.Decided
|
||||
// };
|
||||
}
|
||||
// return;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -233,10 +205,8 @@ namespace Code.OfficeGame.BattleMode.Timeline
|
||||
|
||||
private EntityId[] SimulateSingleFrame(Dictionary<EntityId, EntityProperties> inEntities)
|
||||
{
|
||||
foreach (var entity in inEntities)
|
||||
foreach (var (entityId, entity) in inEntities)
|
||||
{
|
||||
var entityId = entity.Key;
|
||||
|
||||
if (!_actionDecisionResults.TryGetValue(entityId, out var actionDecisionResult)
|
||||
|| !_battleActionDatas.TryGetValue(entityId, out var battleActionData))
|
||||
continue;
|
||||
@@ -244,11 +214,11 @@ namespace Code.OfficeGame.BattleMode.Timeline
|
||||
_battleActions[actionDecisionResult.ActionType]
|
||||
.Tick(
|
||||
TickDelta,
|
||||
entity.Value,
|
||||
entity,
|
||||
actionDecisionResult,
|
||||
battleActionData);
|
||||
|
||||
entity.Value.animator.Update(TickDelta);
|
||||
entity.animator.Update(TickDelta);
|
||||
}
|
||||
|
||||
Physics.Simulate(TickDelta);
|
||||
@@ -327,11 +297,15 @@ namespace Code.OfficeGame.BattleMode.Timeline
|
||||
decisionResult = await _battleActionDeciders[deciderType].GetDecision();
|
||||
decisionResult.ActionType = decidedActionType;
|
||||
|
||||
actionData = _battleActions[decidedActionType].MountAndGetFrames(entity, decisionResult);
|
||||
|
||||
previewCancellationToken.Cancel();
|
||||
previewCancellationToken.Dispose();
|
||||
|
||||
await _preview.WaitAsync(ctx);
|
||||
|
||||
_preview.Release();
|
||||
|
||||
actionData = _battleActions[decidedActionType].MountAndGetFrames(entity, decisionResult);
|
||||
|
||||
if (decisionResult.ResultCode == BattleActionDecisionResultCode.Deciding)
|
||||
{
|
||||
_actionDecisionResults[entityId] = decisionResult;
|
||||
@@ -339,6 +313,8 @@ namespace Code.OfficeGame.BattleMode.Timeline
|
||||
|
||||
previewCancellationToken = new();
|
||||
|
||||
await _preview.WaitAsync(previewCancellationToken.Token);
|
||||
|
||||
PreviewBattleLoopAsync(actionData.TotalFrames, _entities, previewCancellationToken.Token)
|
||||
.Forget();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user