1 | unit UEngine;
|
---|
2 |
|
---|
3 | {$mode delphi}
|
---|
4 | {$IFDEF DARWIN}{$modeswitch Objectivec1}{$ENDIF}
|
---|
5 |
|
---|
6 | interface
|
---|
7 |
|
---|
8 | uses
|
---|
9 | {$IFDEF Darwin}MacOSAll, CocoaAll, CocoaUtils,{$ENDIF}
|
---|
10 | Classes, SysUtils, Graphics, Controls, ExtCtrls, Math, DateUtils,
|
---|
11 | UMetaCanvas, fgl, UMenu, UControls;
|
---|
12 |
|
---|
13 | type
|
---|
14 | TStationShape = (ssCircle, ssSquare, ssTriangle, ssStar, ssPlus, ssPentagon,
|
---|
15 | ssDiamond, ssQuarterCircle, ssHexagon, ssCross, ssHalfCircle, ssHeptagon);
|
---|
16 | TStationShapeSet = set of TStationShape;
|
---|
17 | TEngine = class;
|
---|
18 | TMetroPassengers = class;
|
---|
19 | TMetroLines = class;
|
---|
20 | TMetroLine = class;
|
---|
21 | TMetroTrains = class;
|
---|
22 | TTrackPoint = class;
|
---|
23 | TLineStation = class;
|
---|
24 | TTrackPoints = class;
|
---|
25 | TTrack = class;
|
---|
26 | TTrackLink = class;
|
---|
27 | TTrackLinks = class;
|
---|
28 |
|
---|
29 | TColors = record
|
---|
30 | Background: TColor;
|
---|
31 | Text: TColor;
|
---|
32 | ShapeBackground: TColor;
|
---|
33 | MenuItemText: TColor;
|
---|
34 | MenuItemBackground: TColor;
|
---|
35 | MenuItemBackgroundSelected: TColor;
|
---|
36 | end;
|
---|
37 |
|
---|
38 | { TMapStation }
|
---|
39 |
|
---|
40 | TMapStation = class
|
---|
41 | private
|
---|
42 | procedure ShiftTrackPoints;
|
---|
43 | procedure SortLines;
|
---|
44 | public
|
---|
45 | Engine: TEngine;
|
---|
46 | Shape: TStationShape;
|
---|
47 | Position: TPoint;
|
---|
48 | Passengers: TMetroPassengers;
|
---|
49 | Lines: TMetroLines;
|
---|
50 | ShapeDistance: array[TStationShape] of Integer;
|
---|
51 | OverloadDuration: TDateTime;
|
---|
52 | function IsBestStationForShape(Shape: TStationShape; Check, Current: TLineStation): Boolean;
|
---|
53 | constructor Create;
|
---|
54 | destructor Destroy; override;
|
---|
55 | end;
|
---|
56 |
|
---|
57 | { TMapStations }
|
---|
58 |
|
---|
59 | TMapStations = class(TFPGObjectList<TMapStation>)
|
---|
60 | Engine: TEngine;
|
---|
61 | function GetRect: TRect;
|
---|
62 | function AddNew: TMapStation;
|
---|
63 | end;
|
---|
64 |
|
---|
65 | TLineStation = class
|
---|
66 | Line: TMetroLine;
|
---|
67 | MapStation: TMapStation;
|
---|
68 | TrackPoint: TTrackPoint;
|
---|
69 | end;
|
---|
70 |
|
---|
71 | { TLineStations }
|
---|
72 |
|
---|
73 | TLineStations = class(TFPGObjectList<TLineStation>)
|
---|
74 | Line: TMetroLine;
|
---|
75 | function SearchMapStation(Station: TMapStation): TLineStation;
|
---|
76 | end;
|
---|
77 |
|
---|
78 | { TTrackPoint }
|
---|
79 |
|
---|
80 | TTrackPoint = class
|
---|
81 | LineStation: TLineStation;
|
---|
82 | Position: TPoint;
|
---|
83 | //PositionShift: TPoint;
|
---|
84 | PositionDesigned: TPoint;
|
---|
85 | Pending: Boolean;
|
---|
86 | Track: TTrack;
|
---|
87 | NeighPoints: TTrackPoints;
|
---|
88 | NeighLinks: TTrackLinks;
|
---|
89 | LinkUp: TTrackLink;
|
---|
90 | LinkDown: TTrackLink;
|
---|
91 | procedure Connect(TrackPoint: TTrackPoint);
|
---|
92 | procedure Disconnect(TrackPoint: TTrackPoint);
|
---|
93 | function GetDown: TTrackPoint;
|
---|
94 | function GetUp: TTrackPoint;
|
---|
95 | function GetNeighDown: TTrackPoint;
|
---|
96 | function GetNeighUp: TTrackPoint;
|
---|
97 | function GetLinkDown: TTrackLink;
|
---|
98 | function GetLinkUp: TTrackLink;
|
---|
99 | // Move to TTrackLink later
|
---|
100 | function GetDistance: Integer;
|
---|
101 | constructor Create;
|
---|
102 | destructor Destroy; override;
|
---|
103 | end;
|
---|
104 |
|
---|
105 | { TTrackPoints }
|
---|
106 |
|
---|
107 | TTrackPoints = class(TFPGObjectList<TTrackPoint>)
|
---|
108 | Track: TTrack;
|
---|
109 | function AddNew: TTrackPoint;
|
---|
110 | end;
|
---|
111 |
|
---|
112 | { TTrackLink }
|
---|
113 |
|
---|
114 | TTrackLink = class
|
---|
115 | Points: TTrackPoints;
|
---|
116 | Shift: TPoint;
|
---|
117 | Track: TTrack;
|
---|
118 | constructor Create;
|
---|
119 | destructor Destroy; override;
|
---|
120 | end;
|
---|
121 |
|
---|
122 | { TTrackLinks }
|
---|
123 |
|
---|
124 | TTrackLinks = class(TFPGObjectList<TTrackLink>)
|
---|
125 | Track: TTrack;
|
---|
126 | function SearchPoints(Point1, Point2: TTrackPoint): TTrackLink;
|
---|
127 | function AddNew: TTrackLink;
|
---|
128 | end;
|
---|
129 |
|
---|
130 | TTrack = class
|
---|
131 | Points: TTrackPoints;
|
---|
132 | Links: TTrackLinks;
|
---|
133 | Line: TMetroLine;
|
---|
134 | procedure RouteTrack(TP1, TP2: TTrackPoint);
|
---|
135 | procedure RemoveTrackBetween(TP1, TP2: TTrackPoint);
|
---|
136 | constructor Create;
|
---|
137 | destructor Destroy; override;
|
---|
138 | end;
|
---|
139 |
|
---|
140 | { TTracks }
|
---|
141 |
|
---|
142 | TTracks = class(TFPGObjectList<TTrackLink>)
|
---|
143 | function SearchPointUp(TrackPoint: TTrackPoint; Skip: TTrackLink): TTrackLink;
|
---|
144 | function SearchPointDown(TrackPoint: TTrackPoint; Skip: TTrackLink): TTrackLink;
|
---|
145 | end;
|
---|
146 |
|
---|
147 | { TTrackPointsAngle }
|
---|
148 |
|
---|
149 | TTrackPointsAngle = class
|
---|
150 | Angle: Double;
|
---|
151 | TrackLinks: TTrackLinks;
|
---|
152 | constructor Create;
|
---|
153 | destructor Destroy; override;
|
---|
154 | end;
|
---|
155 |
|
---|
156 | { TTrackPointsAngleGroup }
|
---|
157 |
|
---|
158 | TTrackPointsAngleGroup = class(TFPGObjectList<TTrackPointsAngle>)
|
---|
159 | function SearchAngle(Angle: Double): TTrackPointsAngle;
|
---|
160 | end;
|
---|
161 |
|
---|
162 | { TMetroLine }
|
---|
163 |
|
---|
164 | TMetroLine = class
|
---|
165 | private
|
---|
166 | procedure UpdateEndingLines;
|
---|
167 | public
|
---|
168 | Index: Integer;
|
---|
169 | Engine: TEngine;
|
---|
170 | Color: TColor;
|
---|
171 | LineStations: TLineStations;
|
---|
172 | Trains: TMetroTrains;
|
---|
173 | Track: TTrack;
|
---|
174 | procedure ConnectStation(Station: TMapStation; LineStationDown, LineStationUp: TLineStation);
|
---|
175 | procedure DisconnectStation(ALineStation: TLineStation);
|
---|
176 | function GetTrackLength: Integer;
|
---|
177 | constructor Create;
|
---|
178 | destructor Destroy; override;
|
---|
179 | function IsCircular: Boolean;
|
---|
180 | end;
|
---|
181 |
|
---|
182 | { TMetroLines }
|
---|
183 |
|
---|
184 | TMetroLines = class(TFPGObjectList<TMetroLine>)
|
---|
185 | Engine: TEngine;
|
---|
186 | function AddNew: TMetroLine;
|
---|
187 | function SearchByColor(Color: TColor): TMetroLine;
|
---|
188 | end;
|
---|
189 |
|
---|
190 | { TMetroTrain }
|
---|
191 |
|
---|
192 | TMetroTrain = class
|
---|
193 | private
|
---|
194 | FLine: TMetroLine;
|
---|
195 | LastPosDelta: Integer;
|
---|
196 | LastTrainMoveTime: TDateTime;
|
---|
197 | StationStopTime: TDateTime;
|
---|
198 | procedure SetLine(AValue: TMetroLine);
|
---|
199 | public
|
---|
200 | Passengers: TMetroPassengers;
|
---|
201 | BaseTrackPoint: TTrackPoint;
|
---|
202 | RelPos: Double;
|
---|
203 | Direction: Integer;
|
---|
204 | InStation: Boolean;
|
---|
205 | TargetStation: TLineStation;
|
---|
206 | function GetTargetStationDistance: Integer;
|
---|
207 | function GetPosition: TPoint;
|
---|
208 | function GetAngle: Double;
|
---|
209 | constructor Create;
|
---|
210 | destructor Destroy; override;
|
---|
211 | property Line: TMetroLine read FLine write SetLine;
|
---|
212 | end;
|
---|
213 |
|
---|
214 | { TMetroTrains }
|
---|
215 |
|
---|
216 | TMetroTrains = class(TFPGObjectList<TMetroTrain>)
|
---|
217 | function GetUnusedTrain: TMetroTrain;
|
---|
218 | function GetUnusedCount: Integer;
|
---|
219 | function AddNew: TMetroTrain;
|
---|
220 | end;
|
---|
221 |
|
---|
222 | TMetroPassenger = class
|
---|
223 | Engine: TEngine;
|
---|
224 | Shape: TStationShape;
|
---|
225 | Station: TMapStation;
|
---|
226 | Train: TMetroTrain;
|
---|
227 | end;
|
---|
228 |
|
---|
229 | { TMetroPassengers }
|
---|
230 |
|
---|
231 | TMetroPassengers = class(TFPGObjectList<TMetroPassenger>)
|
---|
232 | Engine: TEngine;
|
---|
233 | function AddNew: TMetroPassenger;
|
---|
234 | end;
|
---|
235 |
|
---|
236 | { TRiver }
|
---|
237 |
|
---|
238 | TRiver = class
|
---|
239 | Points: array of TPoint;
|
---|
240 | procedure Paint(Canvas: TCanvas);
|
---|
241 | end;
|
---|
242 |
|
---|
243 | TRivers = class(TFPGObjectList<TRiver>)
|
---|
244 | end;
|
---|
245 |
|
---|
246 | TMap = class
|
---|
247 | Size: TPoint;
|
---|
248 | Rivers: TRivers;
|
---|
249 | constructor Create;
|
---|
250 | destructor Destroy; override;
|
---|
251 | end;
|
---|
252 |
|
---|
253 | { TView }
|
---|
254 |
|
---|
255 | TView = class
|
---|
256 | private
|
---|
257 | FDestRect: TRect;
|
---|
258 | FSourceRect: TRect;
|
---|
259 | FZoom: Double;
|
---|
260 | procedure SetDestRect(AValue: TRect);
|
---|
261 | procedure SetSourceRect(AValue: TRect);
|
---|
262 | procedure SetZoom(AValue: Double);
|
---|
263 | public
|
---|
264 | function PointDestToSrc(Pos: TPoint): TPoint;
|
---|
265 | function PointSrcToDest(Pos: TPoint): TPoint;
|
---|
266 | constructor Create;
|
---|
267 | property SourceRect: TRect read FSourceRect write SetSourceRect;
|
---|
268 | property DestRect: TRect read FDestRect write SetDestRect;
|
---|
269 | property Zoom: Double read FZoom write SetZoom;
|
---|
270 | end;
|
---|
271 |
|
---|
272 | TGameState = (gsNotStarted, gsRunning, gsPaused, gsGameOver, gsMenu);
|
---|
273 |
|
---|
274 | { TEngine }
|
---|
275 |
|
---|
276 | TEngine = class
|
---|
277 | private
|
---|
278 | FDarkMode: Boolean;
|
---|
279 | FOnDarkModeChange: TNotifyEvent;
|
---|
280 | FState: TGameState;
|
---|
281 | LastMousePos: TPoint;
|
---|
282 | LastFocusedStation: TMapStation;
|
---|
283 | MouseHold: Boolean;
|
---|
284 | LastNewStationTime: TDateTime;
|
---|
285 | LastNewPassengerTime: TDateTime;
|
---|
286 | LastNewWeekTime: TDateTime;
|
---|
287 | LastNewShapeTime: TDateTime;
|
---|
288 | LastTickTime: TDateTime;
|
---|
289 | FTime: TDateTime;
|
---|
290 | FLastTime: TDateTime;
|
---|
291 | MetaCanvas: TMetaCanvas;
|
---|
292 | Menu: TMenu;
|
---|
293 | MenuMain: TMenu;
|
---|
294 | MenuOptions: TMenu;
|
---|
295 | MenuGame: TMenu;
|
---|
296 | LastState: TGameState;
|
---|
297 | TimePerSecond: TDateTime;
|
---|
298 | function GetServedDaysCount: Integer;
|
---|
299 | procedure ResizeView;
|
---|
300 | function GetExistStationShapes: TStationShapeSet;
|
---|
301 | function GetStationOnPos(Pos: TPoint): TMapStation;
|
---|
302 | function GetTrackOnPos(Pos: TPoint): TTrackLink;
|
---|
303 | function GetTrainOnPos(Pos: TPoint): TMetroTrain;
|
---|
304 | procedure DrawLine(Canvas: TCanvas; Pos: TPoint);
|
---|
305 | procedure DrawShape(Canvas: TCanvas; Position: TPoint; Shape: TStationShape;
|
---|
306 | Size: Integer; Angle: Double);
|
---|
307 | procedure DrawClock(Canvas: TCanvas; CanvasSize: TPoint);
|
---|
308 | procedure DrawTrains(Canvas: TCanvas);
|
---|
309 | procedure DrawGameOver(Canvas: TCanvas; CanvasSize: TPoint);
|
---|
310 | procedure DrawStationPassengerOverload(Canvas: TCanvas);
|
---|
311 | procedure DrawLines(Canvas: TCanvas);
|
---|
312 | procedure DrawStations(Canvas: TCanvas);
|
---|
313 | procedure DrawGameControls(Canvas: TCanvas; CanvasSize: TPoint);
|
---|
314 | procedure ComputeShapeDistance;
|
---|
315 | procedure ComputeShapeDistanceStation(Station: TMapStation;
|
---|
316 | UpdatedShape: TStationShape; Distance: Integer);
|
---|
317 | procedure SetDarkMode(AValue: Boolean);
|
---|
318 | procedure InitColors;
|
---|
319 | procedure SetState(AValue: TGameState);
|
---|
320 | procedure TrainMovement;
|
---|
321 | function GetUnusedLine: TMetroLine;
|
---|
322 | procedure ShiftTrackPoints;
|
---|
323 | procedure MenuItemExit(Sender: TObject);
|
---|
324 | procedure MenuItemPlay(Sender: TObject);
|
---|
325 | procedure MenuItemOptions(Sender: TObject);
|
---|
326 | procedure MenuItemGameContinue(Sender: TObject);
|
---|
327 | procedure MenuItemGameExit(Sender: TObject);
|
---|
328 | procedure MenuItemGameRestart(Sender: TObject);
|
---|
329 | procedure MenuItemBack(Sender: TObject);
|
---|
330 | procedure ButtonPlay(Sender: TObject);
|
---|
331 | procedure ButtonPause(Sender: TObject);
|
---|
332 | procedure ButtonFastForward(Sender: TObject);
|
---|
333 | procedure ButtonBackClick(Sender: TObject);
|
---|
334 | procedure DarkModeChanged(Sender: TObject);
|
---|
335 | procedure LanguageChanged(Sender: TObject);
|
---|
336 | procedure FullScreenChanged(Sender: TObject);
|
---|
337 | procedure UpdateInterface;
|
---|
338 | public
|
---|
339 | Colors: TColors;
|
---|
340 | Passengers: TMetroPassengers;
|
---|
341 | Stations: TMapStations;
|
---|
342 | Lines: TMetroLines;
|
---|
343 | Trains: TMetroTrains;
|
---|
344 | ShapeCount: Integer;
|
---|
345 | Map: TMap;
|
---|
346 | View: TView;
|
---|
347 | SelectedLine: TMetroLine;
|
---|
348 | SelectedTrain: TMetroTrain;
|
---|
349 | TrackStationDown: TTrackPoint;
|
---|
350 | TrackStationUp: TTrackPoint;
|
---|
351 | ServedPassengerCount: Integer;
|
---|
352 | RedrawPending: Boolean;
|
---|
353 | ButtonBack: TImage;
|
---|
354 | ImagePassenger: TImage;
|
---|
355 | ImageLocomotive: TImage;
|
---|
356 | ImagePlay: TImage;
|
---|
357 | ImagePause: TImage;
|
---|
358 | ImageFastForward: TImage;
|
---|
359 | HighestServedPassengerCount: Integer;
|
---|
360 | HighestServedDaysCount: Integer;
|
---|
361 | procedure InitMenus;
|
---|
362 | procedure MouseMove(Position: TPoint);
|
---|
363 | procedure MouseUp(Button: TMouseButton; Position: TPoint);
|
---|
364 | procedure MouseDown(Button: TMouseButton; Position: TPoint);
|
---|
365 | procedure KeyUp(Key: Word);
|
---|
366 | procedure MainMenu;
|
---|
367 | procedure Clear;
|
---|
368 | procedure NewGame;
|
---|
369 | procedure Redraw;
|
---|
370 | constructor Create;
|
---|
371 | destructor Destroy; override;
|
---|
372 | procedure Tick;
|
---|
373 | procedure Paint(Canvas: TCanvas; CanvasSize: TPoint);
|
---|
374 | property Time: TDateTime read FTime;
|
---|
375 | property DarkMode: Boolean read FDarkMode write SetDarkMode;
|
---|
376 | property State: TGameState read FState write SetState;
|
---|
377 | property ServedDaysCount: Integer read GetServedDaysCount;
|
---|
378 | property OnDarkModeChange: TNotifyEvent read FOnDarkModeChange
|
---|
379 | write FOnDarkModeChange;
|
---|
380 | end;
|
---|
381 |
|
---|
382 | const
|
---|
383 | clDarkYellow = TColor($00dede);
|
---|
384 | clOrange = TColor($0080ff);
|
---|
385 | clBrown = TColor($003090);
|
---|
386 | clCyan = TColor($FFFF00);
|
---|
387 | LineColors: array[0..8] of TColor = (clBlue, clRed, clDarkYellow, clGreen,
|
---|
388 | clPurple, clGray, clOrange, clBrown, clCyan);
|
---|
389 | StationSize = 30;
|
---|
390 | StationOverloadSize = 60;
|
---|
391 | PassengerSize = 15;
|
---|
392 | TrainSize = 40;
|
---|
393 | LineColorsDist = 50;
|
---|
394 | TrainSpeed = 2000;
|
---|
395 | ImagePassengerName = 'Images/Passenger.png';
|
---|
396 | ImageLocomotiveName = 'Images/Locomotive.png';
|
---|
397 | ImageLeftArrowName = 'Images/Left arrow.png';
|
---|
398 | ImagePlayName = 'Images/Play.png';
|
---|
399 | ImagePauseName = 'Images/Pause.png';
|
---|
400 | ImageFastForwardName = 'Images/Fast forward.png';
|
---|
401 | TrainPassengerCount = 6;
|
---|
402 | StationMinDistance = 100;
|
---|
403 | StationMaxDistance = 300;
|
---|
404 | MaxWaitingPassengers = 10;
|
---|
405 | MaxPassengersOveloadTime = 2;
|
---|
406 | MetroLineThickness = 13;
|
---|
407 | TrackClickDistance = 20;
|
---|
408 | EndStationLength = 50;
|
---|
409 | ShowDistances = False;
|
---|
410 | TimePerSecondNormal = 60 * OneMinute;
|
---|
411 | TimePerSecondFast = 2 * TimePerSecondNormal;
|
---|
412 | NewStationPeriod = 1;
|
---|
413 | NewShapePeriod = 10;
|
---|
414 | NewTrainPeriod = 7; // Each week
|
---|
415 | NewPassengerPeriod = 0.3 * OneSecond;
|
---|
416 | NewPassengerProbability = 0.003;
|
---|
417 | VisiblePassengersPerLine = 6;
|
---|
418 |
|
---|
419 |
|
---|
420 | implementation
|
---|
421 |
|
---|
422 | uses
|
---|
423 | UGeometric, UFormMain, ULanguages, UPixelPointer;
|
---|
424 |
|
---|
425 | resourcestring
|
---|
426 | SZeroZoomNotAlowed = 'Zero zoom not allowed';
|
---|
427 | SAlreadyConnectedTrackPoint = 'Trying to connect already connected track point';
|
---|
428 | SAlreadyDisconnectedTrackPoint = 'Trying to disconnect not connected track point';
|
---|
429 | SGameOver = 'Game Over';
|
---|
430 | SGameOverReason = 'Overcrowding at this station has forced you to resign as metro manager.';
|
---|
431 | SGameOverStatistic = '%d passengers travelled on your metro over %d days.';
|
---|
432 | SDay = 'Day';
|
---|
433 | SNewHighScore = 'New high score!';
|
---|
434 | SOldHighScore = 'Old high score was %d passengers in %d days.';
|
---|
435 |
|
---|
436 | { TTrackLinks }
|
---|
437 |
|
---|
438 | function TTrackLinks.SearchPoints(Point1, Point2: TTrackPoint): TTrackLink;
|
---|
439 | var
|
---|
440 | I: Integer;
|
---|
441 | begin
|
---|
442 | I := 0;
|
---|
443 | while (I < 0) and
|
---|
444 | ((Items[I].Points.First <> Point1) or (Items[I].Points.Last <> Point2)) and
|
---|
445 | ((Items[I].Points.First <> Point2) or (Items[I].Points.Last <> Point1)) do
|
---|
446 | Inc(I);
|
---|
447 | if I < 0 then Result := Items[I]
|
---|
448 | else Result := nil;
|
---|
449 | end;
|
---|
450 |
|
---|
451 | function TTrackLinks.AddNew: TTrackLink;
|
---|
452 | begin
|
---|
453 | Result := TTrackLink.Create;
|
---|
454 | Result.Track := Track;
|
---|
455 | end;
|
---|
456 |
|
---|
457 | { TTrackPoints }
|
---|
458 |
|
---|
459 | function TTrackPoints.AddNew: TTrackPoint;
|
---|
460 | begin
|
---|
461 | Result := TTrackPoint.Create;
|
---|
462 | Result.Track := Track;
|
---|
463 | end;
|
---|
464 |
|
---|
465 | { TTrack }
|
---|
466 |
|
---|
467 | constructor TTrack.Create;
|
---|
468 | begin
|
---|
469 | Points := TTrackPoints.Create;
|
---|
470 | Points.Track := Self;
|
---|
471 | Links := TTrackLinks.Create;
|
---|
472 | Links.Track := Self;
|
---|
473 | end;
|
---|
474 |
|
---|
475 | destructor TTrack.Destroy;
|
---|
476 | begin
|
---|
477 | Points.Free;
|
---|
478 | Links.Free;
|
---|
479 | inherited;
|
---|
480 | end;
|
---|
481 |
|
---|
482 | { TTrackLink }
|
---|
483 |
|
---|
484 | constructor TTrackLink.Create;
|
---|
485 | begin
|
---|
486 | Points := TTrackPoints.Create;
|
---|
487 | Points.FreeObjects := False;
|
---|
488 | end;
|
---|
489 |
|
---|
490 | destructor TTrackLink.Destroy;
|
---|
491 | begin
|
---|
492 | Points.Free;
|
---|
493 | inherited;
|
---|
494 | end;
|
---|
495 |
|
---|
496 | { TRiver }
|
---|
497 |
|
---|
498 | procedure TRiver.Paint(Canvas: TCanvas);
|
---|
499 | begin
|
---|
500 | Canvas.Brush.Color := $ffffe0;
|
---|
501 | Canvas.Brush.Style := bsSolid;
|
---|
502 | Canvas.Polygon(Points);
|
---|
503 | end;
|
---|
504 |
|
---|
505 | { TMap }
|
---|
506 |
|
---|
507 | constructor TMap.Create;
|
---|
508 | begin
|
---|
509 | Rivers := TRivers.Create;
|
---|
510 | end;
|
---|
511 |
|
---|
512 | destructor TMap.Destroy;
|
---|
513 | begin
|
---|
514 | Rivers.Free;
|
---|
515 | inherited;
|
---|
516 | end;
|
---|
517 |
|
---|
518 | { TView }
|
---|
519 |
|
---|
520 | procedure TView.SetDestRect(AValue: TRect);
|
---|
521 | var
|
---|
522 | Diff: TPoint;
|
---|
523 | begin
|
---|
524 | if RectEquals(FDestRect, AValue) then Exit;
|
---|
525 | Diff := Point(Trunc((DestRect.Right - DestRect.Left) / Zoom - (AValue.Right - AValue.Left) / Zoom) div 2,
|
---|
526 | Trunc((DestRect.Bottom - DestRect.Top) / Zoom - (AValue.Bottom - AValue.Top) / Zoom) div 2);
|
---|
527 | FDestRect := AValue;
|
---|
528 | FSourceRect := Bounds(FSourceRect.Left + Diff.X, FSourceRect.Top + Diff.Y,
|
---|
529 | Trunc((DestRect.Right - DestRect.Left) / Zoom),
|
---|
530 | Trunc((DestRect.Bottom - DestRect.Top) / Zoom));
|
---|
531 | end;
|
---|
532 |
|
---|
533 | procedure TView.SetSourceRect(AValue: TRect);
|
---|
534 | var
|
---|
535 | ZX: Double;
|
---|
536 | ZY: Double;
|
---|
537 | begin
|
---|
538 | if RectEquals(FSourceRect, AValue) then Exit;
|
---|
539 | FSourceRect := AValue;
|
---|
540 | ZX := (FDestRect.Right - FDestRect.Left) / (FSourceRect.Right - FSourceRect.Left);
|
---|
541 | ZY := (FDestRect.Bottom - FDestRect.Top) / (FSourceRect.Bottom - FSourceRect.Top);
|
---|
542 | if ZX > ZY then
|
---|
543 | Zoom := ZY
|
---|
544 | else Zoom := ZX;
|
---|
545 | end;
|
---|
546 |
|
---|
547 | procedure TView.SetZoom(AValue: Double);
|
---|
548 | begin
|
---|
549 | if FZoom = AValue then Exit;
|
---|
550 | if AValue = 0 then
|
---|
551 | raise Exception.Create(SZeroZoomNotAlowed);
|
---|
552 | FZoom := AValue;
|
---|
553 | FSourceRect := Bounds(Trunc(FSourceRect.Left + (FSourceRect.Right - FSourceRect.Left) div 2 - (DestRect.Right - DestRect.Left) / Zoom / 2),
|
---|
554 | Trunc(FSourceRect.Top + (FSourceRect.Bottom - FSourceRect.Top) div 2 - (FDestRect.Bottom - DestRect.Top) / Zoom / 2),
|
---|
555 | Trunc((DestRect.Right - DestRect.Left) / Zoom),
|
---|
556 | Trunc((DestRect.Bottom - DestRect.Top) / Zoom));
|
---|
557 | end;
|
---|
558 |
|
---|
559 | function TView.PointDestToSrc(Pos: TPoint): TPoint;
|
---|
560 | begin
|
---|
561 | Result := Point(Trunc(Pos.X / FZoom + FSourceRect.Left),
|
---|
562 | Trunc(Pos.Y / FZoom + FSourceRect.Top));
|
---|
563 | end;
|
---|
564 |
|
---|
565 | function TView.PointSrcToDest(Pos: TPoint): TPoint;
|
---|
566 | begin
|
---|
567 | Result := Point(Trunc((Pos.X - FSourceRect.Left) * FZoom),
|
---|
568 | Trunc((Pos.Y - FSourceRect.Top) * FZoom));
|
---|
569 | end;
|
---|
570 |
|
---|
571 | constructor TView.Create;
|
---|
572 | begin
|
---|
573 | Zoom := 1;
|
---|
574 | end;
|
---|
575 |
|
---|
576 | { TTracks }
|
---|
577 |
|
---|
578 | function TTracks.SearchPointUp(TrackPoint: TTrackPoint; Skip: TTrackLink): TTrackLink;
|
---|
579 | var
|
---|
580 | I: Integer;
|
---|
581 | begin
|
---|
582 | I := 0;
|
---|
583 | while (I < Count) and (Items[I].Points[1] <> TrackPoint) and (Items[I] <> Skip) do Inc(I);
|
---|
584 | if I < Count then Result := Items[I]
|
---|
585 | else Result := nil;
|
---|
586 | end;
|
---|
587 |
|
---|
588 | function TTracks.SearchPointDown(TrackPoint: TTrackPoint; Skip: TTrackLink): TTrackLink;
|
---|
589 | var
|
---|
590 | I: Integer;
|
---|
591 | begin
|
---|
592 | I := 0;
|
---|
593 | while (I < Count) and (Items[I].Points[0] <> TrackPoint) and (Items[I] <> Skip) do Inc(I);
|
---|
594 | if I < Count then Result := Items[I]
|
---|
595 | else Result := nil;
|
---|
596 | end;
|
---|
597 |
|
---|
598 | { TTrackPointsAngleGroup }
|
---|
599 |
|
---|
600 | function TTrackPointsAngleGroup.SearchAngle(Angle: Double): TTrackPointsAngle;
|
---|
601 | var
|
---|
602 | I: Integer;
|
---|
603 | begin
|
---|
604 | I := 0;
|
---|
605 | while (I < Count) and (Items[I].Angle <> Angle) do Inc(I);
|
---|
606 | if I < Count then Result := Items[I]
|
---|
607 | else Result := nil;
|
---|
608 | end;
|
---|
609 |
|
---|
610 | { TTrackPointsAngle }
|
---|
611 |
|
---|
612 | constructor TTrackPointsAngle.Create;
|
---|
613 | begin
|
---|
614 | TrackLinks := TTrackLinks.Create;
|
---|
615 | TrackLinks.FreeObjects := False;
|
---|
616 | end;
|
---|
617 |
|
---|
618 | destructor TTrackPointsAngle.Destroy;
|
---|
619 | begin
|
---|
620 | TrackLinks.Free;
|
---|
621 | inherited;
|
---|
622 | end;
|
---|
623 |
|
---|
624 | { TTrackPoint }
|
---|
625 |
|
---|
626 | procedure TTrackPoint.Connect(TrackPoint: TTrackPoint);
|
---|
627 | var
|
---|
628 | NewLink: TTrackLink;
|
---|
629 | begin
|
---|
630 | if NeighPoints.IndexOf(TrackPoint) = -1 then begin
|
---|
631 | NeighPoints.Add(TrackPoint);
|
---|
632 | TrackPoint.NeighPoints.Add(Self);
|
---|
633 |
|
---|
634 | // Add new link to both self and connected track point
|
---|
635 | NewLink := Track.Links.AddNew;
|
---|
636 | NewLink.Points.Add(TrackPoint);
|
---|
637 | NewLink.Points.Add(Self);
|
---|
638 | NeighLinks.Add(NewLink);
|
---|
639 | TrackPoint.NeighLinks.Add(NewLink);
|
---|
640 | Track.Links.Add(NewLink);
|
---|
641 | end else raise Exception.Create(SAlreadyConnectedTrackPoint);
|
---|
642 | end;
|
---|
643 |
|
---|
644 | procedure TTrackPoint.Disconnect(TrackPoint: TTrackPoint);
|
---|
645 | var
|
---|
646 | Index: Integer;
|
---|
647 | Link: TTrackLink;
|
---|
648 | begin
|
---|
649 | Index := NeighPoints.IndexOf(TrackPoint);
|
---|
650 | if NeighPoints.IndexOf(TrackPoint) <> -1 then begin
|
---|
651 | NeighPoints.Delete(Index);
|
---|
652 | TrackPoint.NeighPoints.Remove(Self);
|
---|
653 |
|
---|
654 | // Remove link from both track points
|
---|
655 | Link := NeighLinks.SearchPoints(Self, TrackPoint);
|
---|
656 | NeighLinks.Remove(Link);
|
---|
657 | TrackPoint.NeighLinks.Remove(Link);
|
---|
658 | Track.Links.Remove(Link);
|
---|
659 | end else raise Exception.Create(SAlreadyDisconnectedTrackPoint);
|
---|
660 | end;
|
---|
661 |
|
---|
662 | function TTrackPoint.GetDown: TTrackPoint;
|
---|
663 | var
|
---|
664 | I: Integer;
|
---|
665 | begin
|
---|
666 | I := Track.Points.IndexOf(Self) - 1;
|
---|
667 | while (I >= 0) and not Assigned(Track.Points[I].LineStation) do
|
---|
668 | Dec(I);
|
---|
669 | if I >= 0 then Result := Track.Points[I]
|
---|
670 | else Result := nil;
|
---|
671 | end;
|
---|
672 |
|
---|
673 | function TTrackPoint.GetUp: TTrackPoint;
|
---|
674 | var
|
---|
675 | I: Integer;
|
---|
676 | begin
|
---|
677 | I := Track.Points.IndexOf(Self) + 1;
|
---|
678 | while (I < Track.Points.Count) and not Assigned(Track.Points[I].LineStation) do
|
---|
679 | Inc(I);
|
---|
680 | if I < Track.Points.Count then Result := Track.Points[I]
|
---|
681 | else Result := nil;
|
---|
682 | end;
|
---|
683 |
|
---|
684 | function TTrackPoint.GetNeighDown: TTrackPoint;
|
---|
685 | var
|
---|
686 | NewIndex: Integer;
|
---|
687 | begin
|
---|
688 | Result := nil;
|
---|
689 | NewIndex := Track.Points.IndexOf(Self) - 1;
|
---|
690 | if NewIndex >= 0 then Result := Track.Points[NewIndex];
|
---|
691 | end;
|
---|
692 |
|
---|
693 | function TTrackPoint.GetNeighUp: TTrackPoint;
|
---|
694 | var
|
---|
695 | NewIndex: Integer;
|
---|
696 | begin
|
---|
697 | Result := nil;
|
---|
698 | if Assigned(Track) then begin
|
---|
699 | NewIndex := Track.Points.IndexOf(Self) + 1;
|
---|
700 | if NewIndex < Track.Points.Count then Result := Track.Points[NewIndex];
|
---|
701 | end;
|
---|
702 | end;
|
---|
703 |
|
---|
704 | function TTrackPoint.GetLinkDown: TTrackLink;
|
---|
705 | begin
|
---|
706 | if Assigned(LinkDown) then Result := LinkDown
|
---|
707 | else begin
|
---|
708 | LinkDown := TTrackLink.Create;
|
---|
709 | LinkDown.Points.Add(GetNeighDown);
|
---|
710 | LinkDown.Points.Add(Self);
|
---|
711 | Result := LinkDown;
|
---|
712 | GetNeighDown.LinkUp := LinkDown;
|
---|
713 | end;
|
---|
714 | end;
|
---|
715 |
|
---|
716 | function TTrackPoint.GetLinkUp: TTrackLink;
|
---|
717 | begin
|
---|
718 | if Assigned(LinkUp) then Result := LinkUp
|
---|
719 | else begin
|
---|
720 | LinkUp := TTrackLink.Create;
|
---|
721 | LinkUp.Points.Add(Self);
|
---|
722 | LinkUp.Points.Add(GetNeighUp);
|
---|
723 | Result := LinkUp;
|
---|
724 | GetNeighUp.LinkDown := LinkUp;
|
---|
725 | end;
|
---|
726 | end;
|
---|
727 |
|
---|
728 | function TTrackPoint.GetDistance: Integer;
|
---|
729 | var
|
---|
730 | Index: Integer;
|
---|
731 | begin
|
---|
732 | Index := Track.Points.IndexOf(Self);
|
---|
733 | Result := Distance(Track.Points[Index + 1].Position, Track.Points[Index].Position);
|
---|
734 | end;
|
---|
735 |
|
---|
736 | constructor TTrackPoint.Create;
|
---|
737 | begin
|
---|
738 | NeighPoints := TTrackPoints.Create;
|
---|
739 | NeighPoints.FreeObjects := False;
|
---|
740 | NeighLinks := TTrackLinks.Create;
|
---|
741 | NeighLinks.FreeObjects := False;
|
---|
742 | end;
|
---|
743 |
|
---|
744 | destructor TTrackPoint.Destroy;
|
---|
745 | begin
|
---|
746 | NeighLinks.Free;
|
---|
747 | NeighPoints.Free;
|
---|
748 | inherited;
|
---|
749 | end;
|
---|
750 |
|
---|
751 | { TLineStations }
|
---|
752 |
|
---|
753 | function TLineStations.SearchMapStation(Station: TMapStation): TLineStation;
|
---|
754 | var
|
---|
755 | I: Integer;
|
---|
756 | begin
|
---|
757 | I := 0;
|
---|
758 | while (I < Count) and (Items[I].MapStation <> Station) do Inc(I);
|
---|
759 | if I < Count then Result := Items[I]
|
---|
760 | else Result := nil;
|
---|
761 | end;
|
---|
762 |
|
---|
763 | { TMetroPassengers }
|
---|
764 |
|
---|
765 | function TMetroPassengers.AddNew: TMetroPassenger;
|
---|
766 | begin
|
---|
767 | Result := TMetroPassenger.Create;
|
---|
768 | Result.Engine := Engine;
|
---|
769 | Result.Shape := TStationShape(Random(Integer(Engine.ShapeCount)));
|
---|
770 | Add(Result);
|
---|
771 | end;
|
---|
772 |
|
---|
773 | { TMetroTrains }
|
---|
774 |
|
---|
775 | function TMetroTrains.GetUnusedTrain: TMetroTrain;
|
---|
776 | var
|
---|
777 | I: Integer;
|
---|
778 | begin
|
---|
779 | I := 0;
|
---|
780 | while (I < Count) and (Assigned(Items[I].Line)) do Inc(I);
|
---|
781 | if I < Count then Result := Items[I]
|
---|
782 | else Result := nil;
|
---|
783 | end;
|
---|
784 |
|
---|
785 | function TMetroTrains.GetUnusedCount: Integer;
|
---|
786 | var
|
---|
787 | I: Integer;
|
---|
788 | begin
|
---|
789 | Result := 0;
|
---|
790 | for I := 0 to Count - 1 do
|
---|
791 | if not Assigned(Items[I].Line) then Inc(Result);
|
---|
792 | end;
|
---|
793 |
|
---|
794 | function TMetroTrains.AddNew: TMetroTrain;
|
---|
795 | begin
|
---|
796 | Result := TMetroTrain.Create;
|
---|
797 | Add(Result);
|
---|
798 | end;
|
---|
799 |
|
---|
800 | { TMapStations }
|
---|
801 |
|
---|
802 | function TMapStations.GetRect: TRect;
|
---|
803 | var
|
---|
804 | I: Integer;
|
---|
805 | begin
|
---|
806 | if Count > 0 then begin
|
---|
807 | with Items[0] do
|
---|
808 | Result := Rect(Position.X, Position.Y, Position.X, Position.Y);
|
---|
809 | for I := 1 to Count - 1 do
|
---|
810 | with Items[I] do begin
|
---|
811 | if Position.X < Result.Left then Result.Left := Position.X;
|
---|
812 | if Position.X > Result.Right then Result.Right := Position.X;
|
---|
813 | if Position.Y < Result.Top then Result.Top := Position.Y;
|
---|
814 | if Position.Y > Result.Bottom then Result.Bottom := Position.Y;
|
---|
815 | end;
|
---|
816 | end else Result := Rect(0, 0, 0, 0);
|
---|
817 | end;
|
---|
818 |
|
---|
819 | function TMapStations.AddNew: TMapStation;
|
---|
820 | var
|
---|
821 | D: Integer;
|
---|
822 | MinD: Integer;
|
---|
823 | I: Integer;
|
---|
824 | Pass: Integer;
|
---|
825 | Angle: Double;
|
---|
826 | L: Integer;
|
---|
827 | const
|
---|
828 | Step = 20;
|
---|
829 | begin
|
---|
830 | Result := TMapStation.Create;
|
---|
831 | Result.Engine := Engine;
|
---|
832 | Angle := Random * 2 * Pi;
|
---|
833 | // Ensure minimum distance between stations
|
---|
834 | Pass := 0;
|
---|
835 | L := Step;
|
---|
836 | repeat
|
---|
837 | Result.Position := Point(Trunc(Engine.Map.Size.X / 2 + Cos(Angle) * L * 1.5),
|
---|
838 | Trunc(Engine.Map.Size.Y / 2 + Sin(Angle) * L));
|
---|
839 | MinD := High(Integer);
|
---|
840 | for I := 0 to Engine.Stations.Count - 1 do begin
|
---|
841 | D := Distance(Engine.Stations[I].Position, Result.Position);
|
---|
842 | if D < MinD then MinD := D;
|
---|
843 | end;
|
---|
844 | Inc(Pass);
|
---|
845 | L := L + StationMinDistance div 2;
|
---|
846 | until (MinD > StationMinDistance) or
|
---|
847 | (Pass > 1000) or (Engine.Stations.Count = 0);
|
---|
848 | Result.Shape := TStationShape(Random(Integer(Engine.ShapeCount)));
|
---|
849 | Add(Result);
|
---|
850 | Engine.ComputeShapeDistance;
|
---|
851 | end;
|
---|
852 |
|
---|
853 | { TMetroLines }
|
---|
854 |
|
---|
855 | function TMetroLines.AddNew: TMetroLine;
|
---|
856 | begin
|
---|
857 | Result := TMetroLine.Create;
|
---|
858 | Result.Color := LineColors[Count];
|
---|
859 | Result.Engine := Engine;
|
---|
860 | Result.Index := Count;
|
---|
861 | Add(Result);
|
---|
862 | end;
|
---|
863 |
|
---|
864 | function TMetroLines.SearchByColor(Color: TColor): TMetroLine;
|
---|
865 | var
|
---|
866 | I: Integer;
|
---|
867 | begin
|
---|
868 | I := 0;
|
---|
869 | while (I < Count) and (Items[I].Color <> Color) do Inc(I);
|
---|
870 | if I < Count then Result := Items[I]
|
---|
871 | else Result := nil;
|
---|
872 | end;
|
---|
873 |
|
---|
874 | { TMetroLine }
|
---|
875 |
|
---|
876 | procedure TMetroLine.UpdateEndingLines;
|
---|
877 | var
|
---|
878 | Index: Integer;
|
---|
879 | NewTrackPoint: TTrackPoint;
|
---|
880 | Angle: Double;
|
---|
881 | EndPoint: TPoint;
|
---|
882 | begin
|
---|
883 | if LineStations.Count >= 2 then begin
|
---|
884 | Index := Track.Points.IndexOf(LineStations.First.TrackPoint);
|
---|
885 | if Index = 0 then begin
|
---|
886 | NewTrackPoint := Track.Points.AddNew;
|
---|
887 | Track.Points.Insert(0, NewTrackPoint);
|
---|
888 | end;
|
---|
889 | Index := Track.Points.IndexOf(LineStations.Last.TrackPoint);
|
---|
890 | if Index = Track.Points.Count - 1 then begin
|
---|
891 | NewTrackPoint := Track.Points.AddNew;
|
---|
892 | Track.Points.Insert(Track.Points.Count, NewTrackPoint);
|
---|
893 | end;
|
---|
894 |
|
---|
895 | Angle := ArcTan2((Track.Points[2].PositionDesigned.Y - Track.Points[1].PositionDesigned.Y),
|
---|
896 | (Track.Points[2].PositionDesigned.X - Track.Points[1].PositionDesigned.X));
|
---|
897 | EndPoint := Point(Round(Track.Points[1].PositionDesigned.X - EndStationLength * Cos(Angle)),
|
---|
898 | Round(Track.Points[1].PositionDesigned.Y - EndStationLength * Sin(Angle)));
|
---|
899 | Track.Points.First.PositionDesigned := EndPoint;
|
---|
900 | Track.Points.First.Position := EndPoint;
|
---|
901 |
|
---|
902 | Angle := ArcTan2((Track.Points[Track.Points.Count - 2].PositionDesigned.Y - Track.Points[Track.Points.Count - 3].PositionDesigned.Y),
|
---|
903 | (Track.Points[Track.Points.Count - 2].PositionDesigned.X - Track.Points[Track.Points.Count - 3].PositionDesigned.X));
|
---|
904 | EndPoint := Point(Round(Track.Points[Track.Points.Count - 2].PositionDesigned.X + EndStationLength * Cos(Angle)),
|
---|
905 | Round(Track.Points[Track.Points.Count - 2].PositionDesigned.Y + EndStationLength * Sin(Angle)));
|
---|
906 | Track.Points.Last.PositionDesigned := EndPoint;
|
---|
907 | Track.Points.Last.Position := EndPoint;
|
---|
908 | end;
|
---|
909 | end;
|
---|
910 |
|
---|
911 | procedure TMetroLine.ConnectStation(Station: TMapStation; LineStationDown, LineStationUp: TLineStation);
|
---|
912 | var
|
---|
913 | Train: TMetroTrain;
|
---|
914 | NewTrackPoint: TTrackPoint;
|
---|
915 | NewLineStation: TLineStation;
|
---|
916 | Index: Integer;
|
---|
917 | begin
|
---|
918 | if not Assigned(Station) then
|
---|
919 | raise Exception.Create('Station have to be defined');
|
---|
920 | if not Assigned(LineStationDown) and not Assigned(LineStationUp) and (LineStations.Count > 0) then
|
---|
921 | raise Exception.Create('No old line station to connect new station');
|
---|
922 | NewLineStation := TLineStation.Create;
|
---|
923 | NewLineStation.Line := Self;
|
---|
924 | NewLineStation.MapStation := Station;
|
---|
925 | Index := 0;
|
---|
926 | if Assigned(LineStationDown) then Index := LineStations.IndexOf(LineStationDown) + 1
|
---|
927 | else if Assigned(LineStationDown) then Index := LineStations.IndexOf(LineStationUp);
|
---|
928 | LineStations.Insert(Index, NewLineStation);
|
---|
929 | Station.Lines.Add(Self);
|
---|
930 |
|
---|
931 | NewTrackPoint := Track.Points.AddNew;
|
---|
932 | NewTrackPoint.LineStation := NewLineStation;
|
---|
933 | NewTrackPoint.Position := Station.Position;
|
---|
934 | NewTrackPoint.PositionDesigned := NewTrackPoint.Position;
|
---|
935 | Index := 0;
|
---|
936 | if Assigned(LineStationDown) then Index := Track.Points.IndexOf(LineStationDown.TrackPoint) + 1
|
---|
937 | else if Assigned(LineStationUp) then Index := Track.Points.IndexOf(LineStationUp.TrackPoint);
|
---|
938 | Track.Points.Insert(Index, NewTrackPoint);
|
---|
939 | NewLineStation.TrackPoint := NewTrackPoint;
|
---|
940 |
|
---|
941 | if Assigned(LineStationDown) then
|
---|
942 | Track.RouteTrack(NewLineStation.TrackPoint.GetDown, NewLineStation.TrackPoint);
|
---|
943 | if Assigned(LineStationUp) then
|
---|
944 | Track.RouteTrack(NewLineStation.TrackPoint, NewLineStation.TrackPoint.GetUp);
|
---|
945 |
|
---|
946 | // Place one train if at least two stations present
|
---|
947 | if (LineStations.Count = 2) then begin
|
---|
948 | Train := Engine.Trains.GetUnusedTrain;
|
---|
949 | if Assigned(Train) then begin
|
---|
950 | Train.Line := Self;
|
---|
951 | Train.TargetStation := LineStations[0];
|
---|
952 | Train.BaseTrackPoint := Track.Points.First;
|
---|
953 | Trains.Add(Train);
|
---|
954 | end;
|
---|
955 | end;
|
---|
956 | UpdateEndingLines;
|
---|
957 | Engine.ComputeShapeDistance;
|
---|
958 | Engine.ShiftTrackPoints;
|
---|
959 | end;
|
---|
960 |
|
---|
961 | procedure TMetroLine.DisconnectStation(ALineStation: TLineStation);
|
---|
962 | var
|
---|
963 | I: Integer;
|
---|
964 | J: Integer;
|
---|
965 | Index: Integer;
|
---|
966 | TP1, TP2: TTrackPoint;
|
---|
967 | IsOnTrack: Boolean;
|
---|
968 | begin
|
---|
969 | // Determine track point range to be removed
|
---|
970 | TP1 := ALineStation.TrackPoint.GetDown;
|
---|
971 | if not Assigned(TP1) then TP1 := Track.Points.First;
|
---|
972 | TP2 := ALineStation.TrackPoint.GetUp;
|
---|
973 | if not Assigned(TP2) then TP2 := Track.Points.Last;
|
---|
974 |
|
---|
975 | // Remove track points from trains
|
---|
976 | for I := 0 to Trains.Count - 1 do
|
---|
977 | with Trains[I] do begin
|
---|
978 | IsOnTrack := False;
|
---|
979 | for J := Track.Points.IndexOf(TP1) to Track.Points.IndexOf(TP2) do
|
---|
980 | if Track.Points[J] = BaseTrackPoint then begin
|
---|
981 | IsOnTrack := True;
|
---|
982 | Break;
|
---|
983 | end;
|
---|
984 | if IsOnTrack then begin
|
---|
985 | if Assigned(BaseTrackPoint) and Assigned(BaseTrackPoint.GetUp) and (BaseTrackPoint.GetUp <> ALineStation.TrackPoint) then
|
---|
986 | BaseTrackPoint := BaseTrackPoint.GetUp
|
---|
987 | else
|
---|
988 | if Assigned(BaseTrackPoint) and Assigned(BaseTrackPoint.GetDown) and (BaseTrackPoint.GetDown <> ALineStation.TrackPoint) then
|
---|
989 | BaseTrackPoint := BaseTrackPoint.GetDown
|
---|
990 | else BaseTrackPoint := nil;
|
---|
991 | end;
|
---|
992 | end;
|
---|
993 |
|
---|
994 | // Delete old trackpoints
|
---|
995 | Index := Track.Points.IndexOf(ALineStation.TrackPoint) - 1;
|
---|
996 | while (Index >= 0) and (not Assigned(Track.Points[Index].LineStation)) do begin
|
---|
997 | Track.Points.Delete(Index);
|
---|
998 | Dec(Index);
|
---|
999 | end;
|
---|
1000 | Index := Index + 1;
|
---|
1001 | Track.Points.Delete(Index);
|
---|
1002 | while (Index < Track.Points.Count) and (not Assigned(Track.Points[Index].LineStation)) do
|
---|
1003 | Track.Points.Delete(Index);
|
---|
1004 |
|
---|
1005 | if ((Index - 1) >= 0) and (Index < Track.Points.Count) then
|
---|
1006 | Track.RouteTrack(Track.Points[Index - 1], Track.Points[Index]);
|
---|
1007 |
|
---|
1008 | ALineStation.MapStation.Lines.Remove(Self);
|
---|
1009 | Index := LineStations.IndexOf(ALineStation);
|
---|
1010 |
|
---|
1011 | for I := 0 to Trains.Count - 1 do
|
---|
1012 | with Trains[I] do begin
|
---|
1013 | if TargetStation = ALineStation then
|
---|
1014 | TargetStation := LineStations[(Index + 1) mod LineStations.Count];
|
---|
1015 | end;
|
---|
1016 |
|
---|
1017 | LineStations.Delete(Index);
|
---|
1018 |
|
---|
1019 | // Remove all trains if less then two stations
|
---|
1020 | if LineStations.Count < 2 then
|
---|
1021 | for I := Trains.Count - 1 downto 0 do begin
|
---|
1022 | Trains[I].Line := nil;
|
---|
1023 | Trains.Delete(I);
|
---|
1024 | end;
|
---|
1025 | UpdateEndingLines;
|
---|
1026 | Engine.ComputeShapeDistance;
|
---|
1027 | Engine.ShiftTrackPoints;
|
---|
1028 | end;
|
---|
1029 |
|
---|
1030 | procedure TTrack.RouteTrack(TP1, TP2: TTrackPoint);
|
---|
1031 | var
|
---|
1032 | NewTrackPoint: TTrackPoint;
|
---|
1033 | Delta: TPoint;
|
---|
1034 | P1, P2: TPoint;
|
---|
1035 | Index1, Index2: Integer;
|
---|
1036 | begin
|
---|
1037 | RemoveTrackBetween(TP1, TP2);
|
---|
1038 | Index1 := Points.IndexOf(TP1);
|
---|
1039 | Index2 := Points.IndexOf(TP2);
|
---|
1040 | P1 := Points[Index1].PositionDesigned;
|
---|
1041 | P2 := Points[Index2].PositionDesigned;
|
---|
1042 | NewTrackPoint := Points.AddNew;
|
---|
1043 | Delta := Point(P2.X - P1.X, P2.Y - P1.Y);
|
---|
1044 | if Abs(Delta.X) > Abs(Delta.Y) then begin
|
---|
1045 | NewTrackPoint.PositionDesigned := Point(P2.X - Sign(Delta.X) * Abs(Delta.Y), P1.Y);
|
---|
1046 | NewTrackPoint.Position := NewTrackPoint.PositionDesigned;
|
---|
1047 | end else begin
|
---|
1048 | NewTrackPoint.PositionDesigned := Point(P1.X, P2.Y - Sign(Delta.Y) * Abs(Delta.X));
|
---|
1049 | NewTrackPoint.Position := NewTrackPoint.PositionDesigned;
|
---|
1050 | end;
|
---|
1051 | Points.Insert(Index1 + 1, NewTrackPoint);
|
---|
1052 | end;
|
---|
1053 |
|
---|
1054 | procedure TTrack.RemoveTrackBetween(TP1, TP2: TTrackPoint);
|
---|
1055 | var
|
---|
1056 | Index1, Index2: Integer;
|
---|
1057 | Temp: Integer;
|
---|
1058 | I: Integer;
|
---|
1059 | begin
|
---|
1060 | Index1 := Points.IndexOf(TP1);
|
---|
1061 | Index2 := Points.IndexOf(TP2);
|
---|
1062 | if (Index1 = -1) then
|
---|
1063 | raise Exception.Create('TrackPoint1 not found');
|
---|
1064 | if (Index2 = -1) then
|
---|
1065 | raise Exception.Create('TrackPoint2 not found');
|
---|
1066 | if Index1 > Index2 then begin
|
---|
1067 | Temp := Index1;
|
---|
1068 | Index1 := Index2;
|
---|
1069 | Index2 := Temp;
|
---|
1070 | end;
|
---|
1071 | for I := 1 to Index2 - Index1 - 1 do
|
---|
1072 | Points.Delete(Index1 + 1);
|
---|
1073 | end;
|
---|
1074 |
|
---|
1075 | function TMetroLine.GetTrackLength: Integer;
|
---|
1076 | var
|
---|
1077 | I: Integer;
|
---|
1078 | begin
|
---|
1079 | Result := 0;
|
---|
1080 | for I := 0 to Track.Points.Count - 1 do
|
---|
1081 | if I > 0 then
|
---|
1082 | Result := Result + Distance(Track.Points[I].Position, Track.Points[I - 1].Position);
|
---|
1083 | end;
|
---|
1084 |
|
---|
1085 | constructor TMetroLine.Create;
|
---|
1086 | begin
|
---|
1087 | LineStations := TLineStations.Create;
|
---|
1088 | LineStations.FreeObjects := True;
|
---|
1089 | Trains := TMetroTrains.Create;
|
---|
1090 | Trains.FreeObjects := False;
|
---|
1091 | Track := TTrack.Create;
|
---|
1092 | Track.Line := Self;
|
---|
1093 | end;
|
---|
1094 |
|
---|
1095 | destructor TMetroLine.Destroy;
|
---|
1096 | begin
|
---|
1097 | Trains.Free;
|
---|
1098 | LineStations.Free;
|
---|
1099 | Track.Free;
|
---|
1100 | inherited;
|
---|
1101 | end;
|
---|
1102 |
|
---|
1103 | function TMetroLine.IsCircular: Boolean;
|
---|
1104 | begin
|
---|
1105 | Result := False;
|
---|
1106 | if LineStations.Count >= 2 then
|
---|
1107 | Result := (LineStations.Last.MapStation = LineStations.First.MapStation);
|
---|
1108 | end;
|
---|
1109 |
|
---|
1110 | { TMetroTrain }
|
---|
1111 |
|
---|
1112 | procedure TMetroTrain.SetLine(AValue: TMetroLine);
|
---|
1113 | begin
|
---|
1114 | if FLine = AValue then Exit;
|
---|
1115 | FLine := AValue;
|
---|
1116 | if AValue = nil then begin
|
---|
1117 | RelPos := 0;
|
---|
1118 | BaseTrackPoint := nil;
|
---|
1119 | TargetStation := nil;
|
---|
1120 | end;
|
---|
1121 | end;
|
---|
1122 |
|
---|
1123 | function TMetroTrain.GetTargetStationDistance: Integer;
|
---|
1124 | var
|
---|
1125 | Current: Integer;
|
---|
1126 | Target: Integer;
|
---|
1127 | I: Integer;
|
---|
1128 | begin
|
---|
1129 | Result := 0;
|
---|
1130 | if Assigned(BaseTrackPoint) and Assigned(TargetStation) then begin
|
---|
1131 | Current := Line.Track.Points.IndexOf(BaseTrackPoint);
|
---|
1132 | Target := Line.Track.Points.IndexOf(TargetStation.TrackPoint);
|
---|
1133 | if Current < Target then begin
|
---|
1134 | for I := Current to Target - 1 do
|
---|
1135 | Result := Result + Line.Track.Points[I].GetDistance;
|
---|
1136 | Result := Result - Trunc(RelPos);
|
---|
1137 | end else
|
---|
1138 | if Current > Target then begin
|
---|
1139 | for I := Current - 1 downto Target do
|
---|
1140 | Result := Result + Line.Track.Points[I].GetDistance;
|
---|
1141 | Result := Result + Trunc(RelPos);
|
---|
1142 | end else Result := Trunc(RelPos);
|
---|
1143 | end;
|
---|
1144 | end;
|
---|
1145 |
|
---|
1146 | function TMetroTrain.GetPosition: TPoint;
|
---|
1147 | var
|
---|
1148 | D: Integer;
|
---|
1149 | Delta: TPoint;
|
---|
1150 | UpPoint: TTrackPoint;
|
---|
1151 | begin
|
---|
1152 | Result := Point(0, 0);
|
---|
1153 | if Assigned(BaseTrackPoint) then
|
---|
1154 | with BaseTrackPoint do begin
|
---|
1155 | UpPoint := BaseTrackPoint.GetNeighUp;
|
---|
1156 | if Assigned(UpPoint) then begin
|
---|
1157 | D := Distance(UpPoint.Position, Position);
|
---|
1158 | if D > 0 then begin
|
---|
1159 | Delta := SubPoint(UpPoint.Position, Position);
|
---|
1160 | Result := Point(Trunc(Position.X + Delta.X * RelPos / D),
|
---|
1161 | Trunc(Position.Y + Delta.Y * RelPos / D));
|
---|
1162 | end;
|
---|
1163 | end;
|
---|
1164 | end;
|
---|
1165 | end;
|
---|
1166 |
|
---|
1167 | function TMetroTrain.GetAngle: Double;
|
---|
1168 | var
|
---|
1169 | UpPoint: TTrackPoint;
|
---|
1170 | begin
|
---|
1171 | Result := 0;
|
---|
1172 | if Assigned(BaseTrackPoint) then
|
---|
1173 | with BaseTrackPoint do begin
|
---|
1174 | UpPoint := BaseTrackPoint.GetNeighUp;
|
---|
1175 | if Assigned(UpPoint) then begin
|
---|
1176 | Result := ArcTan2(UpPoint.Position.Y - Position.Y,
|
---|
1177 | UpPoint.Position.X - Position.X);
|
---|
1178 | end;
|
---|
1179 | end;
|
---|
1180 | end;
|
---|
1181 |
|
---|
1182 | constructor TMetroTrain.Create;
|
---|
1183 | begin
|
---|
1184 | Passengers := TMetroPassengers.Create;
|
---|
1185 | Passengers.FreeObjects := False;
|
---|
1186 | Direction := 1;
|
---|
1187 | Line := nil;
|
---|
1188 | end;
|
---|
1189 |
|
---|
1190 | destructor TMetroTrain.Destroy;
|
---|
1191 | begin
|
---|
1192 | Passengers.Free;
|
---|
1193 | inherited;
|
---|
1194 | end;
|
---|
1195 |
|
---|
1196 | { TMapStation }
|
---|
1197 |
|
---|
1198 | procedure TMapStation.ShiftTrackPoints;
|
---|
1199 | var
|
---|
1200 | TrackLinks: TTrackLinks;
|
---|
1201 | I: Integer;
|
---|
1202 | J: Integer;
|
---|
1203 | Index: Integer;
|
---|
1204 | TP: TTrackPoint;
|
---|
1205 | LS: TLineStation;
|
---|
1206 | Line: TMetroLine;
|
---|
1207 | Angle: Float;
|
---|
1208 | TPAngleGroup: TTrackPointsAngleGroup;
|
---|
1209 | GroupItem: TTrackPointsAngle;
|
---|
1210 | NewTrackLink: TTrackLink;
|
---|
1211 | HAngle: Double;
|
---|
1212 | P1, P2: TPoint;
|
---|
1213 | NewShift: TPoint;
|
---|
1214 | begin
|
---|
1215 | TrackLinks := TTrackLinks.Create;
|
---|
1216 | TrackLinks.FreeObjects := False;
|
---|
1217 |
|
---|
1218 | // Collect all near track points as track links
|
---|
1219 | SortLines;
|
---|
1220 | for I := 0 to Lines.Count - 1 do begin
|
---|
1221 | Line := Lines[I];
|
---|
1222 | LS := Line.LineStations.SearchMapStation(Self);
|
---|
1223 | TP := LS.TrackPoint;
|
---|
1224 | Index := Line.Track.Points.IndexOf(TP);
|
---|
1225 | if Index > 0 then begin
|
---|
1226 | NewTrackLink := Line.Track.Points[Index].GetLinkDown;
|
---|
1227 | TrackLinks.Add(NewTrackLink);
|
---|
1228 | end;
|
---|
1229 | if Index < (Line.Track.Points.Count - 1) then begin
|
---|
1230 | NewTrackLink := Line.Track.Points[Index].GetLinkUp;
|
---|
1231 | TrackLinks.Add(NewTrackLink);
|
---|
1232 | end;
|
---|
1233 | if Line.IsCircular and (Self = Line.LineStations.First.MapStation) and
|
---|
1234 | (Self = Line.LineStations.Last.MapStation) then begin
|
---|
1235 | LS := Line.LineStations.Last;
|
---|
1236 | TP := LS.TrackPoint;
|
---|
1237 | Index := Line.Track.Points.IndexOf(TP);
|
---|
1238 | if Index > 0 then begin
|
---|
1239 | NewTrackLink := Line.Track.Points[Index].GetLinkDown;
|
---|
1240 | TrackLinks.Add(NewTrackLink);
|
---|
1241 | end;
|
---|
1242 | if Index < (Line.Track.Points.Count - 1) then begin
|
---|
1243 | NewTrackLink := Line.Track.Points[Index].GetLinkUp;
|
---|
1244 | TrackLinks.Add(NewTrackLink);
|
---|
1245 | end;
|
---|
1246 | end;
|
---|
1247 | end;
|
---|
1248 |
|
---|
1249 | // Make groups of TrackLinks with same angle
|
---|
1250 | TPAngleGroup := TTrackPointsAngleGroup.Create;
|
---|
1251 | for I := 0 to TrackLinks.Count - 1 do begin
|
---|
1252 | P1 := TrackLinks[I].Points[0].PositionDesigned;
|
---|
1253 | P2 := TrackLinks[I].Points[1].PositionDesigned;
|
---|
1254 | if ComparePoint(P1, Position) and not ComparePoint(P2, Position) then begin
|
---|
1255 | Angle := ArcTan2(P2.Y - Position.Y, P2.X - Position.X);
|
---|
1256 | end else
|
---|
1257 | if ComparePoint(P2, Position) and not ComparePoint(P1, Position) then begin
|
---|
1258 | Angle := ArcTan2(P1.Y - Position.Y, P1.X - Position.X);
|
---|
1259 | end else Angle := 0;// else raise Exception.Create('TrackLink angle error');
|
---|
1260 |
|
---|
1261 | GroupItem := TPAngleGroup.SearchAngle(Angle);
|
---|
1262 | if not Assigned(GroupItem) then begin
|
---|
1263 | GroupItem := TTrackPointsAngle.Create;
|
---|
1264 | GroupItem.Angle := Angle;
|
---|
1265 | TPAngleGroup.Add(GroupItem);
|
---|
1266 | end;
|
---|
1267 | GroupItem.TrackLinks.Add(TrackLinks[I]);
|
---|
1268 | end;
|
---|
1269 |
|
---|
1270 | // Shift TrackLinks according number of lines in group
|
---|
1271 | for I := 0 to TPAngleGroup.Count - 1 do
|
---|
1272 | with TPAngleGroup[I] do begin
|
---|
1273 | for J := 0 to TrackLinks.Count - 1 do
|
---|
1274 | with TrackLinks[J] do begin
|
---|
1275 | // Get orthogonal angle
|
---|
1276 | HAngle := Angle + Pi / 2;
|
---|
1277 | if HAngle > Pi then HAngle := HAngle - Pi;
|
---|
1278 | NewShift.X := Trunc(MetroLineThickness * Cos(HAngle) * (J - (TrackLinks.Count - 1) / 2));
|
---|
1279 | NewShift.Y := Trunc(MetroLineThickness * Sin(HAngle) * (J - (TrackLinks.Count - 1) / 2));
|
---|
1280 | Shift := NewShift;
|
---|
1281 | end;
|
---|
1282 | end;
|
---|
1283 |
|
---|
1284 | FreeAndNil(TPAngleGroup);
|
---|
1285 | FreeAndNil(TrackLinks);
|
---|
1286 | end;
|
---|
1287 |
|
---|
1288 | function MapStationCompareLine(const Item1, Item2: TMetroLine): Integer;
|
---|
1289 | begin
|
---|
1290 | if Item1.Index > Item2.Index then Result := 1
|
---|
1291 | else if Item1.Index < Item2.Index then Result := -1
|
---|
1292 | else Result := 0;
|
---|
1293 | end;
|
---|
1294 |
|
---|
1295 | procedure TMapStation.SortLines;
|
---|
1296 | begin
|
---|
1297 | Lines.Sort(MapStationCompareLine);
|
---|
1298 | end;
|
---|
1299 |
|
---|
1300 | function TMapStation.IsBestStationForShape(Shape: TStationShape;
|
---|
1301 | Check, Current: TLineStation): Boolean;
|
---|
1302 | var
|
---|
1303 | I: Integer;
|
---|
1304 | T: Integer;
|
---|
1305 | Distance: Integer;
|
---|
1306 | StationIndex: Integer;
|
---|
1307 | DirectionUp: Boolean;
|
---|
1308 | DirectionDown: Boolean;
|
---|
1309 | NextStationUp: TLineStation;
|
---|
1310 | NextStationDown: TLineStation;
|
---|
1311 | CurrentLineStation: TLineStation;
|
---|
1312 | begin
|
---|
1313 | Distance := High(Integer);
|
---|
1314 | for I := 0 to Lines.Count - 1 do
|
---|
1315 | with Lines[I] do begin
|
---|
1316 | CurrentLineStation := LineStations.SearchMapStation(Current.MapStation);
|
---|
1317 | StationIndex := LineStations.IndexOf(CurrentLineStation);
|
---|
1318 | if IsCircular then begin
|
---|
1319 | DirectionUp := False;
|
---|
1320 | DirectionDown := False;
|
---|
1321 | for T := 0 to Trains.Count - 1 do begin
|
---|
1322 | if Trains[T].Direction = 1 then DirectionUp := True;
|
---|
1323 | if Trains[T].Direction = -1 then DirectionDown := True;
|
---|
1324 | end;
|
---|
1325 | if StationIndex = 0 then
|
---|
1326 | NextStationDown := LineStations[LineStations.Count - 2]
|
---|
1327 | else
|
---|
1328 | if StationIndex > 0 then
|
---|
1329 | NextStationDown := LineStations[StationIndex - 1];
|
---|
1330 |
|
---|
1331 | if (StationIndex >= 0) and (StationIndex = LineStations.Count - 1) then
|
---|
1332 | NextStationUp := LineStations[1]
|
---|
1333 | else
|
---|
1334 | if (StationIndex >= 0) and (StationIndex < LineStations.Count - 1) then
|
---|
1335 | NextStationUp := LineStations[StationIndex + 1];
|
---|
1336 | end else begin
|
---|
1337 | if StationIndex > 0 then begin
|
---|
1338 | DirectionDown := True;
|
---|
1339 | NextStationDown := LineStations[StationIndex - 1]
|
---|
1340 | end else DirectionDown := False;
|
---|
1341 | if (StationIndex >= 0) and (StationIndex < LineStations.Count - 1) then begin
|
---|
1342 | DirectionUp := True;
|
---|
1343 | NextStationUp := LineStations[StationIndex + 1];
|
---|
1344 | end else DirectionUp := False;
|
---|
1345 | end;
|
---|
1346 | if DirectionDown and (NextStationDown.MapStation.ShapeDistance[Shape] <> -1) and
|
---|
1347 | (NextStationDown.MapStation.ShapeDistance[Shape] < Distance) then begin
|
---|
1348 | Distance := NextStationDown.MapStation.ShapeDistance[Shape];
|
---|
1349 | end;
|
---|
1350 | if DirectionUp and (NextStationUp.MapStation.ShapeDistance[Shape] <> -1) and
|
---|
1351 | (NextStationUp.MapStation.ShapeDistance[Shape] < Distance) then begin
|
---|
1352 | Distance := NextStationUp.MapStation.ShapeDistance[Shape];
|
---|
1353 | end;
|
---|
1354 | end;
|
---|
1355 | Result := (Check.MapStation.ShapeDistance[Shape] <> -1) and
|
---|
1356 | (Check.MapStation.ShapeDistance[Shape] <= Distance);
|
---|
1357 | end;
|
---|
1358 |
|
---|
1359 | constructor TMapStation.Create;
|
---|
1360 | begin
|
---|
1361 | Passengers := TMetroPassengers.Create;
|
---|
1362 | Passengers.FreeObjects := False;
|
---|
1363 | Lines := TMetroLines.Create;
|
---|
1364 | Lines.FreeObjects := False;
|
---|
1365 | end;
|
---|
1366 |
|
---|
1367 | destructor TMapStation.Destroy;
|
---|
1368 | begin
|
---|
1369 | FreeAndNil(Lines);
|
---|
1370 | FreeAndNil(Passengers);
|
---|
1371 | inherited;
|
---|
1372 | end;
|
---|
1373 |
|
---|
1374 | { TEngine }
|
---|
1375 |
|
---|
1376 | procedure TEngine.ResizeView;
|
---|
1377 | var
|
---|
1378 | NewPoint: TPoint;
|
---|
1379 | begin
|
---|
1380 | // Need to see all stations on screen
|
---|
1381 | View.SourceRect := RectEnlarge(Stations.GetRect, 100);
|
---|
1382 |
|
---|
1383 | NewPoint := Point(
|
---|
1384 | Trunc((View.SourceRect.Left + (View.SourceRect.Right - View.SourceRect.Left) / 2) -
|
---|
1385 | (View.DestRect.Left + (View.DestRect.Right - View.DestRect.Left) / 2 / View.Zoom)),
|
---|
1386 | Trunc((View.SourceRect.Top + (View.SourceRect.Bottom - View.SourceRect.Top) / 2) -
|
---|
1387 | (View.DestRect.Top + (View.DestRect.Bottom - View.DestRect.Top) / 2 / View.Zoom)));
|
---|
1388 | View.SourceRect := Bounds(NewPoint.X, NewPoint.Y, Trunc((View.DestRect.Right - View.DestRect.Left) / View.Zoom),
|
---|
1389 | Trunc((View.DestRect.Bottom - View.DestRect.Top) / View.Zoom));
|
---|
1390 | end;
|
---|
1391 |
|
---|
1392 | function TEngine.GetServedDaysCount: Integer;
|
---|
1393 | begin
|
---|
1394 | Result := Trunc(Time);
|
---|
1395 | end;
|
---|
1396 |
|
---|
1397 | function TEngine.GetExistStationShapes: TStationShapeSet;
|
---|
1398 | var
|
---|
1399 | Station: TMapStation;
|
---|
1400 | begin
|
---|
1401 | Result := [];
|
---|
1402 | for Station in Stations do
|
---|
1403 | Result := Result + [Station.Shape];
|
---|
1404 | end;
|
---|
1405 |
|
---|
1406 | function TEngine.GetStationOnPos(Pos: TPoint): TMapStation;
|
---|
1407 | var
|
---|
1408 | I: Integer;
|
---|
1409 | const
|
---|
1410 | ClickDistance = 30;
|
---|
1411 | begin
|
---|
1412 | I := 0;
|
---|
1413 | while (I < Stations.Count) and (Distance(Stations[I].Position, Pos) > ClickDistance) do Inc(I);
|
---|
1414 | if I < Stations.Count then Result := Stations[I]
|
---|
1415 | else Result := nil;
|
---|
1416 | end;
|
---|
1417 |
|
---|
1418 | function TEngine.GetTrackOnPos(Pos: TPoint): TTrackLink;
|
---|
1419 | var
|
---|
1420 | I: Integer;
|
---|
1421 | T: Integer;
|
---|
1422 | D: Integer;
|
---|
1423 | MinD: Integer;
|
---|
1424 | begin
|
---|
1425 | Result := TTrackLink.Create;
|
---|
1426 | Result.Points.Count := 2;
|
---|
1427 | Result.Points[0] := nil;
|
---|
1428 | Result.Points[1] := nil;
|
---|
1429 | I := 0;
|
---|
1430 | MinD := High(Integer);
|
---|
1431 | while (I < Lines.Count) do
|
---|
1432 | with Lines[I] do begin
|
---|
1433 | for T := 1 to Track.Points.Count - 1 do begin
|
---|
1434 | D := PointToLineDistance(Pos, Track.Points[T - 1].Position, Track.Points[T].Position);
|
---|
1435 | if (D < MinD) and (D < TrackClickDistance) then begin
|
---|
1436 | MinD := D;
|
---|
1437 | Result.Points[0] := Track.Points[T - 1];
|
---|
1438 | Result.Points[1] := Track.Points[T];
|
---|
1439 | end;
|
---|
1440 | end;
|
---|
1441 | Inc(I);
|
---|
1442 | end;
|
---|
1443 | end;
|
---|
1444 |
|
---|
1445 | function TEngine.GetTrainOnPos(Pos: TPoint): TMetroTrain;
|
---|
1446 | var
|
---|
1447 | I: Integer;
|
---|
1448 | MinDistance: Integer;
|
---|
1449 | D: Integer;
|
---|
1450 | begin
|
---|
1451 | Result := nil;
|
---|
1452 | MinDistance := High(Integer);
|
---|
1453 | for I := 0 to Trains.Count - 1 do
|
---|
1454 | with Trains[I] do begin
|
---|
1455 | D := Distance(GetPosition, Pos);
|
---|
1456 | if (D < (TrainSize div 2)) and (D < MinDistance) then begin
|
---|
1457 | Result := Trains[I];
|
---|
1458 | MinDistance := D;
|
---|
1459 | end;
|
---|
1460 | end;
|
---|
1461 | end;
|
---|
1462 |
|
---|
1463 | procedure TEngine.DrawLine(Canvas: TCanvas; Pos: TPoint);
|
---|
1464 | var
|
---|
1465 | Delta: TPoint;
|
---|
1466 | begin
|
---|
1467 | Delta := Point(Pos.X - Canvas.PenPos.X, Pos.Y - Canvas.PenPos.Y);
|
---|
1468 | if Abs(Delta.X) > Abs(Delta.Y) then begin
|
---|
1469 | Canvas.LineTo(Pos.X - Sign(Delta.X) * Abs(Delta.Y), Canvas.PenPos.Y);
|
---|
1470 | end else begin
|
---|
1471 | Canvas.LineTo(Canvas.PenPos.X, Pos.Y - Sign(Delta.Y) * Abs(Delta.X));
|
---|
1472 | end;
|
---|
1473 | Canvas.LineTo(Pos.X, Pos.Y);
|
---|
1474 | end;
|
---|
1475 |
|
---|
1476 | procedure TEngine.DrawShape(Canvas: TCanvas; Position: TPoint; Shape: TStationShape;
|
---|
1477 | Size: Integer; Angle: Double);
|
---|
1478 | var
|
---|
1479 | Points: array of TPoint;
|
---|
1480 | I: Integer;
|
---|
1481 | Angle2: Double;
|
---|
1482 | begin
|
---|
1483 | case Shape of
|
---|
1484 | ssSquare: begin
|
---|
1485 | SetLength(Points, 4);
|
---|
1486 | Points[0] := Point(Position.X - Size div 2, Position.Y - Size div 2);
|
---|
1487 | Points[1] := Point(Position.X + Size div 2, Position.Y - Size div 2);
|
---|
1488 | Points[2] := Point(Position.X + Size div 2, Position.Y + Size div 2);
|
---|
1489 | Points[3] := Point(Position.X - Size div 2, Position.Y + Size div 2);
|
---|
1490 | Points := RotatePoints(Position, Points, Angle);
|
---|
1491 | Canvas.Polygon(Points);
|
---|
1492 | end;
|
---|
1493 | ssCircle: Canvas.Ellipse(
|
---|
1494 | Position.X - Size div 2, Position.Y - Size div 2,
|
---|
1495 | Position.X + Size div 2, Position.Y + Size div 2);
|
---|
1496 | ssTriangle: begin
|
---|
1497 | SetLength(Points, 3);
|
---|
1498 | Points[0] := Point(Position.X, Position.Y - Size div 2);
|
---|
1499 | Points[1] := Point(Position.X + Size div 2, Position.Y + Size div 2);
|
---|
1500 | Points[2] := Point(Position.X - Size div 2, Position.Y + Size div 2);
|
---|
1501 | Points := RotatePoints(Position, Points, Angle);
|
---|
1502 | Canvas.Polygon(Points);
|
---|
1503 | end;
|
---|
1504 | ssStar: begin
|
---|
1505 | SetLength(Points, 10);
|
---|
1506 | for I := 0 to 9 do begin
|
---|
1507 | Angle2 := I / 10 * 2 * Pi - Pi / 2;
|
---|
1508 | if (I mod 2) = 0 then
|
---|
1509 | Points[I] := Point(Round(Position.X + Cos(Angle2) * Size / 2),
|
---|
1510 | Round(Position.Y + Sin(Angle2) * Size / 2))
|
---|
1511 | else
|
---|
1512 | Points[I] := Point(Round(Position.X + Cos(Angle2) * Size / 5),
|
---|
1513 | Round(Position.Y + Sin(Angle2) * Size / 5));
|
---|
1514 | end;
|
---|
1515 | Points := RotatePoints(Position, Points, Angle);
|
---|
1516 | Canvas.Polygon(Points);
|
---|
1517 | end;
|
---|
1518 | ssPlus: begin
|
---|
1519 | SetLength(Points, 12);
|
---|
1520 | Points[0] := Point(Position.X + Size div 6, Position.Y - Size div 6);
|
---|
1521 | Points[1] := Point(Position.X + Size div 2, Position.Y - Size div 6);
|
---|
1522 | Points[2] := Point(Position.X + Size div 2, Position.Y + Size div 6);
|
---|
1523 | Points[3] := Point(Position.X + Size div 6, Position.Y + Size div 6);
|
---|
1524 | Points[4] := Point(Position.X + Size div 6, Position.Y + Size div 2);
|
---|
1525 | Points[5] := Point(Position.X - Size div 6, Position.Y + Size div 2);
|
---|
1526 | Points[6] := Point(Position.X - Size div 6, Position.Y + Size div 6);
|
---|
1527 | Points[7] := Point(Position.X - Size div 2, Position.Y + Size div 6);
|
---|
1528 | Points[8] := Point(Position.X - Size div 2, Position.Y - Size div 6);
|
---|
1529 | Points[9] := Point(Position.X - Size div 6, Position.Y - Size div 6);
|
---|
1530 | Points[10] := Point(Position.X - Size div 6, Position.Y - Size div 2);
|
---|
1531 | Points[11] := Point(Position.X + Size div 6, Position.Y - Size div 2);
|
---|
1532 | Points := RotatePoints(Position, Points, Angle);
|
---|
1533 | Canvas.Polygon(Points);
|
---|
1534 | end;
|
---|
1535 | ssPentagon: begin
|
---|
1536 | SetLength(Points, 5);
|
---|
1537 | for I := 0 to 4 do begin
|
---|
1538 | Angle2 := I / 5 * 2 * Pi - Pi / 2;
|
---|
1539 | Points[I] := Point(Round(Position.X + Cos(Angle2) * Size / 2),
|
---|
1540 | Round(Position.Y + Sin(Angle2) * Size / 2));
|
---|
1541 | end;
|
---|
1542 | Points := RotatePoints(Position, Points, Angle);
|
---|
1543 | Canvas.Polygon(Points);
|
---|
1544 | end;
|
---|
1545 | ssHexagon: begin
|
---|
1546 | SetLength(Points, 6);
|
---|
1547 | for I := 0 to 5 do begin
|
---|
1548 | Angle2 := I / 6 * 2 * Pi - Pi / 2;
|
---|
1549 | Points[I] := Point(Round(Position.X + Cos(Angle2) * Size / 2),
|
---|
1550 | Round(Position.Y + Sin(Angle2) * Size / 2));
|
---|
1551 | end;
|
---|
1552 | Points := RotatePoints(Position, Points, Angle);
|
---|
1553 | Canvas.Polygon(Points);
|
---|
1554 | end;
|
---|
1555 | ssDiamond: begin
|
---|
1556 | SetLength(Points, 4);
|
---|
1557 | Points[0] := Point(Position.X, Position.Y - Size div 2);
|
---|
1558 | Points[1] := Point(Position.X + Size div 2, Position.Y);
|
---|
1559 | Points[2] := Point(Position.X, Position.Y + Size div 2);
|
---|
1560 | Points[3] := Point(Position.X - Size div 2, Position.Y);
|
---|
1561 | Points := RotatePoints(Position, Points, Angle);
|
---|
1562 | Canvas.Polygon(Points);
|
---|
1563 | end;
|
---|
1564 | ssCross: begin
|
---|
1565 | SetLength(Points, 12);
|
---|
1566 | Points[0] := Point(Position.X + Size div 6, Position.Y - Size div 6);
|
---|
1567 | Points[1] := Point(Position.X + Size div 2, Position.Y - Size div 6);
|
---|
1568 | Points[2] := Point(Position.X + Size div 2, Position.Y + Size div 6);
|
---|
1569 | Points[3] := Point(Position.X + Size div 6, Position.Y + Size div 6);
|
---|
1570 | Points[4] := Point(Position.X + Size div 6, Position.Y + Size div 2);
|
---|
1571 | Points[5] := Point(Position.X - Size div 6, Position.Y + Size div 2);
|
---|
1572 | Points[6] := Point(Position.X - Size div 6, Position.Y + Size div 6);
|
---|
1573 | Points[7] := Point(Position.X - Size div 2, Position.Y + Size div 6);
|
---|
1574 | Points[8] := Point(Position.X - Size div 2, Position.Y - Size div 6);
|
---|
1575 | Points[9] := Point(Position.X - Size div 6, Position.Y - Size div 6);
|
---|
1576 | Points[10] := Point(Position.X - Size div 6, Position.Y - Size div 2);
|
---|
1577 | Points[11] := Point(Position.X + Size div 6, Position.Y - Size div 2);
|
---|
1578 | Points := RotatePoints(Position, Points, Angle + Pi / 4);
|
---|
1579 | Canvas.Polygon(Points);
|
---|
1580 | end;
|
---|
1581 | ssHalfCircle: Canvas.Pie(
|
---|
1582 | Position.X - Size div 2, Position.Y - Size div 2,
|
---|
1583 | Position.X + Size div 2, Position.Y + Size div 2,
|
---|
1584 | Position.X - Size div 2, Position.Y,
|
---|
1585 | Position.X + Size div 2, Position.Y);
|
---|
1586 | ssQuarterCircle: Canvas.Pie(
|
---|
1587 | Position.X - Size div 2 - Size, Position.Y - Size div 2,
|
---|
1588 | Position.X + Size div 2, Position.Y + Size div 2 + Size,
|
---|
1589 | Position.X + Size div 2, Position.Y + Size div 2,
|
---|
1590 | Position.X - Size div 2, Position.Y - Size div 2);
|
---|
1591 | ssHeptagon: begin
|
---|
1592 | SetLength(Points, 8);
|
---|
1593 | for I := 0 to High(Points) do begin
|
---|
1594 | Angle2 := I / Length(Points) * 2 * Pi - Pi / 2;
|
---|
1595 | Points[I] := Point(Round(Position.X + Cos(Angle2) * Size / 2),
|
---|
1596 | Round(Position.Y + Sin(Angle2) * Size / 2));
|
---|
1597 | end;
|
---|
1598 | Points := RotatePoints(Position, Points, Angle);
|
---|
1599 | Canvas.Polygon(Points);
|
---|
1600 | end;
|
---|
1601 | end;
|
---|
1602 | end;
|
---|
1603 |
|
---|
1604 | procedure TEngine.ComputeShapeDistance;
|
---|
1605 | var
|
---|
1606 | S: TStationShape;
|
---|
1607 | Station: TMapStation;
|
---|
1608 | begin
|
---|
1609 | // NewGame all distances
|
---|
1610 | for Station in Stations do
|
---|
1611 | with Station do begin
|
---|
1612 | for S := Low(ShapeDistance) to High(ShapeDistance) do
|
---|
1613 | ShapeDistance[S] := -1;
|
---|
1614 | end;
|
---|
1615 |
|
---|
1616 | // Propagate shape distance for all stations
|
---|
1617 | // Distace 0 means that station is final target
|
---|
1618 | for Station in Stations do
|
---|
1619 | with Station do begin
|
---|
1620 | ComputeShapeDistanceStation(Station, Shape, 0);
|
---|
1621 | end;
|
---|
1622 | end;
|
---|
1623 |
|
---|
1624 | procedure TEngine.ComputeShapeDistanceStation(Station: TMapStation;
|
---|
1625 | UpdatedShape: TStationShape; Distance: Integer);
|
---|
1626 | var
|
---|
1627 | I: Integer;
|
---|
1628 | T: Integer;
|
---|
1629 | StationIndex: Integer;
|
---|
1630 | DirectionDown: Boolean;
|
---|
1631 | DirectionUp: Boolean;
|
---|
1632 | begin
|
---|
1633 | with Station do begin
|
---|
1634 | if (Distance < ShapeDistance[UpdatedShape]) or (ShapeDistance[UpdatedShape] = -1) then begin
|
---|
1635 | ShapeDistance[UpdatedShape] := Distance;
|
---|
1636 | // Do for all lines connected to station
|
---|
1637 | for I := 0 to Lines.Count - 1 do
|
---|
1638 | with Lines[I] do
|
---|
1639 | for StationIndex := 0 to LineStations.Count - 1 do
|
---|
1640 | if LineStations[StationIndex].MapStation = Station then begin
|
---|
1641 | if not IsCircular then begin
|
---|
1642 | // Update for all adjecent stations
|
---|
1643 | if StationIndex > 0 then
|
---|
1644 | ComputeShapeDistanceStation(LineStations[StationIndex - 1].MapStation,
|
---|
1645 | UpdatedShape, Station.ShapeDistance[UpdatedShape] + 1);
|
---|
1646 | if (StationIndex >= 0) and (StationIndex < LineStations.Count - 1) then
|
---|
1647 | ComputeShapeDistanceStation(LineStations[StationIndex + 1].MapStation,
|
---|
1648 | UpdatedShape, Station.ShapeDistance[UpdatedShape] + 1);
|
---|
1649 | end else begin
|
---|
1650 | // If circular then trains might go in single direction so passengers
|
---|
1651 | // waiting for opposite directions are wrong
|
---|
1652 | DirectionUp := False;
|
---|
1653 | DirectionDown := False;
|
---|
1654 | for T := 0 to Trains.Count - 1 do begin
|
---|
1655 | if Trains[T].Direction = 1 then DirectionUp := True;
|
---|
1656 | if Trains[T].Direction = -1 then DirectionDown := True;
|
---|
1657 | end;
|
---|
1658 | // Update for all adjecent stations
|
---|
1659 | if DirectionUp then begin
|
---|
1660 | if StationIndex = 0 then
|
---|
1661 | ComputeShapeDistanceStation(LineStations[LineStations.Count - 2].MapStation,
|
---|
1662 | UpdatedShape, Station.ShapeDistance[UpdatedShape] + 1);
|
---|
1663 | if StationIndex > 0 then
|
---|
1664 | ComputeShapeDistanceStation(LineStations[StationIndex - 1].MapStation,
|
---|
1665 | UpdatedShape, Station.ShapeDistance[UpdatedShape] + 1);
|
---|
1666 | end;
|
---|
1667 | if DirectionDown then begin
|
---|
1668 | if (StationIndex >= 0) and (StationIndex = LineStations.Count - 1) then
|
---|
1669 | ComputeShapeDistanceStation(LineStations[1].MapStation,
|
---|
1670 | UpdatedShape, Station.ShapeDistance[UpdatedShape] + 1);
|
---|
1671 | if (StationIndex >= 0) and (StationIndex < LineStations.Count - 1) then
|
---|
1672 | ComputeShapeDistanceStation(LineStations[StationIndex + 1].MapStation,
|
---|
1673 | UpdatedShape, Station.ShapeDistance[UpdatedShape] + 1);
|
---|
1674 | end;
|
---|
1675 | end;
|
---|
1676 | end;
|
---|
1677 | end;
|
---|
1678 | end;
|
---|
1679 | end;
|
---|
1680 |
|
---|
1681 | procedure TEngine.SetDarkMode(AValue: Boolean);
|
---|
1682 | begin
|
---|
1683 | if FDarkMode = AValue then Exit;
|
---|
1684 | FDarkMode := AValue;
|
---|
1685 | InitColors;
|
---|
1686 | if Assigned(FOnDarkModeChange) then FOnDarkModeChange(Self);
|
---|
1687 | end;
|
---|
1688 |
|
---|
1689 | procedure TEngine.InitColors;
|
---|
1690 | begin
|
---|
1691 | with Colors do
|
---|
1692 | if FDarkMode then begin
|
---|
1693 | Background := $2f3020;
|
---|
1694 | Text := clWhite;
|
---|
1695 | ShapeBackground := clBlack;
|
---|
1696 | MenuItemText := $cccccc;
|
---|
1697 | MenuItemBackground := $b75C01;
|
---|
1698 | MenuItemBackgroundSelected := $070C81;
|
---|
1699 | end else begin
|
---|
1700 | Background := $eff0e0;
|
---|
1701 | Text := clBlack;
|
---|
1702 | ShapeBackground := clWhite;
|
---|
1703 | MenuItemText := clWhite;
|
---|
1704 | MenuItemBackground := $e78C31;
|
---|
1705 | MenuItemBackgroundSelected := $f7bC61;
|
---|
1706 | end;
|
---|
1707 | end;
|
---|
1708 |
|
---|
1709 | procedure TEngine.SetState(AValue: TGameState);
|
---|
1710 | begin
|
---|
1711 | if FState = AValue then Exit;
|
---|
1712 | FState := AValue;
|
---|
1713 | UpdateInterface;
|
---|
1714 | end;
|
---|
1715 |
|
---|
1716 | procedure TEngine.TrainMovement;
|
---|
1717 | var
|
---|
1718 | I: Integer;
|
---|
1719 | CurrentStation: TLineStation;
|
---|
1720 | P: Integer;
|
---|
1721 | Passenger: TMetroPassenger;
|
---|
1722 | PosDelta: Integer;
|
---|
1723 | TargetStationIndex: Integer;
|
---|
1724 | PosChange: Double;
|
---|
1725 | TP: TTrackPoint;
|
---|
1726 | begin
|
---|
1727 | // Move trains
|
---|
1728 | for I := 0 to Trains.Count - 1 do
|
---|
1729 | with Trains[I] do begin
|
---|
1730 | if not Assigned(TargetStation) and Assigned(BaseTrackPoint) then begin
|
---|
1731 | if (Direction <> 1) and (Direction <> -1) then Direction := 1
|
---|
1732 | else Direction := -Direction;
|
---|
1733 | TP := BaseTrackPoint.GetUp;
|
---|
1734 | if Assigned(TP) then TargetStation := TP.LineStation
|
---|
1735 | else begin
|
---|
1736 | TP := BaseTrackPoint.GetDown;
|
---|
1737 | if Assigned(TP) then TargetStation := TP.LineStation;
|
---|
1738 | end;
|
---|
1739 | end;
|
---|
1740 | if Assigned(Line) then begin
|
---|
1741 | if InStation then begin
|
---|
1742 | if (Time - StationStopTime) > OneHour then begin
|
---|
1743 | CurrentStation := TargetStation;
|
---|
1744 |
|
---|
1745 | // Choose next target station
|
---|
1746 | TargetStationIndex := Line.LineStations.IndexOf(TargetStation) + Direction;
|
---|
1747 | if TargetStationIndex < 0 then begin
|
---|
1748 | if Line.IsCircular then begin
|
---|
1749 | TargetStationIndex := Line.LineStations.Count - 2;
|
---|
1750 | BaseTrackPoint := Line.LineStations.Last.TrackPoint;
|
---|
1751 | RelPos := 0;
|
---|
1752 | end else begin
|
---|
1753 | TargetStationIndex := 1;
|
---|
1754 | Direction := -Direction;
|
---|
1755 | end;
|
---|
1756 | end else
|
---|
1757 | if TargetStationIndex >= Line.LineStations.Count then begin
|
---|
1758 | if Line.IsCircular then begin
|
---|
1759 | TargetStationIndex := 1;
|
---|
1760 | BaseTrackPoint := Line.LineStations.First.TrackPoint;
|
---|
1761 | RelPos := 0;
|
---|
1762 | end else begin
|
---|
1763 | TargetStationIndex := Line.LineStations.Count - 2;
|
---|
1764 | Direction := -Direction;
|
---|
1765 | end;
|
---|
1766 | end;
|
---|
1767 | TargetStation := Line.LineStations[TargetStationIndex];
|
---|
1768 |
|
---|
1769 | // Unload passengers in target station
|
---|
1770 | if Assigned(CurrentStation) then
|
---|
1771 | for P := Passengers.Count - 1 downto 0 do begin
|
---|
1772 | if Passengers[P].Shape = CurrentStation.MapStation.Shape then begin
|
---|
1773 | Passenger := Passengers[P];
|
---|
1774 | Passengers.Delete(P);
|
---|
1775 | Self.Passengers.Remove(Passenger);
|
---|
1776 | Inc(ServedPassengerCount);
|
---|
1777 | end;
|
---|
1778 | end;
|
---|
1779 | // Unload passengers to change line
|
---|
1780 | if Assigned(CurrentStation) then
|
---|
1781 | for P := Passengers.Count - 1 downto 0 do begin
|
---|
1782 | if not CurrentStation.MapStation.IsBestStationForShape(Passengers[P].Shape,
|
---|
1783 | TargetStation, CurrentStation) then begin
|
---|
1784 | Passenger := Passengers[P];
|
---|
1785 | Passengers.Delete(P);
|
---|
1786 | CurrentStation.MapStation.Passengers.Add(Passenger);
|
---|
1787 | Passenger.Station := CurrentStation.MapStation;
|
---|
1788 | end;
|
---|
1789 | end;
|
---|
1790 |
|
---|
1791 | // Load new passengers
|
---|
1792 | if Assigned(CurrentStation) and not Assigned(CurrentStation.MapStation) then
|
---|
1793 | raise Exception.Create('Station have to have MapStation');
|
---|
1794 | if Assigned(CurrentStation) then
|
---|
1795 | for P := CurrentStation.MapStation.Passengers.Count - 1 downto 0 do begin
|
---|
1796 | if (Passengers.Count < TrainPassengerCount) then begin
|
---|
1797 | Passenger := CurrentStation.MapStation.Passengers[P];
|
---|
1798 | if CurrentStation.MapStation.IsBestStationForShape(Passenger.Shape,
|
---|
1799 | TargetStation, CurrentStation) then begin
|
---|
1800 | Passenger.Station := nil;
|
---|
1801 | CurrentStation.MapStation.Passengers.Delete(P);
|
---|
1802 | Passengers.Add(Passenger);
|
---|
1803 | Passenger.Train := Trains[I];
|
---|
1804 | end;
|
---|
1805 | end else Break; // No more space
|
---|
1806 | end;
|
---|
1807 |
|
---|
1808 | LastPosDelta := Abs(GetTargetStationDistance);
|
---|
1809 | InStation := False;
|
---|
1810 | LastTrainMoveTime := Time;
|
---|
1811 | end;
|
---|
1812 | end else begin
|
---|
1813 | PosChange := Direction + Trunc(Direction * TrainSpeed * (Time - LastTrainMoveTime));
|
---|
1814 | RelPos := RelPos + PosChange;
|
---|
1815 | LastTrainMoveTime := Time;
|
---|
1816 | Redraw;
|
---|
1817 | if Assigned(BaseTrackPoint) then
|
---|
1818 | while (Direction = -1) and (RelPos < 0) do begin
|
---|
1819 | if BaseTrackPoint <> Line.LineStations.First.TrackPoint then begin
|
---|
1820 | BaseTrackPoint := BaseTrackPoint.GetNeighDown;
|
---|
1821 | if Assigned(BaseTrackPoint) then
|
---|
1822 | RelPos := RelPos + BaseTrackPoint.GetDistance
|
---|
1823 | else begin
|
---|
1824 | BaseTrackPoint := Line.LineStations.First.TrackPoint;
|
---|
1825 | RelPos := 0;
|
---|
1826 | end;
|
---|
1827 | end else
|
---|
1828 | if Line.IsCircular then begin
|
---|
1829 | BaseTrackPoint := Line.LineStations.Last.TrackPoint;
|
---|
1830 | RelPos := RelPos + BaseTrackPoint.GetDistance;
|
---|
1831 | end else begin
|
---|
1832 | RelPos := 0;
|
---|
1833 | Break;
|
---|
1834 | end;
|
---|
1835 | end;
|
---|
1836 | if Assigned(BaseTrackPoint) then
|
---|
1837 | while (Direction = 1) and (RelPos > BaseTrackPoint.GetDistance) do begin
|
---|
1838 | if BaseTrackPoint <> Line.LineStations.Last.TrackPoint then begin
|
---|
1839 | RelPos := RelPos - BaseTrackPoint.GetDistance;
|
---|
1840 | BaseTrackPoint := BaseTrackPoint.GetNeighUp;
|
---|
1841 | if not Assigned(BaseTrackPoint) then begin
|
---|
1842 | BaseTrackPoint := Line.LineStations.Last.TrackPoint;
|
---|
1843 | RelPos := 0;
|
---|
1844 | end;
|
---|
1845 | end else
|
---|
1846 | if Line.IsCircular then begin
|
---|
1847 | RelPos := RelPos - BaseTrackPoint.GetDistance;
|
---|
1848 | BaseTrackPoint := Line.LineStations.First.TrackPoint;
|
---|
1849 | end else begin
|
---|
1850 | RelPos := BaseTrackPoint.GetDistance;
|
---|
1851 | Break;
|
---|
1852 | end;
|
---|
1853 | end;
|
---|
1854 | PosDelta := Abs(GetTargetStationDistance);
|
---|
1855 | if PosDelta >= LastPosDelta then begin
|
---|
1856 | // We are getting far from station, stop at station
|
---|
1857 | BaseTrackPoint := TargetStation.TrackPoint;
|
---|
1858 | RelPos := 0;
|
---|
1859 | InStation := True;
|
---|
1860 | StationStopTime := Time;
|
---|
1861 | Redraw;
|
---|
1862 | end;
|
---|
1863 | LastPosDelta := PosDelta;
|
---|
1864 | end;
|
---|
1865 | end;
|
---|
1866 | end;
|
---|
1867 | end;
|
---|
1868 |
|
---|
1869 | function TEngine.GetUnusedLine: TMetroLine;
|
---|
1870 | var
|
---|
1871 | I: Integer;
|
---|
1872 | begin
|
---|
1873 | I := 0;
|
---|
1874 | while (I < Lines.Count) and (Lines[I].Track.Points.Count > 0) do Inc(I);
|
---|
1875 | if I < Lines.Count then Result := Lines[I]
|
---|
1876 | else Result := nil;
|
---|
1877 | end;
|
---|
1878 |
|
---|
1879 | procedure TEngine.ShiftTrackPoints;
|
---|
1880 | var
|
---|
1881 | I: Integer;
|
---|
1882 | J: Integer;
|
---|
1883 | //Link1, Link2: TPoint;
|
---|
1884 | NewPoint: TPoint;
|
---|
1885 | MetroLine: TMetroLine;
|
---|
1886 | TrackPoint: TTrackPoint;
|
---|
1887 | MapStation: TMapStation;
|
---|
1888 | begin
|
---|
1889 | // NewGame all trackpoints position shift
|
---|
1890 | for MetroLine in Lines do
|
---|
1891 | for TrackPoint in MetroLine.Track.Points do
|
---|
1892 | TrackPoint.Position := TrackPoint.PositionDesigned;
|
---|
1893 |
|
---|
1894 | // Calculate new position shifts
|
---|
1895 | for MapStation in Stations do
|
---|
1896 | MapStation.ShiftTrackPoints;
|
---|
1897 |
|
---|
1898 | // Compute track points from track shift
|
---|
1899 | for MetroLine in Lines do
|
---|
1900 | with MetroLine do begin
|
---|
1901 | // Update start
|
---|
1902 | if Track.Points.Count > 1 then begin
|
---|
1903 | Track.Points[0].Position := Track.Points[0].PositionDesigned +
|
---|
1904 | Track.Points[0].LinkUp.Shift;
|
---|
1905 | end;
|
---|
1906 |
|
---|
1907 | for I := 1 to Track.Points.Count - 1 do
|
---|
1908 | with Track.Points[I] do
|
---|
1909 | if Assigned(Track.Points[I].LinkDown) and Assigned(Track.Points[I].LinkUp) then begin
|
---|
1910 | {
|
---|
1911 | Link1 := (Track.Points[I].PositionDesigned + Track.Points[I].LinkDown.Shift) -
|
---|
1912 | (Track.Points[I - 1].PositionDesigned + Track.Points[I].LinkDown.Shift);
|
---|
1913 | if (I + 1) < Track.Points.Count then
|
---|
1914 | Link2 := (Track.Points[I + 1].PositionDesigned + Track.Points[I].LinkUp.Shift) -
|
---|
1915 | (Track.Points[I].PositionDesigned + Track.Points[I].LinkUp.Shift)
|
---|
1916 | else Link2 := Link1;
|
---|
1917 |
|
---|
1918 | if ArcTanPoint(Link1) = ArcTanPoint(Link2) then begin
|
---|
1919 | // Parallel lines
|
---|
1920 | NewPoint := Track.Points[I].PositionDesigned + Track.Points[I].LinkDown.Shift;
|
---|
1921 | Track.Points[I].Position := NewPoint;
|
---|
1922 | end else begin}
|
---|
1923 | // Intersected lines
|
---|
1924 | if LineIntersect(Track.Points[I - 1].PositionDesigned + Track.Points[I].LinkDown.Shift,
|
---|
1925 | Track.Points[I].PositionDesigned + Track.Points[I].LinkDown.Shift,
|
---|
1926 | Track.Points[I].PositionDesigned + Track.Points[I].LinkUp.Shift,
|
---|
1927 | Track.Points[I + 1].PositionDesigned + Track.Points[I].LinkUp.Shift, NewPoint) then
|
---|
1928 | Track.Points[I].Position := NewPoint
|
---|
1929 | else begin
|
---|
1930 | // Parallel lines
|
---|
1931 | NewPoint := Track.Points[I].PositionDesigned + Track.Points[I].LinkDown.Shift;
|
---|
1932 | Track.Points[I].Position := NewPoint;
|
---|
1933 | end;
|
---|
1934 | // end;
|
---|
1935 | end;
|
---|
1936 |
|
---|
1937 | // Update ending
|
---|
1938 | if Track.Points.Count > 1 then begin
|
---|
1939 | Track.Points[Track.Points.Count - 1].Position := Track.Points[Track.Points.Count - 1].PositionDesigned -
|
---|
1940 | Track.Points[Track.Points.Count - 1].LinkDown.Shift;
|
---|
1941 | end;
|
---|
1942 | end;
|
---|
1943 |
|
---|
1944 | // Remove all temporal links
|
---|
1945 | for MetroLine in Lines do
|
---|
1946 | with MetroLine do begin
|
---|
1947 | for J := 0 to Track.Points.Count - 1 do
|
---|
1948 | if Assigned(Track.Points[J].LinkUp) then begin
|
---|
1949 | Track.Points[J].LinkUp.Free;
|
---|
1950 | Track.Points[J].LinkUp := nil;
|
---|
1951 | Track.Points[J + 1].LinkDown := nil;
|
---|
1952 | end;
|
---|
1953 | end;
|
---|
1954 | end;
|
---|
1955 |
|
---|
1956 | procedure TEngine.MenuItemExit(Sender: TObject);
|
---|
1957 | begin
|
---|
1958 | FormMain.Close;
|
---|
1959 | end;
|
---|
1960 |
|
---|
1961 | procedure TEngine.MenuItemPlay(Sender: TObject);
|
---|
1962 | begin
|
---|
1963 | NewGame;
|
---|
1964 | end;
|
---|
1965 |
|
---|
1966 | procedure TEngine.MenuItemOptions(Sender: TObject);
|
---|
1967 | begin
|
---|
1968 | Menu := MenuOptions;
|
---|
1969 | Redraw;
|
---|
1970 | end;
|
---|
1971 |
|
---|
1972 | procedure TEngine.MenuItemBack(Sender: TObject);
|
---|
1973 | begin
|
---|
1974 | if Assigned(Menu.Parent) then begin
|
---|
1975 | Menu := Menu.Parent;
|
---|
1976 | Redraw;
|
---|
1977 | end else MenuItemExit(nil);
|
---|
1978 | end;
|
---|
1979 |
|
---|
1980 | procedure TEngine.ButtonPlay(Sender: TObject);
|
---|
1981 | begin
|
---|
1982 | TimePerSecond := TimePerSecondNormal;
|
---|
1983 | if State = gsPaused then State := gsRunning;
|
---|
1984 | UpdateInterface;
|
---|
1985 | end;
|
---|
1986 |
|
---|
1987 | procedure TEngine.ButtonPause(Sender: TObject);
|
---|
1988 | begin
|
---|
1989 | if State = gsRunning then State := gsPaused;
|
---|
1990 | UpdateInterface;
|
---|
1991 | end;
|
---|
1992 |
|
---|
1993 | procedure TEngine.ButtonFastForward(Sender: TObject);
|
---|
1994 | begin
|
---|
1995 | TimePerSecond := TimePerSecondFast;
|
---|
1996 | if State = gsPaused then State := gsRunning;
|
---|
1997 | UpdateInterface;
|
---|
1998 | end;
|
---|
1999 |
|
---|
2000 | procedure TEngine.MenuItemGameContinue(Sender: TObject);
|
---|
2001 | begin
|
---|
2002 | State := LastState;
|
---|
2003 | end;
|
---|
2004 |
|
---|
2005 | procedure TEngine.MenuItemGameExit(Sender: TObject);
|
---|
2006 | begin
|
---|
2007 | State := gsMenu;
|
---|
2008 | Clear;
|
---|
2009 | Menu := MenuMain;
|
---|
2010 | Redraw;
|
---|
2011 | end;
|
---|
2012 |
|
---|
2013 | procedure TEngine.MenuItemGameRestart(Sender: TObject);
|
---|
2014 | begin
|
---|
2015 | NewGame;
|
---|
2016 | end;
|
---|
2017 |
|
---|
2018 | procedure TEngine.DarkModeChanged(Sender: TObject);
|
---|
2019 | begin
|
---|
2020 | DarkMode := TMenuItemCheckBox(Sender).Checked;
|
---|
2021 | InitMenus;
|
---|
2022 | end;
|
---|
2023 |
|
---|
2024 | procedure TEngine.LanguageChanged(Sender: TObject);
|
---|
2025 | var
|
---|
2026 | NewLanguage: TLanguage;
|
---|
2027 | begin
|
---|
2028 | NewLanguage := TLanguage(TMenuItemComboBox(Sender).States.Objects[TMenuItemComboBox(Sender).Index]);
|
---|
2029 | if FormMain.Translator1.Language <> NewLanguage then begin
|
---|
2030 | FormMain.Translator1.Language := NewLanguage;
|
---|
2031 | FormMain.Translator1.Translate;
|
---|
2032 | InitMenus;
|
---|
2033 | end;
|
---|
2034 | end;
|
---|
2035 |
|
---|
2036 | procedure TEngine.FullScreenChanged(Sender: TObject);
|
---|
2037 | begin
|
---|
2038 | FormMain.FullScreen := TMenuItemCheckBox(Sender).Checked;
|
---|
2039 | FormMain.PersistentForm1.SetFullScreen(FormMain.FullScreen);
|
---|
2040 | end;
|
---|
2041 |
|
---|
2042 | procedure TEngine.UpdateInterface;
|
---|
2043 | begin
|
---|
2044 | ImagePlay.Enabled := not ((State = gsRunning) and (TimePerSecond = TimePerSecondNormal));
|
---|
2045 | ImageFastForward.Enabled := not ((State = gsRunning) and (TimePerSecond = TimePerSecondFast));
|
---|
2046 | ImagePause.Enabled := FState = gsRunning; //not (State = gsPaused);
|
---|
2047 | end;
|
---|
2048 |
|
---|
2049 | procedure TEngine.InitMenus;
|
---|
2050 | begin
|
---|
2051 | with MenuMain, Items do begin
|
---|
2052 | Clear;
|
---|
2053 | with AddButton(SBigMetro, nil) do begin
|
---|
2054 | Enabled := False;
|
---|
2055 | FontSize := 60;
|
---|
2056 | FontColor := Colors.Text;
|
---|
2057 | BackgroundColor := clNone;
|
---|
2058 | BackgroundSelectedColor := clNone;
|
---|
2059 | end;
|
---|
2060 | with AddButton(SPlay, MenuItemPlay) do begin
|
---|
2061 | FontSize := 40;
|
---|
2062 | FontColor := Colors.MenuItemText;
|
---|
2063 | BackgroundColor := Colors.MenuItemBackground;
|
---|
2064 | BackgroundSelectedColor := Colors.MenuItemBackgroundSelected;
|
---|
2065 | end;
|
---|
2066 | with AddButton(SOptions, MenuItemOptions) do begin
|
---|
2067 | FontSize := 40;
|
---|
2068 | FontColor := Colors.MenuItemText;
|
---|
2069 | BackgroundColor := Colors.MenuItemBackground;
|
---|
2070 | BackgroundSelectedColor := Colors.MenuItemBackgroundSelected;
|
---|
2071 | end;
|
---|
2072 | with AddButton(SExit, MenuItemExit) do begin
|
---|
2073 | FontSize := 40;
|
---|
2074 | FontColor := Colors.MenuItemText;
|
---|
2075 | BackgroundColor := Colors.MenuItemBackground;
|
---|
2076 | BackgroundSelectedColor := Colors.MenuItemBackgroundSelected;
|
---|
2077 | end;
|
---|
2078 | OnExit := MenuItemExit;
|
---|
2079 | end;
|
---|
2080 |
|
---|
2081 | MenuOptions.Parent := MenuMain;
|
---|
2082 | with MenuOptions, Items do begin
|
---|
2083 | Clear;
|
---|
2084 | with AddComboBox(SLanguage, [], LanguageChanged) do begin
|
---|
2085 | FontSize := 40;
|
---|
2086 | FontColor := Colors.MenuItemText;
|
---|
2087 | BackgroundColor := Colors.MenuItemBackground;
|
---|
2088 | BackgroundSelectedColor := Colors.MenuItemBackgroundSelected;
|
---|
2089 | FormMain.Translator1.LanguageListToStrings(States);
|
---|
2090 | Index := States.IndexOfObject(FormMain.Translator1.Language);
|
---|
2091 | if Index = -1 then Index := 0;
|
---|
2092 | end;
|
---|
2093 | with AddCheckBox(SDarkMode, DarkModeChanged) do begin
|
---|
2094 | FontSize := 40;
|
---|
2095 | FontColor := Colors.MenuItemText;
|
---|
2096 | BackgroundColor := Colors.MenuItemBackground;
|
---|
2097 | BackgroundSelectedColor := Colors.MenuItemBackgroundSelected;
|
---|
2098 | Checked := DarkMode;
|
---|
2099 | end;
|
---|
2100 | with AddCheckBox(SFullScreen, FullScreenChanged) do begin
|
---|
2101 | FontSize := 40;
|
---|
2102 | FontColor := Colors.MenuItemText;
|
---|
2103 | BackgroundColor := Colors.MenuItemBackground;
|
---|
2104 | BackgroundSelectedColor := Colors.MenuItemBackgroundSelected;
|
---|
2105 | Checked := FormMain.FullScreen;
|
---|
2106 | end;
|
---|
2107 | with AddButton(SBack, MenuItemBack) do begin
|
---|
2108 | FontSize := 40;
|
---|
2109 | FontColor := Colors.MenuItemText;
|
---|
2110 | BackgroundColor := Colors.MenuItemBackground;
|
---|
2111 | BackgroundSelectedColor := Colors.MenuItemBackgroundSelected;
|
---|
2112 | end;
|
---|
2113 | OnExit := MenuItemBack;
|
---|
2114 | end;
|
---|
2115 |
|
---|
2116 | with MenuGame, Items do begin
|
---|
2117 | Clear;
|
---|
2118 | with AddButton(SContinue, MenuItemGameContinue) do begin
|
---|
2119 | FontSize := 40;
|
---|
2120 | FontColor := Colors.MenuItemText;
|
---|
2121 | BackgroundColor := Colors.MenuItemBackground;
|
---|
2122 | BackgroundSelectedColor := Colors.MenuItemBackgroundSelected;
|
---|
2123 | end;
|
---|
2124 | with AddButton(SRestart, MenuItemGameRestart) do begin
|
---|
2125 | FontSize := 40;
|
---|
2126 | FontColor := Colors.MenuItemText;
|
---|
2127 | BackgroundColor := Colors.MenuItemBackground;
|
---|
2128 | BackgroundSelectedColor := Colors.MenuItemBackgroundSelected;
|
---|
2129 | end;
|
---|
2130 | with AddButton(SExit, MenuItemGameExit) do begin
|
---|
2131 | FontSize := 40;
|
---|
2132 | FontColor := Colors.MenuItemText;
|
---|
2133 | BackgroundColor := Colors.MenuItemBackground;
|
---|
2134 | BackgroundSelectedColor := Colors.MenuItemBackgroundSelected;
|
---|
2135 | end;
|
---|
2136 | OnExit := MenuItemGameContinue;
|
---|
2137 | end;
|
---|
2138 | end;
|
---|
2139 |
|
---|
2140 | procedure TEngine.ButtonBackClick(Sender: TObject);
|
---|
2141 | begin
|
---|
2142 | Menu := MenuGame;
|
---|
2143 | LastState := State;
|
---|
2144 | State := gsMenu;
|
---|
2145 | Redraw;
|
---|
2146 | end;
|
---|
2147 |
|
---|
2148 | procedure TEngine.DrawClock(Canvas: TCanvas; CanvasSize: TPoint);
|
---|
2149 | var
|
---|
2150 | ClockCenter: TPoint;
|
---|
2151 | Angle: Double;
|
---|
2152 | Text: string;
|
---|
2153 | I: Integer;
|
---|
2154 | Y: Integer;
|
---|
2155 | const
|
---|
2156 | ClockSize = 20;
|
---|
2157 | begin
|
---|
2158 | Canvas.Pen.Style := psSolid;
|
---|
2159 | Canvas.Pen.Color := Colors.Text;
|
---|
2160 | Canvas.Pen.Width := 2;
|
---|
2161 | ClockCenter := Point(CanvasSize.X - 30, 40);
|
---|
2162 | Angle := Time / (12 * OneHour) * 2 * Pi - Pi / 2;
|
---|
2163 | Canvas.EllipseC(ClockCenter.X, ClockCenter.Y, ClockSize, ClockSize);
|
---|
2164 | Canvas.Line(ClockCenter, Point(ClockCenter.X + Round(Cos(Angle) * ClockSize * 0.8),
|
---|
2165 | ClockCenter.Y + Round(Sin(Angle) * ClockSize * 0.8)));
|
---|
2166 | for I := 0 to 12 do begin
|
---|
2167 | Angle := I / 12 * 2 * Pi;
|
---|
2168 | Canvas.Line(ClockCenter.X + Round(Cos(Angle) * ClockSize * 0.8),
|
---|
2169 | ClockCenter.Y + Round(Sin(Angle) * ClockSize * 0.8),
|
---|
2170 | ClockCenter.X + Round(Cos(Angle) * ClockSize * 0.9),
|
---|
2171 | ClockCenter.Y + Round(Sin(Angle) * ClockSize * 0.9));
|
---|
2172 | end;
|
---|
2173 | Y := ClockCenter.Y + ScaleY(5, 96);
|
---|
2174 |
|
---|
2175 | Canvas.Font.Color := Colors.Text;
|
---|
2176 | Text := FormatDateTime('ddd', Time + 2);
|
---|
2177 | Canvas.TextOut(ClockCenter.X - ClockSize - Canvas.TextWidth(Text) - 10,
|
---|
2178 | Y - Canvas.TextHeight(Text) div 2, Text);
|
---|
2179 | Y := Y + Canvas.TextHeight(Text) + ScaleY(5, 96);
|
---|
2180 |
|
---|
2181 | Text := SDay + ' ' + IntToStr(Trunc(Time));
|
---|
2182 | Canvas.TextOut(ClockCenter.X - ClockSize - Canvas.TextWidth(Text) - 10,
|
---|
2183 | Y - Canvas.TextHeight(Text) div 2, Text);
|
---|
2184 | Y := Y + Canvas.TextHeight(Text) + ScaleY(5, 96);
|
---|
2185 |
|
---|
2186 | ImagePause.Canvas := Canvas;
|
---|
2187 | ImagePause.Bounds := Bounds(CanvasSize.X - ScaleX(20 + 10, 96), Y, ScaleX(20, 96), ScaleY(20, 96));
|
---|
2188 | ImagePause.Paint;
|
---|
2189 | Y := Y + ImagePause.Bounds.Height + ScaleY(5, 96);
|
---|
2190 |
|
---|
2191 | ImagePlay.Canvas := Canvas;
|
---|
2192 | ImagePlay.Bounds := Bounds(CanvasSize.X - ScaleX(20 + 10, 96) , Y, ScaleX(20, 96), ScaleY(20, 96));
|
---|
2193 | ImagePlay.Paint;
|
---|
2194 | Y := Y + ImagePlay.Bounds.Height + ScaleY(5, 96);
|
---|
2195 |
|
---|
2196 | ImageFastForward.Canvas := Canvas;
|
---|
2197 | ImageFastForward.Bounds := Bounds(CanvasSize.X - ScaleX(20 + 10, 96) , Y, ScaleX(20, 96), ScaleY(20, 96));
|
---|
2198 | ImageFastForward.Paint;
|
---|
2199 | Y := Y + ImageFastForward.Bounds.Height + ScaleY(5, 96);
|
---|
2200 | end;
|
---|
2201 |
|
---|
2202 | procedure TEngine.DrawTrains(Canvas: TCanvas);
|
---|
2203 | var
|
---|
2204 | P: Integer;
|
---|
2205 | Pos: TPoint;
|
---|
2206 | Points: array of TPoint;
|
---|
2207 | Angle: Double;
|
---|
2208 | ShapePos: TPoint;
|
---|
2209 | Train: TMetroTrain;
|
---|
2210 | Passenger: TMetroPassenger;
|
---|
2211 | begin
|
---|
2212 | for Train in Trains do
|
---|
2213 | with Train do begin
|
---|
2214 | if Assigned(Line) then begin
|
---|
2215 | Canvas.Brush.Color := Line.Color;
|
---|
2216 | Canvas.Brush.Style := bsSolid;
|
---|
2217 | Canvas.Pen.Style := psClear;
|
---|
2218 | Pos := GetPosition;
|
---|
2219 | Angle := GetAngle;
|
---|
2220 |
|
---|
2221 | SetLength(Points, 4);
|
---|
2222 | Points[0] := RotatePoint(Pos, Point(Pos.X - TrainSize div 2, Pos.Y - TrainSize div 3), Angle);
|
---|
2223 | Points[1] := RotatePoint(Pos, Point(Pos.X + TrainSize div 2, Pos.Y - TrainSize div 3), Angle);
|
---|
2224 | Points[2] := RotatePoint(Pos, Point(Pos.X + TrainSize div 2, Pos.Y + TrainSize div 3), Angle);
|
---|
2225 | Points[3] := RotatePoint(Pos, Point(Pos.X - TrainSize div 2, Pos.Y + TrainSize div 3), Angle);
|
---|
2226 | Canvas.Polygon(Points);
|
---|
2227 | Canvas.Brush.Color := clWhite;
|
---|
2228 | P := 0;
|
---|
2229 | for Passenger in Passengers do
|
---|
2230 | with Passenger do begin
|
---|
2231 | ShapePos := Point(Pos.X - Trunc(TrainSize div 3 * 1) + (P mod 3) * TrainSize div 3,
|
---|
2232 | Pos.Y - Trunc(TrainSize div 6 * 1) + (P div 3) * TrainSize div 3);
|
---|
2233 | ShapePos := RotatePoint(Pos, ShapePos, Angle);
|
---|
2234 | DrawShape(Canvas, ShapePos, Shape, TrainSize div 3, Angle + Pi / 2);
|
---|
2235 | Inc(P);
|
---|
2236 | end;
|
---|
2237 | end;
|
---|
2238 | end;
|
---|
2239 | end;
|
---|
2240 |
|
---|
2241 | procedure TEngine.DrawGameOver(Canvas: TCanvas; CanvasSize: TPoint);
|
---|
2242 | var
|
---|
2243 | Y: Integer;
|
---|
2244 | Text: string;
|
---|
2245 | begin
|
---|
2246 | with Canvas do begin
|
---|
2247 | Canvas.Font.Color := Self.Colors.Text;
|
---|
2248 | Brush.Style := bsSolid;
|
---|
2249 | Brush.Color := Self.Colors.Background;
|
---|
2250 |
|
---|
2251 | Y := 100;
|
---|
2252 | Font.Size := 40;
|
---|
2253 | TextOut((CanvasSize.X - TextWidth(SGameOver)) div 2, Y, SGameOver);
|
---|
2254 | Y := Y + Round(TextHeight(SGameOver) * 1.1);
|
---|
2255 |
|
---|
2256 | Font.Size := 14;
|
---|
2257 | TextOut((CanvasSize.X - TextWidth(SGameOverReason)) div 2, Y, SGameOverReason);
|
---|
2258 | Y := Y + Round(TextHeight(SGameOverReason) * 1.1);
|
---|
2259 |
|
---|
2260 | Text := Format(SGameOverStatistic, [ServedPassengerCount, ServedDaysCount]);
|
---|
2261 | TextOut((CanvasSize.X - TextWidth(Text)) div 2, Y, Text);
|
---|
2262 | Y := Y + Round(TextHeight(SGameOverStatistic) * 1.1);
|
---|
2263 |
|
---|
2264 | Y := Y + 16;
|
---|
2265 |
|
---|
2266 | // Calculate new highest score
|
---|
2267 | Text := '';
|
---|
2268 | if (ServedPassengerCount > HighestServedPassengerCount) or
|
---|
2269 | (ServedDaysCount > HighestServedDaysCount) then begin
|
---|
2270 | Text := SNewHighScore + ' ';
|
---|
2271 | end;
|
---|
2272 | Text := Text + Format(SOldHighScore, [HighestServedPassengerCount,
|
---|
2273 | HighestServedDaysCount]);
|
---|
2274 | Canvas.TextOut((CanvasSize.X - TextWidth(Text)) div 2, Y, Text);
|
---|
2275 | Y := Y + Round(TextHeight(Text) * 1.1);
|
---|
2276 | if (ServedPassengerCount > HighestServedPassengerCount) then
|
---|
2277 | HighestServedPassengerCount := ServedPassengerCount;
|
---|
2278 | if (ServedDaysCount > HighestServedDaysCount) then
|
---|
2279 | HighestServedDaysCount := ServedDaysCount;
|
---|
2280 | end;
|
---|
2281 | end;
|
---|
2282 |
|
---|
2283 | procedure TEngine.DrawStationPassengerOverload(Canvas: TCanvas);
|
---|
2284 | var
|
---|
2285 | MapStation: TMapStation;
|
---|
2286 | Angle: Real;
|
---|
2287 | begin
|
---|
2288 | for MapStation in Stations do
|
---|
2289 | with MapStation do begin
|
---|
2290 | if OverloadDuration > 0 then begin
|
---|
2291 | Canvas.Brush.Color := clSilver;
|
---|
2292 | Canvas.Brush.Style := bsSolid;
|
---|
2293 | Canvas.Pen.Color := clSilver;
|
---|
2294 | Canvas.Pen.Style := psSolid;
|
---|
2295 | Angle := OverloadDuration / MaxPassengersOveloadTime * 2 * Pi;
|
---|
2296 | Canvas.Pie(Position.X - StationOverloadSize, Position.Y - StationOverloadSize,
|
---|
2297 | Position.X + StationOverloadSize, Position.Y + StationOverloadSize,
|
---|
2298 | Trunc(Position.X + StationOverloadSize * Cos(Angle)),
|
---|
2299 | Trunc(Position.Y + StationOverloadSize * Sin(Angle)), Position.X + StationOverloadSize, Position.Y);
|
---|
2300 | end;
|
---|
2301 | end;
|
---|
2302 | end;
|
---|
2303 |
|
---|
2304 | procedure TEngine.DrawLines(Canvas: TCanvas);
|
---|
2305 | var
|
---|
2306 | MetroLine: TMetroLine;
|
---|
2307 | S: Integer;
|
---|
2308 | begin
|
---|
2309 | for MetroLine in Lines do
|
---|
2310 | with MetroLine do begin
|
---|
2311 | Canvas.Pen.Color := Color;
|
---|
2312 | Canvas.Pen.Style := psSolid;
|
---|
2313 | Canvas.Pen.Width := MetroLineThickness;
|
---|
2314 | if Track.Points.Count > 0 then Canvas.MoveTo(Track.Points[0].Position);
|
---|
2315 | for S := 1 to Track.Points.Count - 1 do begin
|
---|
2316 | Canvas.LineTo(Track.Points[S].Position);
|
---|
2317 | { if (S = TrackPoints.Count - 1) then begin
|
---|
2318 | Canvas.Pen.EndCap := pecSquare;
|
---|
2319 | Angle := arctan2(((TrackPoints[S].Position.Y - TrackPoints[S - 1].Position.Y),
|
---|
2320 | (TrackPoints[S].Position.X - TrackPoints[S - 1].Position.X));
|
---|
2321 | EndPoint := Point(Round(TrackPoints[S].Position.X + EndStationLength * Cos(Angle)),
|
---|
2322 | Round(TrackPoints[S].Position.Y + EndStationLength * Sin(Angle)));
|
---|
2323 | Canvas.LineTo(EndPoint);
|
---|
2324 | Canvas.MoveTo(Point(Round(EndPoint.X + Cos(Angle + Pi / 2) * EndStationLength / 3),
|
---|
2325 | Round(EndPoint.Y + Sin(Angle + Pi / 2) * EndStationLength / 3)));
|
---|
2326 | Canvas.LineTo(Point(Round(EndPoint.X + Cos(Angle - Pi / 2) * EndStationLength / 3),
|
---|
2327 | Round(EndPoint.Y + Sin(Angle - Pi / 2) * EndStationLength / 3)));
|
---|
2328 | Canvas.Pen.EndCap := pecRound;
|
---|
2329 | end;}
|
---|
2330 | end;
|
---|
2331 | (* Canvas.Pen.Color := Color;
|
---|
2332 | Canvas.Pen.Style := psSolid;
|
---|
2333 | Canvas.Pen.Width := MetroLineThickness div 2;
|
---|
2334 | if Track.Points.Count > 0 then Canvas.MoveTo(Track.Points[0].PositionDesigned);
|
---|
2335 | for S := 1 to Track.Points.Count - 1 do begin
|
---|
2336 | Canvas.LineTo(Track.Points[S].PositionDesigned);
|
---|
2337 | { if (S = TrackPoints.Count - 1) then begin
|
---|
2338 | Canvas.Pen.EndCap := pecSquare;
|
---|
2339 | Angle := arctan2((TrackPoints[S].Position.Y - TrackPoints[S - 1].Position.Y),
|
---|
2340 | (TrackPoints[S].Position.X - TrackPoints[S - 1].Position.X));
|
---|
2341 | EndPoint := Point(Round(TrackPoints[S].Position.X + EndStationLength * Cos(Angle)),
|
---|
2342 | Round(TrackPoints[S].Position.Y + EndStationLength * Sin(Angle)));
|
---|
2343 | Canvas.LineTo(EndPoint);
|
---|
2344 | Canvas.MoveTo(Point(Round(EndPoint.X + Cos(Angle + Pi / 2) * EndStationLength / 3),
|
---|
2345 | Round(EndPoint.Y + Sin(Angle + Pi / 2) * EndStationLength / 3)));
|
---|
2346 | Canvas.LineTo(Point(Round(EndPoint.X + Cos(Angle - Pi / 2) * EndStationLength / 3),
|
---|
2347 | Round(EndPoint.Y + Sin(Angle - Pi / 2) * EndStationLength / 3)));
|
---|
2348 | Canvas.Pen.EndCap := pecRound;
|
---|
2349 | end;}
|
---|
2350 | end;
|
---|
2351 | {
|
---|
2352 | if (TrackPoints.Count > 1) then begin
|
---|
2353 | Canvas.Pen.EndCap := pecSquare;
|
---|
2354 | Angle := arctan2((TrackPoints[1].Position.Y - TrackPoints[0].Position.Y),
|
---|
2355 | (TrackPoints[1].Position.X - TrackPoints[0].Position.X));
|
---|
2356 | Canvas.MoveTo(TrackPoints[0].Position);
|
---|
2357 | EndPoint := Point(Round(TrackPoints[0].Position.X - EndStationLength * Cos(Angle)),
|
---|
2358 | Round(TrackPoints[0].Position.Y - EndStationLength * Sin(Angle)));
|
---|
2359 | Canvas.LineTo(EndPoint);
|
---|
2360 | Canvas.MoveTo(Point(Round(EndPoint.X - Cos(Angle + Pi / 2) * EndStationLength / 3),
|
---|
2361 | Round(EndPoint.Y - Sin(Angle + Pi / 2) * EndStationLength / 3)));
|
---|
2362 | Canvas.LineTo(Point(Round(EndPoint.X - Cos(Angle - Pi / 2) * EndStationLength / 3),
|
---|
2363 | Round(EndPoint.Y - Sin(Angle - Pi / 2) * EndStationLength / 3)));
|
---|
2364 | Canvas.Pen.EndCap := pecRound;
|
---|
2365 | end; }
|
---|
2366 | *)
|
---|
2367 | end;
|
---|
2368 |
|
---|
2369 | // Draw design time lines
|
---|
2370 | if Assigned(TrackStationDown) and Assigned(TrackStationDown.LineStation) then begin
|
---|
2371 | Canvas.Pen.Color := TrackStationDown.Track.Line.Color;
|
---|
2372 | Canvas.MoveTo(TrackStationDown.LineStation.TrackPoint.Position);
|
---|
2373 | DrawLine(Canvas, View.PointDestToSrc(LastMousePos));
|
---|
2374 | end;
|
---|
2375 | if Assigned(TrackStationUp) and Assigned(TrackStationUp.LineStation) then begin
|
---|
2376 | Canvas.Pen.Color := TrackStationUp.Track.Line.Color;
|
---|
2377 | Canvas.MoveTo(TrackStationUp.LineStation.TrackPoint.Position);
|
---|
2378 | DrawLine(Canvas, View.PointDestToSrc(LastMousePos));
|
---|
2379 | end;
|
---|
2380 | end;
|
---|
2381 |
|
---|
2382 | procedure TEngine.DrawStations(Canvas: TCanvas);
|
---|
2383 | var
|
---|
2384 | MapStation: TMapStation;
|
---|
2385 | Passenger: TMetroPassenger;
|
---|
2386 | PassengerPos: TPoint;
|
---|
2387 | Direction: Integer;
|
---|
2388 | begin
|
---|
2389 | Canvas.Pen.Width := 5;
|
---|
2390 | for MapStation in Stations do
|
---|
2391 | with MapStation do begin
|
---|
2392 | Canvas.Pen.Style := psSolid;
|
---|
2393 | if Assigned(SelectedLine) and (Lines.IndexOf(SelectedLine) <> -1) then begin
|
---|
2394 | Canvas.Brush.Style := bsClear;
|
---|
2395 | Canvas.Pen.Color := SelectedLine.Color;
|
---|
2396 | DrawShape(Canvas, Position, Shape, StationSize + Canvas.Pen.Width + 4, 0);
|
---|
2397 | end;
|
---|
2398 |
|
---|
2399 | Canvas.Brush.Color := Colors.ShapeBackground;
|
---|
2400 | Canvas.Brush.Style := bsSolid;
|
---|
2401 | Canvas.Pen.Color := Colors.Text;
|
---|
2402 | DrawShape(Canvas, Position, Shape, StationSize, 0);
|
---|
2403 |
|
---|
2404 | // Draw passengers
|
---|
2405 | Canvas.Pen.Style := psClear;
|
---|
2406 | Canvas.Brush.Color := Colors.Text;
|
---|
2407 | PassengerPos := Point(0, 0);
|
---|
2408 | Direction := 1;
|
---|
2409 | for Passenger in Passengers do
|
---|
2410 | with Passenger do begin
|
---|
2411 | DrawShape(Canvas, Point(Position.X + StationSize + PassengerPos.X,
|
---|
2412 | Position.Y - StationSize div 2 + PassengerPos.Y),
|
---|
2413 | Shape, PassengerSize, 0);
|
---|
2414 | PassengerPos := Point(PassengerPos.X + Direction * (PassengerSize + 2), PassengerPos.Y);
|
---|
2415 | if PassengerPos.X >= (PassengerSize + 2) * VisiblePassengersPerLine then begin
|
---|
2416 | Direction := -Direction;
|
---|
2417 | PassengerPos.X := PassengerPos.X - (PassengerSize + 2);
|
---|
2418 | PassengerPos.Y := PassengerPos.Y + (PassengerSize + 2);
|
---|
2419 | end;
|
---|
2420 | if PassengerPos.X < 0 then begin
|
---|
2421 | Direction := -Direction;
|
---|
2422 | PassengerPos.X := 0;
|
---|
2423 | PassengerPos.Y := PassengerPos.Y + (PassengerSize + 2);
|
---|
2424 | end;
|
---|
2425 | end;
|
---|
2426 |
|
---|
2427 | { if ShowDistances then begin
|
---|
2428 | Canvas.Brush.Style := bsClear;
|
---|
2429 | Text := '';
|
---|
2430 | for P := 0 to 5 do
|
---|
2431 | Text := Text + IntToStr(ShapeDistance[TStationShape(P)]) + ',';
|
---|
2432 | Canvas.TextOut(Position.X + StationSize div 2, Position.Y + StationSize div 2, Text);
|
---|
2433 | end;
|
---|
2434 | }
|
---|
2435 | end;
|
---|
2436 | end;
|
---|
2437 |
|
---|
2438 | procedure TEngine.DrawGameControls(Canvas: TCanvas; CanvasSize: TPoint);
|
---|
2439 | var
|
---|
2440 | I: Integer;
|
---|
2441 | Text: string;
|
---|
2442 | Radius: Integer;
|
---|
2443 | Angle: Real;
|
---|
2444 | Pos: TPoint;
|
---|
2445 | begin
|
---|
2446 | // Line selection
|
---|
2447 | Canvas.Pen.Width := 4;
|
---|
2448 | for I := 0 to High(LineColors) do begin
|
---|
2449 | if Assigned(Lines.SearchByColor(LineColors[I])) then begin
|
---|
2450 | Canvas.Brush.Color := LineColors[I];
|
---|
2451 | Radius := 15;
|
---|
2452 | end else begin
|
---|
2453 | Canvas.Brush.Color := clSilver;
|
---|
2454 | Radius := 5;
|
---|
2455 | end;
|
---|
2456 | Canvas.Pen.Color := Colors.Text;
|
---|
2457 | if Assigned(SelectedLine) and (SelectedLine.Color = LineColors[I]) then begin
|
---|
2458 | Canvas.Pen.Style := psSolid;
|
---|
2459 | end else begin
|
---|
2460 | Canvas.Pen.Style := psClear;
|
---|
2461 | end;
|
---|
2462 |
|
---|
2463 | Canvas.EllipseC(CanvasSize.X div 2 - Length(LineColors) div 2 * LineColorsDist + I * LineColorsDist,
|
---|
2464 | CanvasSize.Y - LineColorsDist, Radius, Radius);
|
---|
2465 | end;
|
---|
2466 |
|
---|
2467 | // Draw unused trains
|
---|
2468 | Text := IntToStr(Trains.GetUnusedCount);
|
---|
2469 | Canvas.Draw(CanvasSize.X div 2 - Length(LineColors) div 2 * LineColorsDist - 100,
|
---|
2470 | CanvasSize.Y - LineColorsDist - ImageLocomotive.Bitmap.Height div 2, ImageLocomotive.Bitmap);
|
---|
2471 | Canvas.Brush.Style := bsClear;
|
---|
2472 | Canvas.Font.Size := 14;
|
---|
2473 | Canvas.Font.Color := Colors.Text;
|
---|
2474 | Canvas.TextOut(CanvasSize.X div 2 - Length(LineColors) div 2 * LineColorsDist - 50 - Canvas.TextWidth(Text),
|
---|
2475 | CanvasSize.Y - LineColorsDist - Canvas.TextHeight(Text) div 2, Text);
|
---|
2476 |
|
---|
2477 | // Status interface
|
---|
2478 | Text := IntToStr(ServedPassengerCount);
|
---|
2479 | Canvas.Draw(CanvasSize.X - 50, CanvasSize.Y - 60, ImagePassenger.Bitmap);
|
---|
2480 | Canvas.Brush.Style := bsClear;
|
---|
2481 | Canvas.Font.Size := 14;
|
---|
2482 | Canvas.Font.Color := Colors.Text;
|
---|
2483 | Canvas.TextOut(CanvasSize.X - 70 - Canvas.TextWidth(Text),
|
---|
2484 | CanvasSize.Y - 55, Text);
|
---|
2485 |
|
---|
2486 | DrawClock(Canvas, CanvasSize);
|
---|
2487 |
|
---|
2488 | // Back button
|
---|
2489 | Canvas.Font.Size := 40;
|
---|
2490 | Canvas.Font.Color := Colors.Text;
|
---|
2491 | ButtonBack.Canvas := Canvas;
|
---|
2492 | ButtonBack.Bounds.Left := 10;
|
---|
2493 | ButtonBack.Bounds.Top := 10;
|
---|
2494 | ButtonBack.Paint;
|
---|
2495 |
|
---|
2496 | // Show train grabbed by mouse
|
---|
2497 | if Assigned(SelectedTrain) then begin
|
---|
2498 | Canvas.Brush.Color := Colors.Text; //SelectedTrain.Line.Color;
|
---|
2499 | Canvas.Brush.Style := bsSolid;
|
---|
2500 | Canvas.Pen.Style := psClear;
|
---|
2501 | Pos := LastMousePos;
|
---|
2502 | Angle := 0;
|
---|
2503 |
|
---|
2504 | Canvas.Polygon([
|
---|
2505 | RotatePoint(Pos, Point(Pos.X - TrainSize div 2, Pos.Y - TrainSize div 3), Angle),
|
---|
2506 | RotatePoint(Pos, Point(Pos.X + TrainSize div 2, Pos.Y - TrainSize div 3), Angle),
|
---|
2507 | RotatePoint(Pos, Point(Pos.X + TrainSize div 2, Pos.Y + TrainSize div 3), Angle),
|
---|
2508 | RotatePoint(Pos, Point(Pos.X - TrainSize div 2, Pos.Y + TrainSize div 3), Angle)
|
---|
2509 | ]);
|
---|
2510 | end;
|
---|
2511 | end;
|
---|
2512 |
|
---|
2513 | procedure TEngine.Tick;
|
---|
2514 | var
|
---|
2515 | Passenger: TMetroPassenger;
|
---|
2516 | MapStation: TMapStation;
|
---|
2517 | begin
|
---|
2518 | if State = gsRunning then begin
|
---|
2519 | FTime := FTime + (Now - LastTickTime) / OneSecond * TimePerSecond;
|
---|
2520 | Redraw; // Redraw on every tick because engine time is changed so clock should be redrawn
|
---|
2521 |
|
---|
2522 | // Add new trains
|
---|
2523 | if (Time - LastNewWeekTime) > NewTrainPeriod then begin
|
---|
2524 | LastNewWeekTime := Time;
|
---|
2525 | Trains.AddNew;
|
---|
2526 | // TODO: Show notification screen with confirmation
|
---|
2527 | Redraw;
|
---|
2528 | end;
|
---|
2529 |
|
---|
2530 | // Add new shape
|
---|
2531 | if (Time - LastNewShapeTime) > NewShapePeriod then begin
|
---|
2532 | LastNewShapeTime := Time;
|
---|
2533 | if ShapeCount <= Integer(High(TStationShape)) then Inc(ShapeCount);
|
---|
2534 | Redraw;
|
---|
2535 | end;
|
---|
2536 |
|
---|
2537 | // Add new stations
|
---|
2538 | if (Time - LastNewStationTime) > NewStationPeriod then begin
|
---|
2539 | LastNewStationTime := Time;
|
---|
2540 | Stations.AddNew;
|
---|
2541 | ResizeView;
|
---|
2542 | Redraw;
|
---|
2543 | end;
|
---|
2544 |
|
---|
2545 | // Add new passengers
|
---|
2546 | if (Time - LastNewPassengerTime) > NewPassengerPeriod then begin
|
---|
2547 | LastNewPassengerTime := Time;
|
---|
2548 | for MapStation in Stations do
|
---|
2549 | with MapStation do
|
---|
2550 | if Random < NewPassengerProbability then begin
|
---|
2551 | Passenger := Self.Passengers.AddNew;
|
---|
2552 | Passenger.Station := MapStation;
|
---|
2553 | Passengers.Add(Passenger);
|
---|
2554 |
|
---|
2555 | // Passenger is not allowed to have same shape
|
---|
2556 | while (Passenger.Shape = Passenger.Station.Shape) or
|
---|
2557 | not (Passenger.Shape in GetExistStationShapes) do
|
---|
2558 | Passenger.Shape := TStationShape((Integer(Passenger.Shape) + 1) mod Integer(ShapeCount));
|
---|
2559 | Redraw;
|
---|
2560 | end;
|
---|
2561 | end;
|
---|
2562 |
|
---|
2563 | // Check station passenger overload state
|
---|
2564 | for MapStation in Stations do
|
---|
2565 | with MapStation do begin
|
---|
2566 | if Passengers.Count > MaxWaitingPassengers then begin
|
---|
2567 | OverloadDuration := OverloadDuration + (FTime - FLastTime);
|
---|
2568 | if OverloadDuration > MaxPassengersOveloadTime then
|
---|
2569 | OverloadDuration := MaxPassengersOveloadTime;
|
---|
2570 | if OverloadDuration < MaxPassengersOveloadTime then Redraw;
|
---|
2571 | end;
|
---|
2572 | if Passengers.Count <= MaxWaitingPassengers then begin
|
---|
2573 | if OverloadDuration > 0 then Redraw;
|
---|
2574 | OverloadDuration := OverloadDuration - (FTime - FLastTime);
|
---|
2575 | if OverloadDuration < 0 then begin
|
---|
2576 | OverloadDuration := 0;
|
---|
2577 | end;
|
---|
2578 | end;
|
---|
2579 | end;
|
---|
2580 |
|
---|
2581 | TrainMovement;
|
---|
2582 |
|
---|
2583 | // Game over
|
---|
2584 | for MapStation in Stations do
|
---|
2585 | with MapStation do begin
|
---|
2586 | if OverloadDuration >= MaxPassengersOveloadTime then begin
|
---|
2587 | State := gsGameOver;
|
---|
2588 | Redraw;
|
---|
2589 | end;
|
---|
2590 | end;
|
---|
2591 |
|
---|
2592 | end;
|
---|
2593 | LastTickTime := Now;
|
---|
2594 | FLastTime := FTime;
|
---|
2595 | end;
|
---|
2596 |
|
---|
2597 | procedure TEngine.MouseMove(Position: TPoint);
|
---|
2598 | var
|
---|
2599 | FocusedStation: TMapStation;
|
---|
2600 | Line: TMetroLine;
|
---|
2601 | LineStationDown: TLineStation;
|
---|
2602 | LineStationUp: TLineStation;
|
---|
2603 | CurrentTrackPoint: TTrackPoint;
|
---|
2604 | begin
|
---|
2605 | if State = gsMenu then begin
|
---|
2606 | Menu.MouseMove(Position);
|
---|
2607 | Redraw;
|
---|
2608 | end;
|
---|
2609 |
|
---|
2610 | LastMousePos := Position;
|
---|
2611 | if MouseHold then begin
|
---|
2612 | FocusedStation := GetStationOnPos(View.PointDestToSrc(Position));
|
---|
2613 | Line := nil;
|
---|
2614 | if Assigned(TrackStationDown) then begin
|
---|
2615 | Line := TrackStationDown.Track.Line;
|
---|
2616 | Redraw;
|
---|
2617 | end;
|
---|
2618 | if Assigned(TrackStationUp) then begin
|
---|
2619 | Line := TrackStationUp.Track.Line;
|
---|
2620 | Redraw;
|
---|
2621 | end;
|
---|
2622 | if Assigned(Line) and not Assigned(LastFocusedStation) and Assigned(FocusedStation) then begin
|
---|
2623 | if Assigned(TrackStationDown) and (TrackStationDown.LineStation.MapStation = FocusedStation) then begin
|
---|
2624 | // Disconnect down
|
---|
2625 | CurrentTrackPoint := TrackStationDown;
|
---|
2626 | TrackStationDown := TrackStationDown.GetDown;
|
---|
2627 | Line.DisconnectStation(CurrentTrackPoint.LineStation);
|
---|
2628 | end else
|
---|
2629 | if Assigned(TrackStationUp) and (TrackStationUp.LineStation.MapStation = FocusedStation) then begin
|
---|
2630 | // Disconnect up
|
---|
2631 | CurrentTrackPoint := TrackStationUp;
|
---|
2632 | if Assigned(TrackStationUp) then
|
---|
2633 | TrackStationUp := TrackStationUp.GetUp;
|
---|
2634 | Line.DisconnectStation(CurrentTrackPoint.LineStation);
|
---|
2635 | end else
|
---|
2636 | if Assigned(Line) and ((not Line.IsCircular) or ((TrackStationDown <> nil) and (TrackStationUp <> nil))) and
|
---|
2637 | ((Line.LineStations.SearchMapStation(FocusedStation) = nil) or
|
---|
2638 | ((Line.LineStations.Count > 0) and
|
---|
2639 | ((Line.LineStations.First.MapStation = FocusedStation) or
|
---|
2640 | (Line.LineStations.Last.MapStation = FocusedStation)) and
|
---|
2641 | ((TrackStationDown = nil) or (TrackStationUp = nil)) and
|
---|
2642 | (not Line.IsCircular))) then begin
|
---|
2643 | if Assigned(TrackStationDown) then LineStationDown := TrackStationDown.LineStation
|
---|
2644 | else LineStationDown := nil;
|
---|
2645 | if Assigned(TrackStationUp) then LineStationUp := TrackStationUp.LineStation
|
---|
2646 | else LineStationUp := nil;
|
---|
2647 | Line.ConnectStation(FocusedStation, LineStationDown, LineStationUp);
|
---|
2648 | if Assigned(TrackStationDown) then TrackStationDown := TrackStationDown.GetUp
|
---|
2649 | else if Assigned(TrackStationUp) then TrackStationUp := TrackStationUp.GetDown;
|
---|
2650 | end;
|
---|
2651 | end;
|
---|
2652 | LastFocusedStation := FocusedStation;
|
---|
2653 | end;
|
---|
2654 | end;
|
---|
2655 |
|
---|
2656 | procedure TEngine.MouseUp(Button: TMouseButton; Position: TPoint);
|
---|
2657 | var
|
---|
2658 | I: Integer;
|
---|
2659 | FocusedTrack: TTrackLink;
|
---|
2660 | begin
|
---|
2661 | if Button = mbLeft then begin
|
---|
2662 | if State = gsMenu then begin
|
---|
2663 | Menu.MouseUp(Button, Position);
|
---|
2664 | Redraw;
|
---|
2665 | end else begin
|
---|
2666 | // Back button
|
---|
2667 | if ButtonBack.Bounds.Contains(Position) then begin
|
---|
2668 | if Assigned(ButtonBack.OnClick) then
|
---|
2669 | ButtonBack.OnClick(ButtonBack);
|
---|
2670 | end;
|
---|
2671 |
|
---|
2672 | // Pause button
|
---|
2673 | if ImagePause.Bounds.Contains(Position) then begin
|
---|
2674 | if Assigned(ImagePause.OnClick) then
|
---|
2675 | ImagePause.OnClick(ImagePause);
|
---|
2676 | end;
|
---|
2677 |
|
---|
2678 | // Play button
|
---|
2679 | if ImagePlay.Bounds.Contains(Position) then begin
|
---|
2680 | if Assigned(ImagePlay.OnClick) then
|
---|
2681 | ImagePlay.OnClick(ImagePlay);
|
---|
2682 | end;
|
---|
2683 |
|
---|
2684 | // Fast forward button
|
---|
2685 | if ImageFastForward.Bounds.Contains(Position) then begin
|
---|
2686 | if Assigned(ImageFastForward.OnClick) then
|
---|
2687 | ImageFastForward.OnClick(ImageFastForward);
|
---|
2688 | end;
|
---|
2689 |
|
---|
2690 | // Place selected train if focused track
|
---|
2691 | if Assigned(SelectedTrain) then begin
|
---|
2692 | SelectedTrain.TargetStation := nil;
|
---|
2693 | SelectedTrain.BaseTrackPoint := nil;
|
---|
2694 | if Assigned(SelectedTrain.Line) then begin
|
---|
2695 | SelectedTrain.Line.Trains.Remove(SelectedTrain);
|
---|
2696 | SelectedTrain.Line := nil;
|
---|
2697 | end;
|
---|
2698 | FocusedTrack := GetTrackOnPos(View.PointDestToSrc(Position));
|
---|
2699 | if Assigned(FocusedTrack.Points[0]) then begin
|
---|
2700 | SelectedTrain.Line := FocusedTrack.Points[0].Track.Line;
|
---|
2701 | SelectedTrain.Line.Trains.Add(SelectedTrain);
|
---|
2702 | SelectedTrain.BaseTrackPoint := FocusedTrack.Points[0];
|
---|
2703 | end else
|
---|
2704 | if Assigned(FocusedTrack.Points[1]) then begin
|
---|
2705 | SelectedTrain.Line := FocusedTrack.Points[1].Track.Line;
|
---|
2706 | SelectedTrain.Line.Trains.Add(SelectedTrain);
|
---|
2707 | SelectedTrain.BaseTrackPoint := FocusedTrack.Points[1];
|
---|
2708 | end;
|
---|
2709 | FocusedTrack.Free;
|
---|
2710 | end;
|
---|
2711 |
|
---|
2712 | // Line color selection
|
---|
2713 | for I := 0 to Lines.Count - 1 do
|
---|
2714 | if Distance(Point(View.DestRect.Right div 2 - Length(LineColors) div 2 * LineColorsDist + I * LineColorsDist,
|
---|
2715 | View.DestRect.Bottom - LineColorsDist), Position) < 20 then begin
|
---|
2716 | SelectedLine := Lines[I];
|
---|
2717 | Exit;
|
---|
2718 | end;
|
---|
2719 |
|
---|
2720 | // Remove single line station on line
|
---|
2721 | if Assigned(TrackStationDown) and (TrackStationDown.Track.Line.LineStations.Count = 1) then begin
|
---|
2722 | TrackStationDown.Track.Line.DisconnectStation(TrackStationDown.Track.Line.LineStations.First);
|
---|
2723 | end;
|
---|
2724 | if Assigned(TrackStationUp) and (TrackStationUp.Track.Line.LineStations.Count = 1) then begin
|
---|
2725 | TrackStationUp.Track.Line.DisconnectStation(TrackStationUp.Track.Line.LineStations.First);
|
---|
2726 | end;
|
---|
2727 | end;
|
---|
2728 | end else
|
---|
2729 | if Button = mbRight then begin
|
---|
2730 | SelectedLine := nil;
|
---|
2731 | end;
|
---|
2732 | MouseHold := False;
|
---|
2733 | TrackStationDown := nil;
|
---|
2734 | TrackStationUp := nil;
|
---|
2735 | SelectedTrain := nil;
|
---|
2736 | Redraw;
|
---|
2737 | end;
|
---|
2738 |
|
---|
2739 | procedure TEngine.MouseDown(Button: TMouseButton; Position: TPoint);
|
---|
2740 | var
|
---|
2741 | Station: TMapStation;
|
---|
2742 | NewLine: TMetroLine;
|
---|
2743 | Track: TTrackLink;
|
---|
2744 | NewIndex: Integer;
|
---|
2745 | begin
|
---|
2746 | if (Button = mbLeft) and (State <> gsMenu) then begin
|
---|
2747 | MouseHold := True;
|
---|
2748 | LastFocusedStation := nil;
|
---|
2749 |
|
---|
2750 | // Train selection
|
---|
2751 | SelectedTrain := GetTrainOnPos(View.PointDestToSrc(Position));
|
---|
2752 | if Assigned(SelectedTrain) then begin
|
---|
2753 | Exit;
|
---|
2754 | end;
|
---|
2755 |
|
---|
2756 | // Select unused train
|
---|
2757 | if (Distance(Position, Point(View.DestRect.Right div 2 - Length(LineColors) div 2 * LineColorsDist - 100,
|
---|
2758 | View.DestRect.Bottom - LineColorsDist)) < 30) and
|
---|
2759 | (Trains.GetUnusedCount > 0) then begin
|
---|
2760 | SelectedTrain := Trains.GetUnusedTrain;
|
---|
2761 | Exit;
|
---|
2762 | end;
|
---|
2763 |
|
---|
2764 | // New track creation from selected station as start
|
---|
2765 | Station := GetStationOnPos(View.PointDestToSrc(Position));
|
---|
2766 | if Assigned(Station) then begin
|
---|
2767 | if Assigned(SelectedLine) and (SelectedLine.LineStations.Count = 0) then NewLine := SelectedLine
|
---|
2768 | else NewLine := GetUnusedLine;
|
---|
2769 | if Assigned(NewLine) then begin
|
---|
2770 | NewLine.ConnectStation(Station, nil, nil);
|
---|
2771 | TrackStationDown := NewLine.Track.Points.Last;
|
---|
2772 | TrackStationUp := nil;
|
---|
2773 | LastFocusedStation := Station;
|
---|
2774 | SelectedLine := NewLine;
|
---|
2775 | Exit;
|
---|
2776 | end;
|
---|
2777 | end;
|
---|
2778 |
|
---|
2779 | // Line selection
|
---|
2780 | Track := GetTrackOnPos(View.PointDestToSrc(Position));
|
---|
2781 | if Assigned(Track) and Assigned(Track.Points[0]) and Assigned(Track.Points[1]) then begin
|
---|
2782 | SelectedLine := Track.Points[0].Track.Line;
|
---|
2783 |
|
---|
2784 | TrackStationDown := Track.Points[0];
|
---|
2785 | NewIndex := TrackStationDown.Track.Points.IndexOf(TrackStationDown);
|
---|
2786 | while Assigned(TrackStationDown) and (not Assigned(TrackStationDown.LineStation)) do begin
|
---|
2787 | NewIndex := NewIndex - 1;
|
---|
2788 | if NewIndex >= 0 then TrackStationDown := TrackStationDown.Track.Points[NewIndex]
|
---|
2789 | else TrackStationDown := nil;
|
---|
2790 | end;
|
---|
2791 | TrackStationUp := Track.Points[1];
|
---|
2792 | NewIndex := TrackStationUp.Track.Points.IndexOf(TrackStationDown);
|
---|
2793 | while Assigned(TrackStationUp) and (not Assigned(TrackStationUp.LineStation)) do begin
|
---|
2794 | NewIndex := NewIndex + 1;
|
---|
2795 | if NewIndex < TrackStationUp.Track.Points.Count then
|
---|
2796 | TrackStationUp := TrackStationUp.Track.Points[NewIndex]
|
---|
2797 | else TrackStationUp := nil;
|
---|
2798 | end;
|
---|
2799 | Track.Free;
|
---|
2800 | Exit;
|
---|
2801 | end;
|
---|
2802 | if Assigned(Track) then Track.Free;
|
---|
2803 | end;
|
---|
2804 | end;
|
---|
2805 |
|
---|
2806 | procedure TEngine.KeyUp(Key: Word);
|
---|
2807 | const
|
---|
2808 | KeyEsc = 27;
|
---|
2809 | KeyF2 = 113;
|
---|
2810 | begin
|
---|
2811 | if Key = KeyEsc then begin
|
---|
2812 | if State = gsMenu then begin
|
---|
2813 | if Assigned(Menu.OnExit) then
|
---|
2814 | Menu.OnExit(nil);
|
---|
2815 | end else begin
|
---|
2816 | ButtonBackClick(nil);
|
---|
2817 | end;
|
---|
2818 | end;
|
---|
2819 | {$IFDEF DEBUG}
|
---|
2820 | if Key = KeyF2 then begin
|
---|
2821 | if State = gsRunning then begin
|
---|
2822 | State := gsGameOver;
|
---|
2823 | Redraw;
|
---|
2824 | end;
|
---|
2825 | end;
|
---|
2826 | {$ENDIF}
|
---|
2827 | end;
|
---|
2828 |
|
---|
2829 | procedure TEngine.MainMenu;
|
---|
2830 | begin
|
---|
2831 | State := gsMenu;
|
---|
2832 | Redraw;
|
---|
2833 | end;
|
---|
2834 |
|
---|
2835 | procedure TEngine.Clear;
|
---|
2836 | begin
|
---|
2837 | Trains.Clear;
|
---|
2838 | Passengers.Clear;
|
---|
2839 | Lines.Clear;
|
---|
2840 | Stations.Clear;
|
---|
2841 | end;
|
---|
2842 |
|
---|
2843 | procedure TEngine.NewGame;
|
---|
2844 | var
|
---|
2845 | NewTrain: TMetroTrain;
|
---|
2846 | I: Integer;
|
---|
2847 | NewStation: TMapStation;
|
---|
2848 | InitialStationCount: Integer;
|
---|
2849 | begin
|
---|
2850 | Clear;
|
---|
2851 | ShapeCount := 3;
|
---|
2852 | ServedPassengerCount := 0;
|
---|
2853 |
|
---|
2854 | // Start with 3 stations with each different shape
|
---|
2855 | InitialStationCount := 3;
|
---|
2856 | for I := 0 to InitialStationCount - 1 do begin
|
---|
2857 | NewStation := Stations.AddNew;
|
---|
2858 | if I = 0 then NewStation.Shape := ssSquare
|
---|
2859 | else if I = 1 then NewStation.Shape := ssCircle
|
---|
2860 | else if I = 2 then NewStation.Shape := ssTriangle;
|
---|
2861 | end;
|
---|
2862 |
|
---|
2863 | for I := 0 to 8 do begin
|
---|
2864 | Lines.AddNew;
|
---|
2865 | NewTrain := TMetroTrain.Create;
|
---|
2866 | Trains.Add(NewTrain);
|
---|
2867 | end;
|
---|
2868 |
|
---|
2869 | ResizeView;
|
---|
2870 |
|
---|
2871 | SelectedLine := nil;
|
---|
2872 | FTime := 0;
|
---|
2873 | FLastTime := 0;
|
---|
2874 | LastNewStationTime := Time;
|
---|
2875 | LastNewPassengerTime := Time;
|
---|
2876 | LastNewWeekTime := Time;
|
---|
2877 | LastNewShapeTime := Time;
|
---|
2878 | LastTickTime := Now;
|
---|
2879 | State := gsRunning;
|
---|
2880 | Redraw;
|
---|
2881 | end;
|
---|
2882 |
|
---|
2883 | procedure TEngine.Redraw;
|
---|
2884 | begin
|
---|
2885 | RedrawPending := True;
|
---|
2886 | end;
|
---|
2887 |
|
---|
2888 | constructor TEngine.Create;
|
---|
2889 | begin
|
---|
2890 | TimePerSecond := TimePerSecondNormal;
|
---|
2891 | ButtonBack := TImage.Create;
|
---|
2892 | ButtonBack.OnClick := ButtonBackClick;
|
---|
2893 | ButtonBack.Bounds := Bounds(0, 0, ScaleX(80, 96), ScaleY(80, 96));
|
---|
2894 | MenuMain := TMenu.Create;
|
---|
2895 | MenuOptions := TMenu.Create;
|
---|
2896 | MenuGame := TMenu.Create;
|
---|
2897 | Menu := MenuMain;
|
---|
2898 | InitMenus;
|
---|
2899 | Stations := TMapStations.Create;
|
---|
2900 | Stations.Engine := Self;
|
---|
2901 | Lines := TMetroLines.Create;
|
---|
2902 | Lines.Engine := Self;
|
---|
2903 | Passengers := TMetroPassengers.Create;
|
---|
2904 | Passengers.Engine := Self;
|
---|
2905 | Map := TMap.Create;
|
---|
2906 | View := TView.Create;
|
---|
2907 | Trains := TMetroTrains.Create;
|
---|
2908 | ImagePassenger := TImage.Create;
|
---|
2909 | ImageLocomotive := TImage.Create;
|
---|
2910 | ImagePlay := TImage.Create;
|
---|
2911 | ImagePlay.OnClick := ButtonPlay;
|
---|
2912 | ImagePause := TImage.Create;
|
---|
2913 | ImagePause.OnClick := ButtonPause;
|
---|
2914 | ImageFastForward := TImage.Create;
|
---|
2915 | ImageFastForward.OnClick := ButtonFastForward;
|
---|
2916 | //if FileExists(ImagePassengerName) then
|
---|
2917 | // ImagePassenger.Picture.LoadFromFile(ImagePassengerName);
|
---|
2918 | //if FileExists(ImageLocomotiveName) then
|
---|
2919 | // ImageLocomotive.Picture.LoadFromFile(ImageLocomotiveName);
|
---|
2920 | MetaCanvas := TMetaCanvas.Create;
|
---|
2921 | InitColors;
|
---|
2922 | end;
|
---|
2923 |
|
---|
2924 | destructor TEngine.Destroy;
|
---|
2925 | begin
|
---|
2926 | FreeAndNil(MetaCanvas);
|
---|
2927 | FreeAndNil(Trains);
|
---|
2928 | FreeAndNil(ImagePlay);
|
---|
2929 | FreeAndNil(ImageFastForward);
|
---|
2930 | FreeAndNil(ImagePause);
|
---|
2931 | FreeAndNil(ImageLocomotive);
|
---|
2932 | FreeAndNil(ImagePassenger);
|
---|
2933 | FreeAndNil(View);
|
---|
2934 | FreeAndNil(Map);
|
---|
2935 | FreeAndNil(Passengers);
|
---|
2936 | FreeAndNil(Stations);
|
---|
2937 | FreeAndNil(Lines);
|
---|
2938 | FreeAndNil(MenuMain);
|
---|
2939 | FreeAndNil(MenuOptions);
|
---|
2940 | FreeAndNil(MenuGame);
|
---|
2941 | FreeAndNil(ButtonBack);
|
---|
2942 | inherited;
|
---|
2943 | end;
|
---|
2944 |
|
---|
2945 | procedure TEngine.Paint(Canvas: TCanvas; CanvasSize: TPoint);
|
---|
2946 | begin
|
---|
2947 | MetaCanvas.Size := Point(Canvas.Width, Canvas.Height);
|
---|
2948 | MetaCanvas.Reset;
|
---|
2949 |
|
---|
2950 | DrawStationPassengerOverload(MetaCanvas);
|
---|
2951 | DrawLines(MetaCanvas);
|
---|
2952 | DrawTrains(MetaCanvas);
|
---|
2953 | DrawStations(MetaCanvas);
|
---|
2954 |
|
---|
2955 | // MainMenu background
|
---|
2956 | Canvas.Brush.Color := Colors.Background;
|
---|
2957 | Canvas.Brush.Style := bsSolid;
|
---|
2958 | Canvas.Clear;
|
---|
2959 |
|
---|
2960 | MetaCanvas.Move(Point(-View.SourceRect.Left, -View.SourceRect.Top));
|
---|
2961 | MetaCanvas.Zoom(View.Zoom);
|
---|
2962 |
|
---|
2963 | // Draw meta canvas to real target canvas
|
---|
2964 | MetaCanvas.DrawTo(Canvas);
|
---|
2965 |
|
---|
2966 | if State <> gsMenu then begin
|
---|
2967 | DrawGameControls(Canvas, CanvasSize);
|
---|
2968 | end;
|
---|
2969 |
|
---|
2970 | // Game over
|
---|
2971 | if State = gsGameOver then
|
---|
2972 | begin
|
---|
2973 | DrawGameOver(Canvas, CanvasSize);
|
---|
2974 | end else
|
---|
2975 | if State = gsMenu then begin
|
---|
2976 | Menu.Paint(Canvas, CanvasSize);
|
---|
2977 | end;
|
---|
2978 |
|
---|
2979 | RedrawPending := False;
|
---|
2980 | end;
|
---|
2981 |
|
---|
2982 | end.
|
---|
2983 |
|
---|