unit UShopScene;

interface
  {
uses
  SysUtils,
  JEN_Header,
  JEN_Math,
  Main,
  USprite,
  UDirector;

type
  TPerkButton = class
    constructor Create(tex: ITexture;const pos: TVec2f; kind: TPerk);
    destructor Destroy; override;
    public
      kind   : TPerk;
      Sprite : TSprite;
      Rect   : TRecti;

      procedure Update(dt: LongInt);
      procedure Draw;
  end;

  TShopScene = class(TScene)
  public
    GuiTex : ITexture;
    BackGroundTex : ITexture;
    GoButton: TSprite;
    Button : array[TPerk] of TPerkButton;
    function GetName: String; override;
    procedure Load; override;
    procedure UnLoad; override;
    procedure Update(dt: LongInt); override;
    procedure Draw; override;
  end;
   }
implementation
     {
const
  PerkName : array[TPerk] of string = ('Heal', 'Power', 'Aglity');
  PerkPrice: array[0..2] of Integer = (1000, 2500, 5000);

function TShopScene.GetName: String;
begin
  Result := 'Shop';
end;

procedure TShopScene.Load;
var
  perk : TPerk;
  GoButtonTex : ITexture;
begin
  ResMan.Load('Media\skills.dds', GuiTex);
  ResMan.Load('Media\guiBackGround.dds', BackGroundTex);
  ResMan.Load('Media\goButton.dds', GoButtonTex);
  GoButtonTex.Filter := tfiNone;
  GoButton := TSprite.Create(GoButtonTex);
  GoButton.AnchorPoint := Vec2f(1, 1);
  GoButton.Scale := 2;
  GoButton.Pos := Vec2f(Display.Width, Display.Height) - Vec2f(50, 50);
  GuiTex.Filter := tfiNone;
       {
//  BackGround.Filter := tfiNone;
  for perk := Low(TPerk) to High(TPerk) do
    Button[perk] := TPerkButton.Create(GuiTex[ord(perk)], Vec2f(Display.Width / (ord(High(TPerk)) + 2) * (ord(perk) +1), Display.Height * 0.5), perk);
   } {
end;

procedure TShopScene.UnLoad;
var
  perk : TPerk;
begin
  GuiTex := nil;
  BackGroundTex := nil;
  GoButton.Free;
  for perk := Low(TPerk) to High(TPerk) do
    Button[perk].Free;
end;

procedure TShopScene.Update;
var
  perk : TPerk;
begin
  for perk := Low(TPerk) to High(TPerk) do
    Button[perk].Update(dt);

  if Input.Hit[ikMouseL] and GoButton.GetRect.Contains(Input.Mouse.Pos) then
    Game.SceneDirector.LoadScene('Game');
end;

procedure TShopScene.Draw;
var
  perk : TPerk;
  str  : string;
  width : Single;
begin
  Game.Camera.Enable := false;
  Render2d.DrawSprite(BackGroundTex, 0, 0, Display.Width, Display.Height, clWhite);
  for perk := Low(TPerk) to High(TPerk) do
    Button[perk].Draw;
  GoButton.Draw;

  Game.Font.Scale := 0.2;
  str := 'Cash:' + IntToStr(Game.Cash);
  width := Game.Font.GetTextWidth(PWideChar(str));
  Game.Font.Print(PWideChar(str), (Display.Width - width)/2, 0);
end;

constructor TPerkButton.Create(tex: ITexture; const pos: TVec2f; kind: TPerk);
begin
  Sprite := TSprite.Create(tex);
  Sprite.Pos := pos;
  Sprite.AnchorPoint := Vec2f(0.5, 0.5);
  Sprite.Scale := 2;
  self.kind := kind;
  Self.Rect := Sprite.GetRect;
end;

destructor TPerkButton.Destroy;
begin
  Sprite.Destroy;
end;

procedure TPerkButton.Update(dt: LongInt);
begin
  if Input.Hit[ikMouseL] and Rect.Contains(Input.Mouse.Pos) and (Game.Cash > PerkPrice[Game.perk[kind]]) and (Game.perk[kind] < 2) then
  begin
    Game.Cash := Game.Cash - PerkPrice[Game.perk[kind]];
    inc(Game.perk[kind]);
  end;

end;

procedure TPerkButton.Draw;
var
  width : Single;
  cost  : String;

begin
  Sprite.Draw;
  Game.Font.Scale := 0.1;
  Game.Font.SetGradColors(Vec4f(1, 0.7, 0.5, 1), Vec4f(1, 0.3, 0.1, 1));
  Game.Font.OutlineColor :=  Vec3f(0.7, 0.1, 0.0);
  Game.Font.OutlineSize := 15;

  width := Game.Font.GetTextWidth(PWideChar(PerkName[kind]));
  Game.Font.Print(PWideChar(PerkName[kind]), Sprite.pos.x - width/2, Sprite.pos.y + Sprite.Height);
  cost := IntToStr(PerkPrice[Game.perk[kind]]) +'$';
  width := Game.Font.GetTextWidth(PWideChar(cost));

  Game.Font.Print(PWideChar(cost), Sprite.pos.x - width/2, Sprite.pos.y + Sprite.Height + 40);
end;
        }
end.
