Changeset 291 for trunk/UUnit.pas
- Timestamp:
- Mar 26, 2019, 1:16:27 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UUnit.pas
r278 r291 6 6 7 7 uses 8 Classes, SysUtils, fgl, UGeometry, DOM, UXMLUtils ;8 Classes, SysUtils, fgl, UGeometry, DOM, UXMLUtils, UItemList; 9 9 10 10 type … … 12 12 { TUnitKind } 13 13 14 TUnitKind = class 15 Id: Integer; 16 Name: string; 14 TUnitKind = class(TItem) 17 15 PowerAttack: Integer; 18 16 PowerDefense: Integer; … … 21 19 Power: Integer; 22 20 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; 26 26 end; 27 27 28 28 { TUnitKinds } 29 29 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) 37 31 constructor Create(FreeObjects: Boolean = True); 38 procedure Assign(Source: TUnitKinds);32 class function GetItemClass: TItemClass; override; 39 33 end; 40 34 … … 89 83 UPlayer, UMap, UGame; 90 84 85 resourcestring 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 91 95 { TUnitKind } 92 96 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); 97 class function TUnitKind.GetFields: TItemFields; 98 begin 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); 106 end; 107 108 procedure TUnitKind.GetValue(Index: Integer; out Value); 109 begin 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)); 118 end; 119 120 procedure TUnitKind.SetValue(Index: Integer; var Value); 121 begin 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)); 130 end; 131 132 class function TUnitKind.GetClassSysName: string; 133 begin 134 Result := 'UnitKind'; 135 end; 136 137 class function TUnitKind.GetClassName: string; 138 begin 139 Result := SUnitKind; 127 140 end; 128 141 … … 243 256 begin 244 257 Player := TGame(Game).Players.FindById(PlayerId); 245 Kind := T Game(Game).GameSystem.UnitKinds.FindById(KindId);258 Kind := TUnitKind(TGame(Game).GameSystem.UnitKinds.FindById(KindId)); 246 259 end; 247 260 … … 296 309 { TUnitKinds } 297 310 298 function TUnitKinds.FindById(Id: Integer): TUnitKind;299 var300 I: Integer;301 begin302 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 begin310 Result := NewId;311 Inc(NewId);312 end;313 314 function TUnitKinds.AddNew(Name: string): TUnitKind;315 begin316 Result := TUnitKind.Create;317 Result.Name := Name;318 Result.Id := GetNewId;319 Add(Result);320 end;321 322 procedure TUnitKinds.LoadFromNode(Node: TDOMNode);323 var324 Node2: TDOMNode;325 NewItem: TUnitKind;326 begin327 Count := 0;328 Node2 := Node.FirstChild;329 while Assigned(Node2) and (Node2.NodeName = 'UnitKind') do begin330 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 var339 I: Integer;340 NewNode2: TDOMNode;341 begin342 for I := 0 to Count - 1 do343 with Items[I] do begin344 NewNode2 := Node.OwnerDocument.CreateElement('UnitKind');345 Node.AppendChild(NewNode2);346 SaveToNode(NewNode2);347 end;348 end;349 350 311 constructor TUnitKinds.Create(FreeObjects: Boolean); 351 312 begin 352 313 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]); 314 end; 315 316 class function TUnitKinds.GetItemClass: TItemClass; 317 begin 318 Result := TUnitKind; 364 319 end; 365 320
Note:
See TracChangeset
for help on using the changeset viewer.