unit main;

interface

uses
  AI,
  D3D,
  MP,
  menu,
  perso,
  Gameplay,
  Environment,
  map,
  sound,
  skybox,
  SysUtils,
  System.Drawing,
  System.Windows.Forms,
  System.Threading,
  Microsoft.DirectX,
  Microsoft.DirectX.DirectInput,
  Microsoft.DirectX.Direct3D;

type
  TGame = class
    private
      map : TMap;
      menu : TMenu;
      cube : TPerso;
      Player1 : TGameP;
      MG : TMG;
      MP : TMP;
      AI : TAI;
      Playsound : TSound;
      skybox:Tskybox;
      environmentG:TEnvironmentG;
      itime : cardinal;
      win : boolean;
      life : integer;
    public
      constructor create;
  end;

implementation

constructor TGame.create;
  begin
    inherited;
    MG := TMG.create;
    MP := TMP.create;
    cube := TPerso.create;
    Player1 := TGameP.Create;
    map := TMap.create;
    AI := TAI.create;
    skybox:= Tskybox.create;
    menu := TMenu.create;
    PlaySound := TSound.Create;
    EnvironmentG := TEnvironmentG.create;
    MG.Initialize;
    //cube.initialize(MG.FDevice);
    PlaySound.LoadAll;
    cube.pos.x := 2;
    cube.pos.y := 0;
    cube.pos.z := 21;
    cube.pos.angle := 1.57;
    cube.initialize(MG.Fdevice,'tiny.x');
    //sol.initialize(MG.FDevice);
    map.initialize('map_65x50.txt');
    Map.Triangle;
    map.OnCreateDevice(MG.FDevice,nil);
    Map.OnResetDevice(MG.FDevice, nil);
    skybox.OnCreateDevice(MG.FDevice,nil);
    skybox.OnResetDevice(MG.FDevice, nil);
    //Player1.initilize;
    Player1.keyboard := Microsoft.DirectX.DirectInput.Device.Create(SystemGuid.keyboard);
    Player1.keyboard.SetCooperativeLevel(self.MG, (CooperativeLevelFlags.Background
                                         or CooperativeLevelFlags.NonExclusive));
    Player1.keyboard.SetDataFormat(DeviceDataFormat.Keyboard);
    Player1.keyboard.Acquire;
    AI.initialize(map.Vecteur,map.tailleX,map.tailleZ,MG.FDevice);
    menu.initialize(MG.FDevice);
    environmentG.initialize(map.TailleX,map.TailleZ,MG.FDevice);
    win := false;
    life := 100;
    menu.launch:=false;
    MG.Show;
    playsound.Musicmenu.Play();
    while (MG.Created) and (not menu.launch) do
    begin
    menu.main(MG.FDevice);
    end;
    Playsound.MusicMenu.Stop;
    Playsound.MusicGame.Play;
    while (MG.Created) and  not win and (life > 0) do
    begin
      itime:= Environment.TickCount;
      AI.ia(cube.pos,life);
      environmentG.ia(map.tailleX, map.TailleZ);
      Player1.GetEnter(Self.cube.pos, Player1.Touches);
      MP.newPosition(cube.pos , Player1.Touches , map.vecteur , map.tailleX , map.tailleZ );
      MG.Render(cube.pos, Cube,AI,EnvironmentG, Map.FvertexBuffer, skybox.Fvertexbuffer, map.Number_triangle, map.Ftexture , skybox.Ftexture);
      application.DoEvents;
      PlaySound.PlayAllSound(cube.pos, player1.Touches);
      sleep(15-(iTime-Environment.TickCount))
    end;

  end;

end.
