| 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, CastleUIControls, CastleOnScreenMenu, |
| 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 |
var |
| 43 |
ViewPlay: TViewPlay; |
| 44 |
|
| 45 |
implementation |
| 46 |
|
| 47 |
uses CastleVectors, CastleColors, |
| 48 |
CastleFilesUtils, CastleUtils, CastleTriangles, CastleShapes, |
| 49 |
CastleComponentSerialize, |
| 50 |
GameViewMainMenu, GameViewAskDialog; |
| 51 |
|
| 52 |
{ TViewPlay ------------------------------------------------------------- } |
| 53 |
|
| 54 |
constructor TViewPlay.Create(AOwner: TComponent); |
| 55 |
begin |
| 56 |
inherited; |
| 57 |
DesignUrl := 'castle-data:/gameviewplay.castle-user-interface'; |
| 58 |
end; |
| 59 |
|
| 60 |
procedure TViewPlay.Start; |
| 61 |
begin |
| 62 |
inherited; |
| 63 |
|
| 64 |
{ turn off head bobbing, it makes a feeling that sprites sometimes "tremble" } |
| 65 |
// WalkNavigation.HeadBobbing := 0; |
| 66 |
|
| 67 |
// see https://castle-engine.io/multiple_viewports_to_display_one_world |
| 68 |
MapViewport.Items.Remove(MapViewport.Camera); |
| 69 |
MapViewport.Items := MainViewport.Items; |
| 70 |
MapViewport.Items.Add(MapViewport.Camera); |
| 71 |
|
| 72 |
MapViewport.Camera.SetView( |
| 73 |
Vector3(5, 92.00, 0.99), |
| 74 |
Vector3(0, -1, 0), |
| 75 |
Vector3(0, 0, 1)); |
| 76 |
|
| 77 |
ButtonBack.OnClick := {$ifdef FPC}@{$endif}ClickBack; |
| 78 |
end; |
| 79 |
|
| 80 |
procedure TViewPlay.Resume; |
| 81 |
begin |
| 82 |
inherited; |
| 83 |
|
| 84 |
{ Without setting ForceCaptureInput, inputs are only passed |
| 85 |
when mouse cursor is over the Viewport. |
| 86 |
|
| 87 |
Usually you set such things in Start method, but here we need to be |
| 88 |
prepared that we may be covered by the transparent ViewAskDialog view. |
| 89 |
When ViewAskDialog is active, we do *not* want to forcefully capture input |
| 90 |
(it would allow user to move by mouse dragging when ViewAskDialog is open). |
| 91 |
So we set this in Resume, and turn off in Pause. } |
| 92 |
Container.ForceCaptureInput := MainViewport.Navigation; |
| 93 |
end; |
| 94 |
|
| 95 |
procedure TViewPlay.Pause; |
| 96 |
begin |
| 97 |
Container.ForceCaptureInput := nil; |
| 98 |
inherited; |
| 99 |
end; |
| 100 |
|
| 101 |
procedure TViewPlay.ClickBack(Sender: TObject); |
| 102 |
begin |
| 103 |
Container.View := ViewMainMenu; |
| 104 |
end; |
| 105 |
|
| 106 |
function TViewPlay.Press(const Event: TInputPressRelease): boolean; |
| 107 |
var |
| 108 |
Triangle: PTriangle; |
| 109 |
begin |
| 110 |
Result := inherited; |
| 111 |
if Result then Exit; |
| 112 |
|
| 113 |
if Event.IsMouseButton(buttonLeft) then |
| 114 |
begin |
| 115 |
Triangle := MainViewport.TriangleHit; |
| 116 |
if (Triangle <> nil) and // we clicked on something that has triangle information (e.g. because it has Spatial with ssDynamicCollisions) |
| 117 |
(Triangle^.MaterialInfo <> nil) and // the clicked triangle has a material information |
| 118 |
( (Triangle^.MaterialInfo.Node.X3DName = 'MA_female_zombie_material') or |
| 119 |
(Triangle^.MaterialInfo.Node.X3DName = 'MA_male_zombie_material')) then |
| 120 |
begin |
| 121 |
ViewAskDialog.Male := Triangle^.MaterialInfo.Node.X3DName = 'MA_male_zombie_material'; |
| 122 |
Container.PushView(ViewAskDialog); |
| 123 |
end; |
| 124 |
end; |
| 125 |
end; |
| 126 |
|
| 127 |
end. |