unit _Effects;

interface

Uses
  QuadEngine,
  _Scene,
  _Geometry,
  _Resources;

const
  MAX_POINTS = 20;

type
TTextEffect = record
    Position: TVector2;
    Life: Byte;         
    MaxLife: Byte;
    Text: String;
    Color: Cardinal;
  end;

TEffects = class(TBasicScene)
  private
    class var intent: TEffects;
  private
    FQuad: IQuadRender;
    FText: array[0..MAX_POINTS] of TTextEffect;
    constructor CreateEffects(QuadEngine: IQuadRender);
  public
    class function Create(QuadEngine: IQuadRender = nil): TEffects;
    destructor Destroy; override;
    procedure AddTextEffect(Text: String; Position: TVector2; Color: Cardinal; Life: Byte);
    procedure Render; override;
    procedure Update(dt: single); override;
  end;

implementation

constructor TEffects.CreateEffects(QuadEngine: IQuadRender);
begin
  if QuadEngine <> nil then
    FQuad:= QuadEngine;
  intent:= self;
end;

class function TEffects.Create(QuadEngine: IQuadRender = nil): TEffects;
begin
  if intent <> nil then
    Result:= intent
  else
    Result:= CreateEffects(QuadEngine);
end;

destructor TEffects.Destroy;
begin
  inherited;
end;

procedure TEffects.AddTextEffect(Text: String; Position: TVector2; Color: Cardinal; Life: Byte);
var
  i: Byte;
begin
  for i:=0 to MAX_POINTS do 
    if FText[i].Life = 0 then
      begin
        FText[i].Position:= Position;
        FText[i].Text := Text; 
        FText[i].Color:= Color;
        FText[i].Life := Life;
        FText[i].MaxLife := Life;
        Break;
      end;
end;

procedure TEffects.Render;
var
  i: Byte;
begin      
  for i:=0 to MAX_POINTS do
    if FText[i].Life > 0 then
      Resources.Font.TextOutAligned(
        FText[i].Position.X,
        FText[i].Position.Y + (FText[i].Life - FText[i].MaxLife) * 0.5,
        0.6,
        PAnsiChar(FText[i].Text),
        trunc(FText[i].Life / FText[i].MaxLife * $ff) * $1000000 + FText[i].Color,
        qfaCenter);
end;

procedure TEffects.Update(dt: single);
var
  i: Byte;
begin
  for i:=0 to MAX_POINTS do
    if FText[i].Life > 0 then
      Dec(FText[i].Life);
end;

end.
