unit ObjectConsole;

interface

uses
  CustomObject, Vec2f, IniFiles, System.SysUtils, Units.Player, Units.Custom,
  QuadEngine;

type
  TObjectConsole = class(TCustomObject)
  private
    FRadius: Single;
    FProgress: Single;
    FTime: Single;
    FHeroProgress: Single;
    FHeroPosition: TVec2f;
  public
    procedure Draw; override;

  published
    procedure Process(const Delta: Double); override;
    procedure Load(Aini: TIniFile); override;
    procedure SetActive(AAction: Boolean); override;
    procedure DrawLight; override;
    procedure SetHero(AHero: TCustomUnit); override;
  end;

implementation

uses
  Resources, GlobalTypes;

procedure TObjectConsole.Load(Aini: TIniFile);
var
  Str: String;
begin
  inherited;
  Str := 'Obj_' + IntToStr(ID);
  FProgress := Aini.ReadFloat(Str, 'Progress', 1);

  FTime := 0;
end;

procedure TObjectConsole.SetHero(AHero: TCustomUnit);
begin
  inherited;
  FTime := 0;
  FHeroPosition := AHero.Position;
  FHeroProgress := FProgress {+ FProgress * 5 * (1 - FHero.EngineerSkill / 100)};

  TalkAction;
end;

procedure TObjectConsole.SetActive(AAction: Boolean);
begin
  inherited;
  FTime := 0;
  FHero := nil;
end;

procedure TObjectConsole.Process(const Delta: Double);
begin
  UpdateParentAction;
  if Action and not ParentAction then
    SetActive(False);

  if Assigned(FHero) and (FHeroPosition.Distance(FHero.Position) > 5) then
    FHero := nil;

  if ParentAction and Assigned(FHero) then
  begin
    FTime := FTime + Delta;
    if FTime >= FHeroProgress then
      SetActive(not Action);
  end;
end;

procedure TObjectConsole.Draw;
begin
  TTextures.Console[Trunc(FTime / FHeroProgress * 5)].DrawRot(Position, 0, 1);
//  TGlobals.QuadRender.DrawCircle(Position, FTime / FHeroProgress * 10, 0, $FF00FF00);
end;

procedure TObjectConsole.DrawLight;
begin
  TGlobals.QuadCamera.Disable;
  if ParentAction then
  begin
    TTextures.QuadGBuffer.DrawLight(
      TVec3f.Create(
        (Position.X - TGlobals.QuadCamera.GetPosition.X) / TGlobals.GWidth,
        (Position.Y - TGlobals.QuadCamera.GetPosition.Y) / TGlobals.GHeight,
        10.0
      ), 0.1, $FFFFCC99 - Trunc(FTime / FHeroProgress * 255) shl 24);
    TTextures.QuadGBuffer.DrawLight(
      TVec3f.Create(
        (Position.X - TGlobals.QuadCamera.GetPosition.X) / TGlobals.GWidth,
        (Position.Y - TGlobals.QuadCamera.GetPosition.Y) / TGlobals.GHeight,
        10.0
      ), 0.1, $0099CCFF + Trunc(FTime / FHeroProgress * 255) shl 24);
  end
  else
    TTextures.QuadGBuffer.DrawLight(
      TVec3f.Create(
        (Position.X - TGlobals.QuadCamera.GetPosition.X) / TGlobals.GWidth,
        (Position.Y - TGlobals.QuadCamera.GetPosition.Y) / TGlobals.GHeight,
        1.0
      ), 0.1, $44FF0000);

   TGlobals.QuadCamera.Enable;
end;

end.
