source: trunk/Building.pas@ 403

Last change on this file since 403 was 344, checked in by chronos, 5 months ago
  • Modified: Improved implementation of TItemList class to be generic class to avoid explicit typecasting.
File size: 3.8 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 procedure Assign(Source: TItem); override;
42 property MapCell: TObject read FMapCell write SetMapCell; // TMapCell;
43 end;
44
45 { TBuildings }
46
47 TBuildings = class(TItemList<TBuilding>)
48 Game: TObject; // TGame;
49 end;
50
51resourcestring
52 SBuildingKind = 'Building';
53 SCost = 'Cost';
54 SSpecialType = 'Special type';
55 SNone = 'None';
56 SCity = 'City';
57 SBonusAttack = 'Unit attack bonus';
58 SBonusDefense = 'Unit defense bonus';
59 SBonusViewRange = 'Unit view range bonus';
60 SBonusGrow = 'Unit grow bonus';
61
62
63implementation
64
65uses
66 Map;
67
68{ TBuilding }
69
70procedure TBuilding.SetMapCell(AValue: TObject);
71var
72 OldValue: TCell;
73begin
74 if FMapCell = AValue then Exit;
75 OldValue := TCell(FMapCell);
76 FMapCell := nil;
77 if Assigned(OldValue) then TCell(OldValue).Building := nil;
78 FMapCell := AValue;
79 if Assigned(FMapCell) then TCell(FMapCell).Building := Self;
80end;
81
82procedure TBuilding.Assign(Source: TItem);
83begin
84 inherited;
85 Kind := TBuilding(Source).Kind;
86end;
87
88{ TBuildingKind }
89
90class function TBuildingKind.GetFields: TItemFields;
91var
92 Field: TItemField;
93begin
94 Result := inherited;
95 Result.AddField(2, 'Cost', SCost, dtInteger);
96 Field := Result.AddField(3, 'SpecialType', SSpecialType, dtEnumeration);
97 Field.EnumStates.Add(SNone);
98 Field.EnumStates.Add(SCity);
99 Result.AddField(4, 'BonusAttack', SBonusAttack, dtInteger);
100 Result.AddField(5, 'BonusDefense', SBonusDefense, dtInteger);
101 Result.AddField(6, 'BonusViewRange', SBonusViewRange, dtInteger);
102 Result.AddField(7, 'BonusGrow', SBonusGrow, dtInteger);
103end;
104
105procedure TBuildingKind.GetValue(Index: Integer; out Value);
106begin
107 if Index = 1 then string(Value) := Name
108 else if Index = 2 then Integer(Value) := Cost
109 else if Index = 3 then TBuildingSpecialType(Value) := SpecialType
110 else if Index = 4 then Integer(Value) := BonusAttack
111 else if Index = 5 then Integer(Value) := BonusDefense
112 else if Index = 6 then Integer(Value) := BonusViewRange
113 else if Index = 7 then Integer(Value) := BonusGrow
114 else inherited;
115end;
116
117procedure TBuildingKind.SetValue(Index: Integer; var Value);
118begin
119 if Index = 1 then Name := string(Value)
120 else if Index = 2 then Cost := Integer(Value)
121 else if Index = 3 then SpecialType := TBuildingSpecialType(Value)
122 else if Index = 4 then BonusAttack := Integer(Value)
123 else if Index = 5 then BonusDefense := Integer(Value)
124 else if Index = 6 then BonusViewRange := Integer(Value)
125 else if Index = 7 then BonusGrow := Integer(Value)
126 else inherited;
127end;
128
129class function TBuildingKind.GetClassSysName: string;
130begin
131 Result := 'BuildingKind';
132end;
133
134class function TBuildingKind.GetClassName: string;
135begin
136 Result := SBuildingKind;
137end;
138
139{ TBuildingKinds }
140
141function TBuildingKinds.FindBySpecialType(SpecialType: TBuildingSpecialType
142 ): TBuildingKind;
143var
144 I: Integer;
145begin
146 I := 0;
147 while (I < Count) and (Items[I].SpecialType <> SpecialType) do Inc(I);
148 if I < Count then Result := Items[I]
149 else Result := nil;
150end;
151
152end.
153
Note: See TracBrowser for help on using the repository browser.