This repository was 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
Expand file tree
/
Copy pathMapScrollManager.pas
More file actions
280 lines (243 loc) · 6.61 KB
/
MapScrollManager.pas
File metadata and controls
280 lines (243 loc) · 6.61 KB
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
unit MapScrollManager;
{$mode objfpc}{$H+}
interface
uses
Classes,
SysUtils,
zgl_math_2d,
zgl_window,
zgl_keyboard,
zgl_mouse,
LogEntityFace,
LogEntity,
Common,
MapScrollManagerFace,
MapDataCells,
MapDataFace,
TerrainViewerFace;
type
{ TMapScrollManager }
TMapScrollManager = class(IMapScrollManager)
public
constructor Create;
private
fLog: ILog;
fMap: IMapData;
fViewX, fViewY: single;
fXSpeed, fYSpeed: single;
fTileWidth, fTileHeight: integer;
fFieldWidth, fFieldHeight: single;
fDisplayCellInfo: boolean;
function GetTileWidth: single;
function GetTileHeight: single;
function GetViewLeft: single;
function GetViewTop: single;
function GetViewRight: single;
function GetViewBottom: single;
procedure Initialize(const aMap: IMapData);
procedure AssignDefaults;
procedure DetermineFieldDimensions;
procedure ScrollMap(const aTime: double);
function GetCellVisible(const aX, aY: integer): boolean;
function GetCellAtWindowPoint(const aX, aY: integer): PCell;
function GetCellNumberAtWindowPoint(const aX, aY: integer): TCellNumber;
public const
DefaultXSpeed = 6; // squares per second
DefaultYSpeed = 6;
DefaultTileWidth = 64;
DefaultTileHeight = 64;
public
property Log: ILog read fLog;
// This property should be assigned
property Map: IMapData read fMap write fMap;
property ViewX: single read fViewX;
property ViewY: single read fViewY;
property XSpeed: single read fXSpeed write fXSpeed;
property YSpeed: single read fYSpeed write fYSpeed;
property TileWidth: integer read fTileWidth write fTileWidth;
property TileHeight: integer read fTileHeight write fTileHeight;
property FieldWidth: single read fFieldWidth;
property FieldHeight: single read fFieldHeight;
property DisplayCellInfo: boolean read fDisplayCellInfo write fDisplayCellInfo;
property ViewLeft: single read GetViewLeft;
property ViewTop: single read GetViewTop;
property ViewRight: single read GetViewRight;
property ViewBottom: single read GetViewBottom;
property CellVisible[const x, y: integer]: boolean read GetCellVisible;
procedure ReceiveInput(const aTime: double);
procedure DrawCellInfo(const aX, aY: integer);
procedure Update;
procedure FocusOnMapCenter;
property CellAtWindowPoint[const x, y: integer]: PCell read GetCellAtWindowPoint;
property CellNumberAtWindowPoint[const x, y: integer]: TCellNumber
read GetCellNumberAtWindowPoint;
function GlobalToScreen(const aRect: zglPRect): zglTRect;
function ScreenX(const aCell: TCellNumber): single;
function ScreenY(const aCell: TCellNumber): single;
destructor Destroy; override;
end;
implementation
{ TMapScrollManager }
constructor TMapScrollManager.Create;
begin
inherited Create;
Initialize(nil);
end;
function TMapScrollManager.GetTileWidth: single;
begin
result := fTileWidth;
end;
function TMapScrollManager.GetTileHeight: single;
begin
result := fTileHeight;
end;
function TMapScrollManager.GetViewLeft: single;
begin
result := ViewX - single(wndWidth) / 2;
end;
function TMapScrollManager.GetViewTop: single;
begin
result := ViewY - single(wndHeight) / 2;
end;
function TMapScrollManager.GetViewRight: single;
begin
result := ViewX + single(wndWidth) / 2;
end;
function TMapScrollManager.GetViewBottom: single;
begin
result := ViewY + single(wndHeight) / 2;
end;
procedure TMapScrollManager.Initialize(const aMap: IMapData);
begin
fLog := TLog.Create(GlobalLogManager, 'MapScroll');
fMap := aMap;
AssignDefaults;
end;
procedure TMapScrollManager.AssignDefaults;
begin
XSpeed := DefaultXSpeed;
YSpeed := DefaultYSpeed;
TileWidth := DefaultTileWidth;
TileHeight := DefaultTileHeight;
end;
procedure TMapScrollManager.DetermineFieldDimensions;
begin
fFieldWidth := Map.Cells.Width * TileWidth;
fFieldHeight := Map.Cells.Height * TileHeight;
Log.Write('Field dimensions determined: '
+ FloatToStr(FieldWidth) + ' x ' + FloatToStr(FieldHeight));
end;
procedure TMapScrollManager.ScrollMap(const aTime: double);
function DeltaX: single; inline;
begin
result := aTime / single(1000) * fXSpeed * single(TileWidth);
end;
function DeltaY: single; inline;
begin
result := aTime / single(1000) * fYSpeed * single(TileHeight);
end;
begin
if key_Down(K_W) then
fViewY -= DeltaY;
if key_Down(K_S) then
fViewY += DeltaY;
if key_Down(K_A) then
fViewX -= DeltaX;
if key_Down(K_D) then
fViewX += DeltaX;
end;
function TMapScrollManager.GetCellVisible(const aX, aY: integer): boolean;
var
x, y: single;
begin
result := true;
x := aX * TileWidth;
y := aY * TileHeight;
if x + TileWidth < ViewLeft then exit(false);
if y + TileWidth < ViewTop then exit(false);
if x > ViewRight then exit(false);
if y > ViewBottom then exit(false);
end;
function TMapScrollManager.GetCellAtWindowPoint(const aX, aY: integer): PCell;
var
number: TCellNumber;
begin
number := CellNumberAtWindowPoint[aX, aY];
if number.IsNegative then
result := nil
else
result := @( Map.Cells.Matrix[number.X, number.Y] );
end;
function TMapScrollManager.GetCellNumberAtWindowPoint(const aX, aY: integer): TCellNumber;
var
xv, yv: single;
x, y: integer;
begin
xv := GetViewLeft + aX;
x := -1;
while xv > 0 do
begin
xv -= TileWidth;
x += 1;
end;
yv := GetViewTop + aY;
y := -1;
while yv > 0 do
begin
yv -= TileHeight;
y += 1;
end;
if Map.Cells.CellExists[x, y] then
begin
result.X := x;
result.Y := y;
end
else
begin
result.X := -1;
result.Y := -1;
end;
end;
procedure TMapScrollManager.ReceiveInput(const aTime: double);
begin
ScrollMap(aTime);
if DisplayCellInfo then
DrawCellInfo(mouse_X, mouse_Y);
end;
procedure TMapScrollManager.DrawCellInfo(const aX, aY: integer);
var
cell: PCell;
begin
cell := CellAtWindowPoint[aX, aY];
if cell = nil then exit;
end;
procedure TMapScrollManager.Update;
begin
DetermineFieldDimensions;
end;
procedure TMapScrollManager.FocusOnMapCenter;
begin
fViewX := FieldWidth / 2;
fViewY := FieldHeight / 2;
end;
function TMapScrollManager.GlobalToScreen(const aRect: zglPRect): zglTRect;
begin
result.X := aRect^.X - ViewLeft;
result.Y := aRect^.Y - ViewTop;
result.W := aRect^.W;
result.H := aRect^.H;
end;
function TMapScrollManager.ScreenX(const aCell: TCellNumber): single;
begin
result := TileWidth * aCell.X - ViewLeft;
end;
function TMapScrollManager.ScreenY(const aCell: TCellNumber): single;
begin
result := TileHeight * aCell.Y - ViewTop;
end;
destructor TMapScrollManager.Destroy;
begin
FreeLog(fLog);
inherited Destroy;
end;
end.