1bit-game-jam/scripts/GameManager.cs

76 lines
1.5 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 NodePath Checkpoint = new NodePath();
public GameInfo(NodePath path)
{
SetCheckpoint(path);
}
public void SetCheckpoint(NodePath path)
{
GD.Print($"{path}");
Checkpoint = path;
}
}
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)
{
GD.Print("Restart stats");
IsPlaying = true;
_gameInfo = new GameInfo(GetPathTo(FirstZone.PlayerSpawnPoint));
}
StartGame();
}
public void StartGame()
{
_gameInfo.GameStart = Time.GetTicksMsec();
Player.GlobalPosition = GetNode<Node2D>(_gameInfo.Checkpoint).GlobalPosition;
}
public void EndGame()
{
_gameInfo.GameEnd = Time.GetTicksMsec();
Player.Invincible = true;
Player.SetProcess(false);
EmitSignal(SignalName.GameOver);
}
public string GetFormattedTimeElapsed() =>
TimeSpan.FromMilliseconds(_gameInfo.GameEnd - _gameInfo.GameStart).ToString(@"hh\:mm\:ss\.fff");
public void OnPlayerDied() => _gameInfo.Attempts++;
public int GetAttempts() => _gameInfo.Attempts;
public void SetCurrentZone(PlayZone zone)
{
GD.Print($"New zone {zone}");
var path = GetPathTo(zone.PlayerSpawnPoint);
_gameInfo.SetCheckpoint(path);
}
}