Debian Codegraph @

examples/user_interface/zombie_fighter_alternative_short_lived_views/code/gameviewplay.pas

raw
1 {
2 Copyright 2016-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 view where you actually play a game. }
17 unit GameViewPlay;
18
19 interface
20
21 uses Classes, CastleControls, CastleOnScreenMenu, CastleUIControls,
22 CastleViewport, CastleSceneCore, CastleScene,
23 CastleCameras, CastleKeysMouse;
24
25 type
26 TViewPlay = class(TCastleView)
27 published
28 { Components designed using CGE editor.
29 These fields will be automatically initialized at Start. }
30 MainViewport, MapViewport: TCastleViewport;
31 ButtonBack: TCastleButton;
32 strict private
33 procedure ClickBack(Sender: TObject);
34 public
35 constructor Create(AOwner: TComponent); override;
36 procedure Start; override;
37 procedure Resume; override;
38 procedure Pause; override;
39 function Press(const Event: TInputPressRelease): boolean; override;
40 end;
41
42 implementation
43
44 uses CastleVectors, CastleColors,
45 CastleFilesUtils, CastleUtils, CastleTriangles, CastleShapes,
46 CastleComponentSerialize,
47 GameViewMainMenu, GameViewAskDialog;
48
49 { TViewPlay ------------------------------------------------------------- }
50
51 constructor TViewPlay.Create(AOwner: TComponent);
52 begin
53 inherited;
54 DesignUrl := 'castle-data:/gameviewplay.castle-user-interface';
55 end;
56
57 procedure TViewPlay.Start;
58 begin
59 inherited;
60
61 { turn off head bobbing, it makes a feeling that sprites sometimes "tremble" }
62 // WalkNavigation.HeadBobbing := 0;
63
64 // see https://castle-engine.io/multiple_viewports_to_display_one_world
65 MapViewport.Items.Remove(MapViewport.Camera);
66 MapViewport.Items := MainViewport.Items;
67 MapViewport.Items.Add(MapViewport.Camera);
68
69 MapViewport.Camera.SetView(
70 Vector3(5, 92.00, 0.99),
71 Vector3(0, -1, 0),
72 Vector3(0, 0, 1));
73
74 ButtonBack.OnClick := {$ifdef FPC}@{$endif}ClickBack;
75 end;
76
77 procedure TViewPlay.Resume;
78 begin
79 inherited;
80
81 { Without setting ForceCaptureInput, inputs are only passed
82 when mouse cursor is over the Viewport.
83
84 Usually you set such things in Start method, but here we need to be
85 prepared that we may be covered by the transparent ViewAskDialog view.
86 When ViewAskDialog is active, we do *not* want to forcefully capture input
87 (it would allow user to move by mouse dragging when ViewAskDialog is open).
88 So we set this in Resume, and turn off in Pause. }
89 Container.ForceCaptureInput := MainViewport.Navigation;
90 end;
91
92 procedure TViewPlay.Pause;
93 begin
94 Container.ForceCaptureInput := nil;
95 inherited;
96 end;
97
98 procedure TViewPlay.ClickBack(Sender: TObject);
99 begin
100 Container.View := TViewMainMenu.CreateUntilStopped;
101 end;
102
103 function TViewPlay.Press(const Event: TInputPressRelease): boolean;
104 var
105 Triangle: PTriangle;
106 Male: Boolean;
107 begin
108 Result := inherited;
109 if Result then Exit;
110
111 if Event.IsMouseButton(buttonLeft) then
112 begin
113 Triangle := MainViewport.TriangleHit;
114 if (Triangle <> nil) and // we clicked on something that has triangle information (e.g. because it has Spatial with ssDynamicCollisions)
115 (Triangle^.MaterialInfo <> nil) and // the clicked triangle has a material information
116 ( (Triangle^.MaterialInfo.Node.X3DName = 'MA_female_zombie_material') or
117 (Triangle^.MaterialInfo.Node.X3DName = 'MA_male_zombie_material')) then
118 begin
119 Male := Triangle^.MaterialInfo.Node.X3DName = 'MA_male_zombie_material';
120 Container.PushView(TViewAskDialog.CreateUntilStopped(Male));
121 end;
122 end;
123 end;
124
125 end.