This repository has been archived by the owner on Jun 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserFaceManager.pas
309 lines (269 loc) · 6.66 KB
/
UserFaceManager.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
unit UserFaceManager;
{ $DEFINE DEBUG_LOG_USER_UNIT_SELECTION}
{$DEFINE DEBUG_LOG_USER_NAVIGATE}
interface
uses
Classes,
SysUtils,
zgl_math_2d,
zgl_mouse,
zgl_keyboard,
zgl_primitives_2d,
NiceTypes,
NiceExceptions,
LogEntity,
LogEntityFace,
Common,
MapDataCells,
UnitManagerFace,
UnitManager,
MapUnitFace,
UnitFactoryFace,
UnitProduction,
MapScrollManager,
ZenGLFCLGraphics,
BasicTankUnit,
LevelData;
type
{ TUserFace }
TUserFace = class
public
constructor Create;
private
fLog: ILog;
fLevel: TLevelData;
fUnitMan: TUnitManager;
fSelectedUnits: TIMapUnits;
fSlUnitsGlow: LongWord;
fSlUnitsGlowSpeed: integer;
fUpdate100: double;
function GetUnitMan: TUnitManager; inline;
function GetScroll: TMapScrollManager; inline;
procedure Initialize;
procedure AssignDefaults;
procedure DrawChosenUnitRect(const aRect: zglTRect);
procedure DrawChosenUnitFrame(const aUnit: IMapUnit);
procedure DrawChosenUnitFrames;
procedure Update100;
procedure Update100ChUnitsGlow;
procedure ProcessLevelInput(const aTime: double);
function ChooseClick: boolean;
function NavigateClick: boolean;
procedure UserSelect;
procedure UserNavigate;
procedure Finalize;
public
property Log: ILog read fLog;
// This property should be assigned
property Level: TLevelData read fLevel write fLevel;
property UnitMan: TUnitManager read GetUnitMan;
property Scroll: TMapScrollManager read GetScroll;
property SelectedUnits: TIMapUnits read fSelectedUnits;
property SlUnitsGlow: LongWord read fSlUnitsGlow;
procedure Draw;
procedure Update(const aTime: double);
procedure ReceiveInput(const aTime: double);
procedure UserConstructBasicTank;
destructor Destroy; override;
end;
implementation
{ TUserFace }
constructor TUserFace.Create;
begin
inherited Create;
Initialize;
end;
function TUserFace.GetUnitMan: TUnitManager;
begin
result := nil;
if Level = nil then exit;
result := Level.UnitMan;
end;
function TUserFace.GetScroll: TMapScrollManager;
begin
result := nil;
if Level = nil then exit;
if Level.MapView = nil then exit;
result := Level.MapView.Scroll;
end;
procedure TUserFace.Initialize;
begin
AssignDefaults;
fLog := TLog.Create(GlobalLogManager, 'UserFace');
fSelectedUnits := TIMapUnits.Create;
end;
procedure TUserFace.AssignDefaults;
begin
fSlUnitsGlow := $000000;
fSlUnitsGlowSpeed := $111111;
end;
procedure TUserFace.DrawChosenUnitRect(const aRect: zglTRect);
var
x, y, w, h: single;
color: LongWord;
begin
UnpackPRect(@aRect, x, y, w, h);
color := SlUnitsGlow and T6Colors.Red;
pr2d_Rect3(x, y, w, h, color);
end;
procedure TUserFace.DrawChosenUnitFrame(const aUnit: IMapUnit);
var
rect: zglPRect;
screenRect: zglTRect;
begin
rect := aUnit.GraphicalRect;
AssertAssigned(Scroll, 'Scroll', TVariableType.Propertie);
screenRect := Scroll.GlobalToScreen(rect);
DrawChosenUnitRect(screenRect);
end;
procedure TUserFace.DrawChosenUnitFrames;
var
u: IMapUnit;
begin
for u in SelectedUnits do
DrawChosenUnitFrame(u);
end;
procedure TUserFace.Update100;
begin
Update100ChUnitsGlow;
end;
procedure TUserFace.Update100ChUnitsGlow;
begin
fSlUnitsGlow += fSlUnitsGlowSpeed;
if (SlUnitsGlow >= $FFFFFF) or (SlUnitsGlow <= $000000) then
fSlUnitsGlowSpeed := - fSlUnitsGlowSpeed;
end;
procedure TUserFace.ProcessLevelInput(const aTime: double);
begin
if ChooseClick then
UserSelect;
if NavigateClick then
UserNavigate;
if key_Press(K_F4) then
UserConstructBasicTank;
end;
function TUserFace.ChooseClick: boolean;
begin
result :=
mouse_Click(M_BLEFT) // left click
and not key_Down(K_CTRL); // without ctrl
end;
function TUserFace.NavigateClick: boolean;
begin
result := mouse_Click(M_BRIGHT); // right click
end;
procedure TUserFace.UserSelect;
{$DEFINE DEBUG_THIS_PROCEDURE}
procedure LogWrite(const aText: string); inline;
begin
{$IFDEF DEBUG_LOG_USER_UNIT_SELECTION}
Log.Write(aText);
{$ENDIF}
end;
var
u: IMapUnit;
begin
SelectedUnits.Clear;
LogWrite('It appears that user wants to select an unit');
u := Level.UnitMan.UnitAtWindowPoint[mouse_X, mouse_Y];
if u = nil then
begin
LogWrite('But there is no unit at this point');
exit; // no unit to UserSelect
end;
LogWrite('Unit selected. Class: "' + u.Reverse.ClassName + '"');
SelectedUnits.Add(u);
end;
procedure TUserFace.UserNavigate;
procedure LogWrite(const aText: string); inline;
begin
{$IFDEF DEBUG_LOG_USER_NAVIGATE}
Log.Write(aText);
{$ENDIF}
end;
var
u: IMapUnit;
moveable: IMoveableMapUnit;
targetCell: TCellNumber;
once: boolean;
begin
once := false;
targetCell := Scroll.CellNumberAtWindowPoint[mouse_X, mouse_Y];
for u in SelectedUnits do
begin
if u.Reverse is IMoveableMapUnit then
begin
LogWrite('Navigating ' + u.Reverse.ClassName + ' to ' + targetCell.ToText);
moveable := u.Reverse as IMoveableMapUnit;
moveable.Navigate(targetCell);
once := true;
end;
end;
if not once then
LogWrite('No moveable units selected.');
end;
procedure TUserFace.Finalize;
begin
FreeAndNil(fSelectedUnits);
FreeLog(fLog);
end;
procedure TUserFace.Draw;
begin
DrawChosenUnitFrames;
end;
procedure TUserFace.Update(const aTime: double);
begin
fUpdate100 += aTime;
if fUpdate100 >= 100 then
begin
Update100;
fUpdate100 -= 100;
end;
end;
procedure TUserFace.ReceiveInput(const aTime: double);
begin
if Level <> nil then
ProcessLevelInput(aTime);
end;
procedure TUserFace.UserConstructBasicTank;
{$DEFINE DEBUG_THIS}
procedure LogWrite(const aText: string);
begin
{$IFDEF DEBUG_THIS}
Log.Write(aText);
{$ENDIF}
end;
var
u: IMapUnit;
factory: IUnitFactory;
productionItem: TUnitProductionItem;
productionsInitiated: integer;
begin
LogWrite('UserConstructBasicTank called');
productionsInitiated := 0;
if SelectedUnits.Count = 0 then
begin
LogWrite('Can not initiate production: Selected is nothing');
exit;
end;
for u in SelectedUnits do
begin
if not (u.Reverse is IUnitFactory) then continue;
factory := u.Reverse as IUnitFactory;
if not factory.IsVehicleFactory then continue;
productionItem := TUnitProductionItem.Create;
productionItem.Progress := 0;
productionItem.TimeCost := 3000;
productionItem.MapUnit := UnitMan.CreateVehicle(TBasicTank, TBasicTankType) as IMapUnit;
factory.Production.Que.Add(productionItem);
inc(productionsInitiated);
end;
LogWrite(' ' + IntToStr(productionsInitiated) + ' productions initiated');
end;
{$UNDEF DEBUG_THIS}
destructor TUserFace.Destroy;
begin
Finalize;
inherited Destroy;
end;
end.