unit uGun;

interface
uses
  zglHeader, uMaterials, uList, LexUtils,
  uMap, uBullet;

type
  TGunList = class(TList)
    Map : TMap;
    Bullets : TBulletList;
    constructor Create(const pMap : TMap; const pBullets : TBulletList); overload;
    function  Add(E : TGunList) : TList; overload;
  end;

  TGun = class(TGunList)
    pX, pY, cX, cY, W, H,
    goalX, goalY : Integer;
    gunAngle, goalAngle : Single;

    fiTimer : Integer;

    Fire : Boolean;
    constructor Create;
    procedure Init; override;
    procedure Draw; override;
    procedure Update; override;
    procedure MouseWork(X, Y : Integer);
  end;
implementation

{ TGunList }

function TGunList.Add(E: TGunList): TList;
begin
  E.Map := (Root as TGunList).Map;
  E.Bullets := (Root as TGunList).Bullets;
  Result := inherited Add(E);
end;

constructor TGunList.Create(const pMap: TMap; const pBullets: TBulletList);
begin
  inherited Create;
  Map := pMap;
  Bullets := pBullets;
end;

{ TGun }

constructor TGun.Create;
begin
  W := 128;
  H := 64;
end;

procedure TGun.Init;
begin
  pX := Map.WinW div 2 - W div 2;
  pY := Map.sY - H;
  cX := Map.WinW div 2;
  cY := Map.sY - H div 2;
end;

procedure TGun.Draw;
begin
//  pr2d_Rect(pX, pY, W, H, $FFFFFF, 255, PR2D_FILL);
  ssprite2d_Draw(m_gun1, pX, pY, W, H, 0);
  ssprite2d_Draw(m_gun1_d, pX, pY + H / 4, W, H / 2, gunAngle);
//  pr2d_Rect(goalX - 32, goalY - 32, 64, 64, $00FF00, 200, PR2D_FILL);
  ssprite2d_Draw(m_mgoal,goalX - 32, goalY - 32, 64, 64, 0);
end;

procedure TGun.MouseWork(X, Y: Integer);
begin
  Fire := true;
  goalX := X;
  goalY := Y;
  goalAngle := asmroll(cX - X, cY - Y) * D180P;
  if (goalAngle < 0) and (goalAngle > -90) then
    goalAngle := 0
  else
  if (goalAngle > 180) or (goalAngle <= -90) then
    goalAngle := 180
end;

procedure TGun.Update;
begin
  if fiTimer > 0 then
    fiTimer := fiTimer - 1
  else
  if roll(gunAngle, goalAngle, 4) then
  if Fire then
  begin
    Bullets.Add(TBullet.Create(trunc(gunAngle), cX, cY, goalX, goalY));
    fiTimer := 25;
    Fire := false;
  end;
end;

end.
