105 lines
3.5 KiB
C#
105 lines
3.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Code.OfficeGame.BattleMode.Actions;
|
|
|
|
namespace Code.OfficeGame.BattleMode.Timeline
|
|
{
|
|
public sealed class BattleTimelineTrack
|
|
{
|
|
private readonly List<BattleFrameSnapshot> _frames = new(512);
|
|
private readonly List<TimelineEvent> _allEvents = new(64);
|
|
|
|
public int FrameCount => _frames.Count;
|
|
public int LastFrame => _frames.Count > 0 ? _frames.Count - 1 : 0;
|
|
public IReadOnlyList<TimelineEvent> AllEvents => _allEvents;
|
|
|
|
public void Initialize(BattleFrameSnapshot frameZero)
|
|
{
|
|
_frames.Clear();
|
|
_allEvents.Clear();
|
|
AppendFrame(frameZero);
|
|
}
|
|
|
|
public BattleFrameSnapshot GetFrame(int frame)
|
|
{
|
|
if (_frames.Count == 0)
|
|
throw new InvalidOperationException("Timeline track is empty.");
|
|
|
|
int clamped = Math.Clamp(frame, 0, _frames.Count - 1);
|
|
return _frames[clamped];
|
|
}
|
|
|
|
public void TruncateAfter(int keepUpToFrame)
|
|
{
|
|
int removeStart = keepUpToFrame + 1;
|
|
if (removeStart < _frames.Count)
|
|
_frames.RemoveRange(removeStart, _frames.Count - removeStart);
|
|
|
|
for (int i = _allEvents.Count - 1; i >= 0; i--)
|
|
{
|
|
if (_allEvents[i].Frame > keepUpToFrame)
|
|
_allEvents.RemoveAt(i);
|
|
}
|
|
}
|
|
|
|
public void AppendFrame(BattleFrameSnapshot snapshot)
|
|
{
|
|
_frames.Add(snapshot);
|
|
if (snapshot.Events is { Length: > 0 })
|
|
_allEvents.AddRange(snapshot.Events);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Первый кадр строго после fromFrame, на котором хотя бы один живой боец завершил действие.
|
|
/// </summary>
|
|
public int FindNextPauseFrame(int fromFrame)
|
|
{
|
|
for (int f = fromFrame + 1; f < _frames.Count; f++)
|
|
{
|
|
if (_frames[f].ReadySlotsMask != 0UL)
|
|
return f;
|
|
}
|
|
|
|
return LastFrame;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Кадр завершения действия конкретного слота (учитывая прерывания).
|
|
/// </summary>
|
|
public int FindActionEndFrameForSlot(int fromFrame, int slotIndex)
|
|
{
|
|
ulong mask = 1UL << slotIndex;
|
|
for (int f = fromFrame + 1; f < _frames.Count; f++)
|
|
{
|
|
if ((_frames[f].ReadySlotsMask & mask) != 0UL)
|
|
return f;
|
|
}
|
|
|
|
return LastFrame;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Возвращает индексы всех живых слотов, которым нужен новый приказ на кадре frame.
|
|
/// </summary>
|
|
public List<int> GetReadySlotsAt(int frame)
|
|
{
|
|
var result = new List<int>();
|
|
var snap = GetFrame(frame);
|
|
|
|
for (int i = 0; i < snap.Entities.Length; i++)
|
|
{
|
|
var e = snap.Entities[i];
|
|
bool isDead = e.PhysicsAndVisuals.CurrentHealth.CurrentHealth <= 0;
|
|
if (isDead) continue;
|
|
|
|
bool markedReady = (snap.ReadySlotsMask & (1UL << i)) != 0UL;
|
|
bool isIdleNone = e.ActionSlot.ActionType == BattleActionType.None;
|
|
|
|
if (markedReady || isIdleNone)
|
|
result.Add(i);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|
|
} |