unit uttr;

interface

uses uttory,glmesh,vectortypes,GLScene, GLObjects,glgeomobjects,SysUtils,
  vectorgeometry,GLtexture,glstate,glmisc,glvectorfileobjects,glfile3ds;

type wagon=record
       // os1,os2:tglsphere;
        podwozie:tgldummycube;
        buda:tglfreeform;
        rozstaw:real;
        zaczep:real;
        bib:tglmateriallibrary;
        end;

type ttr=class
        private
        public
//pot:real;
        v:real;
//tn:integer;
        wagony:array[1..8] of wagon;
        liwagon:byte;
        tpoz:TP;
        trasa:ttory;
        baza:tglbasesceneobject;
        podwozie:tgldummycube;
        poz:XY;
        dlug:real;
        kam1:tglcamera;
        ref1:tgldummycube;
        procedure wczytajwagon(nr:integer;li:string;kat:string);
        procedure wczytaj(na:string);
        procedure wczytajbusa(kat,te:string);
        procedure rusz(dt:double);
        constructor Create;
        procedure zmfr;
        procedure zmba;
end;

implementation

procedure zapisz(s:string);
var f:textfile;
begin
assignfile(f,'aaa.txt');
append(f);
write(f,S);
closefile(f);
end;

constructor ttr.Create;
begin
end;

procedure ttr.zmfr;
begin
        trasa.switchf(trasa.tra[tpoz.t].b);

end;

procedure ttr.zmba;
begin
if (trasa.pnt[trasa.tra[tpoz.t].a].sb<trasa.pnt[trasa.tra[tpoz.t].a].lib) then
        inc(trasa.pnt[trasa.tra[tpoz.t].a].sb)
        else
        trasa.pnt[trasa.tra[tpoz.t].a].sb:=1;
end;

procedure ttr.wczytajwagon(nr:integer;li:string;kat:string);
var np:string;
begin
        np:=copy(li,1,pos(' ',li)-1); delete(li,1,pos(' ',li));
        wagony[nr].rozstaw:=strtofloat(li);

        wagony[nr].podwozie:=tgldummycube.create(baza.addnewchild(tgldummycube));
        wagony[nr].podwozie.parent:=baza;
        wagony[nr].buda:=tglfreeform.create( wagony[nr].podwozie.AddNewChild(tglfreeform));
        wagony[nr].buda.LoadFromFile(kat+np);
        wagony[nr].buda.Parent:= wagony[nr].podwozie;
       // wagony[nr].os1:=tglsphere.create(baza.AddNewChild(tglsphere));
        //wagony[nr].os1.Radius:=20;
       // wagony[nr].os1.parent:=baza;
      //  wagony[nr].os2:=tglsphere.create(baza.AddNewChild(tglsphere));
      //  wagony[nr].os2.parent:=baza;
end;

procedure ttr.wczytaj(na:string);
var lim:string;
f:textfile;
i:integer;
begin
tpoz.p:=0.5;
tpoz.t:=1;
v:=5;

assignfile(f,na+'opis.txt');
reset(f);
readln(f,liwagon);

for i:=1 to liwagon do begin
        readln(f,lim);
        wagony[i].zaczep:=strtofloat(lim);
        readln(f,lim);
        wczytajwagon(i,lim,na);
end;





ref1:=tgldummycube.Create(wagony[1].podwozie.addnewchild(tgldummycube));
ref1.Parent:=wagony[1].buda;
ref1.Position.y:=20;
//ref1.VisibleAtRunTime:=true;

kam1:=tglcamera.create(wagony[1].buda.AddNewChild(tglcamera));
kam1.Position.X:=0;
kam1.Position.y:=-10;
kam1.Position.z:=10;
kam1.Parent:=wagony[1].buda;
kam1.DepthOfView:=500;

kam1.Up.X:=0;
kam1.Up.y:=0;
kam1.Up.z:=1;
kam1.Up.X:=0;
kam1.Up.y:=0;
kam1.Up.z:=1;

kam1.TargetObject:=ref1;

 //    zapisz(format(' G : %d',[tpoz.t]));
