65 lines
1.6 KiB
C#
65 lines
1.6 KiB
C#
using System;
|
|
using Cysharp.Threading.Tasks;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
namespace DanieleMarotta.RiversongCodeShowcase
|
|
{
|
|
[Service(typeof(IGameSpeed))]
|
|
[GameSystemGroup(typeof(EarlyGameSystemGroup))]
|
|
[RequiresWorldReadyForUpdate]
|
|
[UpdateBefore(typeof(WorldTimeSystem))]
|
|
public class GameSpeedSystem : GameSystem, IGameSpeed, IInitializable, IUpdatable, IDisposable
|
|
{
|
|
private const float NormalSpeed = 1;
|
|
|
|
private const float FastSpeed = 2;
|
|
|
|
private const float VeryFastSpeed = 4;
|
|
|
|
public GameSpeedSystem(IServiceLocator serviceLocator) : base(serviceLocator)
|
|
{
|
|
}
|
|
|
|
public int SpeedLevel { get; private set; }
|
|
|
|
public UniTask InitializeAsync()
|
|
{
|
|
SetSpeedLevel(0);
|
|
|
|
return UniTask.CompletedTask;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
SetSpeedLevel(0);
|
|
}
|
|
|
|
public void Update()
|
|
{
|
|
if (Keyboard.current.digit1Key.wasPressedThisFrame)
|
|
SetSpeedLevel(0);
|
|
else if (Keyboard.current.digit2Key.wasPressedThisFrame)
|
|
SetSpeedLevel(1);
|
|
else if (Keyboard.current.digit3Key.wasPressedThisFrame) SetSpeedLevel(2);
|
|
|
|
Time.timeScale = GetTimeScale();
|
|
}
|
|
|
|
public void SetSpeedLevel(int speedLevel)
|
|
{
|
|
SpeedLevel = speedLevel;
|
|
}
|
|
|
|
private float GetTimeScale()
|
|
{
|
|
return SpeedLevel switch
|
|
{
|
|
1 => FastSpeed,
|
|
2 => VeryFastSpeed,
|
|
_ => NormalSpeed
|
|
};
|
|
}
|
|
}
|
|
}
|