unit RoadUnit;

interface
  uses RoadGraphic, RoadSound, RoadPhysic, RoadSys;
type
    PRoadUnit = ^TRoadUnit;
    TRoadUnit = class
      UID : integer;
//      Road    : TRoadBase;
      Physic  : TPhysicBase;
      Graphic : TGraphicBase;
      constructor Construct(PhysicClass : TPhysicClass; GraphicClass : TGraphicClass);
      function isCollide(sPosition : TPosition) : boolean;
      procedure Die;
      destructor Destroy;override;
    end;

    TRoadManager  = class
      ClassArray         : Array of TBaseClass;
      function GetNewUID : integer;
      procedure RegisterRoadClass(RoadClass : TBaseClass);
      private
        CurrentUID  : integer;
    end;

implementation
  uses Scenes;
{ TRoadManager }

function TRoadManager.GetNewUID: integer;
begin
  Result  :=  CurrentUID;
  inc(CurrentUID);
end;

procedure TRoadManager.RegisterRoadClass(RoadClass: TBaseClass);
begin
  SetLength(ClassArray,Length(ClassArray)+1);
  ClassArray[high(ClassArray)]  :=  RoadClass;
end;

{ TRoadUnit }

constructor TRoadUnit.Construct(PhysicClass: TPhysicClass;
  GraphicClass: TGraphicClass);
begin
  UID :=  random(1000000);
  Physic  :=  PhysicClass.Create(Self);
  Graphic :=  GraphicClass.Create(Self);
end;

destructor TRoadUnit.Destroy;
begin
  Graphic.Free;
  Physic.Free;
  inherited;
end;

procedure TRoadUnit.Die;
var
  i     : integer;
begin
  i :=  0;
  with (Scene as TSceneGame) do
  begin
    while i<=UnitCount-1 do
     begin
      if Units[i]<>nil then
        (if Units[i].UID=UID then
          break);
      inc(i);    
     end;
    Units[i]:=Units[UnitCount-1];
    Units[UnitCount-1]:=nil;
    Dec(UnitCount);
  end;
  Free;
end;

function TRoadUnit.isCollide(sPosition : TPosition): boolean;
begin
  if sqrt(
      sqr(sPosition.X-Physic.Position.X)
        +
          sqr(sPosition.Z-Physic.Position.Z)
          ) <0.2 then
            Result  :=  True
              else
                Result  :=  False;
end;

end.
