Changeset 32
- Timestamp:
- Apr 19, 2015, 12:25:37 AM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UEngine.pas
r31 r32 22 22 TTrackPoints = class; 23 23 TTrack = class; 24 TTrackLinks = class; 24 25 25 26 { TMapStation } … … 71 72 PositionDesigned: TPoint; 72 73 Pending: Boolean; 74 Track: TTrack; 73 75 NeighPoints: TTrackPoints; 74 Track: TTrack; 76 NeighLinks: TTrackLinks; 77 procedure Connect(TrackPoint: TTrackPoint); 78 procedure Disconnect(TrackPoint: TTrackPoint); 75 79 function GetDown: TTrackPoint; 76 80 function GetUp: TTrackPoint; … … 94 98 TTrackLink = class 95 99 Points: TTrackPoints; 96 PointDown: TTrackPoint;97 PointUp: TTrackPoint;98 Line: TMetroLine;99 100 Shift: TPoint; 100 101 Track: TTrack; … … 103 104 end; 104 105 106 { TTrackLinks } 107 105 108 TTrackLinks = class(TObjectList) 109 Track: TTrack; 110 function SearchPoints(Point1, Point2: TTrackPoint): TTrackLink; 111 function AddNew: TTrackLink; 106 112 end; 107 113 … … 110 116 Links: TTrackLinks; 111 117 Line: TMetroLine; 118 procedure RouteTrack(TP1, TP2: TTrackPoint); 119 procedure RemoveTrackBetween(TP1, TP2: TTrackPoint); 112 120 constructor Create; 113 121 destructor Destroy; override; … … 150 158 procedure ConnectStation(Station: TMapStation; LineStationDown, LineStationUp: TLineStation); 151 159 procedure DisconnectStation(ALineStation: TLineStation); 152 procedure RouteTrack(TP1, TP2: TTrackPoint);153 procedure RemoveTrackBetween(TP1, TP2: TTrackPoint);154 160 function GetTrackLength: Integer; 155 161 constructor Create; … … 335 341 //TimePerSecond = (60 * OneMinute); 336 342 TimePerSecond = (60 * OneMinute); 337 NewStationPeriod = 1;343 NewStationPeriod = 0.01; 338 344 NewShapePeriod = 10; 339 345 NewTrainPeriod = 7; // Each week … … 350 356 SZeroZoomNotAlowed = 'Zero zoom not allowed'; 351 357 358 { TTrackLinks } 359 360 function TTrackLinks.SearchPoints(Point1, Point2: TTrackPoint): TTrackLink; 361 var 362 I: Integer; 363 begin 364 I := 0; 365 while (I < 0) and 366 ((TTrackLink(Items[I]).Points.First <> Point1) or (TTrackLink(Items[I]).Points.Last <> Point2)) 367 and ((TTrackLink(Items[I]).Points.First <> Point2) or (TTrackLink(Items[I]).Points.Last <> Point1)) do 368 Inc(I); 369 if I < 0 then Result := TTrackLink(Items[I]) 370 else Result := nil; 371 end; 372 373 function TTrackLinks.AddNew: TTrackLink; 374 begin 375 Result := TTrackLink.Create; 376 Result.Track := Track; 377 end; 378 352 379 { TTrackPoints } 353 380 … … 365 392 Points.Track := Self; 366 393 Links := TTrackLinks.Create; 394 Links.Track := Self; 367 395 end; 368 396 … … 475 503 begin 476 504 I := 0; 477 while (I < Count) and (TTrackLink(Items[I]).Point Up<> TrackPoint) and (TTrackLink(Items[I]) <> Skip) do Inc(I);505 while (I < Count) and (TTrackLink(Items[I]).Points[1] <> TrackPoint) and (TTrackLink(Items[I]) <> Skip) do Inc(I); 478 506 if I < Count then Result := TTrackLink(Items[I]) 479 507 else Result := nil; … … 485 513 begin 486 514 I := 0; 487 while (I < Count) and (TTrackLink(Items[I]).Point Down<> TrackPoint) and (TTrackLink(Items[I]) <> Skip) do Inc(I);515 while (I < Count) and (TTrackLink(Items[I]).Points[0] <> TrackPoint) and (TTrackLink(Items[I]) <> Skip) do Inc(I); 488 516 if I < Count then Result := TTrackLink(Items[I]) 489 517 else Result := nil; … … 517 545 518 546 { TTrackPoint } 547 548 procedure TTrackPoint.Connect(TrackPoint: TTrackPoint); 549 var 550 NewLink: TTrackLink; 551 begin 552 if NeighPoints.IndexOf(TrackPoint) = -1 then begin 553 NeighPoints.Add(TrackPoint); 554 TrackPoint.NeighPoints.Add(Self); 555 556 // Add new link to both self and connected track point 557 NewLink := Track.Links.AddNew; 558 NewLink.Points.Add(TrackPoint); 559 NewLink.Points.Add(Self); 560 NeighLinks.Add(NewLink); 561 TrackPoint.NeighLinks.Add(NewLink); 562 Track.Links.Add(NewLink); 563 end else raise Exception.Create('Trying to connect already connected track point'); 564 end; 565 566 procedure TTrackPoint.Disconnect(TrackPoint: TTrackPoint); 567 var 568 Index: Integer; 569 Link: TTrackLink; 570 begin 571 Index := NeighPoints.IndexOf(TrackPoint); 572 if NeighPoints.IndexOf(TrackPoint) <> -1 then begin 573 NeighPoints.Delete(Index); 574 TrackPoint.NeighPoints.Remove(Self); 575 576 // Remove link from both track points 577 Link := NeighLinks.SearchPoints(Self, TrackPoint); 578 NeighLinks.Remove(Link); 579 TrackPoint.NeighLinks.Remove(Link); 580 Track.Links.Remove(Link); 581 end else raise Exception.Create('Trying to disconnect not connected track point'); 582 end; 519 583 520 584 function TTrackPoint.GetDown: TTrackPoint; … … 573 637 NeighPoints := TTrackPoints.Create; 574 638 NeighPoints.OwnsObjects := False; 639 NeighLinks := TTrackLinks.Create; 640 NeighLinks.OwnsObjects := False; 575 641 end; 576 642 577 643 destructor TTrackPoint.Destroy; 578 644 begin 645 NeighLinks.Free; 579 646 NeighPoints.Free; 580 647 inherited Destroy; … … 667 734 L := Step; 668 735 repeat 669 Result.Position := Point(Trunc(Engine.Map.Size.X / 2 + Cos(Angle) * L ),736 Result.Position := Point(Trunc(Engine.Map.Size.X / 2 + Cos(Angle) * L * 1.5), 670 737 Trunc(Engine.Map.Size.Y / 2 + Sin(Angle) * L)); 671 738 MinD := High(Integer); … … 772 839 773 840 if Assigned(LineStationDown) then 774 RouteTrack(NewLineStation.TrackPoint.GetDown, NewLineStation.TrackPoint);841 Track.RouteTrack(NewLineStation.TrackPoint.GetDown, NewLineStation.TrackPoint); 775 842 if Assigned(LineStationUp) then 776 RouteTrack(NewLineStation.TrackPoint, NewLineStation.TrackPoint.GetUp);843 Track.RouteTrack(NewLineStation.TrackPoint, NewLineStation.TrackPoint.GetUp); 777 844 778 845 // Place one train if at least two stations present … … 836 903 837 904 if ((Index - 1) >= 0) and (Index < Track.Points.Count) then 838 RouteTrack(TTrackPoint(Track.Points[Index - 1]), TTrackPoint(Track.Points[Index]));905 Track.RouteTrack(TTrackPoint(Track.Points[Index - 1]), TTrackPoint(Track.Points[Index])); 839 906 840 907 ALineStation.MapStation.Lines.Remove(Self); … … 860 927 end; 861 928 862 procedure T MetroLine.RouteTrack(TP1, TP2: TTrackPoint);929 procedure TTrack.RouteTrack(TP1, TP2: TTrackPoint); 863 930 var 864 931 NewTrackPoint: TTrackPoint; … … 868 935 begin 869 936 RemoveTrackBetween(TP1, TP2); 870 Index1 := Track.Points.IndexOf(TP1);871 Index2 := Track.Points.IndexOf(TP2);872 P1 := TTrackPoint( Track.Points[Index1]).Position;873 P2 := TTrackPoint( Track.Points[Index2]).Position;874 NewTrackPoint := Track.Points.AddNew;937 Index1 := Points.IndexOf(TP1); 938 Index2 := Points.IndexOf(TP2); 939 P1 := TTrackPoint(Points[Index1]).Position; 940 P2 := TTrackPoint(Points[Index2]).Position; 941 NewTrackPoint := Points.AddNew; 875 942 Delta := Point(P2.X - P1.X, P2.Y - P1.Y); 876 943 if Abs(Delta.X) > Abs(Delta.Y) then begin … … 881 948 NewTrackPoint.PositionDesigned := NewTrackPoint.Position; 882 949 end; 883 Track.Points.Insert(Index1 + 1, NewTrackPoint);884 end; 885 886 procedure T MetroLine.RemoveTrackBetween(TP1, TP2: TTrackPoint);950 Points.Insert(Index1 + 1, NewTrackPoint); 951 end; 952 953 procedure TTrack.RemoveTrackBetween(TP1, TP2: TTrackPoint); 887 954 var 888 955 Index1, Index2: Integer; … … 890 957 I: Integer; 891 958 begin 892 Index1 := Track.Points.IndexOf(TP1);893 Index2 := Track.Points.IndexOf(TP2);959 Index1 := Points.IndexOf(TP1); 960 Index2 := Points.IndexOf(TP2); 894 961 if (Index1 = -1) then 895 962 raise Exception.Create('TrackPoint1 not found'); … … 902 969 end; 903 970 for I := 1 to Index2 - Index1 - 1 do 904 Track.Points.Delete(Index1 + 1);971 Points.Delete(Index1 + 1); 905 972 end; 906 973 … … 1056 1123 if Index > 0 then begin 1057 1124 NewTrack := TTrackLink.Create; 1058 NewTrack.PointDown := TTrackPoint(Line.Track.Points[Index - 1]); 1059 NewTrack.PointUp := TTrackPoint(Line.Track.Points[Index]); 1125 NewTrack.Points.Count := 2; 1126 NewTrack.Points[0] := TTrackPoint(Line.Track.Points[Index - 1]); 1127 NewTrack.Points[1] := TTrackPoint(Line.Track.Points[Index]); 1060 1128 Tracks.Add(NewTrack); 1061 1129 end; 1062 1130 if Index < (Line.Track.Points.Count - 1) then begin 1063 1131 NewTrack := TTrackLink.Create; 1064 NewTrack.PointDown := TTrackPoint(Line.Track.Points[Index + 1]); 1065 NewTrack.PointUp := TTrackPoint(Line.Track.Points[Index]); 1132 NewTrack.Points.Count := 2; 1133 NewTrack.Points[0] := TTrackPoint(Line.Track.Points[Index + 1]); 1134 NewTrack.Points[1] := TTrackPoint(Line.Track.Points[Index]); 1066 1135 Tracks.Add(NewTrack); 1067 1136 end; … … 1071 1140 TPAngleGroup := TTrackPointsAngleGroup.Create; 1072 1141 for I := 0 to Tracks.Count - 1 do begin 1073 Angle := ArcTan2(TTrack Link(Tracks[I]).PointDown.PositionDesigned.Y - Position.Y,1074 TTrack Link(Tracks[I]).PointDown.PositionDesigned.X - Position.X);1142 Angle := ArcTan2(TTrackPoint(TTrackLink(Tracks[I]).Points[0]).PositionDesigned.Y - Position.Y, 1143 TTrackPoint(TTrackLink(Tracks[I]).Points[0]).PositionDesigned.X - Position.X); 1075 1144 GroupItem := TPAngleGroup.SearchAngle(Angle); 1076 1145 if not Assigned(GroupItem) then begin … … 1092 1161 Shift.X := Trunc(MetroLineThickness * Cos(HAngle) * (J - (Tracks.Count - 1) / 2)); 1093 1162 Shift.Y := Trunc(MetroLineThickness * Sin(HAngle) * (J - (Tracks.Count - 1) / 2)); 1094 PointDown.PositionShift := Shift;1095 PointUp.PositionShift := Shift;1163 TTrackPoint(Points[0]).PositionShift := Shift; 1164 TTrackPoint(Points[1]).PositionShift := Shift; 1096 1165 end; 1097 1166 end; … … 1236 1305 begin 1237 1306 Result := TTrackLink.Create; 1238 Result.PointDown := nil; 1239 Result.PointUp := nil; 1307 Result.Points.Count := 2; 1308 Result.Points[0] := nil; 1309 Result.Points[1] := nil; 1240 1310 I := 0; 1241 1311 MinD := High(Integer); … … 1246 1316 if (D < MinD) and (D < TrackClickDistance) then begin 1247 1317 MinD := D; 1248 Result.Point Down:= TTrackPoint(Track.Points[T - 1]);1249 Result.Point Up:= TTrackPoint(Track.Points[T]);1318 Result.Points[0] := TTrackPoint(Track.Points[T - 1]); 1319 Result.Points[1] := TTrackPoint(Track.Points[T]); 1250 1320 end; 1251 1321 end; … … 1849 1919 end; 1850 1920 FocusedTrack := GetTrackOnPos(View.PointDestToSrc(Position)); 1851 if Assigned(FocusedTrack.Point Down) then begin1852 SelectedTrain.Line := FocusedTrack.PointDown.Track.Line;1921 if Assigned(FocusedTrack.Points[0]) then begin 1922 SelectedTrain.Line := TTrackPoint(FocusedTrack.Points[0]).Track.Line; 1853 1923 SelectedTrain.Line.Trains.Add(SelectedTrain); 1854 SelectedTrain.BaseTrackPoint := FocusedTrack.PointDown;1924 SelectedTrain.BaseTrackPoint := TTrackPoint(FocusedTrack.Points[0]); 1855 1925 end else 1856 if Assigned(FocusedTrack.Point Down) then begin1857 SelectedTrain.Line := FocusedTrack.PointUp.Track.Line;1926 if Assigned(FocusedTrack.Points[1]) then begin 1927 SelectedTrain.Line := TTrackPoint(FocusedTrack.Points[1]).Track.Line; 1858 1928 SelectedTrain.Line.Trains.Add(SelectedTrain); 1859 SelectedTrain.BaseTrackPoint := FocusedTrack.PointUp;1929 SelectedTrain.BaseTrackPoint := TTrackPoint(FocusedTrack.Points[1]); 1860 1930 end; 1861 1931 FocusedTrack.Free; … … 1916 1986 // Line selection 1917 1987 Track := GetTrackOnPos(View.PointDestToSrc(Position)); 1918 if Assigned(Track) and Assigned(Track.Point Down) and Assigned(Track.PointUp) then begin1919 SelectedLine := T rack.PointDown.Track.Line;1920 1921 TrackStationDown := T rack.PointDown;1988 if Assigned(Track) and Assigned(Track.Points[0]) and Assigned(Track.Points[1]) then begin 1989 SelectedLine := TTrackPoint(Track.Points[0]).Track.Line; 1990 1991 TrackStationDown := TTrackPoint(Track.Points[0]); 1922 1992 NewIndex := TrackStationDown.Track.Points.IndexOf(TrackStationDown); 1923 1993 while Assigned(TrackStationDown) and (not Assigned(TrackStationDown.LineStation)) do begin … … 1926 1996 else TrackStationDown := nil; 1927 1997 end; 1928 TrackStationUp := T rack.PointUp;1998 TrackStationUp := TTrackPoint(Track.Points[1]); 1929 1999 NewIndex := TrackStationUp.Track.Points.IndexOf(TrackStationDown); 1930 2000 while Assigned(TrackStationUp) and (not Assigned(TrackStationUp.LineStation)) do begin
Note:
See TracChangeset
for help on using the changeset viewer.