unit g_GUI;

interface
uses
  g_Data, g_types, g_Camera,g_Monster_Load,g_Monster,g_Tower,g_MiniMap,g_Game,g_Utils,
  GPAHeader,
  {$INCLUDE uses.inc};

procedure GUI_Create();
procedure GUI_Menu_Update(time : float);
procedure GUI_Game_Update(time : float);
procedure GUI_Game_Draw();
procedure GUI_Menu_Draw();

var
 GPA : longint;

implementation

procedure GUI_Create();
begin
  game.GUI := zGUI.Create();
  Game.GUI.Load(iz_dir_local_sys+'gui.txt');

  GPAExtractToFileByID(GPAGetIDByName('font.tga',true), 'temp\graphics\font.tga');
  game_font := gfx.FontMan.Load(iz_dir_local_temp_gfx + 'font.tga');

  Init_MiniMap(@MiniMap);
end;

procedure GUI_Menu_Update(time : float);
var
  pos : izPosition_f;
begin
  pos[0] := 0;
  pos[1] := 0;
  if gfx.Input.Keys[Options.InputOptions.kEscape] then gfx.Quit := true;
  Cur.Pos[0] := gfx.Input.X;
  Cur.Pos[1] := gfx.Input.Y;
  Cur.GamePos[0] := Cur.Pos[0]-Camera.pos[0];
  Cur.GamePos[1] := Cur.Pos[1]-Camera.pos[1];

  if gfx.Input.Keys[13] then begin
    Game_Start(@Game);
    gfx.Input.Keys[13] := false;
  end;
  Game.GUI.Update(GUI_MENU);
end;

procedure GUI_Game_Update(time : float);
var
  pos : izPosition_f;
  i : byte;
begin
  pos[0] := 0;
  pos[1] := 0;
  Cur.Pos[0] := gfx.Input.X;
  Cur.Pos[1] := gfx.Input.Y;
  Cur.GamePos[0] := Cur.Pos[0]-Camera.pos[0];
  Cur.GamePos[1] := Cur.Pos[1]-Camera.pos[1];

  if gfx.Input.Keys[Options.InputOptions.kEscape] then begin
     Game.State := gsMenu;
     gfx.Input.Keys[Options.InputOptions.kEscape] := false;
  end;

  if gfx.Input.Keys[37] then pos[0] := 4 else
  if gfx.Input.Keys[39] then pos[0] := -4;
  if gfx.Input.Keys[38] then pos[1] := 4 else
  if gfx.Input.Keys[40] then pos[1] := -4;

  i := 0;
  while i < Game.GUI.PictureCount do begin
    if ((Game.GUI.Picture[i].Name = 'GMENU')and(Game.GUI.Picture[i].OnClick)) then begin
      Game.GUI.Picture[World.ActiveTower].Visible := false;
      inc(World.ActiveTower);
      if World.ActiveTower > 4 then World.ActiveTower := 1;
      Game.GUI.Picture[World.ActiveTower].Visible := true;
    end;
    inc(i);
  end;

  if gfx.Input.mKeys[0] then begin
    Tower_Add(@World.Tower[World.ActiveTower-1],Cur.GamePos);
    gfx.Input.mKeys[0] := false;
  end;

  Camera_Update(@Camera,pos,time);
  Game.GUI.Update(GUI_GAME);
end;

procedure GUI_Menu_Draw();
var
  pos : izPosition_i;
begin
  Game_text.pos[0] := 10;
  Game_text.pos[1] := 30;
  Gfx.FontMan.Write(game_font,Game_text.pos,gfx.FPSstr);

  Game_text.pos[0] := Options.ScreenWidth div 2 - 250;
  Game_text.pos[1] := Options.ScreenHeight div 2;
  Gfx.FontMan.Write(game_font,Game_text.pos,'Нажмите ' + '"ENTER"' + ' для начала Новой игры');

  Game.GUI.Draw(GUI_MENU);
end;

procedure GUI_Game_Draw();
var
  pos : izPosition_i;
  posi : izPosition_i;
  i:byte;
begin
  Draw_MiniMap(@MiniMap);

  pos[0] := 10-round(Camera.pos[0]);
  pos[1] := 90-round(Camera.pos[1]);
  Gfx.FontMan.Write(game_font,pos,'Уровень:'+inttostr(Game.Level));

  pos[0] := 10-round(Camera.pos[0]);
  pos[1] := 110-round(Camera.pos[1]);
  Gfx.FontMan.Write(game_font,pos,'Капитал:' + inttostr(Game.Money));

  pos[0] := 10-round(Camera.pos[0]);
  pos[1] := 130-round(Camera.pos[1]);
  Gfx.FontMan.Write(game_font,pos,'Убито монстров:' + inttostr(Game.Mobs));

  Game_text.pos[0] := 10-round(Camera.pos[0]);
  Game_text.pos[1] := 30-round(Camera.pos[1]);
  Gfx.FontMan.Write(game_font,Game_text.pos,gfx.FPSstr);

  if Game.Finish then begin
    pos[0] := ScrCenterX-100-round(Camera.pos[0]);
    pos[1] := ScrCenterY-round(Camera.pos[1]);
    Gfx.FontMan.Write(Game_Font,pos,'Игра окончена');
  end;
  Game.GUI.Draw(GUI_GAME);

  pos[0] := Round(Game.GUI.Picture[6].pos[0])-round(Camera.pos[0])-60;
  pos[1] := Round(Game.GUI.Picture[6].pos[1])-round(Camera.pos[1])-60;
  Gfx.FontMan.Write(game_font,pos,'Цена:' + inttostr(World.Tower[World.ActiveTower-1].price));
end;

end.
