Better action system
This commit is contained in:
1 parent
2bd71f8dc5
commit
71bb4c3b5b
110 files changed
+73523
-86288
No files matched your search
@@ -1,6 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using Code.Game.OfficeGame.BattleMode;
|
||||
using Code.Infrastructure.Camera;
|
||||
using Code.OfficeGame.BattleMode.Actions;
|
||||
using Code.OfficeGame.BattleMode.Actions.Deciders;
|
||||
using Code.OfficeGame.BattleMode.UI;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using R3;
|
||||
@@ -17,14 +22,31 @@ namespace Code.OfficeGame.BattleMode.Timeline
|
||||
public ReadOnlyReactiveProperty<int> CurrentBattleFrame => _currentBattleFrame;
|
||||
|
||||
private readonly BattleTimelinePanel _timelinePanel;
|
||||
private List<EntityTimelineActor> _entities;
|
||||
private List<EntityProperties> _entities;
|
||||
private DisposableBag _battleSubscriptions;
|
||||
|
||||
private Dictionary<BattleActionType, BattleAction> _battleActions;
|
||||
private Dictionary<DeciderType, BattleActionDecider> _battleActionDeciders;
|
||||
|
||||
public BattleTimeline(BattleTimelinePanel timelinePanel)
|
||||
private Dictionary<EntityId, BattleActionDecisionResult> _actionDecisionResults = new();
|
||||
private Dictionary<EntityId, BattleActionData> _battleActionDatas = new();
|
||||
private BattleActionDecisionPanel _battleActionDecisionPanel;
|
||||
private CameraStateMachine _cameraStateMachine;
|
||||
|
||||
public BattleTimeline(
|
||||
BattleTimelinePanel timelinePanel,
|
||||
IEnumerable<BattleAction> battleActions,
|
||||
IEnumerable<BattleActionDecider> battleActionDeciders,
|
||||
BattleActionDecisionPanel battleActionDecisionPanel,
|
||||
CameraStateMachine cameraStateMachine)
|
||||
{
|
||||
_cameraStateMachine = cameraStateMachine;
|
||||
_battleActionDecisionPanel = battleActionDecisionPanel;
|
||||
_timelinePanel = timelinePanel;
|
||||
_battleActions = battleActions.ToDictionary(x=>x.ActionType);
|
||||
_battleActionDeciders = battleActionDeciders.ToDictionary(x => x.DeciderType);
|
||||
}
|
||||
public async UniTask RunBattleLoopAsync(List<EntityTimelineActor> entities, CancellationToken ctx)
|
||||
public async UniTask RunBattleLoopAsync(List<EntityProperties> entities, CancellationToken ctx)
|
||||
{
|
||||
_entities = entities;
|
||||
_currentBattleFrame.Value = 0;
|
||||
@@ -37,20 +59,20 @@ 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);
|
||||
// foreach (var entity in entities)
|
||||
// entity.OnActionSet
|
||||
// .Subscribe(action =>
|
||||
// {
|
||||
// action.Frame += _currentBattleFrame.Value;
|
||||
// _timelinePanel.SetAction(action);
|
||||
// })
|
||||
// .AddTo(ref _battleSubscriptions);
|
||||
|
||||
await DecideActions(_entities, ctx);
|
||||
|
||||
while (!IsBattleOver() && !ctx.IsCancellationRequested)
|
||||
while (!ctx.IsCancellationRequested)
|
||||
{
|
||||
List<EntityTimelineActor> entitiesNeedOrders = await PlayTimelineUntilPause(ctx);
|
||||
List<EntityProperties> entitiesNeedOrders = await PlayTimelineUntilPause(ctx);
|
||||
|
||||
await DecideActions(entitiesNeedOrders, ctx);
|
||||
}
|
||||
@@ -61,19 +83,32 @@ namespace Code.OfficeGame.BattleMode.Timeline
|
||||
}
|
||||
}
|
||||
|
||||
private async UniTask DecideActions(List<EntityTimelineActor> entityActionMakers, CancellationToken ct)
|
||||
private async UniTask DecideActions(List<EntityProperties> entities, CancellationToken ctx)
|
||||
{
|
||||
foreach (var entity in entityActionMakers)
|
||||
foreach (var entity in entities)
|
||||
{
|
||||
Debug.Log($"Deciding action for {entity.name}");
|
||||
await entity.DecideAction(ct);
|
||||
|
||||
var entityId = entity.GetEntityId();
|
||||
|
||||
_actionDecisionResults.Remove(entityId);
|
||||
|
||||
_actionDecisionResults[entityId] = await DecideAction(ctx, entity);
|
||||
|
||||
_battleActionDatas.Remove(entityId);
|
||||
|
||||
var decidedAction = _actionDecisionResults[entityId];
|
||||
|
||||
_battleActionDatas[entityId] =
|
||||
_battleActions[decidedAction.ActionType]
|
||||
.MountAndGetFrames(entity, decidedAction);
|
||||
}
|
||||
}
|
||||
|
||||
private async UniTask<List<EntityTimelineActor>> PlayTimelineUntilPause(CancellationToken ctx)
|
||||
private async UniTask<List<EntityProperties>> PlayTimelineUntilPause(CancellationToken ctx)
|
||||
{
|
||||
float accumulator = 0f;
|
||||
var readyEntities = new List<EntityTimelineActor>();
|
||||
var readyEntities = new List<EntityProperties>();
|
||||
|
||||
while (readyEntities.Count == 0 && !ctx.IsCancellationRequested)
|
||||
{
|
||||
@@ -93,16 +128,24 @@ namespace Code.OfficeGame.BattleMode.Timeline
|
||||
return readyEntities;
|
||||
}
|
||||
|
||||
private void SimulateSingleFrame(List<EntityTimelineActor> outReadyEntities)
|
||||
private void SimulateSingleFrame(List<EntityProperties> outReadyEntities)
|
||||
{
|
||||
_currentBattleFrame.Value++;
|
||||
|
||||
foreach (var entity in _entities)
|
||||
{
|
||||
entity.CurrentAction.Tick(TickDelta);
|
||||
var entityId = entity.GetEntityId();
|
||||
|
||||
var finished = _battleActions[_actionDecisionResults[entityId].ActionType]
|
||||
.Tick(
|
||||
TickDelta,
|
||||
entity,
|
||||
_actionDecisionResults[entityId],
|
||||
_battleActionDatas[entityId]);
|
||||
|
||||
entity.animator.Update(TickDelta);
|
||||
|
||||
if (entity.Finished)
|
||||
if (finished)
|
||||
{
|
||||
outReadyEntities.Add(entity);
|
||||
}
|
||||
@@ -112,6 +155,55 @@ namespace Code.OfficeGame.BattleMode.Timeline
|
||||
Physics.SyncTransforms();
|
||||
}
|
||||
|
||||
private bool IsBattleOver() => false;
|
||||
private async UniTask<BattleActionDecisionResult> DecideAction(CancellationToken ctx, EntityProperties entity)
|
||||
{
|
||||
_battleActionDecisionPanel.SetEntityInfo(entity.battleActionTypes);
|
||||
|
||||
BattleActionType decidedAction = BattleActionType.None;
|
||||
|
||||
BattleActionDecisionResult decision = null;
|
||||
|
||||
while (decidedAction == BattleActionType.None || ctx.IsCancellationRequested)
|
||||
{
|
||||
Cursor.lockState = CursorLockMode.Confined;
|
||||
|
||||
((CameraBattleModeState)_cameraStateMachine.CurrentBaseState).battleDeciding = true;
|
||||
|
||||
await _battleActionDecisionPanel.Open(ctx);
|
||||
|
||||
decidedAction = await _battleActionDecisionPanel.GetDecision(ctx);
|
||||
|
||||
if (decidedAction == BattleActionType.None) continue;
|
||||
|
||||
var deciderType = _battleActions[decidedAction].DeciderType;
|
||||
|
||||
_battleActionDeciders[deciderType].MountDecider();
|
||||
|
||||
while (!ctx.IsCancellationRequested)
|
||||
{
|
||||
Cursor.lockState = CursorLockMode.Locked;
|
||||
|
||||
((CameraBattleModeState)_cameraStateMachine.CurrentBaseState).battleDeciding = false;
|
||||
|
||||
await _battleActionDecisionPanel.Close(ctx);
|
||||
|
||||
decision = await _battleActionDeciders[deciderType].GetDecision();
|
||||
|
||||
if (decision.ResultCode == BattleActionDecisionResultCode.CancelDecision)
|
||||
decidedAction = BattleActionType.None;
|
||||
|
||||
if (decision.ResultCode != BattleActionDecisionResultCode.Deciding)
|
||||
break;
|
||||
}
|
||||
|
||||
_battleActionDeciders[deciderType].UnmountDecider();
|
||||
}
|
||||
|
||||
if (decision == null) throw new ArgumentNullException(nameof(decision));
|
||||
|
||||
decision.ActionType = decidedAction;
|
||||
|
||||
return decision;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user