source: trunk/Building.pas

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