Changeset 106 for trunk


Ignore:
Timestamp:
Oct 4, 2022, 3:40:36 PM (2 years ago)
Author:
chronos
Message:
  • Added: Show train direction before placement on track.
Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/UEngine.pas

    r105 r106  
    128128    procedure FindTargetStation;
    129129    function GetTargetStationDistance: Integer;
    130     function GetPosition: TPoint;
    131     function GetAngle: Double;
    132     function GetVector: TVector;
    133130    constructor Create;
    134131    destructor Destroy; override;
     
    190187    CarriageCountVisible: Boolean;
    191188    TerminalCountVisible: Boolean;
     189    GrabbedTrainLastTrackPosition: TTrackPosition;
     190    GrabbedTrainDirection: Integer;
    192191    function GetServedDaysCount: Integer;
    193192    procedure ResizeView;
     
    326325  MaxPassengersOveloadTime = 2;
    327326  MetroLineThickness = 13;
    328   TrackClickDistance = 20;
     327  TrackClickDistance = 10;
    329328  EndStationLength = 50;
    330329  ShowDistances = False;
     
    404403function TMetroCarriage.GetVector: TVector;
    405404begin
    406   Result := Train.GetVector;
     405  Result := Train.TrackPosition.GetVector;
    407406  Result.Position := AddPoint(Result.Position, Point(TrainSize, TrainSize));
    408407end;
     
    829828      Result := Result + Trunc(TrackPosition.RelPos);
    830829    end else Result := Trunc(TrackPosition.RelPos);
    831   end;
    832 end;
    833 
    834 function TMetroTrain.GetPosition: TPoint;
    835 var
    836   D: Integer;
    837   Delta: TPoint;
    838   UpPoint: TTrackPoint;
    839 begin
    840   Result := Point(0, 0);
    841   if Assigned(TrackPosition.BaseTrackPoint) then
    842   with TrackPosition.BaseTrackPoint do begin
    843     UpPoint := TrackPosition.BaseTrackPoint.GetNeighUp;
    844     if Assigned(UpPoint) then begin
    845       D := Distance(UpPoint.Position, Position);
    846       if D > 0 then begin
    847         Delta := SubPoint(UpPoint.Position, Position);
    848         Result := Point(Trunc(Position.X + Delta.X * TrackPosition.RelPos / D),
    849           Trunc(Position.Y + Delta.Y * TrackPosition.RelPos / D));
    850       end;
    851     end;
    852   end;
    853 end;
    854 
    855 function TMetroTrain.GetAngle: Double;
    856 var
    857   UpPoint: TTrackPoint;
    858 begin
    859   Result := 0;
    860   if Assigned(TrackPosition.BaseTrackPoint) then
    861   with TrackPosition.BaseTrackPoint do begin
    862     UpPoint := TrackPosition.BaseTrackPoint.GetNeighUp;
    863     if Assigned(UpPoint) then begin
    864       Result := ArcTan2(UpPoint.Position.Y - Position.Y,
    865         UpPoint.Position.X - Position.X);
    866     end;
    867   end;
    868 end;
    869 
    870 function TMetroTrain.GetVector: TVector;
    871 var
    872   D: Integer;
    873   UpPoint: TTrackPoint;
    874 begin
    875   Result.Position := Point(0, 0);
    876   if Assigned(TrackPosition.BaseTrackPoint) then
    877   with TrackPosition.BaseTrackPoint do begin
    878     UpPoint := TrackPosition.BaseTrackPoint.GetNeighUp;
    879     if Assigned(UpPoint) then begin
    880       D := Distance(UpPoint.Position, Position);
    881       if D > 0 then begin
    882         Result.Direction := SubPoint(UpPoint.Position, Position);
    883         Result.Position := Point(Trunc(Position.X + Result.Direction.X * TrackPosition.RelPos / D),
    884           Trunc(Position.Y + Result.Direction.Y * TrackPosition.RelPos / D));
    885       end;
    886     end;
    887830  end;
    888831end;
     
    11831126  MinDistance := High(Integer);
    11841127  for I := 0 to Trains.Count - 1 do
    1185   with Trains[I] do begin
    1186     D := Distance(GetPosition, Pos);
     1128  with TMetroTrain(Trains[I]) do begin
     1129    D := Distance(TrackPosition.GetVector.Position, Pos);
    11871130    if (D < (TrainSize div 2)) and (D < MinDistance) then begin
    11881131      Result := Trains[I];
     
    21962139      Canvas.Brush.Style := bsSolid;
    21972140      Canvas.Pen.Style := psClear;
    2198       Vector := GetVector;
     2141      Vector := TrackPosition.GetVector;
    21992142      Pos := Vector.Position;
    22002143      Angle := Vector.GetAngle;
     
    27812724  Pos: TPoint;
    27822725  Angle: Double;
     2726  FocusedTrack: TTrackLink;
     2727  Vector: TVector;
     2728  Intersect: TPoint;
     2729  TrackPosition: TTrackPosition;
    27832730begin
    27842731  // Show train grabbed by mouse
     2732  Angle := 0;
     2733  Pos := LastMousePos;
     2734
    27852735  if Assigned(SelectedTrain) then begin
     2736    FocusedTrack := GetTrackOnPos(View.PointDestToSrc(Pos), Intersect);
     2737    if Assigned(FocusedTrack.Points[0]) then begin
     2738      TrackPosition.BaseTrackPoint := FocusedTrack.Points[0];
     2739      TrackPosition.RelPos := Distance(FocusedTrack.Points[0].Position,
     2740        Intersect);
     2741      Vector := TrackPosition.GetVector;
     2742      Angle := Vector.GetAngle;
     2743
     2744      if TrackPosition.GetTrackPosition >
     2745        GrabbedTrainLastTrackPosition.GetTrackPosition then
     2746          GrabbedTrainDirection := 1
     2747      else
     2748      if TrackPosition.GetTrackPosition <
     2749        GrabbedTrainLastTrackPosition.GetTrackPosition then
     2750        GrabbedTrainDirection := -1;
     2751
     2752      GrabbedTrainLastTrackPosition := TrackPosition;
     2753    end;
     2754    FocusedTrack.Free;
     2755
     2756    if GrabbedTrainDirection = -1 then Angle := Angle + Pi;
    27862757    Canvas.Brush.Color := Colors.Text; //SelectedTrain.Line.Color;
    27872758    Canvas.Brush.Style := bsSolid;
    27882759    Canvas.Pen.Style := psClear;
    2789     Pos := LastMousePos;
    2790     Angle := 0;
    2791 
    27922760    Canvas.Polygon([
    27932761      RotatePoint(Pos, Point(Pos.X - TrainSize div 2, Pos.Y - TrainSize div 3), Angle),
    2794       RotatePoint(Pos, Point(Pos.X + TrainSize div 2, Pos.Y - TrainSize div 3), Angle),
    2795       RotatePoint(Pos, Point(Pos.X + TrainSize div 2, Pos.Y + TrainSize div 3), Angle),
     2762      RotatePoint(Pos, Point(Pos.X + TrainSize div 2 - 10, Pos.Y - TrainSize div 3), Angle),
     2763      RotatePoint(Pos, Point(Pos.X + TrainSize div 2, Pos.Y), Angle),
     2764      RotatePoint(Pos, Point(Pos.X + TrainSize div 2 - 10, Pos.Y + TrainSize div 3), Angle),
    27962765      RotatePoint(Pos, Point(Pos.X - TrainSize div 2, Pos.Y + TrainSize div 3), Angle)
    27972766    ]);
     
    30292998          SelectedTrain.TrackPosition.RelPos := Distance(FocusedTrack.Points[0].Position,
    30302999            Intersect);
    3031           if (SelectedTrain.Direction <> 1) and (SelectedTrain.Direction <> -1) then
    3032             SelectedTrain.Direction := 1
    3033             else SelectedTrain.Direction := -SelectedTrain.Direction;
     3000          SelectedTrain.Direction := GrabbedTrainDirection;
    30343001          SelectedTrain.FindTargetStation;
    30353002          SelectedTrain.LastTrainMoveTime := Time;
  • trunk/UTrack.pas

    r93 r106  
    1818    BaseTrackPoint: TTrackPoint;
    1919    RelPos: Double;
     20    function GetTrackPosition: Integer;
    2021    function GetVector: TVector;
    2122    procedure Move(Distance: Double);
     
    4344    function GetLinkDown: TTrackLink;
    4445    function GetLinkUp: TTrackLink;
     46    function GetTrackPosition: Integer;
     47
    4548    // Move to TTrackLink later
    4649    function GetDistance: Integer;
     
    109112
    110113{ TTrackPosition }
     114
     115function TTrackPosition.GetTrackPosition: Integer;
     116begin
     117  Result := Round(RelPos);
     118  if Assigned(BaseTrackPoint) then
     119    Result := Result + BaseTrackPoint.GetTrackPosition;
     120end;
    111121
    112122function TTrackPosition.GetVector: TVector;
     
    411421end;
    412422
     423function TTrackPoint.GetTrackPosition: Integer;
     424var
     425  Index: Integer;
     426  I: Integer;
     427begin
     428  Result := 0;
     429  Index := Track.Points.IndexOf(Self);
     430  for I := 0 to Index - 1 do
     431    Result := Result + Track.Points[I].GetDistance;
     432end;
     433
    413434function TTrackPoint.GetDistance: Integer;
    414435var
Note: See TracChangeset for help on using the changeset viewer.