Debian Codegraph @

examples/deprecated_to_upgrade/isometric_game/code/gameinitialize.pas

raw
1 {
2 Copyright 2011-2023 Michalis Kamburelis.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14 }
15
16 { Game initialization and logic. }
17 unit GameInitialize;
18
19 interface
20
21 implementation
22
23 uses
24 { standard units }
25 SysUtils, Math,
26 { Castle Game Engine units }
27 CastleWindow, CastleFilesUtils, CastleStringUtils,
28 CastleUtils, CastleGLUtils, CastleKeysMouse, CastleMessages, CastleGLImages,
29 CastleImages, CastleColors, CastleLog, CastleApplicationProperties,
30 { game units }
31 GameMap, GamePlayer, GameWindow;
32
33 var
34 Player: TPlayer;
35 ViewMoveX, ViewMoveY: Single;
36 ViewFollowsPlayer: boolean = true;
37
38 procedure WindowRender(Container: TCastleContainer);
39 var
40 RealViewMoveX, RealViewMoveY: Integer;
41
42 procedure DrawImageOnTile(X, Y: Cardinal; DrawableImage: TDrawableImage;
43 const SpecialMoveX: Integer = 0;
44 const SpecialMoveY: Integer = 0);
45 var
46 PosX, PosY: Integer;
47 begin
48 PosX := X * BaseWidth;
49 if Odd(Y) then
50 PosX := PosX + BaseWidth div 2;
51 PosX := PosX + RealViewMoveX + SpecialMoveX;
52 PosY := Y * (BaseHeight div 2);
53 PosY := PosY + RealViewMoveY + SpecialMoveY;
54 DrawableImage.Alpha := acTest;
55 DrawableImage.Draw(PosX, PosY);
56 end;
57
58 var
59 X, Y: Cardinal;
60 MapTile: TMapTile;
61 BaseFitX, BaseFitY: Cardinal;
62 X1, X2, Y1, Y2: Integer;
63 begin
64 BaseFitX := Ceil(Window.Width / BaseWidth) + 1;
65 BaseFitY := Ceil(2 * Window.Height / BaseHeight) + 1;
66
67 if ViewFollowsPlayer then
68 begin
69 { Ignore ViewMoveX/Y, calculate RealView such that the player
70 is in the middle. }
71 RealViewMoveX := Player.XPixel;
72 RealViewMoveY := Player.YPixel;
73 if Player.Moving then
74 begin
75 RealViewMoveX := RealViewMoveX - Round(Player.MovingSmallMoveX);
76 RealViewMoveY := RealViewMoveY - Round(Player.MovingSmallMoveY);
77 end;
78 end else
79 begin
80 RealViewMoveX := Round(ViewMoveX);
81 RealViewMoveY := Round(ViewMoveY);
82 end;
83
84 { First: this is what would be seen if RealViewMoveX/Y is zero. }
85 X1 := -1;
86 X2 := Integer(BaseFitX) - 2;
87 Y1 := -1;
88 Y2 := Integer(BaseFitY) - 2;
89 { Now translate taking RealViewMoveX/Y into account. }
90 X1 := X1 - Ceil(RealViewMoveX / BaseWidth);
91 X2 := X2 - Floor(RealViewMoveX / BaseWidth);
92 Y1 := Y1 - Ceil(2 * RealViewMoveY / BaseHeight);
93 Y2 := Y2 - Floor(2 * RealViewMoveY / BaseHeight);
94 { Eventually correct to be inside 0..Map.Width/Height - 1 range }
95 ClampVar(X1, 0, Map.Width - 1);
96 ClampVar(X2, 0, Map.Width - 1);
97 ClampVar(Y1, 0, Map.Height - 1);
98 ClampVar(Y2, 0, Map.Height - 1);
99
100 for X := X1 to X2 do
101 for Y := Y1 to Y2 do
102 begin
103 MapTile := Map.Items[X, Y];
104 DrawImageOnTile(X, Y, MapTile.BaseTile.DrawableImage);
105 end;
106
107 { TODO: unoptimal code, should draw only the part that fits within the window.
108 We should auto-check width/height of bonus tile, to know when to draw it.
109 Even better, we should record this on the map --- which tile is visible
110 where. }
111 for Y := Map.Height - 1 downto 0 do
112 begin
113 { The order of drawing is important. Player must be drawn
114 on top of some objects and below some others. }
115 if Y = Player.Y then
116 begin
117 if Player.Moving then
118 DrawImageOnTile(Player.X, Player.Y, Player.DrawableImage[Player.Direction],
119 Round(Player.MovingSmallMoveX),
120 Round(Player.MovingSmallMoveY)) else
121 DrawImageOnTile(Player.X, Player.Y, Player.DrawableImage[Player.Direction]);
122 end;
123
124 for X := 0 to Map.Width - 1 do
125 begin
126 MapTile := Map.Items[X, Y];
127 if MapTile.BonusTile <> nil then
128 DrawImageOnTile(X, Y, MapTile.BonusTile.DrawableImage);
129 end;
130 end;
131 end;
132
133 procedure WindowPress(Container: TCastleContainer; const Event: TInputPressRelease);
134 var
135 NewViewMoveX, NewViewMoveY: Integer;
136
137 { Get character from user. Returns #0 if cancelled. }
138 function MessageChar(const S: string): char;
139 var
140 Event: TInputPressRelease;
141 begin
142 Event := MessageKeyMouse(Window,
143 'Enter the character code of new base tile, or Escape to cancel');
144 if (Event.EventType = itKey) and
145 (Event.KeyCharacter <> CharEscape) and
146 (Event.KeyCharacter <> #0) then
147 Result := Event.KeyCharacter else
148 Result := #0;
149 end;
150
151 procedure EditBaseTile;
152 var
153 BaseTile: TBaseTile;
154 C: Char;
155 begin
156 C := MessageChar('Enter the character code of new base tile, or Escape to cancel');
157 if C <> #0 then
158 begin
159 BaseTile := Map.BaseTiles[C];
160 if BaseTile = nil then
161 MessageOK(Window, Format('The character "%s" is not a code ' +
162 'for any base tile', [C])) else
163 Map.Items[Player.X, Player.Y].BaseTile := BaseTile;
164 end;
165 end;
166
167 procedure EditBonusTile;
168 var
169 BonusTile: TBonusTile;
170 C: Char;
171 begin
172 C := MessageChar('Enter the character code of new bonus tile, or "_" to clear or Escape to cancel');
173 if C <> #0 then
174 begin
175 if C = '_' then
176 Map.Items[Player.X, Player.Y].BonusTile := nil else
177 begin
178 BonusTile := Map.BonusTiles[C];
179 if BonusTile = nil then
180 MessageOK(Window, Format('The character "%s" is not a code ' +
181 'for any bonus tile', [C])) else
182 Map.Items[Player.X, Player.Y].BonusTile := BonusTile;
183 end;
184 end;
185 end;
186
187 procedure ShowFieldInfo;
188
189 function TileDescr(Tile: TTile): string;
190 begin
191 if Tile = nil then
192 Result := '<none>' else
193 Result := Format('"%s" (URL "%s")',
194 [Tile.CharCode, Tile.RelativeUrl]);
195 end;
196
197 begin
198 MessageOK(Window, Format(
199 'Position: %d, %d' +nl+
200 'Base tile: %s' +nl+
201 'Bonus tile: %s',
202 [ Player.X, Player.Y,
203 TileDescr(Map.Items[Player.X, Player.Y].BaseTile),
204 TileDescr(Map.Items[Player.X, Player.Y].BonusTile) ]));
205 end;
206
207 var
208 Url: String;
209 begin
210 if Event.EventType = itKey then
211 begin
212 case Event.KeyCharacter of
213 'f': begin
214 ViewFollowsPlayer := not ViewFollowsPlayer;
215 if not ViewFollowsPlayer then
216 begin
217 { Set ViewMoveX/Y initial values such that the player is still
218 in the middle. This is less confusing for user. }
219 ViewMoveToCenterPosition(Player.X, Player.Y,
220 NewViewMoveX, NewViewMoveY);
221 ViewMoveX := NewViewMoveX;
222 ViewMoveY := NewViewMoveY;
223 end;
224 end;
225 'e': EditBaseTile;
226 'E': EditBonusTile;
227 's': begin
228 Url := 'new';
229 if MessageInputQuery(Window, 'Save map as name' +
230 ' (don''t specify here initial path and .map extension)', Url) then
231 Map.SaveToFile('castle-data:/maps/' + Url + '.map');
232 end;
233 'i': ShowFieldInfo;
234 CharEscape: Window.Close;
235 end;
236 end;
237 end;
238
239 procedure WindowUpdate(Container: TCastleContainer);
240 const
241 ViewMoveChangeSpeed = 10.0 * 50.0;
242 begin
243 if not ViewFollowsPlayer then
244 begin
245 if Window.Pressed[keyArrowUp] then ViewMoveY := ViewMoveY - ViewMoveChangeSpeed * Window.Fps.SecondsPassed;
246 if Window.Pressed[keyArrowDown] then ViewMoveY := ViewMoveY + ViewMoveChangeSpeed * Window.Fps.SecondsPassed;
247 if Window.Pressed[keyArrowRight] then ViewMoveX := ViewMoveX - ViewMoveChangeSpeed * Window.Fps.SecondsPassed;
248 if Window.Pressed[keyArrowLeft] then ViewMoveX := ViewMoveX + ViewMoveChangeSpeed * Window.Fps.SecondsPassed;
249 end else
250 begin
251 { At first I placed the commands below in KeyDown, as they work
252 like KeyDown: non-continuously. However, thanks to smooth scrolling
253 of the screen, user is easily fooled and thinks that they work
254 continuously. So he keeps pressing them. So we should check them
255 here. }
256 if Window.Pressed[keyArrowUp] then Player.Move(dirNorth);
257 if Window.Pressed[keyArrowDown] then Player.Move(dirSouth);
258 if Window.Pressed[keyArrowLeft] then Player.Move(dirWest);
259 if Window.Pressed[keyArrowRight] then Player.Move(dirEast);
260
261 if Window.Pressed[keyNumpad7] then Player.Move(dirNorthWest);
262 if Window.Pressed[keyNumpad9] then Player.Move(dirNorthEast);
263 if Window.Pressed[keyNumpad1] then Player.Move(dirSouthWest);
264 if Window.Pressed[keyNumpad3] then Player.Move(dirSouthEast);
265 if Window.Pressed[keyNumpad4] then Player.Move(dirWest);
266 if Window.Pressed[keyNumpad6] then Player.Move(dirEast);
267 if Window.Pressed[keyNumpad2] then Player.Move(dirSouth);
268 if Window.Pressed[keyNumpad8] then Player.Move(dirNorth);
269
270 if Window.Pressed[keyF10] then
271 begin
272 { simulate OpenGL context close + open, this may happen at any time on Android/iOS }
273 Window.Close(false);
274 Window.Open;
275 end;
276 end;
277
278 GameTime := GameTime + Window.Fps.SecondsPassed;
279
280 Player.Update;
281 end;
282
283 { One-time initialization of resources. }
284 procedure ApplicationInitialize;
285 begin
286 { Assign Window callbacks }
287 Window.OnUpdate := @WindowUpdate;
288 Window.OnPress := @WindowPress;
289 Window.OnRender := @WindowRender;
290
291 { For a scalable UI (adjusts to any window size in a smart way), use UIScaling }
292 // Window.Container.UIReferenceWidth := 1024;
293 // Window.Container.UIReferenceHeight := 768;
294 // Window.Container.UIScaling := usEncloseReferenceSize;
295
296 Map := TMap.CreateFromFile('castle-data:/maps/1.map');
297 Player := TPlayer.Create;
298 Player.Teleport(Map.PlayerStartX, Map.PlayerStartY, dirSouth);
299 Player.CalculatePixelPosition;
300 end;
301
302 initialization
303 { This initialization section configures:
304 - Application.OnInitialize
305 - Application.MainWindow
306 - determines initial window size
307
308 You should not need to do anything more in this initialization section.
309 Most of your actual application initialization (in particular, any file reading)
310 should happen inside ApplicationInitialize. }
311
312 Application.OnInitialize := @ApplicationInitialize;
313
314 Window := TCastleWindow.Create(Application);
315 Application.MainWindow := Window;
316
317 { Optionally, adjust window fullscreen state and size at this point.
318 Examples:
319
320 Run fullscreen:
321
322 Window.FullScreen := true;
323
324 Run in a 600x400 window:
325
326 Window.FullScreen := false; // default
327 Window.Width := 600;
328 Window.Height := 400;
329
330 Run in a window taking 2/3 of screen (width and height):
331
332 Window.FullScreen := false; // default
333 Window.Width := Application.ScreenWidth * 2 div 3;
334 Window.Height := Application.ScreenHeight * 2 div 3;
335
336 Note that some platforms (like mobile) ignore these window sizes.
337 }
338
339 { Handle command-line parameters like --fullscreen and --window.
340 By doing this last, you let user to override your fullscreen / mode setup. }
341 Window.ParseParameters;
342
343 { Our drawing routine is not prepared to react perfectly to window size change
344 at runtime. So disable it for now. }
345 Window.ResizeAllowed := raOnlyAtOpen;
346 Window.FpsShowOnCaption := true;
347 finalization
348 FreeAndNil(Player);
349 FreeAndNil(Map);
350 end.