- Timestamp:
- Mar 27, 2015, 1:09:12 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UEngine.pas
r13 r14 15 15 TMetroPassengers = class; 16 16 TMetroLines = class; 17 TMetroLine = class; 17 18 TMetroTrains = class; 18 19 … … 41 42 end; 42 43 43 TTrackPoint = record 44 TTrackPoint = class 45 Line: TMetroLine; 44 46 StationIndex: Integer; 45 47 Point: TPoint; 48 Pending: Boolean; 49 end; 50 51 TTrackPoints = class(TObjectList) 52 Line: TMetroLine; 46 53 end; 47 54 … … 53 60 Stations: TMetroStations; 54 61 Trains: TMetroTrains; 55 TrackPoints: array of TTrackPoint;62 TrackPoints: TTrackPoints; 56 63 procedure ConnectStation(Station: TMetroStation); 57 64 procedure DisconnectStation(Station: TMetroStation); … … 190 197 StationMaxDistance = 300; 191 198 MaxWaitingPassengers = 10; 199 MetroLineThickness = 13; 192 200 193 201 implementation … … 295 303 Train: TMetroTrain; 296 304 StationIndex: Integer; 305 NewTrackPoint: TTrackPoint; 297 306 begin 298 307 Stations.Add(Station); … … 300 309 StationIndex := Stations.Count - 1; 301 310 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); 305 316 end else begin 306 317 if Stations.Count > 1 then … … 329 340 Station.Lines.Remove(Self); 330 341 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, 332 343 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; 335 346 // Remove trains if less then two stations 336 347 if Stations.Count < 2 then … … 346 357 var 347 358 Delta: TPoint; 359 NewTrackPoint: TTrackPoint; 348 360 begin 349 361 Delta := Point(P2.X - P1.X, P2.Y - P1.Y); 350 362 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); 354 368 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); 362 380 end; 363 381 … … 367 385 begin 368 386 Result := 0; 369 for I := 0 to High(TrackPoints)do387 for I := 0 to TrackPoints.Count - 1 do 370 388 if I > 0 then 371 Result := Result + Distance(T rackPoints[I].Point, TrackPoints[I - 1].Point);389 Result := Result + Distance(TTrackPoint(TrackPoints[I]).Point, TTrackPoint(TrackPoints[I - 1]).Point); 372 390 end; 373 391 … … 377 395 begin 378 396 Result := 0; 379 for I := 0 to High(TrackPoints)do begin397 for I := 0 to TrackPoints.Count - 1 do begin 380 398 if I > 0 then 381 Result := Result + Distance(T rackPoints[I].Point, TrackPoints[I - 1].Point);382 if T rackPoints[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; 383 401 end; 384 402 end; … … 390 408 Trains := TMetroTrains.Create; 391 409 Trains.OwnsObjects := False; 410 TrackPoints := TTrackPoints.Create; 411 TrackPoints.Line := Self; 392 412 end; 393 413 394 414 destructor TMetroLine.Destroy; 395 415 begin 416 TrackPoints.Free; 396 417 Trains.Free; 397 418 Stations.Free; … … 427 448 with Line do begin 428 449 Sum := 0; 429 if Length(TrackPoints)> 1 then430 for I := 1 to High(TrackPoints)do begin431 D := Distance(T rackPoints[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); 432 453 if (Sum + D) > Self.TrackPos then begin 433 R := Point(T rackPoints[I].Point.X - TrackPoints[I - 1].Point.X,434 T rackPoints[I].Point.Y - TrackPoints[I - 1].Point.Y);435 Result := Point(Trunc(T rackPoints[I - 1].Point.X + R.X * (TrackPos - Sum) / D),436 Trunc(T rackPoints[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)); 437 458 Exit; 438 459 end else Sum := Sum + D; 439 460 end; 440 if Length(TrackPoints)> 0 then441 Result := T rackPoints[High(TrackPoints)].Point;461 if TrackPoints.Count > 0 then 462 Result := TTrackPoint(TrackPoints.Last).Point; 442 463 end; 443 464 end; … … 453 474 with Line do begin 454 475 Sum := 0; 455 if Length(TrackPoints)> 1 then456 for I := 1 to High(TrackPoints)do begin457 D := Distance(T rackPoints[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); 458 479 if (Sum + D) > Self.TrackPos then begin 459 Result := ArcTan2(T rackPoints[I].Point.Y - TrackPoints[I - 1].Point.Y,460 T rackPoints[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); 461 482 Exit; 462 483 end else Sum := Sum + D; … … 589 610 while (I < Lines.Count) do 590 611 with TMetroLine(Lines[I]) do begin 591 for T := 1 to High(TrackPoints)do begin592 D := PointToLineDistance(Pos, T rackPoints[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); 593 614 if D < Distance then begin 594 615 Result := TMetroLine(Lines[I]); … … 1167 1188 Canvas.Pen.Color := Color; 1168 1189 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))); 1177 1200 Canvas.LineTo(EndPoint); 1178 1201 Canvas.MoveTo(Point(Round(EndPoint.X + Cos(Angle + Pi / 2) * EndStationLength / 3), … … 1180 1203 Canvas.LineTo(Point(Round(EndPoint.X + Cos(Angle - Pi / 2) * EndStationLength / 3), 1181 1204 Round(EndPoint.Y + Sin(Angle - Pi / 2) * EndStationLength / 3))); 1205 Canvas.Pen.EndCap := pecRound; 1182 1206 end; 1183 1207 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))); 1189 1215 Canvas.LineTo(EndPoint); 1190 1216 Canvas.MoveTo(Point(Round(EndPoint.X - Cos(Angle + Pi / 2) * EndStationLength / 3), … … 1192 1218 Canvas.LineTo(Point(Round(EndPoint.X - Cos(Angle - Pi / 2) * EndStationLength / 3), 1193 1219 Round(EndPoint.Y - Sin(Angle - Pi / 2) * EndStationLength / 3))); 1220 Canvas.Pen.EndCap := pecRound; 1194 1221 end; 1195 1222
Note:
See TracChangeset
for help on using the changeset viewer.