1 | unit Units;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, Generics.Collections, Geometry, DOM, XML, ItemList, UnitKind;
|
---|
7 |
|
---|
8 | type
|
---|
9 | TUnit = class
|
---|
10 | private
|
---|
11 | FId: Integer;
|
---|
12 | FMapCell: TObject; // TCell
|
---|
13 | FPlayer: TObject; // TPlayer
|
---|
14 | procedure SetId(AValue: Integer);
|
---|
15 | procedure SetMapCell(AValue: TObject);
|
---|
16 | procedure SetPlayer(AValue: TObject);
|
---|
17 | public
|
---|
18 | Game: TObject; // TGame
|
---|
19 | Kind: TUnitKind;
|
---|
20 | KindId: Integer; // Temporary
|
---|
21 | Power: Integer;
|
---|
22 | Moves: Integer;
|
---|
23 | Position: TPoint;
|
---|
24 | PlayerId: Integer; // Temporary
|
---|
25 | procedure FixRefId;
|
---|
26 | function GetPowerAttack: Integer;
|
---|
27 | function GetPowerDefense: Integer;
|
---|
28 | procedure LoadFromNode(Node: TDOMNode);
|
---|
29 | procedure SaveToNode(Node: TDOMNode);
|
---|
30 | procedure Assign(Source: TUnit);
|
---|
31 | constructor Create;
|
---|
32 | destructor Destroy; override;
|
---|
33 | property Player: TObject read FPlayer write SetPlayer; // TPlayer
|
---|
34 | property MapCell: TObject read FMapCell write SetMapCell; // TMapCell;
|
---|
35 | property Id: Integer read FId write SetId;
|
---|
36 | end;
|
---|
37 |
|
---|
38 | { TUnits }
|
---|
39 |
|
---|
40 | TUnits = class(TObjectList<TUnit>)
|
---|
41 | NewId: Integer;
|
---|
42 | Game: TObject; //TGame;
|
---|
43 | procedure RecalculateItemsId;
|
---|
44 | procedure FixRefId;
|
---|
45 | function FindById(Id: Integer): TUnit;
|
---|
46 | function GetNewId: Integer;
|
---|
47 | procedure LoadFromNode(Node: TDOMNode);
|
---|
48 | procedure SaveToNode(Node: TDOMNode);
|
---|
49 | function AddNew(Kind: TUnitKind; Power: Integer): TUnit;
|
---|
50 | constructor Create(FreeObjects: Boolean = True);
|
---|
51 | procedure Assign(Source: TUnits);
|
---|
52 | end;
|
---|
53 |
|
---|
54 |
|
---|
55 | implementation
|
---|
56 |
|
---|
57 | uses
|
---|
58 | Player, Map, Game;
|
---|
59 |
|
---|
60 | { TUnits }
|
---|
61 |
|
---|
62 | procedure TUnits.RecalculateItemsId;
|
---|
63 | var
|
---|
64 | I: Integer;
|
---|
65 | begin
|
---|
66 | for I := 0 to Count - 1 do
|
---|
67 | Items[I].Id := I + 1;
|
---|
68 | NewId := Count + 1;
|
---|
69 | end;
|
---|
70 |
|
---|
71 | procedure TUnits.FixRefId;
|
---|
72 | var
|
---|
73 | I: Integer;
|
---|
74 | begin
|
---|
75 | for I := 0 to Count - 1 do
|
---|
76 | Items[I].FixRefId;
|
---|
77 | end;
|
---|
78 |
|
---|
79 | function TUnits.FindById(Id: Integer): TUnit;
|
---|
80 | var
|
---|
81 | I: Integer;
|
---|
82 | begin
|
---|
83 | I := 0;
|
---|
84 | while (I < Count) and (Items[I].Id <> Id) do Inc(I);
|
---|
85 | if I < Count then Result := Items[I]
|
---|
86 | else Result := nil;
|
---|
87 | end;
|
---|
88 |
|
---|
89 | function TUnits.GetNewId: Integer;
|
---|
90 | begin
|
---|
91 | Result := NewId;
|
---|
92 | Inc(NewId);
|
---|
93 | end;
|
---|
94 |
|
---|
95 | procedure TUnits.LoadFromNode(Node: TDOMNode);
|
---|
96 | var
|
---|
97 | Node2: TDOMNode;
|
---|
98 | NewItem: TUnit;
|
---|
99 | begin
|
---|
100 | Count := 0;
|
---|
101 | Node2 := Node.FirstChild;
|
---|
102 | while Assigned(Node2) and (Node2.NodeName = 'Unit') do begin
|
---|
103 | NewItem := TUnit.Create;
|
---|
104 | NewItem.Game := Game;
|
---|
105 | NewItem.LoadFromNode(Node2);
|
---|
106 | Add(NewItem);
|
---|
107 | Node2 := Node2.NextSibling;
|
---|
108 | end;
|
---|
109 | end;
|
---|
110 |
|
---|
111 | procedure TUnits.SaveToNode(Node: TDOMNode);
|
---|
112 | var
|
---|
113 | I: Integer;
|
---|
114 | NewNode2: TDOMNode;
|
---|
115 | begin
|
---|
116 | for I := 0 to Count - 1 do
|
---|
117 | with Items[I] do begin
|
---|
118 | NewNode2 := Node.OwnerDocument.CreateElement('Unit');
|
---|
119 | Node.AppendChild(NewNode2);
|
---|
120 | SaveToNode(NewNode2);
|
---|
121 | end;
|
---|
122 | end;
|
---|
123 |
|
---|
124 | function TUnits.AddNew(Kind: TUnitKind; Power: Integer): TUnit;
|
---|
125 | begin
|
---|
126 | Result := TUnit.Create;
|
---|
127 | Result.Id := GetNewId;
|
---|
128 | Result.Kind := Kind;
|
---|
129 | Result.Power := Power;
|
---|
130 | Result.Game := Game;
|
---|
131 | Add(Result);
|
---|
132 | end;
|
---|
133 |
|
---|
134 | constructor TUnits.Create(FreeObjects: Boolean);
|
---|
135 | begin
|
---|
136 | inherited;
|
---|
137 | NewId := 1;
|
---|
138 | end;
|
---|
139 |
|
---|
140 | procedure TUnits.Assign(Source: TUnits);
|
---|
141 | var
|
---|
142 | I: Integer;
|
---|
143 | begin
|
---|
144 | while Count > Source.Count do
|
---|
145 | Delete(Count - 1);
|
---|
146 | while Count < Source.Count do
|
---|
147 | Add(TUnit.Create);
|
---|
148 | for I := 0 to Count - 1 do begin
|
---|
149 | Items[I].Assign(Source[I]);
|
---|
150 | Items[I].Game := Game;
|
---|
151 | end;
|
---|
152 | NewId := Source.NewId;
|
---|
153 | end;
|
---|
154 |
|
---|
155 | { TUnit }
|
---|
156 |
|
---|
157 | procedure TUnit.SetId(AValue: Integer);
|
---|
158 | begin
|
---|
159 | if FId = AValue then Exit;
|
---|
160 | FId := AValue;
|
---|
161 | end;
|
---|
162 |
|
---|
163 | procedure TUnit.SetMapCell(AValue: TObject);
|
---|
164 | var
|
---|
165 | OldValue: TCell;
|
---|
166 | begin
|
---|
167 | if FMapCell = AValue then Exit;
|
---|
168 | OldValue := TCell(FMapCell);
|
---|
169 | FMapCell := nil;
|
---|
170 | if Assigned(OldValue) then OldValue.OneUnit := nil;
|
---|
171 | FMapCell := AValue;
|
---|
172 | if Assigned(FMapCell) then TCell(FMapCell).OneUnit := Self;
|
---|
173 | end;
|
---|
174 |
|
---|
175 | procedure TUnit.SetPlayer(AValue: TObject);
|
---|
176 | var
|
---|
177 | OldValue: TPlayer;
|
---|
178 | begin
|
---|
179 | if FPlayer = AValue then Exit;
|
---|
180 | OldValue := TPlayer(FPlayer);
|
---|
181 | FPlayer := nil;
|
---|
182 | if Assigned(OldValue) then TPlayer(OldValue).Units.Remove(Self);
|
---|
183 | FPlayer := AValue;
|
---|
184 | if Assigned(FPlayer) then TPlayer(FPlayer).Units.Add(Self);
|
---|
185 | end;
|
---|
186 |
|
---|
187 | procedure TUnit.FixRefId;
|
---|
188 | begin
|
---|
189 | if PlayerId <> 0 then begin
|
---|
190 | Player := TGame(Game).Players.FindById(PlayerId);
|
---|
191 | if not Assigned(Player) then
|
---|
192 | raise Exception.Create('Referenced player id ' + IntToStr(PlayerId) + ' not found.');
|
---|
193 | end else Player := nil;
|
---|
194 |
|
---|
195 | if KindId <> 0 then begin
|
---|
196 | Kind := TGame(Game).GameSystem.UnitKinds.FindById(KindId);
|
---|
197 | if not Assigned(Kind) then
|
---|
198 | raise Exception.Create('Referenced unit kind id ' + IntToStr(KindId) + ' not found.');
|
---|
199 | end else Kind := nil;
|
---|
200 | end;
|
---|
201 |
|
---|
202 | function TUnit.GetPowerAttack: Integer;
|
---|
203 | begin
|
---|
204 | Result := Power * Kind.PowerAttack;
|
---|
205 | end;
|
---|
206 |
|
---|
207 | function TUnit.GetPowerDefense: Integer;
|
---|
208 | begin
|
---|
209 | Result := Power * Kind.PowerDefense;
|
---|
210 | end;
|
---|
211 |
|
---|
212 | procedure TUnit.LoadFromNode(Node: TDOMNode);
|
---|
213 | begin
|
---|
214 | Id := ReadInteger(Node, 'Id', 0);
|
---|
215 | Power := ReadInteger(Node, 'Power', 0);
|
---|
216 | Moves := ReadInteger(Node, 'Moves', 0);
|
---|
217 | KindId := ReadInteger(Node, 'Kind', 0);
|
---|
218 | PlayerId := ReadInteger(Node, 'Player', 0);
|
---|
219 | end;
|
---|
220 |
|
---|
221 | procedure TUnit.SaveToNode(Node: TDOMNode);
|
---|
222 | begin
|
---|
223 | WriteInteger(Node, 'Id', Id);
|
---|
224 | WriteInteger(Node, 'Power', Power);
|
---|
225 | WriteInteger(Node, 'Moves', Moves);
|
---|
226 | if Assigned(Kind) then WriteInteger(Node, 'Kind', Kind.Id)
|
---|
227 | else WriteInteger(Node, 'Kind', 0);
|
---|
228 | if Assigned(Player) then WriteInteger(Node, 'Player', TPlayer(Player).Id)
|
---|
229 | else WriteInteger(Node, 'Player', 0);
|
---|
230 | end;
|
---|
231 |
|
---|
232 | procedure TUnit.Assign(Source: TUnit);
|
---|
233 | begin
|
---|
234 | Kind := Source.Kind;
|
---|
235 | Moves := Source.Moves;
|
---|
236 | Power := Source.Power;
|
---|
237 | Id := Source.Id;
|
---|
238 | end;
|
---|
239 |
|
---|
240 | constructor TUnit.Create;
|
---|
241 | begin
|
---|
242 | Player := nil;
|
---|
243 | end;
|
---|
244 |
|
---|
245 | destructor TUnit.Destroy;
|
---|
246 | begin
|
---|
247 | MapCell := nil;
|
---|
248 | Player := nil;
|
---|
249 | inherited;
|
---|
250 | end;
|
---|
251 |
|
---|
252 | end.
|
---|
253 |
|
---|