unit CustomSubject;

interface

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

type
  TCustomSubject = class(TCustomObject)
  private
    FRadius: Single;
    FSubject: TSubject;
    FRotate: TVec2f;
    function IsActive: Boolean; override;
  public
    property Radius: Single read FRadius;
  published
    procedure Load(Aini: TIniFile); override;
    procedure Draw; override;
    procedure DrawLight; override;
    procedure Process(const Delta: Double); override;

    property Subject: TSubject read FSubject write FSubject;
  end;

implementation

uses
  Resources, Level.Map;


procedure TCustomSubject.Load(Aini: TIniFile);
var
  Str: String;
  SubjectType: TSubjectType;
begin
  inherited;
//  FRadius := TLevelMap.FieldSize / 2;
  FRadius := 24;

  Str := 'Obj_' + IntToStr(ID);

  if Aini.SectionExists(Str) then
  begin
    SubjectType := TSubjectType(Aini.ReadInteger(Str, 'Type', 0));
    FRadius := Aini.ReadInteger(Str, 'Radius', 24);
    if SubjectType <> stNone then
    begin
      FSubject := TSubject.Create(SubjectType, Aini.ReadInteger(Str, 'Count', 1));
      FSubject.Damage(Aini.ReadInteger(Str, 'Damage', 0));
    end;
  end;
end;

function TCustomSubject.IsActive: Boolean;
begin
  Result := not Assigned(Subject);
end;

procedure TCustomSubject.Draw;
begin
  if Assigned(Subject) then
  begin
    TTextures.Subjects.DrawRotFrame(Position, 0, 0.5, Integer(Subject.SubjectType) + 1);
    //TGlobals.EarthFont.TextOut(Position, 0.2, PWideChar(IntTOstr(Subject.Count)));
  end;
end;

procedure TCustomSubject.DrawLight;
begin
  if not ParentAction then
  begin
    TShaders.Planet.BindVariableToPS(0, @FRotate, 1);
    TGlobals.QuadRender.SetTextureAdressing(qtaWrap);
    TShaders.Planet.SetShaderState(True);
    TGlobals.QuadRender.SetBlendMode(qbmSrcAlphaAdd);
    TTextures.Shield.DrawRot(Position, 0, FRadius / 64, $FFFFFFFF);
    TTextures.Shield.DrawRot(Position, 0, FRadius / 70, $FFFF88FF);
    TShaders.Planet.SetShaderState(False);
    TGlobals.QuadRender.SetTextureAdressing(qtaClamp);
  end;

  if not ParentAction then
  begin
    TGlobals.QuadCamera.Disable;
    TTextures.QuadGBuffer.DrawLight(
      TVec3f.Create(
        (Position.X - TGlobals.QuadCamera.GetPosition.X) / TGlobals.GWidth,
        (Position.Y - TGlobals.QuadCamera.GetPosition.Y) / TGlobals.GHeight,
        0.02
      ), FRadius / 48, $FFFF88FF);
    TGlobals.QuadCamera.Enable;
  end;
end;

procedure TCustomSubject.Process(const Delta: Double);
begin
  inherited;

  if not ParentAction then
  begin
    FRotate.X := FRotate.X - Delta/10;
    FRotate.Y := FRotate.Y - Delta/10;
  end;
end;

end.