end;

procedure ttr.wczytajbusa(kat,te:string);
var i,na,l:string;
f:textfile;
begin
assignfile(f,kat+'desc.bs1');
reset(f);
repeat
        readln(f,l);
        if copy(l,1,2)='MO' then begin
                delete(l,1,3);
                delete(l,pos('"',l),1);
                na:=copy(l,1,pos('"',l)-1);
        end;
until eof(f);
closefile(f);
tpoz.p:=0.5;
tpoz.t:=1;
v:=5;
liwagon:=1;
wagony[1].bib:=tglmateriallibrary.create(nil);
        wagony[1].rozstaw:=6;
        wagony[1].podwozie:=tgldummycube.create(baza.addnewchild(tgldummycube));
        wagony[1].podwozie.parent:=baza;
        wagony[1].buda:=tglfreeform.create( wagony[1].podwozie.AddNewChild(tglfreeform));
        wagony[1].buda.MaterialLibrary:=wagony[1].bib;
        wagony[1].bib.TexturePaths:=kat+te+'\';

        wagony[1].buda.LoadFromFile(kat+na);
        wagony[1].buda.Parent:= wagony[1].podwozie;
      //  wagony[1].os1:=tglsphere.create(baza.AddNewChild(tglsphere));
        //wagony[nr].os1.Radius:=20;
     //   wagony[1].os1.parent:=baza;
     //   wagony[1].os2:=tglsphere.create(baza.AddNewChild(tglsphere));
    //    wagony[1].os2.parent:=baza;

ref1:=tgldummycube.Create(wagony[1].podwozie.addnewchild(tgldummycube));
ref1.Parent:=wagony[1].buda;
ref1.Position.y:=20;
//ref1.VisibleAtRunTime:=true;

kam1:=tglcamera.create(wagony[1].buda.AddNewChild(tglcamera));
kam1.Position.X:=0;
kam1.Position.y:=-10;
kam1.Position.z:=10;
kam1.Parent:=wagony[1].buda;
kam1.DepthOfView:=500;

kam1.Up.X:=0;
kam1.Up.y:=0;
kam1.Up.z:=1;
kam1.Up.X:=0;
kam1.Up.y:=0;
kam1.Up.z:=1;

kam1.TargetObject:=ref1;
end;

procedure ttr.rusz(dt:double);
var

poz1,poz2:Xy;
pos1,pos2:TP;
nasts:integer;
tmps:integer;
i:integer;
begin
//dt:=0.1;
if v>trasa.tra[tpoz.t].vmax then v:=v-2*dt;
if v<trasa.tra[tpoz.t].vmax then v:=v+1*dt;
 //    zapisz(format(' A : %d',[tpoz.t]));
tpoz:=trasa.dodajdl(tpoz,v*dt);
///    zapisz(format(' A : %d',[tpoz.t]));
for i:=1 to liwagon do begin
if (i=1) then begin
        pos2:=tpoz;
        pos1:=trasa.dodajdl(tpoz,wagony[1].rozstaw);
        end
        else
        begin
        pos1:=trasa.ujmijdl(pos2,wagony[i].zaczep);
        pos2:=trasa.ujmijdl(pos1,wagony[i].rozstaw);
        end;
poz1:=trasa.tbez(pos1.t,pos1.p);
poz2:=trasa.tbez(pos2.t,pos2.p);
//wagony[i].os1.position.x:=poz1.x;
//wagony[i].os1.position.y:=poz1.y;
//wagony[i].os2.position.x:=poz2.x;
//wagony[i].os2.position.y:=poz2.y;

wagony[i].podwozie.position.x:=poz2.x;
wagony[i].podwozie.position.y:=poz2.y;

wagony[i].buda.rollAngle:=arctan2(poz2.y-poz1.y,poz2.x-poz1.x)*180/pi+90;
end;

nasts:=trasa.tra[tpoz.t].b;

  tmps:=1000;

repeat
 tmps:=trasa.switchb(nasts);

until tmps=tpoz.t;



end;



end.

