Changeset 14 for trunk


Ignore:
Timestamp:
Mar 27, 2015, 1:09:12 PM (10 years ago)
Author:
chronos
Message:
  • Modified: TTrackPoint change from record to class. Also TLine.TrackPoints is now TObjectList instead of array.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UEngine.pas

    r13 r14  
    1515  TMetroPassengers = class;
    1616  TMetroLines = class;
     17  TMetroLine = class;
    1718  TMetroTrains = class;
    1819
     
    4142  end;
    4243
    43   TTrackPoint = record
     44  TTrackPoint = class
     45    Line: TMetroLine;
    4446    StationIndex: Integer;
    4547    Point: TPoint;
     48    Pending: Boolean;
     49  end;
     50
     51  TTrackPoints = class(TObjectList)
     52    Line: TMetroLine;
    4653  end;
    4754
     
    5360    Stations: TMetroStations;
    5461    Trains: TMetroTrains;
    55     TrackPoints: array of TTrackPoint;
     62    TrackPoints: TTrackPoints;
    5663    procedure ConnectStation(Station: TMetroStation);
    5764    procedure DisconnectStation(Station: TMetroStation);
     
    190197  StationMaxDistance = 300;
    191198  MaxWaitingPassengers = 10;
     199  MetroLineThickness = 13;
    192200
    193201implementation
     
    295303  Train: TMetroTrain;
    296304  StationIndex: Integer;
     305  NewTrackPoint: TTrackPoint;
    297306begin
    298307  Stations.Add(Station);
     
    300309  StationIndex := Stations.Count - 1;
    301310  if Stations.Count = 1 then begin
    302     SetLength(TrackPoints, Length(TrackPoints) + 1);
    303     TrackPoints[High(TrackPoints)].StationIndex := StationIndex;
    304     TrackPoints[High(TrackPoints)].Point := Station.Position;
     311    NewTrackPoint := TTrackPoint.Create;
     312    NewTrackPoint.StationIndex := StationIndex;
     313    NewTrackPoint.Point := Station.Position;
     314    NewTrackPoint.Line := TrackPoints.Line;
     315    TrackPoints.Add(NewTrackPoint);
    305316  end else begin
    306317     if Stations.Count > 1 then
     
    329340    Station.Lines.Remove(Self);
    330341    if Stations.Count > 0 then begin
    331       while (Length(TrackPoints) > 0) and (not ComparePoint(TrackPoints[High(TrackPoints)].Point,
     342      while (TrackPoints.Count > 0) and (not ComparePoint(TTrackPoint(TrackPoints.Last).Point,
    332343        TMetroStation(Stations.Last).Position)) do
    333         SetLength(TrackPoints, Length(TrackPoints) - 1);
    334     end else SetLength(TrackPoints, 0);
     344        TrackPoints.Delete(TrackPoints.Count - 1);
     345    end else TrackPoints.Clear;
    335346    // Remove trains if less then two stations
    336347    if Stations.Count < 2 then
     
    346357var
    347358  Delta: TPoint;
     359  NewTrackPoint: TTrackPoint;
    348360begin
    349361  Delta := Point(P2.X - P1.X, P2.Y - P1.Y);
    350362  if Abs(Delta.X) > Abs(Delta.Y) then begin
    351     SetLength(TrackPoints, Length(TrackPoints) + 1);
    352     TrackPoints[High(TrackPoints)].Point := Point(P2.X - Sign(Delta.X) * Abs(Delta.Y), P1.Y);
    353     TrackPoints[High(TrackPoints)].StationIndex := StationIndex - 1;
     363    NewTrackPoint := TTrackPoint.Create;
     364    NewTrackPoint.Line := TrackPoints.Line;
     365    NewTrackPoint.Point := Point(P2.X - Sign(Delta.X) * Abs(Delta.Y), P1.Y);
     366    NewTrackPoint.StationIndex := StationIndex - 1;
     367    TrackPoints.Add(NewTrackPoint);
    354368  end else begin
    355     SetLength(TrackPoints, Length(TrackPoints) + 1);
    356     TrackPoints[High(TrackPoints)].Point := Point(P1.X, P2.Y - Sign(Delta.Y) * Abs(Delta.X));
    357     TrackPoints[High(TrackPoints)].StationIndex := StationIndex - 1;
    358   end;
    359   SetLength(TrackPoints, Length(TrackPoints) + 1);
    360   TrackPoints[High(TrackPoints)].Point := P2;
    361   TrackPoints[High(TrackPoints)].StationIndex := StationIndex;
     369    NewTrackPoint := TTrackPoint.Create;
     370    NewTrackPoint.Line := TrackPoints.Line;
     371    NewTrackPoint.Point := Point(P1.X, P2.Y - Sign(Delta.Y) * Abs(Delta.X));
     372    NewTrackPoint.StationIndex := StationIndex - 1;
     373    TrackPoints.Add(NewTrackPoint);
     374  end;
     375  NewTrackPoint := TTrackPoint.Create;
     376  NewTrackPoint.Line := TrackPoints.Line;
     377  NewTrackPoint.Point := P2;
     378  NewTrackPoint.StationIndex := StationIndex;
     379  TrackPoints.Add(NewTrackPoint);
    362380end;
    363381
     
    367385begin
    368386  Result := 0;
    369   for I := 0 to High(TrackPoints) do
     387  for I := 0 to TrackPoints.Count - 1 do
    370388  if I > 0 then
    371     Result := Result + Distance(TrackPoints[I].Point, TrackPoints[I - 1].Point);
     389    Result := Result + Distance(TTrackPoint(TrackPoints[I]).Point, TTrackPoint(TrackPoints[I - 1]).Point);
    372390end;
    373391
     
    377395begin
    378396  Result := 0;
    379   for I := 0 to High(TrackPoints) do begin
     397  for I := 0 to TrackPoints.Count - 1 do begin
    380398    if I > 0 then
    381       Result := Result + Distance(TrackPoints[I].Point, TrackPoints[I - 1].Point);
    382     if TrackPoints[I].StationIndex = StationIndex then Break;
     399      Result := Result + Distance(TTrackPoint(TrackPoints[I]).Point, TTrackPoint(TrackPoints[I - 1]).Point);
     400    if TTrackPoint(TrackPoints[I]).StationIndex = StationIndex then Break;
    383401  end;
    384402end;
     
    390408  Trains := TMetroTrains.Create;
    391409  Trains.OwnsObjects := False;
     410  TrackPoints := TTrackPoints.Create;
     411  TrackPoints.Line := Self;
    392412end;
    393413
    394414destructor TMetroLine.Destroy;
    395415begin
     416  TrackPoints.Free;
    396417  Trains.Free;
    397418  Stations.Free;
     
    427448  with Line do begin
    428449    Sum := 0;
    429     if Length(TrackPoints) > 1 then
    430     for I := 1 to High(TrackPoints) do begin
    431       D := Distance(TrackPoints[I].Point, TrackPoints[I - 1].Point);
     450    if TrackPoints.Count > 1 then
     451    for I := 1 to TrackPoints.Count - 1 do begin
     452      D := Distance(TTrackPoint(TrackPoints[I]).Point, TTrackPoint(TrackPoints[I - 1]).Point);
    432453      if (Sum + D) > Self.TrackPos then begin
    433         R := Point(TrackPoints[I].Point.X - TrackPoints[I - 1].Point.X,
    434           TrackPoints[I].Point.Y - TrackPoints[I - 1].Point.Y);
    435         Result := Point(Trunc(TrackPoints[I - 1].Point.X + R.X * (TrackPos - Sum) / D),
    436           Trunc(TrackPoints[I - 1].Point.Y + R.Y * (TrackPos - Sum) / D));
     454        R := Point(TTrackPoint(TrackPoints[I]).Point.X - TTrackPoint(TrackPoints[I - 1]).Point.X,
     455          TTrackPoint(TrackPoints[I]).Point.Y - TTrackPoint(TrackPoints[I - 1]).Point.Y);
     456        Result := Point(Trunc(TTrackPoint(TrackPoints[I - 1]).Point.X + R.X * (TrackPos - Sum) / D),
     457          Trunc(TTrackPoint(TrackPoints[I - 1]).Point.Y + R.Y * (TrackPos - Sum) / D));
    437458        Exit;
    438459      end else Sum := Sum + D;
    439460    end;
    440     if Length(TrackPoints) > 0 then
    441       Result := TrackPoints[High(TrackPoints)].Point;
     461    if TrackPoints.Count > 0 then
     462      Result := TTrackPoint(TrackPoints.Last).Point;
    442463  end;
    443464end;
     
    453474  with Line do begin
    454475    Sum := 0;
    455     if Length(TrackPoints) > 1 then
    456     for I := 1 to High(TrackPoints) do begin
    457       D := Distance(TrackPoints[I].Point, TrackPoints[I - 1].Point);
     476    if TrackPoints.Count > 1 then
     477    for I := 1 to TrackPoints.Count - 1 do begin
     478      D := Distance(TTrackPoint(TrackPoints[I]).Point, TTrackPoint(TrackPoints[I - 1]).Point);
    458479      if (Sum + D) > Self.TrackPos then begin
    459         Result := ArcTan2(TrackPoints[I].Point.Y - TrackPoints[I - 1].Point.Y,
    460           TrackPoints[I].Point.X - TrackPoints[I - 1].Point.X);
     480        Result := ArcTan2(TTrackPoint(TrackPoints[I]).Point.Y - TTrackPoint(TrackPoints[I - 1]).Point.Y,
     481          TTrackPoint(TrackPoints[I]).Point.X - TTrackPoint(TrackPoints[I - 1]).Point.X);
    461482        Exit;
    462483      end else Sum := Sum + D;
     
    589610  while (I < Lines.Count) do
    590611  with TMetroLine(Lines[I]) do begin
    591     for T := 1 to High(TrackPoints) do begin
    592       D := PointToLineDistance(Pos, TrackPoints[T - 1].Point, TrackPoints[T].Point);
     612    for T := 1 to TrackPoints.Count - 1 do begin
     613      D := PointToLineDistance(Pos, TTrackPoint(TrackPoints[T - 1]).Point, TTrackPoint(TrackPoints[T]).Point);
    593614      if D < Distance then begin
    594615        Result := TMetroLine(Lines[I]);
     
    11671188    Canvas.Pen.Color := Color;
    11681189    Canvas.Pen.Style := psSolid;
    1169     Canvas.Pen.Width := 15;
    1170     if Length(TrackPoints) > 0 then Canvas.MoveTo(TrackPoints[0].Point);
    1171     for S := 1 to High(TrackPoints) do begin
    1172       Canvas.LineTo(TrackPoints[S].Point);
    1173       if (S = High(TrackPoints)) then begin
    1174         Angle := arctan2((TrackPoints[S].Point.Y - TrackPoints[S - 1].Point.Y), (TrackPoints[S].Point.X - TrackPoints[S - 1].Point.X));
    1175         EndPoint := Point(Round(TrackPoints[S].Point.X + EndStationLength * Cos(Angle)),
    1176           Round(TrackPoints[S].Point.Y + EndStationLength * Sin(Angle)));
     1190    Canvas.Pen.Width := MetroLineThickness;
     1191    if TrackPoints.Count > 0 then Canvas.MoveTo(TTrackPoint(TrackPoints[0]).Point);
     1192    for S := 1 to TrackPoints.Count - 1 do begin
     1193      Canvas.LineTo(TTrackPoint(TrackPoints[S]).Point);
     1194      if (S = TrackPoints.Count - 1) then begin
     1195        Canvas.Pen.EndCap := pecSquare;
     1196        Angle := arctan2((TTrackPoint(TrackPoints[S]).Point.Y - TTrackPoint(TrackPoints[S - 1]).Point.Y),
     1197          (TTrackPoint(TrackPoints[S]).Point.X - TTrackPoint(TrackPoints[S - 1]).Point.X));
     1198        EndPoint := Point(Round(TTrackPoint(TrackPoints[S]).Point.X + EndStationLength * Cos(Angle)),
     1199          Round(TTrackPoint(TrackPoints[S]).Point.Y + EndStationLength * Sin(Angle)));
    11771200        Canvas.LineTo(EndPoint);
    11781201        Canvas.MoveTo(Point(Round(EndPoint.X + Cos(Angle + Pi / 2) * EndStationLength / 3),
     
    11801203        Canvas.LineTo(Point(Round(EndPoint.X + Cos(Angle - Pi / 2) * EndStationLength / 3),
    11811204          Round(EndPoint.Y + Sin(Angle - Pi / 2) * EndStationLength / 3)));
     1205        Canvas.Pen.EndCap := pecRound;
    11821206      end;
    11831207    end;
    1184     if (Length(TrackPoints) > 1) then begin
    1185       Angle := arctan2((TrackPoints[1].Point.Y - TrackPoints[0].Point.Y), (TrackPoints[1].Point.X - TrackPoints[0].Point.X));
    1186       Canvas.MoveTo(TrackPoints[0].Point);
    1187       EndPoint := Point(Round(TrackPoints[0].Point.X - EndStationLength * Cos(Angle)),
    1188         Round(TrackPoints[0].Point.Y - EndStationLength * Sin(Angle)));
     1208    if (TrackPoints.Count > 1) then begin
     1209      Canvas.Pen.EndCap := pecSquare;
     1210      Angle := arctan2((TTrackPoint(TrackPoints[1]).Point.Y - TTrackPoint(TrackPoints[0]).Point.Y),
     1211        (TTrackPoint(TrackPoints[1]).Point.X - TTrackPoint(TrackPoints[0]).Point.X));
     1212      Canvas.MoveTo(TTrackPoint(TrackPoints[0]).Point);
     1213      EndPoint := Point(Round(TTrackPoint(TrackPoints[0]).Point.X - EndStationLength * Cos(Angle)),
     1214        Round(TTrackPoint(TrackPoints[0]).Point.Y - EndStationLength * Sin(Angle)));
    11891215      Canvas.LineTo(EndPoint);
    11901216      Canvas.MoveTo(Point(Round(EndPoint.X - Cos(Angle + Pi / 2) * EndStationLength / 3),
     
    11921218      Canvas.LineTo(Point(Round(EndPoint.X - Cos(Angle - Pi / 2) * EndStationLength / 3),
    11931219        Round(EndPoint.Y - Sin(Angle - Pi / 2) * EndStationLength / 3)));
     1220      Canvas.Pen.EndCap := pecRound;
    11941221    end;
    11951222
Note: See TracChangeset for help on using the changeset viewer.