Changeset 105


Ignore:
Timestamp:
Oct 4, 2022, 11:25:28 AM (2 years ago)
Author:
chronos
Message:
  • Modified: Improved trains placement behavior by mouse drag and drop.
Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Install/snap/local/build.sh

    r85 r105  
    44
    55pushd ../../..
    6 snapcraft --debug --use-lxd $@
     6snapcraft --debug --use-lxd -v $@
    77popd
    88
  • trunk/Install/snap/snapcraft.yaml

    r85 r105  
    3636      install -s -m 755 BigMetro $SNAPCRAFT_PART_INSTALL/usr/bin
    3737      install -d -m 755 $SNAPCRAFT_PART_INSTALL/usr/share/BigMetro/languages
    38       install -m 755 Languages/BigMetro.po $SNAPCRAFT_PART_INSTALL/usr/share/BigMetro/languages
    39       install -m 755 Languages/BigMetro.cs.po $SNAPCRAFT_PART_INSTALL/usr/share/BigMetro/languages
     38      install -m 755 Languages/*.pot $SNAPCRAFT_PART_INSTALL/usr/share/BigMetro/languages
     39      install -m 755 Languages/*.po $SNAPCRAFT_PART_INSTALL/usr/share/BigMetro/languages
    4040      install -d -m 755 $SNAPCRAFT_PART_INSTALL/usr/share/applications
    4141      install -m 755 Install/snap/local/BigMetro.desktop $SNAPCRAFT_PART_INSTALL/usr/share/applications
  • trunk/Packages/Common/UGeometric.pas

    r93 r105  
    2222function AddPoint(const P1, P2: TPoint): TPoint;
    2323function SubPoint(const P1, P2: TPoint): TPoint;
    24 function PointToLineDistance(const P, V, W: TPoint): Integer;
     24function PointToLineDistance(const P, V, W: TPoint; out Intersect: TPoint): Integer;
    2525function ComparePoint(P1, P2: TPoint): Boolean;
    2626function RotatePoint(Center, P: TPoint; Angle: Double): TPoint;
     
    5959end;
    6060
    61 function PointToLineDistance(const P, V, W: TPoint): Integer;
     61function PointToLineDistance(const P, V, W: TPoint; out Intersect: TPoint): Integer;
    6262var
    6363  l2, t: Double;
     
    7777  if T < 0 then begin
    7878    Result := Distance(P, V);       // Beyond the 'v' end of the segment
    79     exit;
     79    Intersect := V;
     80    Exit;
    8081  end
    8182  else if T > 1 then begin
    8283    Result := Distance(P, W);  // Beyond the 'w' end of the segment
     84    Intersect := W;
    8385    Exit;
    8486  end;
     
    8688  TT.Y := Trunc(V.Y + T * (W.Y - V.Y));
    8789  Result := Distance(P, TT);
     90  Intersect := TT;
    8891end;
    8992
  • trunk/UEngine.pas

    r103 r105  
    126126    TargetStation: TLineStation;
    127127    Carriages: TMetroCarriages;
     128    procedure FindTargetStation;
    128129    function GetTargetStationDistance: Integer;
    129130    function GetPosition: TPoint;
     
    193194    function GetExistStationShapes: TStationShapeSet;
    194195    function GetStationOnPos(Pos: TPoint): TMapStation;
    195     function GetTrackOnPos(Pos: TPoint): TTrackLink;
     196    function GetTrackOnPos(Pos: TPoint; out Intersect: TPoint): TTrackLink;
    196197    function GetTrainOnPos(Pos: TPoint): TMetroTrain;
    197198    function GetCarriageOnPos(Pos: TPoint): TMetroCarriage;
     
    217218    procedure SetDarkMode(AValue: Boolean);
    218219    procedure SetState(AValue: TGameState);
    219     procedure TrainMovement;
     220    procedure TrainsMovement;
     221    procedure TrainMovement(Train: TMetroTrain);
    220222    function GetUnusedLine: TMetroLine;
    221223    procedure ShiftTrackPoints;
     
    781783end;
    782784
     785procedure TMetroTrain.FindTargetStation;
     786var
     787  TP: TTrackPoint;
     788begin
     789  if Direction > 0 then begin
     790    TP := TrackPosition.BaseTrackPoint.GetUp;
     791    if Assigned(TP) then begin
     792      TargetStation := TLineStation(TP.OwnerPoint);
     793    end else begin
     794      TP := TrackPosition.BaseTrackPoint.GetDown;
     795      if Assigned(TP) then
     796        TargetStation := TLineStation(TP.OwnerPoint);
     797    end;
     798  end else
     799  if Direction < 0 then begin
     800    if Assigned(TrackPosition.BaseTrackPoint.OwnerPoint) then
     801      TargetStation := TLineStation(TrackPosition.BaseTrackPoint.OwnerPoint)
     802    else begin
     803      TP := TrackPosition.BaseTrackPoint.GetUp;
     804      if Assigned(TP) then
     805        TargetStation := TLineStation(TP.OwnerPoint);
     806    end;
     807  end;
     808  LastPosDelta := Abs(GetTargetStationDistance);
     809end;
     810
    783811function TMetroTrain.GetTargetStationDistance: Integer;
    784812var
     
    789817  Result := 0;
    790818  if Assigned(TrackPosition.BaseTrackPoint) and Assigned(TargetStation) then begin
    791   Current := Line.Track.Points.IndexOf(TrackPosition.BaseTrackPoint);
    792   Target := Line.Track.Points.IndexOf(TargetStation.TrackPoint);
    793   if Current < Target then begin
    794     for I := Current to Target - 1 do
    795       Result := Result + Line.Track.Points[I].GetDistance;
    796     Result := Result - Trunc(TrackPosition.RelPos);
    797   end else
    798   if Current > Target then begin
    799     for I := Current - 1 downto Target do
    800       Result := Result + Line.Track.Points[I].GetDistance;
    801     Result := Result + Trunc(TrackPosition.RelPos);
    802   end else Result := Trunc(TrackPosition.RelPos);
     819    Current := Line.Track.Points.IndexOf(TrackPosition.BaseTrackPoint);
     820    Target := Line.Track.Points.IndexOf(TargetStation.TrackPoint);
     821    if Current < Target then begin
     822      for I := Current to Target - 1 do
     823        Result := Result + Line.Track.Points[I].GetDistance;
     824      Result := Result - Trunc(TrackPosition.RelPos);
     825    end else
     826    if Current > Target then begin
     827      for I := Current - 1 downto Target do
     828        Result := Result + Line.Track.Points[I].GetDistance;
     829      Result := Result + Trunc(TrackPosition.RelPos);
     830    end else Result := Trunc(TrackPosition.RelPos);
    803831  end;
    804832end;
     
    11161144end;
    11171145
    1118 function TEngine.GetTrackOnPos(Pos: TPoint): TTrackLink;
     1146function TEngine.GetTrackOnPos(Pos: TPoint; out Intersect: TPoint): TTrackLink;
    11191147var
    11201148  I: Integer;
     
    11221150  D: Integer;
    11231151  MinD: Integer;
     1152  TempIntersect: TPoint;
    11241153begin
    11251154  Result := TTrackLink.Create;
     
    11301159  MinD := High(Integer);
    11311160  while (I < Lines.Count) do
    1132   with Lines[I] do begin
     1161  with TMetroLine(Lines[I]) do begin
    11331162    for T := 1 to Track.Points.Count - 1 do begin
    1134       D := PointToLineDistance(Pos, Track.Points[T - 1].Position, Track.Points[T].Position);
     1163      D := PointToLineDistance(Pos, Track.Points[T - 1].Position, Track.Points[T].Position,
     1164        TempIntersect);
    11351165      if (D < MinD) and (D < TrackClickDistance) then begin
    11361166        MinD := D;
    11371167        Result.Points[0] := Track.Points[T - 1];
    11381168        Result.Points[1] := Track.Points[T];
     1169        Intersect := TempIntersect;
    11391170      end;
    11401171    end;
     
    14291460end;
    14301461
    1431 procedure TEngine.TrainMovement;
     1462procedure TEngine.TrainsMovement;
    14321463var
    14331464  I: Integer;
     1465begin
     1466  for I := 0 to Trains.Count - 1 do
     1467    TrainMovement(Trains[I]);
     1468end;
     1469
     1470procedure TEngine.TrainMovement(Train: TMetroTrain);
     1471var
    14341472  J: Integer;
    14351473  CurrentStation: TLineStation;
     
    14391477  TargetStationIndex: Integer;
    14401478  PosChange: Double;
    1441   TP: TTrackPoint;
    14421479  Done: Boolean;
    14431480begin
    1444   // Move trains
    1445   for I := 0 to Trains.Count - 1 do
    1446   with TMetroTrain(Trains[I]) do begin
    1447     if not Assigned(TargetStation) and Assigned(TrackPosition.BaseTrackPoint) then begin
    1448       if (Direction <> 1) and (Direction <> -1) then Direction := 1
    1449         else Direction := -Direction;
    1450       TP := TrackPosition.BaseTrackPoint.GetUp;
    1451       if Assigned(TP) then TargetStation := TLineStation(TP.OwnerPoint)
    1452       else begin
    1453         TP := TrackPosition.BaseTrackPoint.GetDown;
    1454         if Assigned(TP) then TargetStation := TLineStation(TP.OwnerPoint);
    1455       end;
    1456     end;
     1481  with Train do begin
    14571482    if Assigned(Line) then begin
    14581483      if InStation then begin
     
    15671592      end else begin
    15681593        PosChange := Direction + Trunc(Direction * TrainSpeed * (Time - LastTrainMoveTime));
    1569         TrackPosition.RelPos := TrackPosition.RelPos + PosChange;
    15701594        LastTrainMoveTime := Time;
    15711595        Redraw;
    1572         if Assigned(TrackPosition.BaseTrackPoint) then
    1573         while (Direction = -1) and (TrackPosition.RelPos < 0) do begin
    1574           if TrackPosition.BaseTrackPoint <> Line.LineStations.First.TrackPoint then begin
    1575             TrackPosition.BaseTrackPoint := TrackPosition.BaseTrackPoint.GetNeighDown;
    1576             if Assigned(TrackPosition.BaseTrackPoint) then
    1577               TrackPosition.RelPos := TrackPosition.RelPos + TrackPosition.BaseTrackPoint.GetDistance
    1578             else begin
    1579               TrackPosition.BaseTrackPoint := Line.LineStations.First.TrackPoint;
    1580               TrackPosition.RelPos := 0;
    1581             end;
    1582           end else
    1583           if Line.IsCircular then begin
    1584             TrackPosition.BaseTrackPoint := Line.LineStations.Last.TrackPoint;
    1585             TrackPosition.RelPos := TrackPosition.RelPos + TrackPosition.BaseTrackPoint.GetDistance;
    1586           end else begin
     1596        TrackPosition.Move(PosChange);
     1597
     1598        if Assigned(TargetStation) then begin
     1599          PosDelta := Abs(GetTargetStationDistance);
     1600          if PosDelta >= LastPosDelta then begin
     1601            // We are getting far from station, stop at station
     1602            TrackPosition.BaseTrackPoint := TargetStation.TrackPoint;
    15871603            TrackPosition.RelPos := 0;
    1588             Break;
     1604            InStation := True;
     1605            StationStopTime := Time;
     1606            Redraw;
    15891607          end;
    1590         end;
    1591         if Assigned(TrackPosition.BaseTrackPoint) then
    1592         while (Direction = 1) and (TrackPosition.RelPos > TrackPosition.BaseTrackPoint.GetDistance) do begin
    1593           if TrackPosition.BaseTrackPoint <> Line.LineStations.Last.TrackPoint then begin
    1594             TrackPosition.RelPos := TrackPosition.RelPos - TrackPosition.BaseTrackPoint.GetDistance;
    1595             TrackPosition.BaseTrackPoint := TrackPosition.BaseTrackPoint.GetNeighUp;
    1596             if not Assigned(TrackPosition.BaseTrackPoint) then begin
    1597               TrackPosition.BaseTrackPoint := Line.LineStations.Last.TrackPoint;
    1598               TrackPosition.RelPos := 0;
    1599             end;
    1600           end else
    1601           if Line.IsCircular then begin
    1602             TrackPosition.RelPos := TrackPosition.RelPos - TrackPosition.BaseTrackPoint.GetDistance;
    1603             TrackPosition.BaseTrackPoint := Line.LineStations.First.TrackPoint;
    1604           end else begin
    1605             TrackPosition.RelPos := TrackPosition.BaseTrackPoint.GetDistance;
    1606             Break;
    1607           end;
    1608         end;
    1609         PosDelta := Abs(GetTargetStationDistance);
    1610         if PosDelta >= LastPosDelta then begin
    1611           // We are getting far from station, stop at station
    1612           TrackPosition.BaseTrackPoint := TargetStation.TrackPoint;
    1613           TrackPosition.RelPos := 0;
    1614           InStation := True;
    1615           StationStopTime := Time;
    1616           Redraw;
    16171608        end;
    16181609        LastPosDelta := PosDelta;
     
    22532244        end;
    22542245      end;
     2246
     2247{      // Target station links
     2248      if Assigned(TargetStation) then begin
     2249        Canvas.Pen.Width := 1;
     2250        Canvas.Pen.Style := psSolid;
     2251        Canvas.Pen.Color := Colors.MenuItemText;
     2252        Canvas.MoveTo(Pos);
     2253        Canvas.LineTo(TargetStation.TrackPoint.Position);
     2254      end;
     2255}
    22552256    end;
    22562257  end;
     
    28942895    end;
    28952896
    2896     TrainMovement;
     2897    TrainsMovement;
    28972898
    28982899    // Game over
     
    29822983  FocusedTrain: TMetroTrain;
    29832984  FocusedStation: TMapStation;
     2985  Intersect: TPoint;
    29842986begin
    29852987  if Button = mbLeft then begin
     
    30203022          end;
    30213023        end;
    3022         FocusedTrack := GetTrackOnPos(View.PointDestToSrc(Position));
     3024        FocusedTrack := GetTrackOnPos(View.PointDestToSrc(Position), Intersect);
    30233025        if Assigned(FocusedTrack.Points[0]) then begin
    30243026          SelectedTrain.Line := TMetroLine(FocusedTrack.Points[0].Track.Owner);
    30253027          SelectedTrain.Line.Trains.Add(SelectedTrain);
    30263028          SelectedTrain.TrackPosition.BaseTrackPoint := FocusedTrack.Points[0];
    3027         end else
    3028         if Assigned(FocusedTrack.Points[1]) then begin
    3029           SelectedTrain.Line := TMetroLine(FocusedTrack.Points[1].Track.Owner);
    3030           SelectedTrain.Line.Trains.Add(SelectedTrain);
    3031           SelectedTrain.TrackPosition.BaseTrackPoint := FocusedTrack.Points[1];
     3029          SelectedTrain.TrackPosition.RelPos := Distance(FocusedTrack.Points[0].Position,
     3030            Intersect);
     3031          if (SelectedTrain.Direction <> 1) and (SelectedTrain.Direction <> -1) then
     3032            SelectedTrain.Direction := 1
     3033            else SelectedTrain.Direction := -SelectedTrain.Direction;
     3034          SelectedTrain.FindTargetStation;
     3035          SelectedTrain.LastTrainMoveTime := Time;
    30323036        end;
    30333037        FocusedTrack.Free;
     
    30933097  Track: TTrackLink;
    30943098  NewIndex: Integer;
     3099  Intersection: TPoint;
    30953100begin
    30963101  if (Button = mbLeft) then begin
     
    31493154
    31503155      // Line selection
    3151       Track := GetTrackOnPos(View.PointDestToSrc(Position));
     3156      Track := GetTrackOnPos(View.PointDestToSrc(Position), Intersection);
    31523157      if Assigned(Track) and Assigned(Track.Points[0]) and Assigned(Track.Points[1]) then begin
    31533158        SelectedLine := TMetroLine(Track.Points[0].Track.Owner);
     
    31793184const
    31803185  KeyEsc = 27;
     3186{$IFDEF DEBUG}
    31813187  KeyF2 = 113;
    31823188  KeyF3 = 114;
     
    31863192  KeyF7 = 118;
    31873193  KeyF8 = 119;
     3194{$ENDIF}
    31883195  KeyT = 84;
    31893196  KeyC = 67;
Note: See TracChangeset for help on using the changeset viewer.