Changeset 291 for trunk/UUnit.pas


Ignore:
Timestamp:
Mar 26, 2019, 1:16:27 PM (6 years ago)
Author:
chronos
Message:
  • Modified: UnitKind list and items converted to general item list.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UUnit.pas

    r278 r291  
    66
    77uses
    8   Classes, SysUtils, fgl, UGeometry, DOM, UXMLUtils;
     8  Classes, SysUtils, fgl, UGeometry, DOM, UXMLUtils, UItemList;
    99
    1010type
     
    1212  { TUnitKind }
    1313
    14   TUnitKind = class
    15     Id: Integer;
    16     Name: string;
     14  TUnitKind = class(TItem)
    1715    PowerAttack: Integer;
    1816    PowerDefense: Integer;
     
    2119    Power: Integer;
    2220    ViewRange: Integer;
    23     procedure Assign(Source: TUnitKind);
    24     procedure LoadFromNode(Node: TDOMNode);
    25     procedure SaveToNode(Node: TDOMNode);
     21    class function GetFields: TItemFields; override;
     22    procedure GetValue(Index: Integer; out Value); override;
     23    procedure SetValue(Index: Integer; var Value); override;
     24    class function GetClassSysName: string; override;
     25    class function GetClassName: string; override;
    2626  end;
    2727
    2828  { TUnitKinds }
    2929
    30   TUnitKinds = class(TFPGObjectList<TUnitKind>)
    31     NewId: Integer;
    32     function FindById(Id: Integer): TUnitKind;
    33     function GetNewId: Integer;
    34     function AddNew(Name: string): TUnitKind;
    35     procedure LoadFromNode(Node: TDOMNode);
    36     procedure SaveToNode(Node: TDOMNode);
     30  TUnitKinds = class(TItemList)
    3731    constructor Create(FreeObjects: Boolean = True);
    38     procedure Assign(Source: TUnitKinds);
     32    class function GetItemClass: TItemClass; override;
    3933  end;
    4034
     
    8983  UPlayer, UMap, UGame;
    9084
     85resourcestring
     86  SStack = 'Stack';
     87  SCost = 'Cost';
     88  SMoves = 'Moves';
     89  SPowerAttack =  'Attack power';
     90  SPowerDefense = 'Defense power';
     91  SViewRange = 'View range';
     92  SUnitKind = 'Unit kind';
     93
     94
    9195{ TUnitKind }
    9296
    93 procedure TUnitKind.Assign(Source: TUnitKind);
    94 begin
    95   Id := Source.Id;
    96   Name := Source.Name;
    97   PowerAttack := Source.PowerAttack;
    98   PowerDefense := Source.PowerDefense;
    99   Moves := Source.Moves;
    100   Cost := Source.Cost;
    101   Power := Source.Power;
    102   ViewRange := Source.ViewRange;
    103 end;
    104 
    105 procedure TUnitKind.LoadFromNode(Node: TDOMNode);
    106 begin
    107   Id := ReadInteger(Node, 'Id', 0);
    108   Name := ReadString(Node, 'Name', '');
    109   Power := ReadInteger(Node, 'Stack', 0);
    110   Cost := ReadInteger(Node, 'Cost', 0);
    111   Moves := ReadInteger(Node, 'Moves', 0);
    112   PowerAttack := ReadInteger(Node, 'PowerAttack', 0);
    113   PowerDefense := ReadInteger(Node, 'PowerDefense', 0);
    114   ViewRange := ReadInteger(Node, 'ViewRange', 0);
    115 end;
    116 
    117 procedure TUnitKind.SaveToNode(Node: TDOMNode);
    118 begin
    119   WriteInteger(Node, 'Id', Id);
    120   WriteString(Node, 'Name', Name);
    121   WriteInteger(Node, 'Stack', Power);
    122   WriteInteger(Node, 'Cost', Cost);
    123   WriteInteger(Node, 'Moves', Moves);
    124   WriteInteger(Node, 'PowerAttack', PowerAttack);
    125   WriteInteger(Node, 'PowerDefense', PowerDefense);
    126   WriteInteger(Node, 'ViewRange', ViewRange);
     97class function TUnitKind.GetFields: TItemFields;
     98begin
     99  Result := inherited;
     100  Result.AddField(2, 'Stack', SStack, dtInteger);
     101  Result.AddField(3, 'Cost', SCost, dtInteger);
     102  Result.AddField(4, 'Moves', SMoves, dtInteger);
     103  Result.AddField(5, 'PowerAttack', SPowerAttack, dtInteger);
     104  Result.AddField(6, 'PowerDefense', SPowerDefense, dtInteger);
     105  Result.AddField(7, 'ViewRange', SViewRange, dtInteger);
     106end;
     107
     108procedure TUnitKind.GetValue(Index: Integer; out Value);
     109begin
     110  if Index = 1 then string(Value) := Name
     111  else if Index = 2 then Integer(Value) := Power
     112  else if Index = 3 then Integer(Value) := Cost
     113  else if Index = 4 then Integer(Value) := Moves
     114  else if Index = 5 then Integer(Value) := PowerAttack
     115  else if Index = 6 then Integer(Value) := PowerDefense
     116  else if Index = 7 then Integer(Value) := ViewRange
     117  else raise Exception.Create('Unsupported value index ' + IntToStr(Index));
     118end;
     119
     120procedure TUnitKind.SetValue(Index: Integer; var Value);
     121begin
     122  if Index = 1 then Name := string(Value)
     123  else if Index = 2 then Power := Integer(Value)
     124  else if Index = 3 then Cost := Integer(Value)
     125  else if Index = 4 then Moves := Integer(Value)
     126  else if Index = 5 then PowerAttack := Integer(Value)
     127  else if Index = 6 then PowerDefense := Integer(Value)
     128  else if Index = 7 then ViewRange := Integer(Value)
     129  else raise Exception.Create('Unsupported value index ' + IntToStr(Index));
     130end;
     131
     132class function TUnitKind.GetClassSysName: string;
     133begin
     134  Result := 'UnitKind';
     135end;
     136
     137class function TUnitKind.GetClassName: string;
     138begin
     139  Result := SUnitKind;
    127140end;
    128141
     
    243256begin
    244257  Player := TGame(Game).Players.FindById(PlayerId);
    245   Kind := TGame(Game).GameSystem.UnitKinds.FindById(KindId);
     258  Kind := TUnitKind(TGame(Game).GameSystem.UnitKinds.FindById(KindId));
    246259end;
    247260
     
    296309{ TUnitKinds }
    297310
    298 function TUnitKinds.FindById(Id: Integer): TUnitKind;
    299 var
    300   I: Integer;
    301 begin
    302   I := 0;
    303   while (I < Count) and (Items[I].Id <> Id) do Inc(I);
    304   if I < Count then Result := Items[I]
    305     else Result := nil;
    306 end;
    307 
    308 function TUnitKinds.GetNewId: Integer;
    309 begin
    310   Result := NewId;
    311   Inc(NewId);
    312 end;
    313 
    314 function TUnitKinds.AddNew(Name: string): TUnitKind;
    315 begin
    316   Result := TUnitKind.Create;
    317   Result.Name := Name;
    318   Result.Id := GetNewId;
    319   Add(Result);
    320 end;
    321 
    322 procedure TUnitKinds.LoadFromNode(Node: TDOMNode);
    323 var
    324   Node2: TDOMNode;
    325   NewItem: TUnitKind;
    326 begin
    327   Count := 0;
    328   Node2 := Node.FirstChild;
    329   while Assigned(Node2) and (Node2.NodeName = 'UnitKind') do begin
    330     NewItem := TUnitKind.Create;
    331     NewItem.LoadFromNode(Node2);
    332     Add(NewItem);
    333     Node2 := Node2.NextSibling;
    334   end;
    335 end;
    336 
    337 procedure TUnitKinds.SaveToNode(Node: TDOMNode);
    338 var
    339   I: Integer;
    340   NewNode2: TDOMNode;
    341 begin
    342   for I := 0 to Count - 1 do
    343   with Items[I] do begin
    344     NewNode2 := Node.OwnerDocument.CreateElement('UnitKind');
    345     Node.AppendChild(NewNode2);
    346     SaveToNode(NewNode2);
    347   end;
    348 end;
    349 
    350311constructor TUnitKinds.Create(FreeObjects: Boolean);
    351312begin
    352313  inherited;
    353   NewId := 1;
    354 end;
    355 
    356 procedure TUnitKinds.Assign(Source: TUnitKinds);
    357 var
    358   I: Integer;
    359 begin
    360   while Count > Source.Count do Delete(Count - 1);
    361   while Count < Source.Count do AddNew('');
    362   for I := 0 to Count - 1 do
    363     Items[I].Assign(Source.Items[I]);
     314end;
     315
     316class function TUnitKinds.GetItemClass: TItemClass;
     317begin
     318  Result := TUnitKind;
    364319end;
    365320
Note: See TracChangeset for help on using the changeset viewer.