unit ObjectIPADConsole;

interface

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

type
  TObjectIPADConsole = class(TCustomObject)
  private
    FRadius: Single;
    FProgress: Single;
    FTime: Single;
    FHeroProgress: Single;
    FHeroPosition: TVec2f;
    FName: String;
    function GetProgress: Single;
  published
    procedure Process(const Delta: Double); override;
    procedure Load(Aini: TIniFile); override;
    procedure SetActive(AAction: Boolean); override;
    procedure SetHero(AHero: TCustomUnit); override;
    property Name: String read FName;
    property Progress: Single read GetProgress;
  end;

implementation

uses
  Resources;

function TObjectIPADConsole.GetProgress: Single;
begin
  Result := FTime / FProgress;
end;

procedure TObjectIPADConsole.Load(Aini: TIniFile);
var
  Str: String;
begin
  inherited;
  Str := 'Obj_' + IntToStr(ID);
  FProgress := Aini.ReadFloat(Str, 'Progress', 1);
  FName := Aini.ReadString(Str, 'Name', 'Консоль '+ IntToStr(ID));
end;

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

  TalkAction;
end;

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

procedure TObjectIPADConsole.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;

end.
