Debian Codegraph @

examples/platformer/code/gamefallingobstacle.pas

raw
1 {
2 Copyright 2021-2021 Andrzej Kilijański, 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 { Behavior of falling obstacles (TFallingObstacle). }
17 unit GameFallingObstacle;
18
19 interface
20
21 uses Classes, Generics.Collections,
22 CastleVectors, CastleScene, CastleTransform;
23
24 type
25 TFallingObstacle = class(TCastleBehavior)
26 strict private
27 Scene: TCastleScene;
28 RBody: TCastleRigidBody;
29 { Set to true when player was bellow and started falling }
30 IsFalling: Boolean;
31 public
32 constructor Create(AOwner: TComponent); override;
33 procedure ParentAfterAttach; override;
34 procedure Update(const SecondsPassed: Single; var RemoveMe: TRemoveType); override;
35 procedure HitPlayer;
36
37 procedure CollisionEnter(const CollisionDetails: TPhysicsCollisionDetails);
38 end;
39
40 TFallingObstaclesList = {$ifdef FPC}specialize{$endif} TObjectList<TFallingObstacle>;
41
42 implementation
43
44 uses GameViewPlay;
45
46 { TFallingObstacle ----------------------------------------------------------- }
47
48 constructor TFallingObstacle.Create(AOwner: TComponent);
49 begin
50 inherited Create(AOwner);
51 IsFalling := false;
52 end;
53
54 procedure TFallingObstacle.ParentAfterAttach;
55 begin
56 inherited ParentAfterAttach;
57
58 Scene := Parent as TCastleScene;
59
60 { TCastleRigidBody was added in the editor, here we configure only the events. }
61 RBody := Scene.FindBehavior(TCastleRigidBody) as TCastleRigidBody;
62 if RBody <> nil then
63 RBody.OnCollisionEnter := {$ifdef FPC}@{$endif}CollisionEnter;
64 end;
65
66 procedure TFallingObstacle.Update(const SecondsPassed: Single;
67 var RemoveMe: TRemoveType);
68 var
69 RayHitThat: TCastleTransform;
70 begin
71 inherited Update(SecondsPassed, RemoveMe);
72
73 if not IsFalling then
74 begin
75 { Here we wait for player to start falling down }
76
77 if RBody <> nil then
78 begin
79 RayHitThat := RBody.PhysicsRayCast(Scene.Translation, Vector3(0, -1, 0), 300).Transform;
80
81 { Check was that a player? }
82 if (RayHitThat <> nil) and (Pos('ScenePlayer', RayHitThat.Name) > 0) then
83 begin
84 { Start falling down }
85 RBody.Gravity := true;
86 RBody.LinearVelocity := Vector3(0, -500, 0);
87 IsFalling := true;
88 end;
89 end;
90 end;
91 end;
92
93 procedure TFallingObstacle.HitPlayer;
94 begin
95 ViewPlay.HitPlayer;
96 Scene.Exists := false;
97 end;
98
99 procedure TFallingObstacle.CollisionEnter(
100 const CollisionDetails: TPhysicsCollisionDetails);
101 begin
102 // TODO: We should check by instance reference, not by name
103 if Pos('ScenePlayer', CollisionDetails.OtherTransform.Name) > 0 then
104 begin
105 HitPlayer;
106 end else
107 begin
108 { Once the spike hits the ground, it should no longer be able to
109 hurt the player. }
110 RBody.Exists := false;
111 end;
112 end;
113
114 end.