2024-05-19 15:59:41 +00:00
|
|
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System;
|
|
|
|
using Godot;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Rokojori
|
|
|
|
{
|
2024-09-14 06:41:52 +00:00
|
|
|
[Tool]
|
2025-01-21 20:58:56 +00:00
|
|
|
[GlobalClass, Icon("res://addons/rokojori_action_library/Icons/TimeLineManager.svg") ]
|
2025-01-08 18:46:17 +00:00
|
|
|
public partial class TimeLineManager:NetworkNode
|
2024-05-19 15:59:41 +00:00
|
|
|
{
|
|
|
|
[Export]
|
2025-02-12 16:48:15 +00:00
|
|
|
public TimeLine[] timeLines = new TimeLine[ 0 ];
|
2024-05-19 15:59:41 +00:00
|
|
|
|
2025-01-03 12:09:23 +00:00
|
|
|
[Export]
|
2025-01-13 18:11:12 +00:00
|
|
|
public TimeLine gameTimeTimeLine;
|
2025-01-03 12:09:23 +00:00
|
|
|
|
|
|
|
[Export]
|
2025-01-08 18:46:17 +00:00
|
|
|
public TimeLine realTimeTimeLine;
|
2025-01-03 12:09:23 +00:00
|
|
|
|
|
|
|
[Export]
|
|
|
|
public bool computeRealtimeWithEngineDelta;
|
|
|
|
|
|
|
|
|
2024-05-19 15:59:41 +00:00
|
|
|
List<TimeLineRunner> _runners = new List<TimeLineRunner>();
|
|
|
|
|
|
|
|
int _idCounter = 0;
|
|
|
|
bool _initialized = false;
|
|
|
|
|
|
|
|
public override void _Ready()
|
|
|
|
{
|
|
|
|
Initialize();
|
|
|
|
}
|
|
|
|
|
2025-01-19 20:35:51 +00:00
|
|
|
public static TimeLineManager Get()
|
|
|
|
{
|
|
|
|
return Unique<TimeLineManager>.Get();
|
|
|
|
}
|
|
|
|
|
2025-01-03 12:09:23 +00:00
|
|
|
float _lastUpdate = 0;
|
|
|
|
DateTime lastUpdated = DateTime.Now;
|
|
|
|
float _estimatedDelta = 0;
|
|
|
|
float _estimatedDeltaFilter = 0.9f;
|
|
|
|
float _unscaledDelta = 0;
|
|
|
|
double _lastRealtimePosition = 0;
|
|
|
|
double _realtimePosition = 0;
|
|
|
|
|
|
|
|
float _lastTimeScale = 1;
|
|
|
|
|
|
|
|
public override void _Process( double delta )
|
|
|
|
{
|
|
|
|
UpdateRealTime( delta );
|
|
|
|
|
2025-01-19 20:35:51 +00:00
|
|
|
if ( ! Engine.IsEditorHint() && gameTimeTimeLine != null )
|
2025-01-03 12:09:23 +00:00
|
|
|
{
|
2025-01-19 20:35:51 +00:00
|
|
|
Engine.TimeScale = gameTimeTimeLine.runner.modulatedSpeed;
|
2025-01-03 12:09:23 +00:00
|
|
|
}
|
|
|
|
|
2025-01-21 20:58:56 +00:00
|
|
|
_runners.ForEach( r => r.UpdateTimeLine( unscaledTimeDelta ) );
|
2025-01-03 12:09:23 +00:00
|
|
|
}
|
|
|
|
|
2025-01-08 18:46:17 +00:00
|
|
|
public int GetTimeLineIndex( TimeLine timeLine )
|
|
|
|
{
|
|
|
|
return Arrays.IndexOf( timeLines, timeLine );
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Modulate( TimeLine timeline, AnimationCurve c, Action<bool> onReady )
|
2025-01-03 12:09:23 +00:00
|
|
|
{
|
|
|
|
var runner = GetRunner( GetTimeLineIndex( timeline ) );
|
|
|
|
runner.Modulate( c, onReady );
|
2025-01-19 20:35:51 +00:00
|
|
|
}
|
2025-01-18 20:02:20 +00:00
|
|
|
|
2025-01-03 12:09:23 +00:00
|
|
|
void UpdateRealTime( double engineDelta )
|
2025-01-18 12:49:14 +00:00
|
|
|
{
|
2025-01-03 12:09:23 +00:00
|
|
|
var now = DateTime.Now;
|
|
|
|
var unscaled = (float) ( ( now - lastUpdated ).TotalSeconds );
|
|
|
|
lastUpdated = now;
|
|
|
|
|
|
|
|
if ( unscaled < 1f/20f )
|
|
|
|
{
|
|
|
|
_estimatedDelta += _estimatedDeltaFilter * ( unscaled - _estimatedDelta );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( computeRealtimeWithEngineDelta )
|
|
|
|
{
|
|
|
|
_unscaledDelta = (float)( Engine.TimeScale == 0 ? _estimatedDelta : ( engineDelta / Engine.TimeScale ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_unscaledDelta = _estimatedDelta;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_lastRealtimePosition = _realtimePosition;
|
|
|
|
_realtimePosition += _unscaledDelta;
|
|
|
|
}
|
|
|
|
|
|
|
|
public float unscaledTimeDelta => _unscaledDelta;
|
|
|
|
public double realtime => _realtimePosition;
|
|
|
|
|
|
|
|
|
2024-05-19 15:59:41 +00:00
|
|
|
void Initialize()
|
|
|
|
{
|
|
|
|
if ( _initialized )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_initialized = true;
|
2025-01-19 20:35:51 +00:00
|
|
|
_runners = Lists.Map( timeLines, tl => new TimeLineRunner( tl, this ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
public static TimeLine Ensure( TimeLine timeline )
|
|
|
|
{
|
|
|
|
if ( timeline != null )
|
|
|
|
{
|
|
|
|
return timeline;
|
|
|
|
}
|
|
|
|
|
|
|
|
var tm = TimeLineManager.Get();
|
|
|
|
|
|
|
|
return tm.gameTimeTimeLine;
|
2024-05-19 15:59:41 +00:00
|
|
|
}
|
|
|
|
|
2025-01-08 18:46:17 +00:00
|
|
|
public TimeLineRunner GetRunner( TimeLine timeline )
|
|
|
|
{
|
|
|
|
return GetRunner( _runners.FindIndex( r => r.timeLine == timeline ) );
|
|
|
|
}
|
2025-01-19 20:35:51 +00:00
|
|
|
|
2025-01-08 18:46:17 +00:00
|
|
|
public TimeLineRunner GetRunner( int index )
|
2024-05-19 15:59:41 +00:00
|
|
|
{
|
|
|
|
if ( index < 0 || index >= _runners.Count )
|
|
|
|
{
|
|
|
|
return null ;
|
|
|
|
}
|
|
|
|
|
|
|
|
return _runners[ index ];
|
|
|
|
}
|
|
|
|
|
2025-01-19 20:35:51 +00:00
|
|
|
int _CreateID()
|
|
|
|
{
|
2024-05-19 15:59:41 +00:00
|
|
|
_idCounter ++;
|
|
|
|
return _idCounter;
|
|
|
|
}
|
|
|
|
|
2025-01-19 20:35:51 +00:00
|
|
|
public static int CreateID()
|
2024-05-19 15:59:41 +00:00
|
|
|
{
|
2025-01-19 20:35:51 +00:00
|
|
|
var tm = TimeLineManager.Get();
|
|
|
|
return tm._CreateID();
|
2024-05-19 15:59:41 +00:00
|
|
|
}
|
2025-01-19 20:35:51 +00:00
|
|
|
|
2024-05-19 15:59:41 +00:00
|
|
|
}
|
|
|
|
}
|