unit UPlayer;

interface

uses
  JEN_Header,
  JEN_Math,
  UWeapon;


type
  TBonusType = (btNone, btHeals, btPowerUp, btUtimate);
  TPlayer = class
  constructor Create;
  private
    FTex            : ITexture;
    FShieldTex      : ITexture;
    FShieldStart    : LongInt;
    FDieTime        : LongInt;
    FSpeed          : Single;
    FPos            : TVec2f;
    FWeapon         : TWeapon;
  public
    Health          : Single;
    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
  UGame,
  UMain;

constructor TPlayer.Create;
begin
  ResMan.Load('Media\Ship.dds', FTex);
  ResMan.Load('Media\Shield.dds', FShieldTex);
  Restart;
end;

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

  FPos.y := Screen_Height -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;

  Dec(RestartCount);
  if RestartCount = -1 then
  begin
    FDieTime := Utils.Time;
    Game.Fail;
    Exit;
  end;

  Health  := 100;
  FShieldStart := Utils.Time;
  FWeapon.SetWeapon(wtLaser1);
end;

procedure TPlayer.Hit(Weapon: TWeaponType);
begin
  if FShieldStart <> -1 then
    Exit;

  Health := Health - WeaponParam[Weapon].Power;
  if Health <= 0 then
    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);
var
  Dir : TVec2f;
begin
  Dir := Vec2f(0, 0);
  if Input.Down[ikA] then
    Dir.X := Dir.X-1;

  if Input.Down[ikD] then
    Dir.X := Dir.X+1;

  if Input.Down[ikW] then
    Dir.Y := Dir.Y-1;

  if Input.Down[ikS] then
    Dir.Y := Dir.Y+1;

  if Input.Hit[ikE] then
  if UltimateCount > 0 then
  begin
    Game.Boom;
    Dec(UltimateCount);
  end;

  if Input.Down[ikSpace] then FWeapon.Shoot(FPos,Vec2f(0,0) ,Vec2f(0,0));

  if (FShieldStart <> -1) and (Utils.Time - FShieldStart > 5000) then
    FShieldStart := -1;

  FPos := FPos + Dir.Normal * FSpeed * dt;
  FPos := FPos.Max(Vec2f(Size/2, 400)).Min(Vec2f(WorldSize - Size/2, Screen_Height -Size/2));
end;

procedure TPlayer.OnRender;
var
  i : Integer;
  Alpha : Single;
begin
  if (FShieldStart <> -1) then
    Render2d.DrawSprite(FShieldTex, Pos.x - Game.Camera.x - 64, Pos.Y - 64, 128, 128,  Vec4f(0.2, 0.3, 0.5, 0.3 + abs(Cos(Max(Utils.Time - FShieldStart - 3500, 0.0)*0.005))) );

  Alpha := 1;
  if FDieTime <> -1 then
    Alpha := Max(1000-(Utils.Time - FDieTime), 0)/1000;
  Render2d.DrawSprite(FTex, Pos.x - Game.Camera.x - Size/2, Pos.y - Size/2, Size, Size, Vec4f(0.25, 0.3, 0.7, Alpha));
end;


end.
