File: gamestateplay.pas

package info (click to toggle)
castle-game-engine 6.4%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 194,520 kB
  • sloc: pascal: 364,585; ansic: 8,606; java: 2,851; objc: 2,601; cpp: 1,412; xml: 851; makefile: 725; sh: 563; php: 26
file content (173 lines) | stat: -rw-r--r-- 5,357 bytes parent folder | download | duplicates (2)
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
{
  Copyright 2016-2017 Michalis Kamburelis.

  This file is part of "Castle Game Engine".

  "Castle Game Engine" is free software; see the file COPYING.txt,
  included in this distribution, for details about the copyright.

  "Castle Game Engine" 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.

  ----------------------------------------------------------------------------
}

{ Game state where you actually play a game. }
unit GameStatePlay;

interface

uses Classes, CastleControls, CastleUIState, CastleOnScreenMenu,
  CastleSceneManager, CastleSceneCore, CastleScene,
  CastleCameras, CastleKeysMouse;

type
  TStatePlay = class(TUIState)
  strict private
    SimpleBackground: TCastleSimpleBackground;
    SceneManager: TCastleSceneManager;
    Scene: TCastleScene;
    ViewportRect: TCastleRectangleControl;
    Viewport: TCastleViewport;
    ButtonBack: TCastleButton;
    LabelInstructions: TCastleLabel;
    procedure BackClick(Sender: TObject);
  public
    procedure Start; override;
    procedure Resume; override;
    procedure Pause; override;
    function Press(const Event: TInputPressRelease): boolean; override;
  end;

var
  StatePlay: TStatePlay;

implementation

uses CastleVectors, CastleColors, CastleWindow, CastleUIControls,
  CastleFilesUtils, CastleUtils, CastleTriangles, CastleShapes,
  GameStateMainMenu, GameStateAskDialog;

{ TStatePlay ------------------------------------------------------------- }

procedure TStatePlay.Start;
begin
  inherited;

  SimpleBackground := TCastleSimpleBackground.Create(FreeAtStop);
  SimpleBackground.Color := Black;
  InsertFront(SimpleBackground);

  Scene := TCastleScene.Create(FreeAtStop);
  Scene.Load(ApplicationData('level1.x3d'));
  Scene.Spatial := [ssRendering, ssDynamicCollisions];
  Scene.ProcessEvents := true;
  Scene.Attributes.PhongShading := true; // looks better
  { zombie sprites are rendered using blending, and you can see multiple
    sprites sometimes at once. So we need sorting, to render them correctly.
    This is actually the default now. }
  //Scene.Attributes.BlendingSort := bs3D;

  SceneManager := TCastleSceneManager.Create(FreeAtStop);
  SceneManager.FullSize := false;
  SceneManager.Left := 10;
  SceneManager.Bottom := 10;
  SceneManager.Width := 800;
  SceneManager.Height := 748;
  SceneManager.Items.Add(Scene);
  SceneManager.MainScene := Scene;
  SceneManager.NavigationType := ntWalk;
  SceneManager.WalkCamera.MoveSpeed := 10;
  { turn off head bobbing, it makes a feeling that sprites sometimes "tremble" }
  SceneManager.WalkCamera.HeadBobbing := 0;
  InsertFront(SceneManager);

  ViewportRect := TCastleRectangleControl.Create(FreeAtStop);
  ViewportRect.FullSize := false;
  ViewportRect.Left := 820;
  ViewportRect.Bottom := 10;
  ViewportRect.Width := 256;
  ViewportRect.Height := 256;
  ViewportRect.Color := Silver;
  InsertFront(ViewportRect);

  Viewport := TCastleViewport.Create(FreeAtStop);
  Viewport.FullSize := false;
  Viewport.Left := 10;
  Viewport.Bottom := 10;
  Viewport.Width := 236;
  Viewport.Height := 236;
  Viewport.SceneManager := SceneManager;
  Viewport.Transparent := true;
  Viewport.NavigationType := ntNone;
  Viewport.RequiredCamera.SetView(
    Vector3(5, 92.00, 0.99),
    Vector3(0, -1, 0),
    Vector3(0, 0, 1));
  ViewportRect.InsertFront(Viewport);

  LabelInstructions := TCastleLabel.Create(FreeAtStop);
  LabelInstructions.Caption :=
    'Walk around using AWSD and arrow keys.' + NL +
    'Click on a zombie sprite to show a dialog.';
  LabelInstructions.Anchor(vpTop, -100);
  LabelInstructions.Anchor(hpRight, -10);
  LabelInstructions.Color := Yellow;
  InsertFront(LabelInstructions);

  ButtonBack := TCastleButton.Create(FreeAtStop);
  ButtonBack.Caption := 'Back to Main Menu';
  ButtonBack.OnClick := @BackClick;
  ButtonBack.Anchor(vpTop, -10);
  ButtonBack.Anchor(hpRight, -10);
  InsertFront(ButtonBack);
end;

procedure TStatePlay.Resume;
begin
  inherited;

  { Without setting ForceCaptureInput, inputs are only passed
    when mouse cursor is over the SceneManager.

    Usually you set such things in Start method, but here we need to be
    prepared that we may be covered by the transparent StateAskDialog state.
    When StateAskDialog is active, we do *not* want to forcefully capture input
    (it would allow user to move by mouse dragging when StateAskDialog is open).
    So we set this in Resume, and turn off in Pause. }
  StateContainer.ForceCaptureInput := SceneManager;
end;

procedure TStatePlay.Pause;
begin
  StateContainer.ForceCaptureInput := nil;
  inherited;
end;

procedure TStatePlay.BackClick(Sender: TObject);
begin
  TUIState.Current := StateMainMenu;
end;

function TStatePlay.Press(const Event: TInputPressRelease): boolean;
var
  Triangle: PTriangle;
begin
  Result := inherited;
  if Result then Exit;

  if Event.IsMouseButton(mbLeft) then
  begin
    Triangle := SceneManager.TriangleHit;
    if (Triangle <> nil) and
       ( (Triangle^.Material.X3DName = 'MA_female_zombie_material') or
         (Triangle^.Material.X3DName = 'MA_male_zombie_material')) then
    begin
      StateAskDialog.Male := Triangle^.Material.X3DName = 'MA_male_zombie_material';
      TUIState.Push(StateAskDialog);
    end;
  end;
end;

end.