1bit-game-jam/scripts/GameManager.cs
2023-08-19 14:25:16 +03:00

63 lines
1.2 KiB
C#

using System;
using Godot;
public partial class GameManager : Node
{
public struct GameInfo
{
public ulong GameStart = 0;
public ulong GameEnd = 0;
public int Attempts = 0;
public Node2D Checkpoint;
public GameInfo(Node2D checkpoint)
{
Checkpoint = checkpoint;
}
}
public static bool IsPlaying = true;
[Export] public PlayZone FirstZone;
[Export] public Player Player;
[Signal]
public delegate void GameOverEventHandler();
private static GameInfo _gameInfo;
public override void _Ready()
{
if (!IsPlaying)
{
IsPlaying = true;
_gameInfo = new GameInfo(FirstZone.PlayerSpawnPoint);
}
StartGame();
}
public void StartGame()
{
_gameInfo.GameStart = Time.GetTicksMsec();
Player.Position = _gameInfo.Checkpoint.GlobalPosition;
}
public void EndGame()
{
_gameInfo.GameEnd = Time.GetTicksMsec();
EmitSignal(SignalName.GameOver);
}
public string GetFormattedTimeElapsed() =>
TimeSpan.FromMilliseconds(_gameInfo.GameEnd - _gameInfo.GameStart).ToString(@"hh\:mm\:ss.fff");
public void OnPlayerDied() => _gameInfo.Attempts++;
public void SetCurrentZone(PlayZone zone)
{
GD.Print($"New zone {zone}");
_gameInfo.Checkpoint = zone.PlayerSpawnPoint;
}
}