unit Model;

interface

uses
  SysUtils, Graphics, GLTexture, GLScene,
  GLObjects, GLGeomObjects, Forms,
  Jpeg, OpenGL1x, GLMisc, GLfileSMD,
  loader, GLVectorFileObjects,
  Map, GLProxyObjects, MForm;
type

  TEtDecObjs=record
  F : TGLFreeForm;
  Name : string;
  C  : byte;
  R  : real;
  end;

  TDekor=record
    F : TGLFreeFormProxy;
     Et : byte;
  end;

  TPoint=record
    X, Y, Z : real;
  end;
  TOpt=record
  live, u1, u2, speed  : byte;

  end;
  TPers=record
    A : TGLActorProxy;
    Au : TGLCylinder;
    Gun : TGLDummyCube;
    // P2 : TGLPolygon;
    P1 : TglSprite;
    O : Topt;
    Et : byte;
    
    MA : integer;
    MP : Tpoint;
  end;

  TliveObj=record
    Name, ModelFN : String;
    Sound : array[1..5] of String;
    A : TGLActor;
    O : Topt;
    AShoot : byte;
    shooter : boolean;
    Gun, Au : Tpoint;
    R, H : real;
  end;

  var
    EtLiveObj : array[1..10] of TliveObj;
    Pers: array[1..100] of Tpers;

    EtDecO : array[1..20] of   TEtDecObjs;
    DecO : array[1..40] of TDekor;

    Etalon : TGLDummyCube;
    MofEt, MofP, MofED, MofD : byte;

procedure loadft(N : byte);
procedure loadlo(FN : string);
procedure creatPers(T : byte; X, Y : real);
procedure loaddecor(FN : string);
procedure creatdecor(T : byte; X, Y : real);

implementation

procedure loadft(N : byte);
var
  i : byte;
