- Timestamp:
- Apr 12, 2015, 2:32:33 PM (10 years ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UEngine.pas
r27 r28 143 143 TMetroTrain = class 144 144 private 145 FLine: TMetroLine; 145 146 LastPosDelta: Integer; 146 147 LastTrainMoveTime: TDateTime; 147 148 StationStopTime: TDateTime; 149 procedure SetLine(AValue: TMetroLine); 148 150 public 149 151 Passengers: TMetroPassengers; 150 Line: TMetroLine; 152 151 153 BaseTrackPoint: TTrackPoint; 152 154 RelPos: Double; … … 159 161 constructor Create; 160 162 destructor Destroy; override; 163 property Line: TMetroLine read FLine write SetLine; 161 164 end; 162 165 … … 187 190 end; 188 191 192 { TView } 193 189 194 TView = class 190 Size: TPoint; 191 Zoom: Double; 195 private 196 FDestRect: TRect; 197 FZoom: Double; 198 procedure SetDestRect(AValue: TRect); 199 procedure SetZoom(AValue: Double); 200 public 201 SourceRect: TRect; 202 constructor Create; 203 property DestRect: TRect read FDestRect write SetDestRect; 204 property Zoom: Double read FZoom write SetZoom; 192 205 end; 193 206 … … 213 226 function GetStationOnPos(Pos: TPoint): TMapStation; 214 227 function GetTrackOnPos(Pos: TPoint): TTrack; 228 function GetTrainOnPos(Pos: TPoint): TMetroTrain; 215 229 procedure DrawLine(Canvas: TCanvas; Pos: TPoint); 216 230 procedure DrawShape(Canvas: TCanvas; Position: TPoint; Shape: TStationShape; … … 233 247 View: TView; 234 248 SelectedLine: TMetroLine; 249 SelectedTrain: TMetroTrain; 235 250 TrackStationDown: TTrackPoint; 236 251 TrackStationUp: TTrackPoint; … … 285 300 UGeometric; 286 301 302 resourcestring 303 SZeroZoomNotAlowed = 'Zero zoom not allowed'; 304 305 { TView } 306 307 procedure TView.SetDestRect(AValue: TRect); 308 var 309 Diff: TPoint; 310 begin 311 if RectEquals(FDestRect, AValue) then Exit; 312 Diff := Point(Trunc((DestRect.Right - DestRect.Left) / Zoom - (AValue.Right - AValue.Left) / Zoom) div 2, 313 Trunc((DestRect.Bottom - DestRect.Top) / Zoom - (AValue.Bottom - AValue.Top) / Zoom) div 2); 314 FDestRect := AValue; 315 SourceRect := Bounds(SourceRect.Left + Diff.X, SourceRect.Top + Diff.Y, 316 Trunc((DestRect.Right - DestRect.Left) / Zoom), 317 Trunc((DestRect.Bottom - DestRect.Top) / Zoom)); 318 end; 319 320 procedure TView.SetZoom(AValue: Double); 321 begin 322 if FZoom = AValue then Exit; 323 if AValue = 0 then 324 raise Exception.Create(SZeroZoomNotAlowed); 325 FZoom := AValue; 326 SourceRect := Bounds(Trunc(SourceRect.Left + (SourceRect.Right - SourceRect.Left) div 2 - (DestRect.Right - DestRect.Left) / Zoom / 2), 327 Trunc(SourceRect.Top + (SourceRect.Bottom - SourceRect.Top) div 2 - (DestRect.Bottom - DestRect.Top) / Zoom / 2), 328 Trunc((DestRect.Right - DestRect.Left) / Zoom), 329 Trunc((DestRect.Bottom - DestRect.Top) / Zoom)); 330 end; 331 332 constructor TView.Create; 333 begin 334 Zoom := 1.5; 335 end; 336 287 337 { TTracks } 288 338 … … 361 411 NewIndex: Integer; 362 412 begin 413 Result := nil; 363 414 NewIndex := Line.TrackPoints.IndexOf(Self) - 1; 364 if NewIndex >= 0 then Result := TTrackPoint(Line.TrackPoints[NewIndex]) 365 else Result := nil; 415 if NewIndex >= 0 then Result := TTrackPoint(Line.TrackPoints[NewIndex]); 366 416 end; 367 417 … … 370 420 NewIndex: Integer; 371 421 begin 372 NewIndex := Line.TrackPoints.IndexOf(Self) + 1; 373 if NewIndex < Line.TrackPoints.Count then Result := TTrackPoint(Line.TrackPoints[NewIndex]) 374 else Result := nil; 422 Result := nil; 423 if Assigned(Line) then begin 424 NewIndex := Line.TrackPoints.IndexOf(Self) + 1; 425 if NewIndex < Line.TrackPoints.Count then Result := TTrackPoint(Line.TrackPoints[NewIndex]); 426 end; 375 427 end; 376 428 … … 731 783 732 784 { TMetroTrain } 785 786 procedure TMetroTrain.SetLine(AValue: TMetroLine); 787 begin 788 if FLine = AValue then Exit; 789 FLine := AValue; 790 if AValue = nil then begin 791 RelPos := 0; 792 BaseTrackPoint := nil; 793 TargetStation := nil; 794 end; 795 end; 733 796 734 797 function TMetroTrain.GetTargetStationDistance: Integer; … … 1013 1076 end; 1014 1077 Inc(I); 1078 end; 1079 end; 1080 1081 function TEngine.GetTrainOnPos(Pos: TPoint): TMetroTrain; 1082 var 1083 I: Integer; 1084 MinDistance: Integer; 1085 D: Integer; 1086 begin 1087 Result := nil; 1088 MinDistance := High(Integer); 1089 for I := 0 to Trains.Count - 1 do 1090 with TMetroTrain(Trains[I]) do begin 1091 D := Distance(GetPosition, Pos); 1092 if (D < (TrainSize div 2)) and (D < MinDistance) then begin 1093 Result := TMetroTrain(Trains[I]); 1094 MinDistance := D; 1095 end; 1015 1096 end; 1016 1097 end; … … 1201 1282 for I := 0 to Trains.Count - 1 do 1202 1283 with TMetroTrain(Trains[I]) do begin 1284 if not Assigned(TargetStation) and Assigned(BaseTrackPoint) then begin 1285 Direction := 1; 1286 TargetStation := BaseTrackPoint.GetUp.LineStation; 1287 end; 1203 1288 if Assigned(Line) then begin 1204 1289 if InStation then begin … … 1426 1511 ShapePos: TPoint; 1427 1512 begin 1428 1513 // Draw trains 1429 1514 for I := 0 to Trains.Count - 1 do 1430 1515 with TMetroTrain(Trains[I]) do begin … … 1576 1661 procedure TEngine.MouseUp(Button: TMouseButton; Position: TPoint); 1577 1662 var 1578 Station: TMapStation; 1579 Line: TMetroLine; 1580 I: Integer; 1663 I: Integer; 1664 FocusedTrack: TTrack; 1581 1665 begin 1582 1666 if Button = mbLeft then begin 1667 // Place selected train if focused track 1668 if Assigned(SelectedTrain) then begin 1669 SelectedTrain.TargetStation := nil; 1670 SelectedTrain.BaseTrackPoint := nil; 1671 if Assigned(SelectedTrain.Line) then begin 1672 SelectedTrain.Line.Trains.Remove(SelectedTrain); 1673 SelectedTrain.Line := nil; 1674 end; 1675 FocusedTrack := GetTrackOnPos(Position); 1676 if Assigned(FocusedTrack.PointDown) then begin 1677 SelectedTrain.Line := FocusedTrack.PointDown.Line; 1678 SelectedTrain.Line.Trains.Add(SelectedTrain); 1679 SelectedTrain.BaseTrackPoint := FocusedTrack.PointDown; 1680 end else 1681 if Assigned(FocusedTrack.PointDown) then begin 1682 SelectedTrain.Line := FocusedTrack.PointUp.Line; 1683 SelectedTrain.Line.Trains.Add(SelectedTrain); 1684 SelectedTrain.BaseTrackPoint := FocusedTrack.PointUp; 1685 end; 1686 end; 1583 1687 1584 1688 // Line color selection 1585 1689 for I := 0 to Lines.Count - 1 do 1586 if Distance(Point(View. Size.Xdiv 2 - Length(LineColors) div 2 * LineColorsDist + I * LineColorsDist,1587 View. Size.Y- LineColorsDist), Position) < 20 then begin1690 if Distance(Point(View.DestRect.Right div 2 - Length(LineColors) div 2 * LineColorsDist + I * LineColorsDist, 1691 View.DestRect.Bottom - LineColorsDist), Position) < 20 then begin 1588 1692 SelectedLine := TMetroLine(Lines[I]); 1589 1693 Exit; … … 1604 1708 TrackStationDown := nil; 1605 1709 TrackStationUp := nil; 1710 SelectedTrain := nil; 1606 1711 end; 1607 1712 … … 1618 1723 MouseHold := True; 1619 1724 LastFocusedStation := nil; 1725 1726 // Train selection 1727 SelectedTrain := GetTrainOnPos(Position); 1728 if Assigned(SelectedTrain) then begin 1729 Exit; 1730 end; 1731 1732 // Select unused train 1733 if (Distance(Position, Point(View.DestRect.Right div 2 - Length(LineColors) div 2 * LineColorsDist - 100, 1734 View.DestRect.Bottom - LineColorsDist)) < 30) and 1735 (Trains.GetUnusedCount > 0) then begin 1736 SelectedTrain := Trains.GetUnusedTrain; 1737 Exit; 1738 end; 1620 1739 1621 1740 // Line selection … … 1673 1792 1674 1793 // Start with 3 stations with each different shape 1675 InitialStationCount := 3 ;1794 InitialStationCount := 30; 1676 1795 for I := 0 to InitialStationCount - 1 do begin 1677 1796 NewStation := Stations.AddNew; … … 1742 1861 PassengerPos: TPoint; 1743 1862 Direction: Integer; 1863 Points: array of TPoint; 1744 1864 const 1745 1865 GameOverText = 'Game Over'; … … 1889 2009 end; 1890 2010 1891 // Interface 2011 // Draw unused trains 2012 Text := IntToStr(Trains.GetUnusedCount); 2013 Canvas.Draw(Canvas.Width div 2 - Length(LineColors) div 2 * LineColorsDist - 100, 2014 Canvas.Height - LineColorsDist - ImageLocomotive.Picture.Bitmap.Height div 2, ImageLocomotive.Picture.Bitmap); 2015 Canvas.Brush.Style := bsClear; 2016 Canvas.TextOut(Canvas.Width div 2 - Length(LineColors) div 2 * LineColorsDist - 50 - Canvas.TextWidth(Text), 2017 Canvas.Height - LineColorsDist - Canvas.TextHeight(Text) div 2, Text); 2018 2019 2020 // Status interface 1892 2021 Text := IntToStr(ServedPassengerCount); 1893 2022 Canvas.Draw(Canvas.Width - 140, 20, ImagePassenger.Picture.Bitmap); … … 1895 2024 Canvas.TextOut(Canvas.Width - 146 - Canvas.TextWidth(Text), 25, Text); 1896 2025 1897 Text := IntToStr(Trains.GetUnusedCount);1898 Canvas.Draw(Canvas.Width - 240, 20, ImageLocomotive.Picture.Bitmap);1899 Canvas.Brush.Style := bsClear;1900 Canvas.TextOut(Canvas.Width - 246 - Canvas.TextWidth(Text), 25, Text);1901 1902 2026 DrawClock(Canvas); 2027 2028 if Assigned(SelectedTrain) then begin 2029 Canvas.Brush.Color := clBlack; //SelectedTrain.Line.Color; 2030 Canvas.Brush.Style := bsSolid; 2031 Canvas.Pen.Style := psClear; 2032 Pos := LastMousePos; 2033 Angle := 0; 2034 2035 SetLength(Points, 4); 2036 Points[0] := RotatePoint(Pos, Point(Pos.X - TrainSize div 2, Pos.Y - TrainSize div 3), Angle); 2037 Points[1] := RotatePoint(Pos, Point(Pos.X + TrainSize div 2, Pos.Y - TrainSize div 3), Angle); 2038 Points[2] := RotatePoint(Pos, Point(Pos.X + TrainSize div 2, Pos.Y + TrainSize div 3), Angle); 2039 Points[3] := RotatePoint(Pos, Point(Pos.X - TrainSize div 2, Pos.Y + TrainSize div 3), Angle); 2040 Canvas.Polygon(Points); 2041 end; 1903 2042 1904 2043 // Game over -
trunk/UFormMain.lfm
r22 r28 19 19 Width = 1139 20 20 Align = alClient 21 OnClick = PaintBox1Click22 21 OnMouseDown = PaintBox1MouseDown 23 22 OnMouseMove = PaintBox1MouseMove -
trunk/UFormMain.pas
r22 r28 20 20 procedure FormKeyPress(Sender: TObject; var Key: char); 21 21 procedure FormShow(Sender: TObject); 22 procedure PaintBox1Click(Sender: TObject);23 22 procedure PaintBox1MouseDown(Sender: TObject; Button: TMouseButton; 24 23 Shift: TShiftState; X, Y: Integer); … … 68 67 begin 69 68 Randomize; 70 Engine.View.Size := Point(PaintBox1.Width, PaintBox1.Height); 71 Engine.Map.Size := Engine.View.Size; 69 Engine.Map.Size := Point(PaintBox1.Width, PaintBox1.Height); 72 70 Engine.Reset; 73 end;74 75 procedure TFormMain.PaintBox1Click(Sender: TObject);76 begin77 78 71 end; 79 72 … … 98 91 procedure TFormMain.PaintBox1Paint(Sender: TObject); 99 92 begin 93 Engine.View.DestRect := Rect(0, 0, PaintBox1.Width, PaintBox1.Height); 100 94 Engine.Paint(PaintBox1.Canvas); 101 95 end; … … 103 97 procedure TFormMain.PaintBox1Resize(Sender: TObject); 104 98 begin 105 //Engine.106 99 end; 107 100 -
trunk/UGeometric.pas
r27 r28 22 22 function ArcTan2Point(Point: TPoint): Float; 23 23 function ArcTanPoint(Point: TPoint): Float; 24 function RectEquals(A, B: TRect): Boolean; 24 25 25 26 implementation … … 125 126 end; 126 127 128 function RectEquals(A, B: TRect): Boolean; 129 begin 130 Result := (A.Left = B.Left) and (A.Top = B.Top) and 131 (A.Right = B.Right) and (A.Bottom = B.Bottom); 132 end; 133 134 127 135 end. 128 136
Note:
See TracChangeset
for help on using the changeset viewer.