Changeset 15 for trunk


Ignore:
Timestamp:
Mar 27, 2015, 1:13:50 PM (10 years ago)
Author:
chronos
Message:
  • Modified: Moved train movement to separate method.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UEngine.pas

    r14 r15  
    158158      UpdatedShape: TStationShape; Distance: Integer);
    159159    function GetTime: TDateTime;
     160    procedure TrainMovement;
    160161  public
    161162    Passengers: TMetroPassengers;
     
    793794end;
    794795
    795 procedure TEngine.DrawClock(Canvas: TCanvas);
    796 var
    797   ClockCenter: TPoint;
    798   Angle: Double;
    799   Text: string;
    800   I: Integer;
    801 const
    802   ClockSize = 20;
    803 begin
    804   Canvas.Pen.Style := psSolid;
    805   Canvas.Pen.Color := clBlack;
    806   Canvas.Pen.Width := 2;
    807   ClockCenter := Point(Canvas.Width - 30, 40);
    808   Angle := Time / (12 * OneHour) * 2 * Pi - Pi / 2;
    809   Canvas.EllipseC(ClockCenter.X, ClockCenter.Y, ClockSize, ClockSize);
    810   Canvas.Line(ClockCenter, Point(ClockCenter.X + Round(Cos(Angle) * ClockSize * 0.8),
    811     ClockCenter.Y + Round(Sin(Angle) * ClockSize * 0.8)));
    812   Text := FormatDateTime('ddd', Time);
    813   Canvas.TextOut(ClockCenter.X - ClockSize - Canvas.TextWidth(Text) - 5, ClockCenter.Y -
    814     Canvas.TextWidth(Text) div 2, Text);
    815   for I := 0 to 12 do begin
    816     Angle := I / 12 * 2 * Pi;
    817     Canvas.Line(ClockCenter.X + Round(Cos(Angle) * ClockSize * 0.8),
    818       ClockCenter.Y + Round(Sin(Angle) * ClockSize * 0.8),
    819       ClockCenter.X + Round(Cos(Angle) * ClockSize * 0.9),
    820       ClockCenter.Y + Round(Sin(Angle) * ClockSize * 0.9));
    821   end;
    822 end;
    823 
    824 procedure TEngine.DrawTrains(Canvas: TCanvas);
    825 var
    826   I: Integer;
     796procedure TEngine.TrainMovement;
     797var
     798  I: Integer;
     799  CurrentStationIndex: Integer;
     800  CurrentStation: TMetroStation;
    827801  P: Integer;
    828   Pos: TPoint;
    829   Points: array of TPoint;
    830   Angle: Double;
    831   ShapePos: TPoint;
    832 begin
    833     // Draw trains
    834   for I := 0 to Trains.Count - 1 do
    835   with TMetroTrain(Trains[I]) do begin
    836     if Assigned(Line) then begin
    837       Canvas.Brush.Color := Line.Color;
    838       Canvas.Brush.Style := bsSolid;
    839       Canvas.Pen.Style := psClear;
    840       Pos := GetPosition;
    841       Angle := GetAngle;
    842 
    843       SetLength(Points, 4);
    844       Points[0] := RotatePoint(Pos, Point(Pos.X - TrainSize div 2, Pos.Y - TrainSize div 3), Angle);
    845       Points[1] := RotatePoint(Pos, Point(Pos.X + TrainSize div 2, Pos.Y - TrainSize div 3), Angle);
    846       Points[2] := RotatePoint(Pos, Point(Pos.X + TrainSize div 2, Pos.Y + TrainSize div 3), Angle);
    847       Points[3] := RotatePoint(Pos, Point(Pos.X - TrainSize div 2, Pos.Y + TrainSize div 3), Angle);
    848       Canvas.Polygon(Points);
    849       Canvas.Brush.Color := clWhite;
    850       for P := 0 to Passengers.Count - 1 do
    851       with TMetroPassenger(Passengers[P]) do begin
    852         ShapePos := Point(Pos.X - Trunc(TrainSize div 3 * 1) + (P mod 3) * TrainSize div 3,
    853           Pos.Y - Trunc(TrainSize div 6 * 1) + (P div 3) * TrainSize div 3);
    854         ShapePos := RotatePoint(Pos, ShapePos, Angle);
    855         DrawShape(Canvas, ShapePos, Shape, TrainSize div 3, Angle + Pi / 2);
    856       end;
    857     end;
    858   end;
    859 
    860 end;
    861 
    862 procedure TEngine.Tick;
    863 const
    864   NewStationPeriod = OneSecond * 20;
    865   NewPassengerPeriod = OneSecond;
    866   NewPassengerProbability = 0.1;
    867 var
    868802  Passenger: TMetroPassenger;
    869   I: Integer;
    870   P: Integer;
    871803  PosDelta: Integer;
    872   CurrentStation: TMetroStation;
    873   CurrentStationIndex: Integer;
    874 begin
    875   if State = gsRunning then begin
    876 
    877   // Add new trains
    878   if (Now - LastNewWeekTime) > 7 then begin
    879     LastNewWeekTime := Now;
    880     Trains.AddNew;
    881     // TODO: Show notification screen with confirmation
    882   end;
    883 
    884   // Add new shape
    885   if (Now - LastNewShapeTime) > 10 then begin
    886     LastNewShapeTime := Now;
    887     if ShapeCount <= Integer(High(TStationShape)) then Inc(ShapeCount);
    888   end;
    889 
    890   // Add new stations
    891   if (Now - LastNewStationTime) > NewStationPeriod then begin
    892     LastNewStationTime := Now;
    893     Stations.AddNew;
    894   end;
    895 
    896   // Add new passengers
    897   if (Now - LastNewPassengerTime) > NewPassengerPeriod then begin
    898     LastNewPassengerTime := Now;
    899     for I := 0 to Stations.Count - 1 do
    900     with TMetroStation(Stations[I]) do
    901     if Random < NewPassengerProbability then begin
    902       Passenger := Self.Passengers.AddNew;
    903       Passenger.Station := TMetroStation(Stations[I]);
    904       Passengers.Add(Passenger);
    905 
    906       // Passenger is not allowed to have same shape
    907       while (Passenger.Shape = Passenger.Station.Shape) or not (Passenger.Shape in GetExistStationShapes) do
    908         Passenger.Shape := TStationShape((Integer(Passenger.Shape) + 1) mod Integer(ShapeCount));
    909     end;
    910   end;
    911 
     804begin
    912805  // Move trains
    913806  for I := 0 to Trains.Count - 1 do
     
    1004897    end;
    1005898  end;
     899end;
     900
     901procedure TEngine.DrawClock(Canvas: TCanvas);
     902var
     903  ClockCenter: TPoint;
     904  Angle: Double;
     905  Text: string;
     906  I: Integer;
     907const
     908  ClockSize = 20;
     909begin
     910  Canvas.Pen.Style := psSolid;
     911  Canvas.Pen.Color := clBlack;
     912  Canvas.Pen.Width := 2;
     913  ClockCenter := Point(Canvas.Width - 30, 40);
     914  Angle := Time / (12 * OneHour) * 2 * Pi - Pi / 2;
     915  Canvas.EllipseC(ClockCenter.X, ClockCenter.Y, ClockSize, ClockSize);
     916  Canvas.Line(ClockCenter, Point(ClockCenter.X + Round(Cos(Angle) * ClockSize * 0.8),
     917    ClockCenter.Y + Round(Sin(Angle) * ClockSize * 0.8)));
     918  Text := FormatDateTime('ddd', Time);
     919  Canvas.TextOut(ClockCenter.X - ClockSize - Canvas.TextWidth(Text) - 5, ClockCenter.Y -
     920    Canvas.TextWidth(Text) div 2, Text);
     921  for I := 0 to 12 do begin
     922    Angle := I / 12 * 2 * Pi;
     923    Canvas.Line(ClockCenter.X + Round(Cos(Angle) * ClockSize * 0.8),
     924      ClockCenter.Y + Round(Sin(Angle) * ClockSize * 0.8),
     925      ClockCenter.X + Round(Cos(Angle) * ClockSize * 0.9),
     926      ClockCenter.Y + Round(Sin(Angle) * ClockSize * 0.9));
     927  end;
     928end;
     929
     930procedure TEngine.DrawTrains(Canvas: TCanvas);
     931var
     932  I: Integer;
     933  P: Integer;
     934  Pos: TPoint;
     935  Points: array of TPoint;
     936  Angle: Double;
     937  ShapePos: TPoint;
     938begin
     939    // Draw trains
     940  for I := 0 to Trains.Count - 1 do
     941  with TMetroTrain(Trains[I]) do begin
     942    if Assigned(Line) then begin
     943      Canvas.Brush.Color := Line.Color;
     944      Canvas.Brush.Style := bsSolid;
     945      Canvas.Pen.Style := psClear;
     946      Pos := GetPosition;
     947      Angle := GetAngle;
     948
     949      SetLength(Points, 4);
     950      Points[0] := RotatePoint(Pos, Point(Pos.X - TrainSize div 2, Pos.Y - TrainSize div 3), Angle);
     951      Points[1] := RotatePoint(Pos, Point(Pos.X + TrainSize div 2, Pos.Y - TrainSize div 3), Angle);
     952      Points[2] := RotatePoint(Pos, Point(Pos.X + TrainSize div 2, Pos.Y + TrainSize div 3), Angle);
     953      Points[3] := RotatePoint(Pos, Point(Pos.X - TrainSize div 2, Pos.Y + TrainSize div 3), Angle);
     954      Canvas.Polygon(Points);
     955      Canvas.Brush.Color := clWhite;
     956      for P := 0 to Passengers.Count - 1 do
     957      with TMetroPassenger(Passengers[P]) do begin
     958        ShapePos := Point(Pos.X - Trunc(TrainSize div 3 * 1) + (P mod 3) * TrainSize div 3,
     959          Pos.Y - Trunc(TrainSize div 6 * 1) + (P div 3) * TrainSize div 3);
     960        ShapePos := RotatePoint(Pos, ShapePos, Angle);
     961        DrawShape(Canvas, ShapePos, Shape, TrainSize div 3, Angle + Pi / 2);
     962      end;
     963    end;
     964  end;
     965
     966end;
     967
     968procedure TEngine.Tick;
     969const
     970  NewStationPeriod = OneSecond * 20;
     971  NewPassengerPeriod = OneSecond;
     972  NewPassengerProbability = 0.1;
     973var
     974  Passenger: TMetroPassenger;
     975  I: Integer;
     976begin
     977  if State = gsRunning then begin
     978
     979  // Add new trains
     980  if (Now - LastNewWeekTime) > 7 then begin
     981    LastNewWeekTime := Now;
     982    Trains.AddNew;
     983    // TODO: Show notification screen with confirmation
     984  end;
     985
     986  // Add new shape
     987  if (Now - LastNewShapeTime) > 10 then begin
     988    LastNewShapeTime := Now;
     989    if ShapeCount <= Integer(High(TStationShape)) then Inc(ShapeCount);
     990  end;
     991
     992  // Add new stations
     993  if (Now - LastNewStationTime) > NewStationPeriod then begin
     994    LastNewStationTime := Now;
     995    Stations.AddNew;
     996  end;
     997
     998  // Add new passengers
     999  if (Now - LastNewPassengerTime) > NewPassengerPeriod then begin
     1000    LastNewPassengerTime := Now;
     1001    for I := 0 to Stations.Count - 1 do
     1002    with TMetroStation(Stations[I]) do
     1003    if Random < NewPassengerProbability then begin
     1004      Passenger := Self.Passengers.AddNew;
     1005      Passenger.Station := TMetroStation(Stations[I]);
     1006      Passengers.Add(Passenger);
     1007
     1008      // Passenger is not allowed to have same shape
     1009      while (Passenger.Shape = Passenger.Station.Shape) or not (Passenger.Shape in GetExistStationShapes) do
     1010        Passenger.Shape := TStationShape((Integer(Passenger.Shape) + 1) mod Integer(ShapeCount));
     1011    end;
     1012  end;
     1013
     1014  TrainMovement;
    10061015
    10071016  // Game over
Note: See TracChangeset for help on using the changeset viewer.