begin
  SetCurrentDir(ExtractFilePath(Application.ExeName)+'Data\temp\');
  EtLiveObj[MofEt].A:=TGLActor(Etalon.AddNewChild(TGLActor));
  EtLiveObj[MofEt].A.Name:='ETA'+inttostr(1000+MofEt);
  EtLiveObj[MofEt].A.LoadFromFile('m.smd');
  EtLiveObj[MofEt].A.Material.Texture.Image.LoadFromFile('jpg.jpg');
  EtLiveObj[MofEt].A.Material.Texture.Disabled:=false;
  EtLiveObj[MofEt].A.Scale.SetVector(0.01,0.01,0.01);
  for i:=1 to N do
  if FileExists(inttostr(i)+'.smd') then
  EtLiveObj[MofEt].A.AddDataFromFile(inttostr(i)+'.smd');
  EtLiveObj[MofEt].A.SwitchToAnimation('1');
  EtLiveObj[MofEt].A.AnimationMode:=aamLoop;
end;

procedure loadlo(FN : string);
var
  F : textfile;
  i : integer;
  S : string;
begin
  assign(F,FN);
  reset(F);
  readln(F,S);
  if S='__cdmlex live obj__' then
  begin
    MofEt:=MofEt+1;
    readln(F,  EtLiveObj[MofEt].Name);
    readln(F,  EtLiveObj[MofEt].ModelFN);
    if UnZ(EtLiveObj[MofEt].ModelFN) then
    if EtLiveObj[MofEt].Name='Герой' then
    loadft(5) else loadft(5);
    readln(F,  EtLiveObj[MofEt].O.live);
    readln(F,  S);
    if S='TRUE' then
    EtLiveObj[MofEt].Shooter:=true else
    EtLiveObj[MofEt].Shooter:=false;
    readln(F,  EtLiveObj[MofEt].O.u1);
    readln(F,  EtLiveObj[MofEt].O.u2);
    readln(F,  EtLiveObj[MofEt].O.speed);

    readln(F,  EtLiveObj[MofEt].Au.X);
    EtLiveObj[MofEt].Au.X:=EtLiveObj[MofEt].Au.X/70;
    readln(F,  EtLiveObj[MofEt].Au.Y);
    EtLiveObj[MofEt].Au.Y:=EtLiveObj[MofEt].Au.Y/70;
    readln(F,  EtLiveObj[MofEt].Au.Z);
    EtLiveObj[MofEt].Au.Z:=EtLiveObj[MofEt].Au.Z/70;

    readln(F,  EtLiveObj[MofEt].R);
    EtLiveObj[MofEt].R:=EtLiveObj[MofEt].R/35;
    readln(F,  EtLiveObj[MofEt].H);
    EtLiveObj[MofEt].H:=EtLiveObj[MofEt].H/35;
    for i:=1 to 5 do
    readln(F, EtLiveObj[MofEt].Sound[i]);
    readln(F, EtLiveObj[MofEt].AShoot);
    if EtLiveObj[MofEt].Shooter then
    begin
      readln(F, EtLiveObj[MofEt].Gun.X);
      EtLiveObj[MofEt].Gun.X:=EtLiveObj[MofEt].Gun.X/500;
      readln(F, EtLiveObj[MofEt].Gun.Y);
      EtLiveObj[MofEt].Gun.Y:=EtLiveObj[MofEt].Gun.Y/500;
      readln(F, EtLiveObj[MofEt].Gun.Z);
      EtLiveObj[MofEt].Gun.Z:=EtLiveObj[MofEt].Gun.Z/500;
    end;
   end;
   closefile(F);
end;

procedure creatPers(T : byte; X, Y : real);
begin
   MofP:=MofP+1;
   Pers[MofP].Et:=T;
   Pers[MofP].O:=EtLiveObj[T].O;
   Pers[MofP].A:=TGLActorProxy(L.GLVis.AddNewChild(TGLActorProxy));
   Pers[MofP].A.Name:='MPP'+inttostr(1000+MofP);
    if EtLiveObj[T].A<>nil then
    With Pers[MofP].A do
    begin
      ProxyOptions:=[pooObjects];
      MasterObject:=EtLiveObj[T].A;
      Direction:=EtLiveObj[T].A.Direction;
      Up:=EtLiveObj[T].A.Up;
      Scale.SetVector(0.03,0.03,0.03);
      Position.SetPoint(X,Y,0);
    end;
    if EtLiveObj[T].shooter then
      begin
        Pers[MofP].Gun:=TGLDummyCube(Pers[MofP].A.AddNewChild(TGLDummyCube));
        Pers[MofP].Gun.Position.SetPoint(EtLiveObj[T].Gun.X,EtLiveObj[T].Gun.Y,EtLiveObj[T].Gun.Z);
        Pers[MofP].Gun.Scale.SetVector(10,10,10);
        Pers[MofP].P1:=TglSprite(Pers[MofP].Gun.AddNewChild(TglSprite));
        with Pers[MofP].P1 do
          begin
            //TurnAngle:=90;
            Material.MaterialLibrary:=QForm.ML3;
            Material.LibMaterialName:='Fire';
            visible:=false;
          end;
      end;
    Pers[MofP].Au:=TGLCylinder(L.GLUnVis.AddNewChild(TGLCylinder));
    Pers[MofP].Au.Name:='APP'+inttostr(1000+MofP);
    Pers[MofP].Au.PitchAngle:=90;
    Pers[MofP].Au.TopRadius:=EtLiveObj[T].R;
    Pers[MofP].Au.BottomRadius:=Pers[MofP].Au.TopRadius;
    Pers[MofP].Au.Height:=EtLiveObj[T].H;
    Pers[MofP].Au.Position.X:=Pers[MofP].A.Position.X-EtLiveObj[T].Au.X;
    Pers[MofP].Au.Position.Y:=Pers[MofP].A.Position.Y-EtLiveObj[T].Au.Y;
    Pers[MofP].Au.Position.Z:=Pers[MofP].Au.Height/2-EtLiveObj[T].Au.Z;
end;

procedure loaddecor(FN : string);
var
  F : textfile;
  i : integer;
  S : string;
begin
  MofED:=MofED+1;
  EtDecO[MofED].F:=TGLFreeForm(Etalon.AddNewChild(TGLFreeForm));
  EtDecO[MofED].F.Name:='ETD'+inttostr(1000+MofED);
  EtDecO[MofED].F.LoadFromFile(FN+'.3ds');
  if FileExists(FN+'.jpg') then
  EtDecO[MofED].F.Material.Texture.Image.LoadFromFile(FN+'.jpg');
  EtDecO[MofED].F.Material.Texture.Disabled:=false;
  if FileExists(FN+'.txt') then
  begin
    assign(F,FN+'.txt');
    reset(F);
    readln(F, EtDecO[MofED].Name);
    readln(F, S);
    if S='r' then EtDecO[MofED].C:=1;
    if S='c' then EtDecO[MofED].C:=2;
    readln(F, EtDecO[MofED].R);
    EtDecO[MofED].R:=EtDecO[MofED].R/1000;
    closefile(F);
  end;
end;

procedure creatdecor(T : byte; X, Y : real);
begin
   MofD:=MofD+1;
   DecO[MofD].Et:=T;
   DecO[MofD].F:=TGLFreeFormProxy(L.GLVis.AddNewChild(TGLFreeFormProxy));
   DecO[MofD].F.Name:='MPD'+inttostr(1000+MofD);
    if EtDecO[T].F<>nil then
    With DecO[MofD].F do
    begin
      ProxyOptions:=[pooObjects];
      MasterObject:=EtDecO[T].F;
      Direction:=EtDecO[T].F.Direction;
      Up:=EtDecO[T].F.Up;
      Scale.SetVector(0.3,0.3,0.3);
      Position.SetPoint(X,Y,0);
    end;
end;

end.
