source: tags/1.4.0/Building.pas

Last change on this file was 409, checked in by chronos, 2 weeks ago

Merged revision(s) 403-408 from trunk:

  • Added: Cover image.
  • Fixed: Crash on cyclic map new move creation.
  • Modified: TView class moved into separate unit.
  • Added: French translation.
  • Fixed: Do not stop running game on Quit action.
  • Fixed: Cell cities were not correctly stored the saved game.
  • Fixed: ItemList references were loaded by item index instead of item id.
  • Fixed: Wrong default map image path initialization.
File size: 5.2 KB
Line 
1unit Building;
2
3interface
4
5uses
6 Classes, SysUtils, ItemList;
7
8type
9 TBuildingSpecialType = (stNone, stCity);
10
11 { TBuildingKind }
12
13 TBuildingKind = class(TItem)
14 Cost: Integer;
15 SpecialType: TBuildingSpecialType;
16 BonusAttack: Integer;
17 BonusDefense: Integer;
18 BonusViewRange: Integer;
19 BonusGrow: Integer;
20 class function GetFields: TItemFields; override;
21 procedure GetValue(Index: Integer; out Value); override;
22 procedure SetValue(Index: Integer; var Value); override;
23 class function GetClassSysName: string; override;
24 class function GetClassName: string; override;
25 end;
26
27 { TBuildingKinds }
28
29 TBuildingKinds = class(TItemList<TBuildingKind>)
30 function FindBySpecialType(SpecialType: TBuildingSpecialType): TBuildingKind;
31 end;
32
33 { TBuilding }
34
35 TBuilding = class(TItem)
36 private
37 FMapCell: TObject;
38 procedure SetMapCell(AValue: TObject);
39 public
40 Kind: TBuildingKind;
41 Game: TObject; // TGame
42 class function GetFields: TItemFields; override;
43 procedure GetValue(Index: Integer; out Value); override;
44 procedure SetValue(Index: Integer; var Value); override;
45 function GetReferenceList(Index: Integer): TBaseItemList; override;
46 procedure Assign(Source: TItem); override;
47 property MapCell: TObject read FMapCell write SetMapCell; // TMapCell;
48 end;
49
50 { TBuildings }
51
52 TBuildings = class(TItemList<TBuilding>)
53 Game: TObject; // TGame;
54 function CreateItem(Name: string = ''): TBuilding; override;
55 procedure Assign(Source: TItemList<TBuilding>); override;
56 end;
57
58resourcestring
59 SBuildingKind = 'Building';
60 SCost = 'Cost';
61 SSpecialType = 'Special type';
62 SNone = 'None';
63 SCity = 'City';
64 SBonusAttack = 'Unit attack bonus';
65 SBonusDefense = 'Unit defense bonus';
66 SBonusViewRange = 'Unit view range bonus';
67 SBonusGrow = 'Unit grow bonus';
68
69
70implementation
71
72uses
73 Map, Game;
74
75{ TBuilding }
76
77procedure TBuilding.SetMapCell(AValue: TObject);
78var
79 OldValue: TCell;
80begin
81 if FMapCell = AValue then Exit;
82 OldValue := TCell(FMapCell);
83 FMapCell := nil;
84 if Assigned(OldValue) then TCell(OldValue).Building := nil;
85 FMapCell := AValue;
86 if Assigned(FMapCell) then TCell(FMapCell).Building := Self;
87end;
88
89class function TBuilding.GetFields: TItemFields;
90var
91 Field: TItemField;
92begin
93 Result := inherited;
94 Field := Result.AddField(2, 'Kind', SBuildingKind, dtReference);
95end;
96
97procedure TBuilding.GetValue(Index: Integer; out Value);
98begin
99 if Index = 1 then string(Value) := Name
100 else if Index = 2 then TBuildingKind(Value) := Kind
101 else inherited;
102end;
103
104procedure TBuilding.SetValue(Index: Integer; var Value);
105begin
106 if Index = 1 then Name := string(Value)
107 else if Index = 2 then Kind := TBuildingKind(Value)
108 else inherited;
109end;
110
111function TBuilding.GetReferenceList(Index: Integer): TBaseItemList;
112begin
113 if Index = 2 then Result := TGame(Game).GameSystem.BuildingKinds.BaseItemList
114 else Result := nil;
115end;
116
117procedure TBuilding.Assign(Source: TItem);
118begin
119 inherited;
120 Kind := TBuilding(Source).Kind;
121end;
122
123{ TBuildings }
124
125function TBuildings.CreateItem(Name: string): TBuilding;
126begin
127 Result := inherited;
128 Result.Game := Game;
129end;
130
131procedure TBuildings.Assign(Source: TItemList<TBuilding>);
132var
133 I: Integer;
134begin
135 inherited;
136 for I := 0 to Count - 1 do
137 Items[I].Game := Game;
138end;
139
140{ TBuildingKind }
141
142class function TBuildingKind.GetFields: TItemFields;
143var
144 Field: TItemField;
145begin
146 Result := inherited;
147 Result.AddField(2, 'Cost', SCost, dtInteger);
148 Field := Result.AddField(3, 'SpecialType', SSpecialType, dtEnumeration);
149 Field.EnumStates.Add(SNone);
150 Field.EnumStates.Add(SCity);
151 Result.AddField(4, 'BonusAttack', SBonusAttack, dtInteger);
152 Result.AddField(5, 'BonusDefense', SBonusDefense, dtInteger);
153 Result.AddField(6, 'BonusViewRange', SBonusViewRange, dtInteger);
154 Result.AddField(7, 'BonusGrow', SBonusGrow, dtInteger);
155end;
156
157procedure TBuildingKind.GetValue(Index: Integer; out Value);
158begin
159 if Index = 1 then string(Value) := Name
160 else if Index = 2 then Integer(Value) := Cost
161 else if Index = 3 then TBuildingSpecialType(Value) := SpecialType
162 else if Index = 4 then Integer(Value) := BonusAttack
163 else if Index = 5 then Integer(Value) := BonusDefense
164 else if Index = 6 then Integer(Value) := BonusViewRange
165 else if Index = 7 then Integer(Value) := BonusGrow
166 else inherited;
167end;
168
169procedure TBuildingKind.SetValue(Index: Integer; var Value);
170begin
171 if Index = 1 then Name := string(Value)
172 else if Index = 2 then Cost := Integer(Value)
173 else if Index = 3 then SpecialType := TBuildingSpecialType(Value)
174 else if Index = 4 then BonusAttack := Integer(Value)
175 else if Index = 5 then BonusDefense := Integer(Value)
176 else if Index = 6 then BonusViewRange := Integer(Value)
177 else if Index = 7 then BonusGrow := Integer(Value)
178 else inherited;
179end;
180
181class function TBuildingKind.GetClassSysName: string;
182begin
183 Result := 'BuildingKind';
184end;
185
186class function TBuildingKind.GetClassName: string;
187begin
188 Result := SBuildingKind;
189end;
190
191{ TBuildingKinds }
192
193function TBuildingKinds.FindBySpecialType(SpecialType: TBuildingSpecialType
194 ): TBuildingKind;
195var
196 I: Integer;
197begin
198 I := 0;
199 while (I < Count) and (Items[I].SpecialType <> SpecialType) do Inc(I);
200 if I < Count then Result := Items[I]
201 else Result := nil;
202end;
203
204end.
205
Note: See TracBrowser for help on using the repository browser.