Changeset 16


Ignore:
Timestamp:
Mar 27, 2015, 10:26:05 PM (10 years ago)
Author:
chronos
Message:
  • Added: Space bar key have a pause function now to stop train movement and time flow.
Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/UEngine.pas

    r15 r16  
    144144    LastNewWeekTime: TDateTime;
    145145    LastNewShapeTime: TDateTime;
     146    LastTickTime: TDateTime;
    146147    ImagePassenger: TImage;
    147148    ImageLocomotive: TImage;
     149    FTime: TDateTime;
    148150    function GetExistStationShapes: TStationShapeSet;
    149151    function GetStationOnPos(Pos: TPoint): TMetroStation;
     
    157159    procedure ComputeShapeDistanceStation(Station: TMetroStation;
    158160      UpdatedShape: TStationShape; Distance: Integer);
    159     function GetTime: TDateTime;
    160161    procedure TrainMovement;
    161162  public
     
    171172    ServedPassengerCount: Integer;
    172173    State: TGameState;
    173     StartTime: TDateTime;
    174174    procedure Tick;
    175175    procedure MouseMove(Position: TPoint);
     
    180180    destructor Destroy; override;
    181181    procedure Paint(Canvas: TCanvas);
    182     property Time: TDateTime read GetTime;
     182    property Time: TDateTime read FTime;
    183183  end;
    184184
     
    199199  MaxWaitingPassengers = 10;
    200200  MetroLineThickness = 13;
     201  TrackClickDistance = 20;
    201202
    202203implementation
     
    604605  T: Integer;
    605606  D: Integer;
     607  MinD: Integer;
    606608const
    607609  Distance = 30;
     
    609611  Result := nil;
    610612  I := 0;
     613  MinD := High(Integer);
    611614  while (I < Lines.Count) do
    612615  with TMetroLine(Lines[I]) do begin
    613616    for T := 1 to TrackPoints.Count - 1 do begin
    614617      D := PointToLineDistance(Pos, TTrackPoint(TrackPoints[T - 1]).Point, TTrackPoint(TrackPoints[T]).Point);
    615       if D < Distance then begin
     618      if (D < MinD) and (D < TrackClickDistance) then begin
     619        MinD := D;
    616620        Result := TMetroLine(Lines[I]);
    617         Break;
    618621      end;
    619622    end;
    620     if Assigned(Result) then Break;
    621623    Inc(I);
    622624  end;
     
    789791end;
    790792
    791 function TEngine.GetTime: TDateTime;
    792 begin
    793   Result := (Now - StartTime) / OneSecond * (60 * OneMinute);
    794 end;
    795 
    796793procedure TEngine.TrainMovement;
    797794var
     
    976973begin
    977974  if State = gsRunning then begin
     975    FTime := FTime + (Now - LastTickTime) / OneSecond * (60 * OneMinute);
    978976
    979977  // Add new trains
     
    10211019
    10221020  end;
     1021  LastTickTime := Now;
    10231022end;
    10241023
     
    11051104procedure TEngine.Reset;
    11061105var
    1107   NewLine: TMetroLine;
    11081106  NewTrain: TMetroTrain;
    11091107  I: Integer;
    11101108  NewStation: TMetroStation;
     1109  InitialStationCount: Integer;
    11111110begin
    11121111  Passengers.Clear;
     
    11181117
    11191118  // Start with 3 stations with each different shape
    1120   NewStation := Stations.AddNew;
    1121   NewStation.Shape := ssSquare;
    1122   NewStation := Stations.AddNew;
    1123   NewStation.Shape := ssCircle;
    1124   NewStation := Stations.AddNew;
    1125   NewStation.Shape := ssTriangle;
     1119  InitialStationCount := 30;
     1120  for I := 0 to InitialStationCount - 1 do begin
     1121    NewStation := Stations.AddNew;
     1122    if I = 0 then NewStation.Shape := ssSquare
     1123    else if I = 1 then NewStation.Shape := ssCircle
     1124    else if I = 2 then NewStation.Shape := ssTriangle;
     1125  end;
    11261126
    11271127  for I := 0 to 2 do begin
    1128     NewLine := Lines.AddNew;
     1128    Lines.AddNew;
    11291129    NewTrain := TMetroTrain.Create;
    11301130    Trains.Add(NewTrain);
     
    11361136  LastNewWeekTime := Now;
    11371137  LastNewShapeTime := Now;
     1138  LastTickTime := Now;
     1139  FTime := 0;
    11381140  State := gsRunning;
    1139   StartTime := Now;
    11401141end;
    11411142
  • trunk/UFormMain.lfm

    r2 r16  
    99  OnCreate = FormCreate
    1010  OnDestroy = FormDestroy
     11  OnKeyPress = FormKeyPress
    1112  OnShow = FormShow
    1213  LCLVersion = '1.5'
  • trunk/UFormMain.pas

    r3 r16  
    1818    procedure FormCreate(Sender: TObject);
    1919    procedure FormDestroy(Sender: TObject);
     20    procedure FormKeyPress(Sender: TObject; var Key: char);
    2021    procedure FormShow(Sender: TObject);
    2122    procedure PaintBox1Click(Sender: TObject);
     
    5253begin
    5354  Engine.Free;
     55end;
     56
     57procedure TFormMain.FormKeyPress(Sender: TObject; var Key: char);
     58begin
     59  if (Key = #32) then begin
     60    if Engine.State = gsRunning then Engine.State := gsPaused
     61    else if Engine.State = gsPaused then Engine.State := gsRunning;
     62  end;
    5463end;
    5564
Note: See TracChangeset for help on using the changeset viewer.