Debian Codegraph @

examples/physics/physics_3d_shooter/code/gameviewmain.pas

raw
1 {
2 Copyright 2022-2022 Michalis Kamburelis, Andrzej Kilijański.
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 { Main view, where most of the application logic takes place. }
17 unit GameViewMain;
18
19 interface
20
21 uses Classes,
22 CastleVectors, CastleComponentSerialize,
23 CastleUIControls, CastleControls, CastleKeysMouse, CastleCameras,
24 CastleViewport;
25
26 type
27 { Main view, where most of the application logic takes place. }
28 TViewMain = class(TCastleView)
29 published
30 { Components designed using CGE editor.
31 These fields will be automatically initialized at Start. }
32 LabelFps: TCastleLabel;
33 WalkNavigation: TCastleWalkNavigation;
34 Viewport: TCastleViewport;
35 public
36 constructor Create(AOwner: TComponent); override;
37 procedure Start; override;
38 procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override;
39 function Press(const Event: TInputPressRelease): Boolean; override;
40 end;
41
42 var
43 ViewMain: TViewMain;
44
45 implementation
46
47 uses SysUtils,
48 CastleTransform;
49
50 { TViewMain ----------------------------------------------------------------- }
51
52 constructor TViewMain.Create(AOwner: TComponent);
53 begin
54 inherited;
55 DesignUrl := 'castle-data:/gameviewmain.castle-user-interface';
56 end;
57
58 procedure TViewMain.Start;
59 begin
60 inherited;
61 end;
62
63 procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: Boolean);
64 begin
65 inherited;
66 { This virtual method is executed every frame (many times per second). }
67 LabelFps.Caption := 'FPS: ' + Container.Fps.ToString;
68 WalkNavigation.MouseLook := buttonRight in Container.MousePressed;
69 end;
70
71 function TViewMain.Press(const Event: TInputPressRelease): Boolean;
72
73 procedure AddCollectionOfBoxes;
74 var
75 Boxes: TCastleTransform;
76 begin
77 Boxes := TransformLoad('castle-data:/collection_of_boxes.castle-transform', FreeAtStop);
78 Boxes.Translation := Viewport.Camera.Translation + Vector3(0, 3, 0) + Viewport.Camera.Direction * 20;
79 Boxes.Direction := Viewport.Camera.Direction;
80 Viewport.Items.Add(Boxes);
81 end;
82
83 procedure AddBullet;
84 var
85 BulletOwner: TComponent;
86 Bullet: TCastleTransform;
87 BulletRigidBody: TCastleRigidBody;
88 begin
89 { Bullet's owner is BulletOwner, not directly FreeAtStop.
90 This way we know that names are local within BulletOwner,
91 and we can later look for 'BulletRigidBody' without risking that we
92 will find rigid body from some older bullet. }
93 BulletOwner := TComponent.Create(FreeAtStop);
94 Bullet := TransformLoad('castle-data:/bullet_with_physics.castle-transform', BulletOwner);
95 Bullet.Translation := Viewport.Camera.Translation;
96 Bullet.Direction := Viewport.Camera.Direction;
97 Bullet.Collides := false; // do not collide with player
98 BulletRigidBody := BulletOwner.FindRequiredComponent('BulletRigidBody') as TCastleRigidBody;
99 BulletRigidBody.LinearVelocity := Viewport.Camera.Direction * 100;
100 { You can turn off gravity for Bullet to make it easier to shoot high objects
101 even when initial LinearVelocity would be low.
102 Of course this is non-realistic. }
103 //BulletRigidBody.Gravity := false;
104 Viewport.Items.Add(Bullet);
105
106 { Instead of setting BulletRigidBody.LinearVelocity directly (see line above)
107 you can also add force to push the bullet.
108 Both ApplyImpulse and AddForce can be used.
109 Try it out -- comment out above "BulletRigidBody.LinearVelocity := ..."
110 and uncomment one of the lines below. }
111 //BulletRigidBody.ApplyImpulse(Viewport.Camera.Direction * 50, Viewport.Camera.Translation);
112 //BulletRigidBody.AddForce(Viewport.Camera.Direction * 5000, false);
113 end;
114
115 begin
116 Result := inherited;
117 if Result then Exit; // allow the ancestor to handle keys
118
119 if Event.IsMouseButton(buttonLeft) then
120 begin
121 if mkShift in Event.ModifiersDown then
122 AddCollectionOfBoxes
123 else
124 AddBullet;
125 Exit(true);
126 end;
127 end;
128
129 end.