unit CustomBullet;

interface

uses
  QuadEngine, Vec2f, Units.Custom, System.Generics.Collections, QuadFX;

type
  TBulletClass = class of TCustomBullet;

  TCustomBullet = class
  private
    FIsNeedToKill: Boolean;
    FOldPosition: TVec2f;
    FPosition: TVec2f;
    FVector: TVec2f;
    FSpeed: Single;
    FParentUnit: TCustomUnit;
    FDamage: Integer;
  public
    class var BulletList: TObjectList<TCustomBullet>;
    class procedure ProcessList(const Delta: Double);
    class procedure DrawList;

    constructor Create(AParentUnit: TCustomUnit; APosition, AVector: TVec2f);
    destructor Destroy; override;
    procedure Process(const Delta: Double); virtual;
    procedure Draw; virtual;
    procedure Coll(APoint, AVector: TVec2f); virtual;

    property Position: TVec2f read FPosition;
    property OldPosition: TVec2f read FOldPosition;
    property Vector: TVec2f read FVector;
    property Speed: Single read FSpeed;
    property IsNeedToKill: Boolean read FIsNeedToKill;
    property ParentUnit: TCustomUnit read FParentUnit;
    property Damage: Integer read FDamage;
  end;

implementation

uses
  Resources, Level.Map, Collision;

constructor TCustomBullet.Create(AParentUnit: TCustomUnit; APosition, AVector: TVec2f);
begin
  FIsNeedToKill := False;
  FSpeed := 1000;
  FVector := AVector;
  FOldPosition := APosition;
  FPosition := APosition + FVector.Normalize * 100;
  FParentUnit := AParentUnit;
  BulletList.Add(Self);
  FDamage := 35;
end;

destructor TCustomBullet.Destroy;
begin
  inherited;
end;

procedure TCustomBullet.Coll(APoint, AVector: TVec2f);
var
  Effect: IQuadFXEffect;
begin
  FIsNeedToKill := True;
  TEffects.QuadFXLayer.CreateEffect(TEffects.BulletShot, APoint, Effect);
end;

procedure TCustomBullet.Process(const Delta: Double);
var
  Range: TMapRange;
  X, Y: Integer;
begin
  FOldPosition := FPosition;
  FPosition := FPosition + FVector.Normalize * FSpeed * Delta;
 { Range := TGlobals.GameModes.Planet.LevelMap.GetMapRange(FPosition, OldPosition);
  for Y := Range.A.Y to Range.B.Y do
    for X := Range.A.X to Range.B.Y do
   }

end;

procedure TCustomBullet.Draw;
begin
 // TGlobals.QuadRender.DrawQuadLine(Position - FVector.Normalize * 100, Position, 1, 1, $00FFFFFF, $CCFFFFFF);
end;

class procedure TCustomBullet.ProcessList(const Delta: Double);
var
  i: Integer;
begin
  for i := BulletList.Count - 1 downto 0 do
    if BulletList[i].IsNeedToKill then
      BulletList.Delete(i)
    else
      BulletList[i].Process(Delta);
end;

class procedure TCustomBullet.DrawList;
var
  i: Integer;
begin
  for i := 0 to BulletList.Count - 1 do
    BulletList[i].Draw;
end;

initialization
  TCustomBullet.BulletList := TObjectList<TCustomBullet>.Create;

finalization
  if Assigned(TCustomBullet.BulletList) then
    TCustomBullet.BulletList.Free;

end.
