Debian Codegraph @

examples/isometric_game/code/gameviewmain.pas

raw
1 {
2 Copyright 2022-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 { Main view, where most of the application logic takes place. }
17 unit GameViewMain;
18
19 interface
20
21 uses Classes,
22 CastleVectors, CastleComponentSerialize,
23 CastleUIControls, CastleControls, CastleKeysMouse, CastleViewport, CastleScene;
24
25 type
26 { Main view, where most of the application logic takes place. }
27 TViewMain = class(TCastleView)
28 published
29 { Components designed using CGE editor.
30 These fields will be automatically initialized at Start. }
31 LabelFps: TCastleLabel;
32 MainViewport: TCastleViewport;
33 public
34 constructor Create(AOwner: TComponent); override;
35 procedure Start; override;
36 procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override;
37 function Press(const Event: TInputPressRelease): Boolean; override;
38 end;
39
40 var
41 ViewMain: TViewMain;
42
43 implementation
44
45 uses SysUtils,
46 CastleUtils, CastleImages;
47
48 { TViewMain ----------------------------------------------------------------- }
49
50 constructor TViewMain.Create(AOwner: TComponent);
51 begin
52 inherited;
53 DesignUrl := 'castle-data:/gameviewmain.castle-user-interface';
54 end;
55
56 procedure TViewMain.Start;
57 const
58 { The calculation of ImagePosition below assumes that all images have
59 the same width and height (you can play with Image.Size if you want to resize them),
60 and that they should be arranged in isometric fashion -- that is,
61 images in each row have alternating shift. }
62 TileWidth = 70;
63 TileHeight = 36;
64
65 { URL of a random ground image. }
66 function RandomGround: String;
67 begin
68 Result := 'castle-data:/map_objects/ground/soil_' + IntToStr(RandomIntRangeInclusive(1, 7)) + '_ta.png';
69 end;
70
71 { URL of a random tree image. }
72 function RandomTree: String;
73 begin
74 case Random(2) of
75 0: Result := 'castle-data:/map_objects/trees/kambi_birch_01_mature_autumn_thc.png';
76 1: Result := 'castle-data:/map_objects/trees/kambi_birch_01_mature_winter_thc.png';
77 else raise EInternalError.Create('Unexpected random result in RandomTree');
78 end;
79 end;
80
81 { Return position of the middle of tile (X,Y).
82 This position is in the coordinates inside MainViewport world,
83 so you can use it to set TCastleTransform.Translation, like TCastleImageTransform.Translation. }
84 function TilePosition(const X, Y: Integer): TVector2;
85 begin
86 { This is simplest and good for image layout as a simple grid. }
87 //Result := Vector2(X * TileWidth, Y * TileHeight);
88
89 { This makes isometric image layout. }
90 Result := Vector2(
91 X * TileWidth + Iff(Odd(Y), TileWidth / 2, 0),
92 Y * TileHeight / 2);
93
94 { Change the Result to point to the middle of the tile. }
95 Result := Result + Vector2(TileWidth / 2, TileHeight / 2);
96 end;
97
98 { Create TCastleImageTransform and add it to the viewport.
99
100 The image is loaded from given Url.
101
102 The image is placed on tile X, Y. The middle of the tile will match the Pivot
103 point of the image.
104
105 The front-to-back order is determined by Z. Larger Z -> in front. }
106 procedure AddImage(const Url: String; const X, Y: Integer; const Pivot: TVector2;
107 const Z: Single);
108 var
109 Image: TCastleImageTransform;
110 begin
111 Image := TCastleImageTransform.Create(FreeAtStop);
112 Image.Url := Url;
113 Image.AlphaChannel := acTest;
114 Image.Translation := Vector3(TilePosition(X, Y), Z);
115 Image.Pivot := Pivot;
116
117 { Using SmoothScaling=false as our sample images are small
118 and seem to look better as pixel-art.
119
120 But everything would also work with SmoothScaling=true.
121 Note: We have applied on images alpha bleeding
122 (see https://castle-engine.io/manual_alpha_bleeding.php)
123 using castle-image-viewer .
124 This means they have good transition between opaque and transparent pixels,
125 which is necessary for SmoothScaling=true.
126
127 In the end these images just seem better as pixel-art (SmoothScaling=false).
128 But both options work -- it's a matter of visual taste at this point. }
129 Image.SmoothScaling := false;
130
131 MainViewport.Items.Add(Image);
132 end;
133
134 const
135 TilesCountX = 20;
136 TilesCountY = 40;
137 var
138 X, Y: Integer;
139 begin
140 inherited;
141
142 { Fill the map with random ground + occasional trees }
143 for X := 0 to TilesCountX - 1 do
144 for Y := 0 to TilesCountY - 1 do
145 begin
146 AddImage(RandomGround, X, Y, Vector2(0.5, 0.5), 0);
147 if Random(10) = 0 then
148 begin
149 AddImage(RandomTree, X, Y, Vector2(0.5, 0.1),
150 // Place trees in front of the ground, bottom trees more in front
151 1 + (TilesCountY - Y));
152 end;
153 end;
154
155 { Place camera such that it looks at the center tile initially }
156 MainViewport.Camera.Translation := Vector3(
157 TilePosition(TilesCountX div 2, TilesCountY div 2),
158 100);
159 end;
160
161 procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: Boolean);
162 begin
163 inherited;
164 { This virtual method is executed every frame (many times per second). }
165 LabelFps.Caption :=
166 'FPS: ' + Container.Fps.ToString + NL +
167 'DynamicBatching (toggle with B): ' + BoolToStr(MainViewport.DynamicBatching, true) + NL +
168 'Stats: ' + MainViewport.Statistics.ToString;
169 end;
170
171 function TViewMain.Press(const Event: TInputPressRelease): Boolean;
172 begin
173 Result := inherited;
174 if Result then Exit; // allow the ancestor to handle keys
175
176 if Event.IsKey(keyB) then
177 begin
178 MainViewport.DynamicBatching := not MainViewport.DynamicBatching;
179 Exit(true); // key was handled
180 end;
181 end;
182
183 end.