Changeset 95 for trunk/UEngine.pas


Ignore:
Timestamp:
Sep 28, 2022, 12:23:17 AM (20 months ago)
Author:
chronos
Message:
  • Added: Show dialog about new train every start of new week.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UEngine.pas

    r94 r95  
    77uses
    88  {$IFDEF DARWIN}MacOSAll, CocoaAll, CocoaUtils,{$ENDIF}
    9   Classes, SysUtils, Graphics, Controls, ExtCtrls, Math, DateUtils, URegistry,
    10   UMetaCanvas, Generics.Collections, Generics.Defaults, UMenu, UControls,
    11   UMetroPassenger, UColors, UView, URiver, UTrack, UCity, UGeometric;
     9  Classes, SysUtils, Graphics, Controls, ExtCtrls, Math, DateUtils, Types,
     10  URegistry, UMetaCanvas, Generics.Collections, Generics.Defaults, UMenu,
     11  UControls, UMetroPassenger, UColors, UView, URiver, UTrack, UCity, UGeometric;
    1212
    1313type
     
    148148  end;
    149149
    150   TGameState = (gsNotStarted, gsRunning, gsPaused, gsGameOver, gsMenu);
     150  TGameState = (gsNotStarted, gsRunning, gsPaused, gsGameOver, gsMenu, gsNewWeek);
    151151
    152152  { TEngine }
     
    187187    procedure DrawTrains(Canvas: TCanvas);
    188188    procedure DrawGameOver(Canvas: TCanvas; CanvasSize: TPoint);
    189     procedure DrawNewTrain(Canvas: TCanvas; CanvasSize: TPoint);
     189    procedure DrawNewWeek(Canvas: TCanvas; CanvasSize: TPoint);
    190190    procedure DrawStationPassengerOverload(Canvas: TCanvas);
    191191    procedure DrawLines(Canvas: TCanvas);
     
    210210    procedure ButtonPause(Sender: TObject);
    211211    procedure ButtonFastForward(Sender: TObject);
     212    procedure ButtonNewTrain(Sender: TObject);
    212213    procedure ButtonBackClick(Sender: TObject);
    213214    procedure DarkModeChanged(Sender: TObject);
     
    217218    procedure InitCities;
    218219  public
     220    Week: Integer;
    219221    Colors: TColors;
    220222    Passengers: TMetroPassengers;
     
    241243    ImageFastForward: TImage;
    242244    ImageCarriage: TImage;
     245    ImageNewTrain: TImage;
    243246    HighestServedPassengerCount: Integer;
    244247    HighestServedDaysCount: Integer;
     
    320323  SNoOldStationToConnectNew = 'No old line station to connect new station';
    321324  SStationWithoutMapStation = 'Station have to have MapStation';
     325  SNewTrain = 'You get a new train for your metro';
     326  SWeek = 'week';
     327  STrain = 'Train';
     328  SPlay = 'Play';
     329  SOptions = 'Options';
     330  SExit = 'Exit';
     331  SBigMetro = 'Big Metro';
     332  SDarkMode = 'Dark mode';
     333  SLanguage = 'Language';
     334  SCzech = 'Czech';
     335  SEnglish = 'English';
     336  SBack = 'Back';
     337  SAutomatic = 'Automatic';
     338  SFullScreen = 'Full screen';
     339  SContinue = 'Continue';
     340  SRestart = 'Try again';
    322341
    323342  // Cities
     
    16661685end;
    16671686
     1687procedure TEngine.ButtonNewTrain(Sender: TObject);
     1688begin
     1689  Trains.AddNew;
     1690  if Random < 0.2 then Carriages.AddNew;
     1691  State := gsRunning;
     1692  Redraw;
     1693end;
     1694
    16681695procedure TEngine.MenuItemGameContinue(Sender: TObject);
    16691696begin
     
    18321859  I: Integer;
    18331860  Y: Integer;
     1861  IsDay: Boolean;
    18341862const
    18351863  ClockSize = 20;
    18361864begin
    1837   if (Time / OneHour > 6) and (Time / OneHour < 18) then begin
     1865  IsDay := (((Time / OneHour) mod 24) > 6) and (((Time / OneHour) mod 24) < 18);
     1866  if IsDay then begin
    18381867    Canvas.Brush.Style := bsSolid;
    18391868    Canvas.Brush.Color := Colors.Background;
     
    18501879  Canvas.Brush.Style := bsClear;
    18511880
    1852   if (Time / OneHour > 6) and (Time / OneHour < 18) then begin
     1881  if IsDay then begin
    18531882    Canvas.Pen.Color := Colors.Text;
    18541883  end else begin
     
    18991928procedure TEngine.DrawTrains(Canvas: TCanvas);
    19001929var
    1901   I: Integer;
    19021930  P: Integer;
    19031931  Pos: TPoint;
     
    19782006    Brush.Color := Self.Colors.Background;
    19792007
    1980     Y := 100;
     2008    Y := ScaleY(100, 96);
    19812009    Font.Size := 40;
    19822010    TextOut((CanvasSize.X - TextWidth(SGameOver)) div 2, Y, SGameOver);
     
    19912019    Y := Y + Round(TextHeight(SGameOverStatistic) * 1.1);
    19922020
    1993     Y := Y + 16;
     2021    Y := Y + ScaleY(16, 96);
    19942022
    19952023    // Calculate new highest score
     
    20102038end;
    20112039
    2012 procedure TEngine.DrawNewTrain(Canvas: TCanvas; CanvasSize: TPoint);
    2013 begin
    2014 
     2040procedure TEngine.DrawNewWeek(Canvas: TCanvas; CanvasSize: TPoint);
     2041var
     2042  Text: string;
     2043  TextSize: TSize;
     2044  Y: Integer;
     2045  X: Integer;
     2046const
     2047  ImageZoom = 4;
     2048begin
     2049  with Canvas do begin
     2050    Pen.Color := Self.Colors.Text;
     2051    Pen.Style := psSolid;
     2052    Pen.Width := ScaleX(2, 96);
     2053    Brush.Color := Self.Colors.Background2;
     2054    Brush.Style := bsSolid;
     2055    Rectangle(Bounds(CanvasSize.X div 4, CanvasSize.Y div 4, CanvasSize.X div 2, CanvasSize.Y div 2));
     2056    Pen.Style := psClear;
     2057    X := CanvasSize.X div 4 + ScaleX(10, 96);
     2058    Y := CanvasSize.Y div 4 + ScaleX(10, 96);
     2059
     2060    Text := IntToStr(Week) + '. ' + SWeek;
     2061    Font.Size := 30;
     2062    Font.Color := Self.Colors.Text;
     2063    TextSize := TextExtent(Text);
     2064    TextOut(X, Y, Text);
     2065    Y := Y + Round(1.1 * TextSize.Height);
     2066
     2067    Text := SNewTrain;
     2068    Font.Size := 14;
     2069    Font.Color := Self.Colors.Text;
     2070    TextSize := TextExtent(Text);
     2071    TextOut(X, Y, Text);
     2072    Y := Y + Round(1.1 * TextSize.Height);
     2073
     2074    ImageNewTrain.Canvas := Canvas;
     2075    ImageNewTrain.Bounds := Bounds((CanvasSize.X - ImageLocomotive.Bitmap.Width * ImageZoom) div 2,
     2076      (CanvasSize.Y - ImageLocomotive.Bitmap.Height * ImageZoom) div 2,
     2077      ImageLocomotive.Bitmap.Width * ImageZoom,
     2078      ImageLocomotive.Bitmap.Height * ImageZoom);
     2079    ImageNewTrain.Paint;
     2080
     2081    Y := (CanvasSize.Y - ImageLocomotive.Bitmap.Height * ImageZoom) div 2 +
     2082      Round(ImageLocomotive.Bitmap.Height * ImageZoom * 1.1);
     2083
     2084    Text := STrain;
     2085    Font.Size := 20;
     2086    Font.Color := Self.Colors.Text;
     2087    TextSize := TextExtent(Text);
     2088    TextOut((CanvasSize.X - TextSize.Width) div 2, Y, Text);
     2089  end;
    20152090end;
    20162091
     
    22802355    Redraw; // Redraw on every tick because engine time is changed so clock should be redrawn
    22812356
    2282   // Add new trains
    2283   if (Time - LastNewWeekTime) > NewTrainPeriod then begin
    2284     LastNewWeekTime := Time;
    2285     Trains.AddNew;
    2286     if Random < 0.2 then Carriages.AddNew;
    2287     // TODO: Show notification screen with confirmation
    2288     Redraw;
    2289   end;
    2290 
    2291   // Add new shape
    2292   if (Time - LastNewShapeTime) > NewShapePeriod then begin
    2293     LastNewShapeTime := Time;
    2294     if ShapeCount <= Integer(High(TStationShape)) then Inc(ShapeCount);
    2295     Redraw;
    2296   end;
    2297 
    2298   // Add new stations
    2299   if (Time - LastNewStationTime) > NewStationPeriod then begin
    2300     LastNewStationTime := Time;
    2301     Stations.AddNew;
    2302     ResizeView;
    2303     Redraw;
    2304   end;
    2305 
    2306   // Add new passengers
    2307   if (Time - LastNewPassengerTime) > NewPassengerPeriod then begin
    2308     LastNewPassengerTime := Time;
     2357    // Add new trains
     2358    if (Time - LastNewWeekTime) > NewTrainPeriod then begin
     2359      LastNewWeekTime := Time;
     2360      Inc(Week);
     2361      State := gsNewWeek;
     2362      Redraw;
     2363    end;
     2364
     2365    // Add new shape
     2366    if (Time - LastNewShapeTime) > NewShapePeriod then begin
     2367      LastNewShapeTime := Time;
     2368      if ShapeCount <= Integer(High(TStationShape)) then Inc(ShapeCount);
     2369      Redraw;
     2370    end;
     2371
     2372    // Add new stations
     2373    if (Time - LastNewStationTime) > NewStationPeriod then begin
     2374      LastNewStationTime := Time;
     2375      Stations.AddNew;
     2376      ResizeView;
     2377      Redraw;
     2378    end;
     2379
     2380    // Add new passengers
     2381    if (Time - LastNewPassengerTime) > NewPassengerPeriod then begin
     2382      LastNewPassengerTime := Time;
     2383      for MapStation in Stations do
     2384      with MapStation do
     2385      if Random < NewPassengerProbability then begin
     2386        Passenger := Self.Passengers.AddNew;
     2387        Passenger.Shape := TStationShape(Random(Integer(ShapeCount)));
     2388        Passengers.Add(Passenger);
     2389
     2390        // Passenger is not allowed to have same shape
     2391        while (Passenger.Shape = Shape) or
     2392        not (Passenger.Shape in GetExistStationShapes) do
     2393          Passenger.Shape := TStationShape((Integer(Passenger.Shape) + 1) mod Integer(ShapeCount));
     2394        Redraw;
     2395      end;
     2396    end;
     2397
     2398    // Check station passenger overload state
    23092399    for MapStation in Stations do
    2310     with MapStation do
    2311     if Random < NewPassengerProbability then begin
    2312       Passenger := Self.Passengers.AddNew;
    2313       Passenger.Shape := TStationShape(Random(Integer(ShapeCount)));
    2314       Passengers.Add(Passenger);
    2315 
    2316       // Passenger is not allowed to have same shape
    2317       while (Passenger.Shape = Shape) or
    2318       not (Passenger.Shape in GetExistStationShapes) do
    2319         Passenger.Shape := TStationShape((Integer(Passenger.Shape) + 1) mod Integer(ShapeCount));
    2320       Redraw;
    2321     end;
    2322   end;
    2323 
    2324   // Check station passenger overload state
    2325   for MapStation in Stations do
    2326   with MapStation do begin
    2327     if Passengers.Count > MaxWaitingPassengers then begin
    2328       OverloadDuration := OverloadDuration + (FTime - FLastTime);
    2329       if OverloadDuration > MaxPassengersOveloadTime then
    2330         OverloadDuration := MaxPassengersOveloadTime;
    2331       if OverloadDuration < MaxPassengersOveloadTime then Redraw;
    2332     end;
    2333     if Passengers.Count <= MaxWaitingPassengers then begin
    2334       if OverloadDuration > 0 then Redraw;
    2335       OverloadDuration := OverloadDuration - (FTime - FLastTime);
    2336       if OverloadDuration < 0 then begin
    2337         OverloadDuration := 0;
    2338       end;
    2339     end;
    2340   end;
    2341 
    2342   TrainMovement;
    2343 
    2344   // Game over
    2345   for MapStation in Stations do
    2346   with MapStation do begin
    2347     if OverloadDuration >= MaxPassengersOveloadTime then begin
    2348       State := gsGameOver;
    2349       Redraw;
    2350     end;
    2351   end;
    2352 
     2400    with MapStation do begin
     2401      if Passengers.Count > MaxWaitingPassengers then begin
     2402        OverloadDuration := OverloadDuration + (FTime - FLastTime);
     2403        if OverloadDuration > MaxPassengersOveloadTime then
     2404          OverloadDuration := MaxPassengersOveloadTime;
     2405        if OverloadDuration < MaxPassengersOveloadTime then Redraw;
     2406      end;
     2407      if Passengers.Count <= MaxWaitingPassengers then begin
     2408        if OverloadDuration > 0 then Redraw;
     2409        OverloadDuration := OverloadDuration - (FTime - FLastTime);
     2410        if OverloadDuration < 0 then begin
     2411          OverloadDuration := 0;
     2412        end;
     2413      end;
     2414    end;
     2415
     2416    TrainMovement;
     2417
     2418    // Game over
     2419    for MapStation in Stations do
     2420    with MapStation do begin
     2421      if OverloadDuration >= MaxPassengersOveloadTime then begin
     2422        State := gsGameOver;
     2423        Redraw;
     2424      end;
     2425    end;
    23532426  end;
    23542427  LastTickTime := Now;
     
    24252498      Menu.MouseUp(Button, Position);
    24262499      Redraw;
    2427     end else begin
    2428       // Back button
    2429       if ButtonBack.Bounds.Contains(Position) then begin
    2430         if Assigned(ButtonBack.OnClick) then
    2431           ButtonBack.OnClick(ButtonBack);
    2432       end;
    2433 
    2434       // Pause button
    2435       if ImagePause.Bounds.Contains(Position) then begin
    2436         if Assigned(ImagePause.OnClick) then
    2437           ImagePause.OnClick(ImagePause);
    2438       end;
    2439 
    2440       // Play button
    2441       if ImagePlay.Bounds.Contains(Position) then begin
    2442         if Assigned(ImagePlay.OnClick) then
    2443           ImagePlay.OnClick(ImagePlay);
    2444       end;
    2445 
    2446       // Fast forward button
    2447       if ImageFastForward.Bounds.Contains(Position) then begin
    2448         if Assigned(ImageFastForward.OnClick) then
    2449           ImageFastForward.OnClick(ImageFastForward);
    2450       end;
     2500    end else
     2501    if State = gsNewWeek then begin
     2502      ImageNewTrain.MouseUp(Position);
     2503    end
     2504    else
     2505    if State = gsRunning then begin
     2506      ButtonBack.MouseUp(Position);
     2507      ImagePause.MouseUp(Position);
     2508      ImagePlay.MouseUp(Position);
     2509      ImageFastForward.MouseUp(Position);
    24512510
    24522511      // Place selected train if focused track
     
    26082667  KeyF3 = 114;
    26092668  KeyF4 = 115;
     2669  KeyF5 = 116;
    26102670begin
    26112671  if Key = KeyEsc then begin
     
    26352695      Redraw;
    26362696    end;
     2697  end else
     2698  if Key = KeyF5 then begin
     2699    if State = gsRunning then begin
     2700      State := gsNewWeek;
     2701      Redraw;
     2702    end;
    26372703  end;
    26382704  {$ENDIF}
     
    26472713procedure TEngine.Clear;
    26482714begin
     2715  Week := 0;
    26492716  Trains.Clear;
    26502717  Passengers.Clear;
     
    26732740  end;
    26742741
    2675   for I := 0 to 8 do begin
     2742  for I := 0 to 2 do begin
    26762743    Lines.AddNew;
    26772744    NewTrain := TMetroTrain.Create;
     
    27542821  ImageFastForward := TImage.Create;
    27552822  ImageFastForward.OnClick := ButtonFastForward;
     2823  ImageNewTrain := TImage.Create;
     2824  ImageNewTrain.OnClick := ButtonNewTrain;
    27562825  //if FileExists(ImagePassengerName) then
    27572826  //  ImagePassenger.Picture.LoadFromFile(ImagePassengerName);
     
    27702839  FreeAndNil(Trains);
    27712840  FreeAndNil(Carriages);
     2841  FreeAndNil(ImageNewTrain);
    27722842  FreeAndNil(ImagePlay);
    27732843  FreeAndNil(ImageFastForward);
     
    28092879  MetaCanvas.DrawTo(Canvas);
    28102880
    2811   if State <> gsMenu then begin
    2812     DrawGameControls(Canvas, CanvasSize);
    2813   end;
    2814 
    28152881  // Game over
    28162882  if State = gsGameOver then
     
    28202886  if State = gsMenu then begin
    28212887    Menu.Paint(Canvas, CanvasSize);
     2888  end else
     2889  if State = gsNewWeek then begin
     2890    DrawNewWeek(Canvas, CanvasSize);
     2891  end else
     2892  if State in [gsRunning, gsPaused] then begin
     2893    DrawGameControls(Canvas, CanvasSize);
    28222894  end;
    28232895
Note: See TracChangeset for help on using the changeset viewer.