unit GameMode.Manager;

interface

uses
  System.Classes, System.Generics.Collections, GameMode.Custom, Windows, SysUtils;

type
  TGameModeManager = class
  private
    FActiveGameMode: TGameModeCustom;
    FNextGameMode: TGameModeCustom;
  public
    constructor Create;
    procedure RefreshGameMode;
    procedure Process(const Delta: Double);
    procedure Draw;
    procedure SetActiveGameMode(AActiveGameMode: TGameModeCustom);
  end;

implementation

uses
  Resources, GameOverlay;

{ TGameModeManager }

constructor TGameModeManager.Create;
begin
  FNextGameMode := nil;
  FActiveGameMode := nil;
end;

procedure TGameModeManager.RefreshGameMode;
begin
  if FNextGameMode = FActiveGameMode then
    Exit;
  if Assigned(FActiveGameMode) then
    FActiveGameMode.Deactivation;
  FActiveGameMode := FNextGameMode;
  if Assigned(FActiveGameMode) then
    FActiveGameMode.Activation;
end;

procedure TGameModeManager.SetActiveGameMode(AActiveGameMode: TGameModeCustom);
begin
  FNextGameMode := AActiveGameMode;
end;

procedure TGameModeManager.Process(const Delta: Double);
begin
  if TGlobals.QuadInput.IsKeyPress(VK_F1) then
    TGlobals.GameModeManager.SetActiveGameMode(TGlobals.GameModes.Level);

  if TGlobals.QuadInput.IsKeyPress(VK_F2) then
    TGlobals.GameModeManager.SetActiveGameMode(TGlobals.GameModes.StarGate);

  if TGlobals.QuadInput.IsKeyPress(VK_F3) then
    TGlobals.GameModeManager.SetActiveGameMode(TGlobals.GameModes.FeesSquad);

  if TGlobals.QuadInput.IsKeyPress(VK_F4) then
    TGlobals.GameModeManager.SetActiveGameMode(TGlobals.GameModes.Computer);

  if TGlobals.QuadInput.IsKeyPress(VK_F12) then
    TGlobals.SwitchSpeed;

  if Assigned(FActiveGameMode) then
  begin
    FActiveGameMode.Process(Delta);
    if FActiveGameMode.DrawOverlay then
      TGameOverlay.Process(Delta);
  end;

  TGlobals.QuadWindow.SetCaption(PWideChar(Format('IGDC 126 - Exploration FPS: %f', [TGlobals.QuadTimer.GetFPS])));
end;

procedure TGameModeManager.Draw;
begin
  if Assigned(FActiveGameMode) then
  begin
    FActiveGameMode.Draw;
    if FActiveGameMode.DrawOverlay then
      TGameOverlay.Draw;
  end;
end;

end.
