Great timeline system (Age of AI WON)
This commit is contained in:
1 parent
2ce0391c16
commit
e312131725
182 files changed
+380070
-470221
No files matched your search
@@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using Code.Infrastructure.UI;
|
||||
@@ -5,90 +6,193 @@ using Code.OfficeGame.BattleMode.Timeline;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using LitMotion;
|
||||
using LitMotion.Extensions;
|
||||
using R3;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Code.OfficeGame.BattleMode.UI
|
||||
{
|
||||
public class BattleTimelinePanel : UIPanel
|
||||
{
|
||||
[SerializeField] private int maxFrames = 250;
|
||||
[SerializeField] private GameObject timelineKeyPrefab;
|
||||
[SerializeField] private float duration = 0.35f;
|
||||
[SerializeField] private RectTransform timelineItself;
|
||||
[SerializeField] private TMP_Text prevAction;
|
||||
|
||||
public void SetPreviousActionText(string text) => prevAction.text = text;
|
||||
[SerializeField] private TMP_Text frameCounterText;
|
||||
|
||||
// TODO Вырезать или пощадить
|
||||
private readonly Dictionary<EntityId, int> _entityEndFrames = new();
|
||||
private readonly Dictionary<EntityId, RectTransform> _entityKeys = new();
|
||||
[Header("Управление ползунком")]
|
||||
[SerializeField] private Slider timelineSlider;
|
||||
[SerializeField] private Button playPauseButton;
|
||||
[SerializeField] private Button stepBackButton;
|
||||
[SerializeField] private Button stepForwardButton;
|
||||
[SerializeField] private Button returnToCommitButton;
|
||||
|
||||
private int _lastKnownCurrentFrame;
|
||||
[Header("Маркеры и цветные полосы событий")]
|
||||
[SerializeField] private RectTransform markersContainer;
|
||||
[SerializeField] private Image markerStripePrefab;
|
||||
[SerializeField] private RectTransform commitMarkerLine;
|
||||
|
||||
[SerializeField] private Color[] stageInterruptionColors =
|
||||
{
|
||||
new(1.0f, 0.85f, 0.2f, 0.95f), // 1-е прерывание
|
||||
new(1.0f, 0.55f, 0.2f, 0.95f), // 2-е прерывание
|
||||
new(0.95f, 0.3f, 0.5f, 0.95f) // 3+ прерывание
|
||||
};
|
||||
[SerializeField] private Color hitEventColor = new(1f, 0.3f, 0.1f, 0.9f);
|
||||
[SerializeField] private Color deathEventColor = new(0.85f, 0.05f, 0.15f, 1f);
|
||||
|
||||
private readonly List<Image> _spawnedMarkers = new();
|
||||
private bool _suppressSliderCallback;
|
||||
private MotionHandle _motionHandle;
|
||||
|
||||
public void SetAction(TimelineBattleAction action)
|
||||
|
||||
public void SetPreviousActionText(string text)
|
||||
{
|
||||
_entityEndFrames[action.EntityId] = action.Frame;
|
||||
|
||||
if (!_entityKeys.TryGetValue(action.EntityId, out RectTransform keyRect))
|
||||
{
|
||||
keyRect = (RectTransform)Instantiate(timelineKeyPrefab, timelineItself).transform;
|
||||
_entityKeys[action.EntityId] = keyRect;
|
||||
}
|
||||
|
||||
UpdateKeyPosition(keyRect, action.Frame - _lastKnownCurrentFrame);
|
||||
if (prevAction != null) prevAction.text = text;
|
||||
}
|
||||
public void OnFrameTick(int currentFrame)
|
||||
{
|
||||
_lastKnownCurrentFrame = currentFrame;
|
||||
|
||||
foreach (var (entityId, endFrame) in _entityEndFrames)
|
||||
{
|
||||
if (_entityKeys.TryGetValue(entityId, out RectTransform keyRect))
|
||||
public IDisposable BindTimeline(BattleTimeline timeline)
|
||||
{
|
||||
var bag = new DisposableBag();
|
||||
|
||||
timelineSlider.wholeNumbers = true;
|
||||
|
||||
// 1. Границы ползунка растут до конца последнего действия
|
||||
timeline.MaxFrame
|
||||
.Subscribe(maxFrame =>
|
||||
{
|
||||
int remainingFrames = Mathf.Max(0, endFrame - currentFrame);
|
||||
UpdateKeyPosition(keyRect, remainingFrames);
|
||||
}
|
||||
timelineSlider.minValue = 0;
|
||||
timelineSlider.maxValue = Mathf.Max(1, maxFrame);
|
||||
UpdateFrameCounter(timeline.DisplayFrame.CurrentValue, maxFrame);
|
||||
})
|
||||
.AddTo(ref bag);
|
||||
|
||||
// 2. Текущий кадр двигает ползунок без рекурсии
|
||||
timeline.DisplayFrame
|
||||
.Subscribe(frame =>
|
||||
{
|
||||
_suppressSliderCallback = true;
|
||||
timelineSlider.SetValueWithoutNotify(frame);
|
||||
_suppressSliderCallback = false;
|
||||
UpdateFrameCounter(frame, timeline.MaxFrame.CurrentValue);
|
||||
})
|
||||
.AddTo(ref bag);
|
||||
|
||||
// 3. Ползунок заблокирован только во время реального исполнения
|
||||
timeline.Mode
|
||||
.Subscribe(mode => timelineSlider.interactable = mode != TimelineMode.BattlePlaying)
|
||||
.AddTo(ref bag);
|
||||
|
||||
// 4. Обновление цветных полос прерываний, ударов и смертей при перепекании
|
||||
timeline.OnTrackRebaked
|
||||
.Subscribe(track => RebuildMarkers(track, timeline.CommittedFrame.CurrentValue))
|
||||
.AddTo(ref bag);
|
||||
|
||||
timeline.CommittedFrame
|
||||
.Subscribe(commitFrame => UpdateCommitLine(commitFrame, timeline.MaxFrame.CurrentValue))
|
||||
.AddTo(ref bag);
|
||||
|
||||
// 5. Пользователь тянет ползунок -> скраббинг таймлайна
|
||||
timelineSlider.OnValueChangedAsObservable()
|
||||
.Subscribe(val =>
|
||||
{
|
||||
if (_suppressSliderCallback) return;
|
||||
timeline.ScrubToFrame((int)val);
|
||||
})
|
||||
.AddTo(ref bag);
|
||||
|
||||
if (playPauseButton != null)
|
||||
playPauseButton.OnClickAsObservable().Subscribe(_ => timeline.TogglePreviewPlayPause()).AddTo(ref bag);
|
||||
|
||||
if (stepBackButton != null)
|
||||
stepBackButton.OnClickAsObservable().Subscribe(_ => timeline.StepFrame(-1)).AddTo(ref bag);
|
||||
|
||||
if (stepForwardButton != null)
|
||||
stepForwardButton.OnClickAsObservable().Subscribe(_ => timeline.StepFrame(+1)).AddTo(ref bag);
|
||||
|
||||
if (returnToCommitButton != null)
|
||||
returnToCommitButton.OnClickAsObservable().Subscribe(_ => timeline.ReturnToCommittedFrame()).AddTo(ref bag);
|
||||
|
||||
return bag;
|
||||
}
|
||||
|
||||
private void RebuildMarkers(BattleTimelineTrack track, int committedFrame)
|
||||
{
|
||||
for (int i = 0; i < _spawnedMarkers.Count; i++)
|
||||
if (_spawnedMarkers[i] != null) Destroy(_spawnedMarkers[i].gameObject);
|
||||
_spawnedMarkers.Clear();
|
||||
|
||||
float maxFrame = Mathf.Max(1, track.LastFrame);
|
||||
UpdateCommitLine(committedFrame, track.LastFrame);
|
||||
|
||||
if (markerStripePrefab == null || markersContainer == null) return;
|
||||
|
||||
var events = track.AllEvents;
|
||||
for (int i = 0; i < events.Count; i++)
|
||||
{
|
||||
var ev = events[i];
|
||||
Color? color = ev.EventType switch
|
||||
{
|
||||
TimelineEventType.ActionCompleted => GetInterruptionColor(ev.InterruptionStage),
|
||||
TimelineEventType.HitLanded => hitEventColor,
|
||||
TimelineEventType.EntityDied => deathEventColor,
|
||||
_ => null
|
||||
};
|
||||
|
||||
if (!color.HasValue) continue;
|
||||
|
||||
var stripe = Instantiate(markerStripePrefab, markersContainer);
|
||||
stripe.color = color.Value;
|
||||
PlaceNormalizedX((RectTransform)stripe.transform, ev.Frame / maxFrame);
|
||||
_spawnedMarkers.Add(stripe);
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateKeyPosition(RectTransform keyRect, int relativeFrame)
|
||||
private Color GetInterruptionColor(int stage)
|
||||
{
|
||||
var rectWidth = timelineItself.rect.width;
|
||||
var frameSize = rectWidth / maxFrames;
|
||||
if (stageInterruptionColors == null || stageInterruptionColors.Length == 0)
|
||||
return Color.yellow;
|
||||
int idx = Mathf.Clamp(stage - 1, 0, stageInterruptionColors.Length - 1);
|
||||
return stageInterruptionColors[idx];
|
||||
}
|
||||
|
||||
keyRect.anchoredPosition = new Vector2(frameSize * relativeFrame - rectWidth / 2f, 0f);
|
||||
private void UpdateCommitLine(int committedFrame, int maxFrame)
|
||||
{
|
||||
if (commitMarkerLine == null) return;
|
||||
PlaceNormalizedX(commitMarkerLine, committedFrame / (float)Mathf.Max(1, maxFrame));
|
||||
}
|
||||
|
||||
private static void PlaceNormalizedX(RectTransform rect, float normalized01)
|
||||
{
|
||||
normalized01 = Mathf.Clamp01(normalized01);
|
||||
rect.anchorMin = new Vector2(normalized01, 0f);
|
||||
rect.anchorMax = new Vector2(normalized01, 1f);
|
||||
rect.anchoredPosition = Vector2.zero;
|
||||
}
|
||||
|
||||
private void UpdateFrameCounter(int current, int max)
|
||||
{
|
||||
if (frameCounterText != null)
|
||||
frameCounterText.SetText($"{current} / {max}");
|
||||
}
|
||||
|
||||
public override async UniTask Open(CancellationToken cts = default)
|
||||
{
|
||||
gameObject.SetActive(true);
|
||||
|
||||
float startY = -((RectTransform)transform).rect.height;
|
||||
float targetY = 0f;
|
||||
|
||||
_motionHandle = LMotion.Create(startY, targetY, duration)
|
||||
_motionHandle = LMotion.Create(startY, 0f, duration)
|
||||
.WithEase(Ease.OutCubic)
|
||||
.WithScheduler(MotionScheduler.UpdateIgnoreTimeScale)
|
||||
.BindToAnchoredPositionY((RectTransform)transform);
|
||||
|
||||
await _motionHandle.ToUniTask(cts);
|
||||
}
|
||||
|
||||
public override async UniTask Close(CancellationToken cts = default)
|
||||
{
|
||||
float startY = 0;
|
||||
float targetY = ((RectTransform)transform).rect.height;
|
||||
|
||||
_motionHandle = LMotion.Create(startY, targetY, duration)
|
||||
_motionHandle = LMotion.Create(0f, targetY, duration)
|
||||
.WithEase(Ease.InBack)
|
||||
.WithScheduler(MotionScheduler.UpdateIgnoreTimeScale)
|
||||
.BindToAnchoredPositionY((RectTransform)transform);
|
||||
|
||||
await _motionHandle.ToUniTask(cts);
|
||||
|
||||
gameObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user