File: game.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 (227 lines) | stat: -rw-r--r-- 7,014 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
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
{
  Copyright 2017-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.

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

{ Implements the game logic. }
unit Game;

interface

implementation

uses SysUtils, Classes, Generics.Collections,
  CastleWindow, CastleScene, CastleControls, CastleLog, X3DNodes, CastleTransform,
  CastleFilesUtils, CastleSceneCore, CastleKeysMouse, CastleColors,
  CastleCameras, CastleVectors, CastleRenderer, CastleBoxes, CastleSceneManager,
  CastleUIControls, CastleApplicationProperties;

var
  Window: TCastleWindow;
  SceneManager: TCastleSceneManager; //< Shortcut for Window.SceneManager
  Level: TCastleScene;
  BoxTemplate, SphereTemplate: TCastleScene;

procedure LoadLevel(const URL: string; const MeshCollider: boolean);

  function CreatePlaneCollider(const ParentBody: TRigidBody): TPlaneCollider;
  begin
    Result := TPlaneCollider.Create(ParentBody);
    Result.Normal := Vector3(0, 1, 0);
    Result.Distance := 0;
    Result.Restitution := 0.3;
  end;

  function CreateMeshCollider(const ParentBody: TRigidBody): TMeshCollider;
  begin
    Result := TMeshCollider.Create(ParentBody);
    Result.Scene := Level;
    Result.Restitution := 0.3;
  end;

var
  LevelBody: TRigidBody;
  MoveLimit: TBox3D;
begin
  { free previous level, which also frees all related rigid bodies }
  FreeAndNil(Level);

  // SceneManager.Items.Clear; // not needed, we already freed everything
  SceneManager.ClearCameras; // recreate new camera for new level

  Level := TCastleScene.Create(Application);
  Level.Load(URL);
  Level.Spatial := [ssRendering, ssDynamicCollisions];
  Level.ProcessEvents := true;
  Level.Attributes.PhongShading := true; // nicer lighting

  LevelBody := TRigidBody.Create(Level);
  LevelBody.Dynamic := false;

  if MeshCollider then
    CreateMeshCollider(LevelBody)
  else
    CreatePlaneCollider(LevelBody);

  { assign this only once LevelBody and LevelCollider
    are fully configured, this initializes physics engine }
  Level.RigidBody := LevelBody;

  SceneManager.Items.Add(Level);
  SceneManager.MainScene := Level;

  // make gravity work even if your position is over the world bbox
  MoveLimit := SceneManager.Items.BoundingBox;
  MoveLimit.Max := MoveLimit.Max + Vector3(0, 1000, 0);
  SceneManager.MoveLimit := MoveLimit;
end;

type
  TEventHandler = class
    class procedure LoadLevelSimple(Sender: TObject);
    class procedure LoadLevelComplex(Sender: TObject);
  end;

class procedure TEventHandler.LoadLevelSimple(Sender: TObject);
begin
  LoadLevel(ApplicationData('level_simple.x3dv'), false);
end;

class procedure TEventHandler.LoadLevelComplex(Sender: TObject);
begin
  LoadLevel(ApplicationData('level_complex.x3dv'), true);
end;

{ One-time initialization of resources. }
procedure ApplicationInitialize;
var
  ButtonLevelSimple, ButtonLevelComplex: TCastleButton;
begin
  SceneManager := Window.SceneManager;

  LoadLevel(ApplicationData('level_simple.x3dv'), false);

  SceneManager.NavigationType := ntWalk;
  // rotating by dragging would cause trouble when clicking to spawn boxes/spheres
  SceneManager.WalkCamera.Input :=
    SceneManager.WalkCamera.Input - [ciMouseDragging];
  // easy way to make the simulation feel more dynamic
  SceneManager.TimeScale := 2;

  BoxTemplate := TCastleScene.Create(Application);
  BoxTemplate.Load(ApplicationData('box.x3d'));

  SphereTemplate := TCastleScene.Create(Application);
  SphereTemplate.Load(ApplicationData('sphere.x3d'));

  Window.Container.UIReferenceWidth := 1024;
  Window.Container.UIReferenceHeight := 768;
  Window.Container.UIScaling := usEncloseReferenceSize;

  ButtonLevelSimple := TCastleButton.Create(Application);
  ButtonLevelSimple.Caption := 'Simple Level (Plane Collider)';
  ButtonLevelSimple.OnClick := @TEventHandler(nil).LoadLevelSimple;
  ButtonLevelSimple.Anchor(hpLeft, 10);
  ButtonLevelSimple.Anchor(vpTop, -10);
  Window.Controls.InsertFront(ButtonLevelSimple);

  ButtonLevelComplex := TCastleButton.Create(Application);
  ButtonLevelComplex.Caption := 'Complex Level (Mesh Collider)';
  ButtonLevelComplex.OnClick := @TEventHandler(nil).LoadLevelComplex;
  ButtonLevelComplex.Anchor(hpLeft, 10);
  ButtonLevelComplex.Anchor(vpTop, -10 - ButtonLevelSimple.CalculatedHeight - 10);
  Window.Controls.InsertFront(ButtonLevelComplex);
end;

procedure WindowRender(Container: TUIContainer);
begin
  UIFont.PrintStrings(10, 10, Yellow, [
    Format('FPS: %s', [Container.Fps.ToString]),
    'Left mouse button - spawn box',
    'Right mouse button - spawn sphere',
    'AWSD, arrows - move, rotate',
    'F4 - toggle mouse look'
  ], false, 0);
end;

procedure WindowPress(Container: TUIContainer; const Event: TInputPressRelease);

  procedure Spawn(const Template: TCastleScene; const Collider: TCollider;
    const RigidBody: TRigidBody);
  var
    Scene: TCastleScene;
    CameraPos, CameraDir, CameraUp: TVector3;
  begin
    Scene := Template.Clone(Level);

    SceneManager.Camera.GetView(CameraPos, CameraDir, CameraUp);
    Scene.Translation := CameraPos + CameraDir * 2.0;
    Scene.Direction := CameraDir;

    SceneManager.Items.Add(Scene);

    RigidBody.InitialLinearVelocity := CameraDir * 4.0;
    Scene.RigidBody := RigidBody;
  end;

var
  C: TWalkCamera;
  RigidBody: TRigidBody;
  BoxCollider: TBoxCollider;
  SphereCollider: TSphereCollider;
begin
  if Event.IsKey(K_F4) then
  begin
    C := SceneManager.WalkCamera;
    C.MouseLook := not C.MouseLook;
  end;

  if Event.IsMouseButton(mbLeft) then
  begin
    RigidBody := TRigidBody.Create(BoxTemplate);

    BoxCollider := TBoxCollider.Create(RigidBody);
    BoxCollider.Size := BoxTemplate.BoundingBox.Size;
    BoxCollider.Restitution := 0.3;
    BoxCollider.Density := 100.0;
    Spawn(BoxTemplate, BoxCollider, RigidBody);
  end;

  if Event.IsMouseButton(mbRight) then
  begin
    RigidBody := TRigidBody.Create(SphereTemplate);

    SphereCollider := TSphereCollider.Create(RigidBody);
    SphereCollider.Radius := SphereTemplate.BoundingBox.Size.X / 2;
    SphereCollider.Friction := 0.4;
    SphereCollider.Restitution := 0.2;
    SphereCollider.Density := 20.0;
    Spawn(SphereTemplate, SphereCollider, RigidBody);
  end;
end;

initialization
  { Set ApplicationName early, as our log uses it. }
  ApplicationProperties.ApplicationName := 'physics_3d_demo';

  InitializeLog;

  { initialize Application callbacks }
  Application.OnInitialize := @ApplicationInitialize;

  { create Window and initialize Window callbacks }
  Window := TCastleWindow.Create(Application);
  Application.MainWindow := Window;
  Window.OnRender := @WindowRender;
  Window.OnPress := @WindowPress;
end.