Changeset 289 for trunk/UBuilding.pas


Ignore:
Timestamp:
Mar 24, 2019, 11:15:07 PM (6 years ago)
Author:
chronos
Message:
  • Added: Allow to add building kinds to game system.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UBuilding.pas

    r265 r289  
    99
    1010type
     11
     12  { TBuildingKind }
     13
    1114  TBuildingKind = class
    1215    Id: Integer;
    1316    Name: string;
    1417    Cost: Integer;
     18    procedure Assign(Source: TBuildingKind);
     19    procedure LoadFromNode(Node: TDOMNode);
     20    procedure SaveToNode(Node: TDOMNode);
    1521  end;
    1622
     
    1925  TBuildingKinds = class(TFPGObjectList<TBuildingKind>)
    2026    function AddNew(Name: string): TBuildingKind;
     27    procedure LoadFromNode(Node: TDOMNode);
     28    procedure SaveToNode(Node: TDOMNode);
     29    procedure Assign(Source: TBuildingKinds);
    2130  end;
    2231
     
    2534  end;
    2635
     36resourcestring
     37  SBuilding = 'Building';
     38
    2739
    2840implementation
     41
     42{ TBuildingKind }
     43
     44procedure TBuildingKind.Assign(Source: TBuildingKind);
     45begin
     46  Name := Source.Name;
     47  Cost := Source.Cost;
     48end;
     49
     50procedure TBuildingKind.LoadFromNode(Node: TDOMNode);
     51begin
     52  Id := ReadInteger(Node, 'Id', 0);
     53  Name := ReadString(Node, 'Name', '');
     54  Cost := ReadInteger(Node, 'Cost', 0);
     55end;
     56
     57procedure TBuildingKind.SaveToNode(Node: TDOMNode);
     58begin
     59  WriteInteger(Node, 'Id', Id);
     60  WriteString(Node, 'Name', Name);
     61  WriteInteger(Node, 'Cost', Cost);
     62end;
    2963
    3064{ TBuildingKinds }
     
    3771end;
    3872
     73procedure TBuildingKinds.LoadFromNode(Node: TDOMNode);
     74var
     75  Node2: TDOMNode;
     76  NewItem: TBuildingKind;
     77begin
     78  Count := 0;
     79  Node2 := Node.FirstChild;
     80  while Assigned(Node2) and (Node2.NodeName = 'BuildingKind') do begin
     81    NewItem := TBuildingKind.Create;
     82    NewItem.LoadFromNode(Node2);
     83    Add(NewItem);
     84    Node2 := Node2.NextSibling;
     85  end;
     86end;
     87
     88procedure TBuildingKinds.SaveToNode(Node: TDOMNode);
     89var
     90  I: Integer;
     91  NewNode2: TDOMNode;
     92begin
     93  for I := 0 to Count - 1 do
     94  with Items[I] do begin
     95    NewNode2 := Node.OwnerDocument.CreateElement('BuildingKind');
     96    Node.AppendChild(NewNode2);
     97    SaveToNode(NewNode2);
     98  end;
     99end;
     100
     101procedure TBuildingKinds.Assign(Source: TBuildingKinds);
     102var
     103  I: Integer;
     104begin
     105  while Count > Source.Count do Delete(Count - 1);
     106  while Count < Source.Count do AddNew('');
     107  for I := 0 to Count - 1 do
     108    Items[I].Assign(Source.Items[I]);
     109end;
     110
    39111end.
    40112
Note: See TracChangeset for help on using the changeset viewer.