Debian Codegraph @

examples/physics/physics_asteroids/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, CastleTransform,
24 CastleScene, CastleViewport;
25
26 type
27 { Main view, where most of the application logic takes place. }
28
29 TBullet = class(TCastleTransform)
30 strict private
31 Duration: Single;
32 public
33 constructor Create(AOwner: TComponent; const BulletSpriteImage: TCastleTransform); reintroduce;
34 procedure Update(const SecondsPassed: Single; var RemoveMe: TRemoveType); override;
35 end;
36
37 TViewMain = class(TCastleView)
38 published
39 { Components designed using CGE editor.
40 These fields will be automatically initialized at Start. }
41 LabelFps: TCastleLabel;
42 Ship: TCastleTransform;
43 ShipRigidBody: TCastleRigidBody;
44 Viewport: TCastleViewport;
45 strict private
46 const
47 ThrustForce = 300;
48 TorqueValue = 10000;
49 var
50 BulletSpriteImage: TCastleImageTransform;
51 function InputLeft: Boolean;
52 function InputRight: Boolean;
53 function InputUp: Boolean;
54 function InputDown: Boolean;
55 public
56 constructor Create(AOwner: TComponent); override;
57 procedure Start; override;
58 procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override;
59 function Press(const Event: TInputPressRelease): Boolean; override;
60 end;
61
62 var
63 ViewMain: TViewMain;
64
65 implementation
66
67 uses SysUtils, CastleLog, Math;
68
69 { TBullet -------------------------------------------------------------------- }
70
71 constructor TBullet.Create(AOwner: TComponent; const BulletSpriteImage: TCastleTransform);
72 var
73 RBody: TCastleRigidBody;
74 Col: TCastleSphereCollider;
75 begin
76 inherited Create(AOwner);
77
78 // BulletSpriteImage is reused for all bullets
79 Add(BulletSpriteImage);
80
81 RBody := TCastleRigidBody.Create(Self);
82 RBody.Setup2D;
83 RBody.Dynamic := true;
84 RBody.MaxLinearVelocity := 0;
85 RBody.Gravity := false;
86 AddBehavior(RBody);
87
88 Col := TCastleSphereCollider.Create(Self);
89 Col.Mass := 1;
90
91 AddBehavior(Col);
92 end;
93
94 procedure TBullet.Update(const SecondsPassed: Single;
95 var RemoveMe: TRemoveType);
96 begin
97 inherited Update(SecondsPassed, RemoveMe);
98
99 Duration := Duration + SecondsPassed;
100 if Duration > 3 then
101 RemoveMe := rtRemoveAndFree;
102 end;
103
104 { TViewMain ----------------------------------------------------------------- }
105
106 function TViewMain.InputLeft: Boolean;
107 begin
108 Result :=
109 Container.Pressed.Items[keyA] or
110 Container.Pressed.Items[keyArrowLeft];
111 end;
112
113 function TViewMain.InputRight: Boolean;
114 begin
115 Result :=
116 Container.Pressed.Items[keyD] or
117 Container.Pressed.Items[keyArrowRight];
118 end;
119
120 function TViewMain.InputUp: Boolean;
121 begin
122 Result :=
123 Container.Pressed.Items[keyW] or
124 Container.Pressed.Items[keyArrowUp];
125 end;
126
127 function TViewMain.InputDown: Boolean;
128 begin
129 Result :=
130 Container.Pressed.Items[keyS] or
131 Container.Pressed.Items[keyArrowDown];
132 end;
133
134 constructor TViewMain.Create(AOwner: TComponent);
135 begin
136 inherited;
137 DesignUrl := 'castle-data:/gameviewmain.castle-user-interface';
138 end;
139
140 procedure TViewMain.Start;
141 begin
142 inherited;
143
144 BulletSpriteImage := TCastleImageTransform.Create(FreeAtStop);
145 BulletSpriteImage.URL := 'castle-data:/graphics/bullet/bullet.png';
146 BulletSpriteImage.Scale := Vector3(2, 2, 2);
147 end;
148
149 procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: Boolean);
150 var
151 Torque: TVector3;
152 Direction: TVector3;
153 begin
154 inherited;
155 { This virtual method is executed every frame (many times per second). }
156 LabelFps.Caption := 'FPS: ' + Container.Fps.ToString;
157
158 if InputUp then
159 begin
160 Direction := Vector3(0, 1, 0);
161 ShipRigidBody.AddForce(Direction * ThrustForce, true);
162 end;
163
164 Torque := Vector3(0, 0, 0);
165
166 if InputLeft then
167 Torque := Vector3(0, 0, 1);
168
169 if InputRight then
170 Torque := Vector3(0, 0, -1);
171
172 if not Torque.IsPerfectlyZero then
173 ShipRigidBody.AddTorque(Torque * TorqueValue);
174 end;
175
176 function TViewMain.Press(const Event: TInputPressRelease): Boolean;
177 var
178 Bullet: TBullet;
179 BullletBody: TCastleRigidBody;
180 Direction: TVector3;
181 begin
182 Result := inherited;
183 if Result then Exit; // allow the ancestor to handle keys
184
185 { This virtual method is executed when user presses
186 a key, a mouse button, or touches a touch-screen.
187
188 Note that each UI control has also events like OnPress and OnClick.
189 These events can be used to handle the "press", if it should do something
190 specific when used in that UI control.
191 The TViewMain.Press method should be used to handle keys
192 not handled in children controls.
193 }
194
195 // Use this to handle keys:
196 if Event.IsKey(keySpace) then
197 begin
198 Bullet := TBullet.Create(FreeAtStop, BulletSpriteImage);
199 Bullet.Translation := Ship.LocalToWorld(Vector3(0, Ship.LocalBoundingBox.SizeY / 2 + 5 , 0));
200 Viewport.Items.Add(Bullet);
201
202 Direction := Ship.LocalToWorldDirection(Vector3(0,1,0));
203
204 BullletBody := Bullet.FindBehavior(TCastleRigidBody) as TCastleRigidBody;
205 { Change Bullet.Translation to Vector3(0,1,0) to keep the bullets rotating }
206 BullletBody.ApplyImpulse(Direction * 500, {Vector3(0,1,0)} Bullet.Translation);
207
208 Exit(true); // key was handled
209 end;
210 end;
211
212 end.