Changeset 130 for trunk/UEngine.pas


Ignore:
Timestamp:
May 1, 2023, 4:02:31 PM (13 months ago)
Author:
chronos
Message:
  • Added: Allow to save/load game in multiple slots.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UEngine.pas

    r129 r130  
    1010  URegistry, UMetaCanvas, Generics.Collections, Generics.Defaults, UMenu,
    1111  UControls, UMetroPassenger, UColors, UView, URiver, UTrack, UCity, UGeometric,
    12   UTranslator, DOM, XMLRead, XMLWrite, UXMLUtils, UItems;
     12  UTranslator, DOM, XMLRead, XMLWrite, UXMLUtils, UItems, FileUtil;
    1313
    1414type
     
    227227    MenuGame: TMenu;
    228228    MenuCustomGame: TMenu;
     229    MenuGameSlots: TMenu;
    229230    LastState: TGameState;
    230231    TimePerSecond: TDateTime;
     
    237238    GrabbedTrainDirection: Integer;
    238239    LastGrabbedTrain: TMetroTrain;
    239     SavedGameFileName: string;
    240240    function GetMetroLineThickness: Integer;
    241241    function GetServedDaysCount: Integer;
     
    286286    procedure MenuItemGameLoad(Sender: TObject);
    287287    procedure MenuItemGameSave(Sender: TObject);
     288    procedure MenuItemGameSlotLoad(Sender: TObject);
     289    procedure MenuItemGameSlotSave(Sender: TObject);
    288290    procedure MenuItemBack(Sender: TObject);
    289291    procedure ButtonPlay(Sender: TObject);
     
    304306    function GetImprovementBitmap(Improvement: TMetroImprovement): TBitmap;
    305307    procedure EvaluateImprovement(Improvement: TMetroImprovement);
     308    procedure ReloadGameSlots(Save: Boolean);
     309    function GetGameSlotFileName(Index: Integer): string;
    306310  public
    307311    // Test
     
    465469  SVisualStyle = 'Visual style';
    466470  SStationStyle = 'Station style';
     471  SSlot = 'Slot';
    467472
    468473  // Cities
     
    22872292procedure TEngine.MenuItemGameLoad(Sender: TObject);
    22882293begin
    2289   LoadFromFile(SavedGameFileName);
     2294  ReloadGameSlots(False);
     2295  Menu := MenuGameSlots;
     2296end;
     2297
     2298procedure TEngine.MenuItemGameSave(Sender: TObject);
     2299begin
     2300  ReloadGameSlots(True);
     2301  Menu := MenuGameSlots;
     2302end;
     2303
     2304procedure TEngine.MenuItemGameSlotLoad(Sender: TObject);
     2305begin
     2306  LoadFromFile(GetGameSlotFileName(Integer(TMenuItemButton(Sender).Ref)));
    22902307  State := gsRunning;
    22912308end;
    22922309
    2293 procedure TEngine.MenuItemGameSave(Sender: TObject);
    2294 begin
    2295   SaveToFile(SavedGameFileName);
     2310procedure TEngine.MenuItemGameSlotSave(Sender: TObject);
     2311begin
     2312  SaveToFile(GetGameSlotFileName(Integer(TMenuItemButton(Sender).Ref)));
    22962313  State := LastState;
    22972314end;
     
    24262443    miTerminal: Inc(AvailableTerminals);
    24272444  end;
     2445end;
     2446
     2447procedure TEngine.ReloadGameSlots(Save: Boolean);
     2448var
     2449  I: Integer;
     2450  Action: TNotifyEvent;
     2451  FileName: string;
     2452  FileDateTime: TDateTime;
     2453  ControlName: string;
     2454const
     2455  GameSlotCount = 6;
     2456begin
     2457  with MenuGameSlots, Items do begin
     2458    Clear;
     2459    Parent := Menu;
     2460    if Save then Action := MenuItemGameSlotSave
     2461      else Action := MenuItemGameSlotLoad;
     2462    for I := 0 to GameSlotCount - 1 do begin
     2463      FileName := GetGameSlotFileName(Integer(I));
     2464      ControlName := SSlot + ' ' + IntToStr(I);
     2465      if FileExists(FileName) then begin
     2466        FileAge(FileName, FileDateTime);
     2467        ControlName := ControlName + ' (' + DateTimeToStr(FileDateTime) + ')';
     2468      end;
     2469      with AddButton(ControlName, Action) do begin
     2470        Ref := TObject(I);
     2471        Enabled := FileExists(FileName) or Save;
     2472        TextSize := 40;
     2473        TextColor := Colors.MenuItemText;
     2474        TextDisabledColor := Colors.MenuItemDisabledText;
     2475        BackgroundColor := Colors.MenuItemBackground;
     2476        BackgroundSelectedColor := Colors.MenuItemBackgroundSelected;
     2477      end;
     2478    end;
     2479    with AddButton(SExit, MenuItemBack) do begin
     2480      TextSize := 40;
     2481      TextColor := Colors.MenuItemText;
     2482      TextDisabledColor := Colors.MenuItemDisabledText;
     2483      BackgroundColor := Colors.MenuItemBackground;
     2484      BackgroundSelectedColor := Colors.MenuItemBackgroundSelected;
     2485    end;
     2486    OnExit := MenuItemBack;
     2487  end;
     2488end;
     2489
     2490function TEngine.GetGameSlotFileName(Index: Integer): string;
     2491begin
     2492  Result := GetAppConfigDir(False) + 'Saved game ' + IntToStr(Index) + TransLinesExt;
    24282493end;
    24292494
     
    24702535    end;
    24712536    with AddButton(SLoad, MenuItemGameLoad) do begin
    2472       Enabled := FileExists(SavedGameFileName);
    24732537      TextSize := 40;
    24742538      TextColor := Colors.MenuItemText;
     
    25272591    end;
    25282592    with AddButton(SLoad, MenuItemGameLoad) do begin
    2529       Enabled := FileExists(SavedGameFileName);
    25302593      TextSize := 40;
    25312594      TextColor := Colors.MenuItemText;
     
    39574020  FTime := ReadDateTime(Node, 'Time', 0);
    39584021  FLastTime := FTime;
     4022  City := Cities.SearchBySysName(ReadString(Node, 'CityName', ''));
    39594023
    39604024  NewNode := Node.FindNode(DOMString(TView.GetClassSysName));
     
    40074071  WriteInteger(Node, 'State', Integer(State));
    40084072  WriteDateTime(Node, 'Time', FTime);
     4073  if Assigned(City) then WriteString(Node, 'CityName', City.SysName);
    40094074
    40104075  NewNode := Node.OwnerDocument.CreateElement(DOMString(TView.GetClassSysName));
     
    40784143begin
    40794144  inherited;
    4080   SavedGameFileName := GetAppConfigDir(False) + 'LastGameState' + TransLinesExt;
    40814145  MovableTracks := True;
    40824146  Colors := TColors.Create;
     
    40954159  MenuGame := TMenu.Create;
    40964160  MenuCustomGame := TMenu.Create;
     4161  MenuGameSlots := TMenu.Create;
    40974162  Menu := MenuMain;
    40984163  InitMenus;
     
    41594224  FreeAndNil(MenuCareer);
    41604225  FreeAndNil(MenuGame);
     4226  FreeAndNil(MenuGameSlots);
    41614227  FreeAndNil(Cities);
    41624228  FreeAndNil(ButtonBack);
Note: See TracChangeset for help on using the changeset viewer.