unit uGame;
interface
uses
  LexUtils, uHero, uBonus, GSound, zglHeader, uConst, uMaterials;

procedure Init;
procedure Load;
procedure Update;
procedure Draw;
procedure Start(Level : Integer);

var
 Hero  : THero;
 Bonus : array[1..10] of TBonus;
 MyLevel : Integer;
 reQuest : Boolean = False;
 BG : Boolean;
// CamY : Integer;
implementation
uses
  u_Menu, u_SceneM, uQuest, u_Win, u_Player;

procedure Init;
var
  I : Integer;
begin
  Hero := THero.Create;
  Hero.pX := 300;
  Hero.pY := 300;
  Hero.W := 200;
  Hero.H := 64;
  Hero.mY := 1;
  for I := 1 to Length(Bonus) do
    Bonus[I] := TBonus.Create(Hero, 128*I);

  Win := t_Win.Create;
  MyLevel := 1;
  StartQuest(1);
end;

procedure Start(Level : Integer);
begin
  MyLevel := Level;
  reQuest := True;
  Main.SetScene(tg_game, true);
end;

procedure Load;
var
  I : Integer;
begin
  if reQuest then
    StartQuest(MyLevel);
  reQuest := False;
  Hero.Init;
  Quest.LNow := 0;
  Wined := False;
  for I := 1 to Length(Bonus) do
    Bonus[I].Init1(128*I);
  BG := random > 0.5;
end;

procedure Update;
var
  I : Integer;
begin
  if key_Press(K_ESCAPE) then
  begin
    Menu.S := 0;
    Main.SetScene(tg_menu, False, 25);
  end;
  if Wined then
    Win.Work
  else
  begin
    if Hero.Health > 0 then
    Hero.Update
    else
      Hero.pY := Hero.pY + 15;

    for I := 1 to Length(Bonus) do
      Bonus[I].Update;

    if Quest.Update then
      Win.Init(iPlayer.Point);
    if Hero.pY > 2*WinH then
    begin
      Main.SetScene(tg_game, true);
      PlaySound(1);
    end;
  end;
end;

procedure Draw;
var
  I : Integer;
  TexCoord : array[0..3] of zglTPoint2D;
begin
  //pr2d_Rect(0, 0, WinW, WinH, $A0A0A0, $FF, PR2D_FILL);
  if BG then
  ssprite2d_Draw(mBg, 0, 0, WinW, WinH, 0)
  else
  ssprite2d_Draw(mBg2, 0, 0, WinW, WinH, 0);
  Hero.Draw;
  for I := 1 to 10 do
    Bonus[I].Draw;

  pengine2d_Draw();

  TexCoord[0].X := 0;
  TexCoord[0].Y := 1 - Hero.Magic / 256;

  TexCoord[1].X := 1;
  TexCoord[1].Y := 1 - Hero.Magic / 256;

  TexCoord[2].X := 1;
  TexCoord[2].Y := 1;

  TexCoord[3].X := 0;
  TexCoord[3].Y := 1;

  texture2d_Draw(mGUI_magic, TexCoord, 5, 10 + 256 - Hero.Magic, 32, Hero.Magic, 0, 250);
  ssprite2d_Draw(mGUI_bg1, 5, 10, 128, 256, 0);
  asprite2d_Draw(mMagic, 5, 10, 64, 64, 0, 1);

  if Hero.Power > 0 then
  begin
    for I := 0 to Hero.Power - 2 do
      ssprite2d_Draw(mGUI_power, 5 + 64 + 10*I, 10 + 5, 8, 32, 0);
    ssprite2d_Draw(mGUI_power, 5 + 64 + 10*(Hero.Power - 1), 10 + 5, 8, 32, 0, Hero.PowerTime);
  end;

  for I := 0 to Hero.Health - 1 do
    ssprite2d_Draw(mGUI_health, 5 + 31, 10 + 65 + I*28, 32, 32, 0, 225);

  ssprite2d_Draw(mPoint, (WinW - 256) div 2, 0, 256, 64, 0, 150);
  text_Draw(mFont_Ed, WinW div 2, 24,
  AnsiToUtf8(Int2S(iPlayer.Point)),
  TEXT_HALIGN_CENTER or TEXT_VALIGN_CENTER);

  ssprite2d_Draw(mMoney, 0, WinH - 64, 128, 64, 0, 150);
  text_Draw(mFont_Ed, 75, WinH - 28,
  AnsiToUtf8(Int2S(iPlayer.Money)),
  TEXT_HALIGN_CENTER or TEXT_VALIGN_CENTER);

  Quest.Draw;

  if Wined then
    Win.Draw;
end;

end.
