unit uGame;

{$mode delphi}

interface

uses
  Classes, SysUtils, uScene, zglHeader, uMap, uFGame, uMeForm;
type

  { TEditor }

  { TGame }

  TGame = class(tScene)
    Map : TMap;
    GameLevel,
    mouse_cX, mouse_cY,
    mouse_dX, mouse_dY : Integer;
    WinForm,
    Form : TMeForm;

    FGame : TFGame;
    NeedPortal : Boolean;
    LiveObjTimer : Integer;
    constructor Create(const PSceneMng : tSceneMng); override;
    procedure SetParam(Param : Integer); override;
    procedure Draw; override;
    procedure Update; override;
  end;
implementation
uses
  uGlobal, uLiveObj, uHero, ufgame_m3, uGUI, uScreen;

{ TGame }

constructor TGame.Create(const PSceneMng: tSceneMng);
begin
  SceneMng := PSceneMng;

  FGame := TFGame_m3.Create();
//  FGame.Start(8, 8);
  SetParam(1);
end;

procedure TGame.SetParam(Param: Integer);
var
  EC, maxEC : Integer;
  I, X, Y, hX, hY : Integer;
begin
  if Param > 0 then
  begin
    GameLevel := Param;
    Map.Free;

    Map := TMap.Create(8 + GameLevel * 2, 8 + GameLevel * 2, WinW, WinH);


    if Map.GetRandPos(hX, hY) then
    begin
      for X := hX - 1 to hX + 1 do
      for Y := hY - 1 to hY + 1 do
      if (X >= 0) and (X <= Map.W) and (Y >= 0) and (Y <= Map.H) then
      begin
        Map.TileA[X][Y].Free:= false;
        Map.TileA[X][Y].Sprite := cPortal - 1;
        Map.TileA[X][Y].FH := true;
      end;
    end;

    maxEC := Map.GetFreeCount;
    EC := 2 + Param;
    if EC > maxEC then
      EC := maxEC;

    for I := 1 to EC - 1 do
    if Map.GetRandPos(X, Y) then
      TLiveObj.Create(X, Y, Map, Param, true);

    if Map.GetRandPos(X, Y) then
      TLiveObj.Create(X, Y, Map, Param + 2, false);
    NeedPortal := true;

    for X := hX - 1 to hX + 1 do
    for Y := hY - 1 to hY + 1 do
    if (X >= 0) and (X <= Map.W) and (Y >= 0) and (Y <= Map.H) then
      Map.TileA[X][Y].Free := true;

    Map.hX := hX;
    Map.hY := hY;


    Map.Hero := THero.Create(hX, hY, Map, -1, false);

    FGame.Disable:=true;
    FGame.Alpha:=0;
  end;
end;

procedure TGame.Draw;
{var
  I, vX, vY : Integer;}
begin
  pr2d_Rect(0, 0, WinW, WinH, $A0A0A0, $FF, PR2D_FILL);

  Map.Draw;

 { for I := Map.Obj.Count - 1 downto 0 do
    TLiveObj(Map.Obj[I]).Draw;    }

  gui_draw;

  if (FGame <> nil) and (FGame.Alpha > 5) then
    FGame.Draw;


end;

procedure TGame.Update;
var
  Win : Boolean;
  X, Y : Integer;
begin
  if (FGame <> nil) and (FGame.Alpha > 5) then
  begin
    FGame.Update(mouse_X, mouse_Y);
    if FGame.Disable then
    begin

      if (FGame.GWin) then
      begin
        if Map.EnemyDynObj <> nil then
        begin
          Map.EnemyDynObj.Live := 0;
          Map.EnemyDynObj := nil;
        end;
       { h_live := h_max_live;
        h_mana := h_max_mana; }

        if hm_exp >= h_max_exp then
        begin
          h_max_exp := h_max_exp + 2;
          case random(4) of
            0 : h_max_live := h_max_live + 1;
            1 : h_max_mana := h_max_mana + 1;
            2 : h_max_sh := h_max_sh + 1;
          end;
          h_live := h_max_live;
          h_mana := h_max_mana;
          h_sh := h_max_sh;
        end;
      end
      else
      begin
        TLiveObj(Map.Hero).SetTo(Map.hX, Map.hY);
        h_live := 1;
        h_mana := 1;
      end;
    end;
  end
  else
  begin
    Map.MouseMove(mouse_X, mouse_Y, 1, X, Y);
    Map.Update;

    if Map.Fight and (Map.EnemyDynObj <> nil) then
    begin
      FGame.Start(8, 8);
      e_heart := 0;
      e_live := Map.EnemyDynObj.Live;
      e_max_live  := e_live;
      e_max_heart := Map.EnemyDynObj.Heart;

      show_e := true;

      em_heart := e_heart;
      em_live := e_live;

      h_exp := 0;
      hm_live  := h_live;
      hm_sh    := h_sh;
      hm_mana  := h_mana;
      hm_exp   := h_exp;
      hm_sword := h_sword;
    end;

    if key_Down(K_W) then Map.camY := Map.camY - 1;
    if key_Down(K_S) then Map.camY := Map.camY + 1;
    if key_Down(K_A) then Map.camX := Map.camX - 1;
    if key_Down(K_D) then Map.camX := Map.camX + 1;

  end;

  if (NeedPortal) and (Map.Obj.Count = 1) and Map.GetRandPos(X, Y) then
  begin
    NeedPortal := false;
    Map.TileA[X][Y].Sprite := cPortal;
  end;

  if THero(Map.Hero).OnPortal then
  begin
    if GameLevel < 6 then
      SceneMng.ChangeScenePar(GameLevel + 1, 0)
    else
      SceneMng.ChangeScene(Screen2, 0);
  end;


  if LiveObjTimer > 50 then
  begin
    LiveObjTimer := 0;
    if h_mana < h_max_mana then
      h_mana := h_mana + 1;
    if h_live < h_max_live then
      h_live := h_live + 1;
  end
  else
    LiveObjTimer := LiveObjTimer + 1;



end;



end.

