-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodegameunit.pas
315 lines (257 loc) · 9.58 KB
/
modegameunit.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
309
310
311
312
313
314
315
{
Copyright 2003-2023 Michalis Kamburelis.
This file is part of "malfunction".
"malfunction" is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"malfunction" is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with "malfunction"; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
----------------------------------------------------------------------------
}
unit ModeGameUnit;
interface
uses X3DNodes, CastleSceneManager, CastleProjection, CastleTransform;
type
TMalfunctionSceneManager = class(TCastleSceneManager)
public
procedure Update(const SecondsPassed: Single;
var HandleInput: Boolean); override;
procedure AdjustProjection;
end;
var
SceneManager: TMalfunctionSceneManager;
procedure PlayGame(const SceneURL: string);
implementation
uses SysUtils, Math,
CastleWindow, GameGeneral, CastleGLUtils,
CastleVectors, CastleUtils, LevelUnit, CastleBoxes, CastleMessages, PlayerShipUnit,
CastleImages,
ShipsAndRockets, CastleKeysMouse, CastleFilesUtils, CastleColors,
CastleStringUtils, CastleScene, CastleGLImages,
CastleUIControls, CastleCameras, CastleApplicationProperties,
CastleRenderContext;
var
kokpit_gl: TDrawableImage;
crossh_gl: TDrawableImage;
{crossh_orig_* to oryginalne (tzn. wzgledem ekranu 640x480) rozmiary
crosshair image (upakowanego w crossh_list) }
crossh_orig_width, crossh_orig_height: integer;
{ TMalfunctionSceneManager --------------------------------------------------- }
procedure TMalfunctionSceneManager.AdjustProjection;
var
wholeMoveLimit: TBox3D;
const
AngleOfViewY = 30;
begin
Camera.ProjectionType := ptPerspective;
Camera.Perspective.FieldOfView := DegToRad(AngleOfViewY);
Camera.Perspective.FieldOfViewAxis := faVertical;
Camera.ProjectionNear := PLAYER_SHIP_CAMERA_RADIUS;
{ Player jest zawsze w obrebie MoveLimit i widzi rzeczy w obrebie
levelScene.BoundingBox. Wiec far = dlugosc przekatnej
Box3DSum(MoveLimit, levelScene.BoundingBox) bedzie na pewno wystarczajace. }
wholeMoveLimit := levelScene.BoundingBox + MoveLimit;
Camera.ProjectionFar := PointsDistance(wholeMoveLimit.Data[0], wholeMoveLimit.Data[1]);
end;
procedure TMalfunctionSceneManager.Update(const SecondsPassed: Single;
var HandleInput: Boolean);
begin
inherited;
{ TODO: For some reason, doing this once, in modeEnter, is not enough.
We have to do this every frame.
Otherwise camera projection gets reset to default, which is not good
for us -- we need larger ProjectionNear to Z-fighting on some far objects. }
AdjustProjection;
Camera.SetView(
playerShip.Translation,
playerShip.Direction,
playerShip.Up);
end;
{ TGame2DControls ------------------------------------------------------------ }
type
TGame2DControls = class(TCastleUserInterface)
public
procedure Render; override;
end;
procedure TGame2DControls.Render;
procedure radarDraw2D;
const
{ na ekranie jest kwadrat radaru wielkosci Size oddalony od gornej
i prawej krawedzi ekranu o ScreenMargin. We wnetrzu tego kwadratu
w srodku jest mniejszy kwadrat wielkosci Size-2*InsideMargin
i jego wnetrze odpowiada powierzchni XY MoveLimit. }
ScreenMargin = 10;
Size = 100;
InsideMargin = 5;
var
MinInsideX, MaxInsideX, MinInsideY, MaxInsideY: Integer;
procedure MoveLimitPosToPixel(const pos: TVector3; var x, y: TGLint);
begin
x := Round(MapRange(pos[0], MoveLimit.Data[0].X, MoveLimit.Data[1].X, MinInsideX, MaxInsideX));
y := Round(MapRange(pos[1], MoveLimit.Data[0].Y, MoveLimit.Data[1].Y, MinInsideY, MaxInsideY));
end;
var
i: integer;
x, y: TGLint;
begin
// TODO: No direct drawing using GL fixed-function anymore
(*
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
MinInsideX := Window.Width - ScreenMargin - Size + InsideMargin;
MaxInsideX := Window.Width - ScreenMargin - InsideMargin;
MinInsideY := Window.Height - ScreenMargin - Size + InsideMargin;
MaxInsideY := Window.Height - ScreenMargin - InsideMargin;
glColor4f(0, 0, 0, 0.5);
glRectf(Window.Width-ScreenMargin-Size, Window.Height-ScreenMargin-Size,
Window.Width-ScreenMargin, Window.Height-ScreenMargin);
MoveLimitPosToPixel(playerShip.Translation, x, y);
glColor4f(1, 1, 1, 0.8);
glBegin(GL_LINES);
glVertex2i(MinInsideX, y); glVertex2i(MaxInsideX, y);
glVertex2i(x, MinInsideY); glVertex2i(x, MaxInsideY);
glEnd;
glColor4f(1, 1, 0, 0.8);
RenderContext.PointSize := 2;
glBegin(GL_POINTS);
for i := 0 to enemyShips.Count-1 do
if enemyShips[i] <> nil then
begin
MoveLimitPosToPixel(enemyShips[i].Translation, x, y);
glVertex2i(x, y);
end;
glEnd;
RenderContext.PointSize := 1;
glDisable(GL_BLEND);
*)
end;
begin
kokpit_gl.Draw(0, 0);
if playerShip.drawCrosshair then
crossh_gl.Draw(
(Window.Width - crossh_orig_width) div 2,
(Window.Height - crossh_orig_height) div 2);
if playerShip.drawRadar then
radarDraw2D;
playerShip.PlayerShipDraw2D;
end;
{ mode enter/exit ----------------------------------------------------------- }
var
Controls: TGame2DControls;
{ Set before SetGameMode(modeGame); }
GameModeLevelUrl: String;
procedure Press(Container: TUIContainer; const Event: TInputPressRelease); forward;
procedure Update(Container: TUIContainer); forward;
procedure modeEnter;
begin
Controls := TGame2DControls.Create(nil);
SceneManager := TMalfunctionSceneManager.Create(nil);
SceneManager.AutoNavigation := false;
Window.Controls.InsertFront(SceneManager);
Window.Controls.InsertFront(Controls);
Window.Controls.InsertFront(Notifications);
Window.OnPress := @Press;
Window.OnUpdate := @Update;
Window.AutoRedisplay := true;
NewPlayerShip;
LoadLevel(GameModeLevelUrl);
// once LevelScene initialized
SceneManager.AdjustProjection;
end;
procedure modeExit;
begin
PlayerShip := nil; // will be freed be freeing SceneManager that owns it
UnloadLevel;
{ Check Window <> nil, as it may be already nil (during destruction)
now in case of some errors }
if Window <> nil then
Window.AutoRedisplay := false;
Window.Controls.Remove(Controls);
Window.Controls.Remove(Notifications);
Window.Controls.Remove(SceneManager);
FreeAndNil(Controls);
FreeAndNil(SceneManager);
end;
procedure PlayGame(const SceneURL: string);
begin
GameModeLevelUrl := SceneURL;
SetGameMode(modeGame);
end;
{ glw callbacks ----------------------------------------------------------- }
procedure Press(Container: TUIContainer; const Event: TInputPressRelease);
var fname: string;
begin
if Event.EventType <> itKey then Exit;
case Event.key of
keySpace: playerShip.FireRocket(playerShip.Direction, 1);
keyEscape:
if MessageYesNo(Window, 'End this game and return to menu ?') then
SetGameMode(modeMenu);
keyC:
if Window.Pressed.Modifiers=[mkShift, mkCtrl] then
with playerShip do CheatDontCheckCollisions := not CheatDontCheckCollisions else
if Window.Pressed.Modifiers=[] then
with playerShip do drawCrosshair := not drawCrosshair;
keyI:
if Window.Pressed[keyShift] and Window.Pressed[keyCtrl] then
with playerShip do CheatImmuneToRockets := not CheatImmuneToRockets;
keyR:
with playerShip do drawRadar := not drawRadar;
keyF5:
begin
fname := FileNameAutoInc('malfunction_screen_%d.png');
Window.SaveScreen(fname);
Notifications.Show('Screen saved to '+fname);
end;
end;
end;
procedure Update(Container: TUIContainer);
begin
if playerShip.ShipLife <= 0 then
begin
MessageOK(Window,['Your ship has been destroyed !','Game over.']);
SetGameMode(modeMenu);
Exit;
end;
playerShip.PlayerShipUpdate;
ShipsAndRocketsUpdate;
end;
{ Open/Close Window -------------------------------------------------------- }
procedure ContextOpen;
var
crossh_img: TCastleImage;
kokpit_img: TCastleImage;
begin
kokpit_img := LoadImage('castle-data:/images/kokpit.png',
[TRGBAlphaImage, TGrayscaleAlphaImage]);
kokpit_img.Resize(Window.width, kokpit_img.Height * Window.Height div 480);
kokpit_gl := TDrawableImage.Create(kokpit_img, false { smooth scaling }, true { owns image });
{ przyjmujemy ze crosshair.png bylo przygotowane dla ekranu 640x480.
Resizujemy odpowiednio do naszego okienka. }
crossh_img := LoadImage('castle-data:/images/crosshair.png',
[TRGBAlphaImage, TGrayscaleAlphaImage]);
crossh_orig_width := crossh_img.Width;
crossh_orig_height := crossh_img.Height;
crossh_img.Resize(crossh_img.Width * Window.width div 640,
crossh_img.Height * Window.height div 480);
crossh_gl := TDrawableImage.Create(crossh_img, false { smooth scaling }, true { owns image });
end;
procedure ContextClose;
begin
FreeAndNil(kokpit_gl);
FreeAndNil(crossh_gl);
end;
initialization
// TODO: we need EnableFixedFunction to work, as we do some rendering directly
TGLFeatures.RequestCapabilities := rcForceFixedFunction;
gameModeEnter[modeGame] := @modeEnter;
gameModeExit[modeGame] := @modeExit;
ApplicationProperties.OnGLContextOpen.Add(@ContextOpen);
ApplicationProperties.OnGLContextClose.Add(@ContextClose);
end.