riversong code showcase
This commit is contained in:
64
Source/Riversong/Game/GameSpeed/GameSpeedSystem.cs
Normal file
64
Source/Riversong/Game/GameSpeed/GameSpeedSystem.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
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
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
9
Source/Riversong/Game/GameSpeed/IGameSpeed.cs
Normal file
9
Source/Riversong/Game/GameSpeed/IGameSpeed.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace DanieleMarotta.RiversongCodeShowcase
|
||||
{
|
||||
public interface IGameSpeed
|
||||
{
|
||||
int SpeedLevel { get; }
|
||||
|
||||
void SetSpeedLevel(int speedLevel);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user