unit Bonuses;

interface

uses
  JEN_Header,
  JEN_Math;

const
  BonusesCount = 10;

type
  TBonuseKind = (bkNone, bkSubmachineGun, bkShotgun, bkHeals, bkBurst, bkLast);
  TBonuse = record
    Pos : TVec3f;
    Kind : TBonuseKind;
    CreateTime : LongInt;
  end;

  TBonuseManager = class
  constructor Create;
  private
    type TVrtxFormat = record
      Pos  : TVec2f;
    end;
  var
    FShader : IShaderProgram;
    FPosAttrib : IShaderAttrib;
    FMatrixUniform : IShaderUniform;
    FAlphaSizeUniform : IShaderUniform;
    FGlobalPosUniform : IShaderUniform;
    FVPosUniform : IShaderUniform;
    FVrtBuff : IGeomBuffer;
    FBonuseTex : array[TBonuseKind] of ITexture;
    FBonuse : array[1..BonusesCount] of TBonuse;
  public
    procedure Add(Pos: TVec3f);
    procedure OnUpdate(dt: LongInt);
    procedure OnDraw;
  end;

implementation

uses
  Player,
  Main;

constructor TBonuseManager.Create;
var
  I : Integer;
  Vertex : array[1..4] of TVrtxFormat;
begin
  Vertex[1].Pos := vec2f(-1, -1);
  Vertex[2].Pos := vec2f(1, -1);
  Vertex[3].Pos := vec2f(1, 1);
  Vertex[4].Pos := vec2f(-1, 1);

  FVrtBuff := Render.CreateGeomBuffer(gbVertex, 4, sizeof(TVrtxFormat), @Vertex[1]);

  FShader := ResMan.LoadShader('Media\Billboard.xml').Compile;
  FMatrixUniform := FShader.Uniform('Jen_ModelViewProjectionMatrix', utMat4);
  FAlphaSizeUniform := FShader.Uniform('AlphaSize', utVec2);
  FGlobalPosUniform := FShader.Uniform('GlobalPos', utVec3);
  FVPosUniform := FShader.Uniform('Jen_ViewPos', utVec3);
  FPosAttrib := FShader.Attrib('PosAttr', atVec2f, False);

  i:=1;
  FShader.Uniform('DiffMap', utInt).Value(i);
  FBonuseTex[bkSubmachineGun] := ResMan.LoadTexture('Media\BonusSubmachine.dds');
  FBonuseTex[bkShotgun] := ResMan.LoadTexture('Media\BonusShotgun.dds');
  FBonuseTex[bkHeals] := ResMan.LoadTexture('Media\BonusHeals.dds');
  FBonuseTex[bkBurst] := ResMan.LoadTexture('Media\BonusBurst.dds');
end;

procedure TBonuseManager.Add(Pos: TVec3f);
var
  I, J: Integer;
begin

  for I := 1 to BonusesCount do
    if FBonuse[i].Kind = bkNone then
    begin
      FBonuse[i].CreateTime := Utils.Time;
      FBonuse[i].Kind := TBonuseKind(1 + Random(Ord(bkBurst)-1) );
      FBonuse[i].Pos := Pos + vec3f(0,1,0);
      Exit;
    end;

end;

procedure TBonuseManager.OnUpdate(dt: LongInt);
var
  I : Integer;
begin

  for I := 1 to BonusesCount do
    if FBonuse[i].Kind <> bkNone then
    begin

      if (Game.Player.GetPos - FBonuse[i].Pos).Length < 4 then
      begin
        case FBonuse[i].Kind of
          bkHeals : Game.Player.Heals := Min(Game.Player.Heals + 25, 100);
          bkShotgun : begin Game.Player.Weapon.Free; Game.Player.Weapon := TShotgun.Create; end;
          bkSubmachineGun :begin Game.Player.Weapon.Free; Game.Player.Weapon := TSmg.Create; end;
        end;

        FBonuse[i].Kind := bkNone;
      end;


      if (Utils.Time - FBonuse[i].CreateTime> 15000) then
        FBonuse[i].Kind := bkNone;

    end;
end;

procedure TBonuseManager.OnDraw;
var
  m : TMat4f;
  p : TVec3f;
  s : TVec2f;
  I : Integer;
begin
  m := Render.Matrix[mtProj] * Render.Matrix[mtView];
  p := Render.CameraPos;

  Render.DepthWrite := False;
  Render.DepthTest := True;
  Render.BlendType := btAdd;

 // FTex.Bind;
  FShader.Bind;
  FMatrixUniform.Value(m);
  FVPosUniform.Value(p);

  FVrtBuff.Bind;
  FPosAttrib.Value(sizeof(TVrtxFormat),0);
  FPosAttrib.Enable;

  for I := 1 to BonusesCount do
    if FBonuse[i].Kind <> bkNone then
    begin
      s := vec2f( min((Utils.Time - FBonuse[i].CreateTime)/1000 ,1.0) + min((14000 - (Utils.Time - FBonuse[i].CreateTime))/1000,0) ,3+sin(Utils.Time/1000)/3);
      FAlphaSizeUniform.Value(s);
      FBonuseTex[FBonuse[i].Kind].Bind;
      FGlobalPosUniform.Value(FBonuse[i].Pos);
       FVrtBuff.Draw(gmQuads, 4, false);
    end;

end;


end.
