unit GameOverlay;

interface

uses QuadEngine, Vec2f, SysUtils, Classes, Generics.Collections;

type
  TLanguage = (lAncient, lEarth);

  TSpeech = record
    CharID: Byte;
    Text: string;
    Language: TLanguage;
    class operator Equal(const A, B: TSpeech): Boolean;
  end;

  TGameOverlay = class sealed
  private
  class var
    FSpeechSide: Boolean;
    FSpeech: TSpeech;
    FSpeechTime: Double;
    FDialogQueue: TQueue<TSpeech>;
    FEnabled: Boolean;
  public
    class procedure Process(const Delta: Double);
    class procedure Draw;
    class procedure AddDialog(ACharID: Byte; AText: string; ALanguage: TLanguage); overload;
    class procedure AddDialog(const ASpeech: TSpeech); overload;
    class procedure OnTimer;
    class procedure SetEnabled(AEnabled: Boolean);
    class function GetDialogCount: Integer;
  end;

implementation

uses
  Resources;

{ TGameOverlay }

class procedure TGameOverlay.SetEnabled(AEnabled: Boolean);
begin
  FEnabled := AEnabled;
end;

class function TGameOverlay.GetDialogCount: Integer;
begin
  Result := FDialogQueue.Count;
end;

class procedure TGameOverlay.AddDialog(ACharID: Byte; AText: string;
  ALanguage: TLanguage);
var
  Speech: TSpeech;
begin
  Speech.CharID := ACharID;
  Speech.Text := AText;
  Speech.Language := ALanguage;

  FDialogQueue.Enqueue(Speech);
end;

class procedure TGameOverlay.AddDialog(const ASpeech: TSpeech);
begin
  FDialogQueue.Enqueue(ASpeech);
end;

class procedure TGameOverlay.Draw;
begin
  if not FEnabled then
    Exit;

  TGlobals.QuadRender.SetBlendMode(qbmSrcAlpha);

  TGlobals.AncientsFont.TextOut(TVec2f.Create(512, 40), 1, PWideChar(IntToStr(Trunc(TGlobals.FTLTimer)) + '.' + Format('%.2d', [trunc((TGlobals.FTLTimer - Trunc(TGlobals.FTLTimer))*100)])), $FF99CCFF, qfaCenter);

  if (FSpeechTime > 0) then
  begin
    TGlobals.QuadRender.SetBlendMode(qbmSrcAlpha);

    TGlobals.QuadRender.Rectangle(TVec2f.Create(180, 568), TVec2f.Create(860, 668), $AA000000);

    if FSpeechSide then
      TTextures.Portraits.DrawFrame(TVec2f.Create(100, 568), FSpeech.CharID)
    else
      TTextures.Portraits.DrawFrame(TVec2f.Create(860, 568), FSpeech.CharID);

    if FSpeech.Language = lAncient then
      TGlobals.AncientsFont.TextOut(TVec2f.Create(200, 598), 1, PWideChar(FSpeech.Text))
    else
      TGlobals.EarthFont.TextOut(TVec2f.Create(200, 598), 0.6, PWideChar(FSpeech.Text))
  end;
end;

class procedure TGameOverlay.Process(const Delta: Double);
begin
  if not FEnabled then
    Exit;

  if FSpeechTime > 0 then
    FSpeechTime := FSpeechTime - delta;

  if TGlobals.QuadInput.IsKeyPress(32) then
    FSpeechTime := 0;

  if (FDialogQueue.Count > 0) and (FSpeechTime <= 0) then
  begin
    FSpeech := FDialogQueue.Dequeue;
    FSpeechTime := 4;
    FSpeechSide := not FSpeechSide;
  end;

  TGlobals.FTLTimer := TGlobals.FTLTimer - Delta;
  if TGlobals.FTLTimer < 0 then
  begin
    TGlobals.FTLTimer := 7 * 60;
    TGlobals.SwitchSpeed;
    OnTimer;
  end;
end;

class procedure TGameOverlay.OnTimer;
begin

end;

{ TSpeech }

class operator TSpeech.Equal(const A, B: TSpeech): Boolean;
begin
  Result := (A.CharID = B.CharID) and (A.Text = B.Text) and (A.Language = B.Language);
end;

initialization
  TGameOverlay.FDialogQueue := TQueue<TSpeech>.Create;

finalization
  TGameOverlay.FDialogQueue.Free;

end.
