unit g_Game;

interface
uses
  g_Data,g_Types,g_Land,g_Monster_Load,
  {$INCLUDE uses.inc};

procedure Game_Start(self : zGame);
procedure Check_for_complte(self : zGame);
procedure Game_NextLevel(self : zGame);
procedure Game_Over(self : zGame);

procedure Add_Money_and_Mobs(self : zGame);
implementation

procedure Game_Start(self : zGame);
begin
  self^.MapName := 'map\1.map';
  LandInit(self^.MapName);
  Game.Level := 0;
  self^.ChangeLevelDelay := 10000;
  self^.LTC := 0;
  self^.NextLevel := false;
  self^.State := gsGame;
  self^.Money := 120;
  self^.Mobs := 0;
  self^.numLevel := World.MonsterTypeCount*3;

  World.MaxMonster := 100;
  World.MonsterCreateLTC := GetTickCount;
  World.CharacterCount := World.MonsterTypeCount;
  World.CharacterActiveCount := World.MonsterTypeCount;
  World.EndCharAC := World.CharacterCount;
  SetLength(World.Characters,World.MonsterTypeCount);
  SetLength(World.Monsters,World.MonsterTypeCount);

  World.TowerCount := World.TowerTypeCount;
  SetLength(World.Tower,World.TowerCount);
  World.ActiveTower := 1;
end;

procedure Add_Money_and_Mobs(self : zGame);
begin
  self^.Money := self^.Money+1;
  inc(self^.Mobs);
end;

procedure Check_for_complte(self : zGame);
var
  i : word;
  s : int;
begin
  if World.Base.Life <= 0 then Game_Over(self);
  if self^.NextLevel then Game_NextLevel(self) else begin
    s := 0;
    i := World.MonsterTypeCount;
    while i<World.CharacterCount do begin
      if World.Characters[i].State = csDead then inc(s);
      inc(i);
    end;
    if s = World.CharacterCount - World.MonsterTypeCount then
    begin
      self^.LTC := GetTickCount;
      self^.NextLevel := true;
    end;
  end;
end;

procedure Game_NextLevel(self : zGame);
begin
  if self^.Level = self^.numLevel then Game_Over(self) else begin
    self^.TTC := GetTickCount;
    if self^.TTC-self^.LTC > self^.ChangeLevelDelay then begin
      Monster_Add;
      inc(self^.Level);
      self^.NextLevel := false;
      self^.LTC := self.TTC;
    end;
  end;
end;

procedure Game_Over(self : zGame);
begin
  self^.Finish := true;
end;

end.
