unit UPlayer;

interface

uses
  Squall,
  JEN_Header,
  JEN_Math,
  UWeapon;


type
  TBonusType = (btNone, btHeals, btPowerUp, btUtimate);
  TPlayer = class
  constructor Create;
  destructor Destroy; override;
  private
    FSample         : LongInt;
    FTex            : ITexture;
    FShieldTex      : ITexture;
    FShieldStart    : LongInt;
    FDieTime        : LongInt;
    FSpeed          : Single;
    FPos            : TVec2f;
    FWeapon         : TWeapon;
  public
    RestartCount    : LongInt;
    UltimateCount   : LongInt;
  const
    Size : Single = 64;
    function Pos : TVec2f;
    procedure Coll;
    procedure Hit(Weapon: TWeaponType);
    procedure GetBonus(Bonus: TBonusType);
    procedure OnUpdate(dt: LongInt);
    procedure OnRender;
    procedure Restart;
end;

implementation

uses
  UGameScene,
  Main;

constructor TPlayer.Create;
begin
  FSample := SQUALL_Sample_LoadFile('Media\explosion2.wav', 1, 0);
  ResMan.Load('Media\Ship.dds', FTex);
  ResMan.Load('Media\Shield.dds', FShieldTex);
  Restart;
end;

destructor TPlayer.Destroy;
begin
  SQUALL_Sample_Unload(FSample);
end;

procedure TPlayer.Restart;
begin
  RestartCount := 3;
  FShieldStart := -1;
  FDieTime     := -1;
  UltimateCount := 1;
 FWeapon.SetWeapon(wtLaser1);

  FPos.y := Game.ScreenHeight -Size/2;
  FPos.x := WorldSize / 2 ;
  FSpeed := 0.3;
end;

function TPlayer.Pos : TVec2f;
begin
  Result := FPos;
end;

procedure TPlayer.Coll;
begin
  if FShieldStart <> -1 then
    Exit;

  SQUALL_Channel_SetVolume(SQUALL_Sample_Play(FSample, 0, 0, 1), 100);
  Dec(RestartCount);
  if RestartCount = -1 then
  begin
    FDieTime := Helpers.Time;
  //  Game.Fail;
    Exit;
  end;
  FShieldStart := Helpers.Time;
  FWeapon.SetWeapon(wtLaser1);
end;

procedure TPlayer.Hit(Weapon: TWeaponType);
begin
  Coll;
end;

procedure TPlayer.GetBonus(Bonus: TBonusType);
begin
  case Bonus of
  btHeals : if RestartCount < 3 then Inc(RestartCount);
  btPowerUp : FWeapon.Upgrade;
  btUtimate : if UltimateCount < 3 then inc(UltimateCount);
  end;
end;

procedure TPlayer.OnUpdate(dt: LongInt);
begin

  FWeapon.Shoot(FPos,Vec2f(0,0) ,Vec2f(0,0));

  if (FShieldStart <> -1) and (Helpers.Time - FShieldStart > 3000) then
    FShieldStart := -1;

  FPos := FPos + Vec2f(Input.Mouse.Delta.x, Input.Mouse.Delta.y);
  FPos := FPos.Max(Vec2f(Size/2, 400)).Min(Vec2f(WorldSize - Size/2, Game.ScreenHeight -Size/2 - 80));
end;

procedure TPlayer.OnRender;
var
  i : Integer;
  Alpha : Single;
begin
  Render.BlendType := btAdd;
  if (FShieldStart <> -1) then
    Render2d.DrawSprite(FShieldTex.Frame, Pos.x -  64 + Game.OffSet.x, Pos.Y - 64  + Game.OffSet.y, 128, 128,  Vec4f(0.2, 0.3, 0.5, 0.3 + abs(Cos(Max(Helpers.Time - FShieldStart - 1500, 0.0)*0.005))) );

  Render.BlendType := btNormal;
  Alpha := 1;
  if FDieTime <> -1 then
    Alpha := Max(1000-(Helpers.Time - FDieTime), 0)/1000;
  Render2d.DrawSprite(FTex.Frame, Pos.x - Size/2  + Game.OffSet.x, Pos.y - Size/2  + Game.OffSet.y, Size, Size, Vec4f(1, 1, 1, Alpha));
end;


end.
