| 1 |
{ |
| 2 |
Copyright 2024-2024 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 |
{ Component to manage rocks. } |
| 17 |
unit GameRocksManager; |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses Classes, |
| 22 |
CastleVectors, CastleTransform, CastleUiControls, CastleComponentSerialize, |
| 23 |
CastleControls, CastleTimeUtils, CastleViewport; |
| 24 |
|
| 25 |
type |
| 26 |
{ Manage rocks. } |
| 27 |
TRocksManager = class(TCastleUserInterface) |
| 28 |
private |
| 29 |
RockFactory: TCastleComponentFactory; |
| 30 |
TimeToNextRock: TFloatTime; |
| 31 |
FRocksDestroyed: Cardinal; |
| 32 |
procedure RockCollisionEnter(const CollisionDetails: TPhysicsCollisionDetails); |
| 33 |
public |
| 34 |
{ Set this to the parent of all rocks. |
| 35 |
Should not be transformed, because (for simplicity) code here assumes |
| 36 |
that translations/rotations inside this correspond to |
| 37 |
the translations/rotations in the world coordinates of the viewport. } |
| 38 |
RocksParent: TCastleTransform; |
| 39 |
|
| 40 |
constructor Create(AOwner: TComponent); override; |
| 41 |
procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override; |
| 42 |
|
| 43 |
{ Number of rocks destroyed. E.g. to show player score. } |
| 44 |
property RocksDestroyed: Cardinal read FRocksDestroyed; |
| 45 |
|
| 46 |
{ Make sure rock resources, like the texture, is initialized. |
| 47 |
Otherwise the first rock creation would cause a noticeable stutter |
| 48 |
when spawning. } |
| 49 |
procedure InitializeRockResources(const ParentViewport: TCastleViewport); |
| 50 |
end; |
| 51 |
|
| 52 |
implementation |
| 53 |
|
| 54 |
uses SysUtils, |
| 55 |
CastleUtils, CastleRectangles, CastleApplicationProperties, |
| 56 |
GameBehaviors; |
| 57 |
|
| 58 |
constructor TRocksManager.Create(AOwner: TComponent); |
| 59 |
begin |
| 60 |
inherited; |
| 61 |
RockFactory := TCastleComponentFactory.Create(Self); |
| 62 |
RockFactory.Url := 'castle-data:/rocks/rock.castle-transform'; |
| 63 |
|
| 64 |
TimeToNextRock := RandomFloatRange(0.25, 1.0); |
| 65 |
end; |
| 66 |
|
| 67 |
procedure TRocksManager.InitializeRockResources(const ParentViewport: TCastleViewport); |
| 68 |
var |
| 69 |
TestRock: TCastleTransform; |
| 70 |
begin |
| 71 |
TestRock := RockFactory.ComponentLoad(Self) as TCastleTransform; |
| 72 |
ParentViewport.PrepareResources(TestRock); |
| 73 |
// FreeAndNil(TestRock) // do not free it, let it exist, to hold reference count to the texture |
| 74 |
end; |
| 75 |
|
| 76 |
type |
| 77 |
TRockDesign = class(TPersistent) |
| 78 |
published |
| 79 |
SceneToRotate: TCastleTransform; |
| 80 |
RockRigidBody: TCastleRigidBody; |
| 81 |
end; |
| 82 |
|
| 83 |
procedure TRocksManager.Update(const SecondsPassed: Single; var HandleInput: Boolean); |
| 84 |
|
| 85 |
procedure SpawnRock; |
| 86 |
var |
| 87 |
Rock: TCastleTransform; |
| 88 |
RockDesign: TRockDesign; |
| 89 |
RockOwner: TComponent; |
| 90 |
RockBehavior: TRockBehavior; |
| 91 |
RockAutoRemoveBehavior: TAutoRemoveLeftBehavior; |
| 92 |
begin |
| 93 |
// create a rock |
| 94 |
RockDesign := TRockDesign.Create; |
| 95 |
try |
| 96 |
RockOwner := TComponent.Create(Self); |
| 97 |
|
| 98 |
Rock := RockFactory.ComponentLoad(RockOwner, RockDesign) as TCastleTransform; |
| 99 |
|
| 100 |
RockAutoRemoveBehavior := TAutoRemoveLeftBehavior.Create(RockOwner); |
| 101 |
RockAutoRemoveBehavior.RemoveOwner := RockOwner; |
| 102 |
Rock.AddBehavior(RockAutoRemoveBehavior); |
| 103 |
|
| 104 |
RockBehavior := TRockBehavior.Create(RockOwner); |
| 105 |
RockBehavior.RemoveOwner := RockOwner; |
| 106 |
Rock.AddBehavior(RockBehavior); |
| 107 |
|
| 108 |
RockDesign.SceneToRotate.AddBehavior(TRotateBehavior.Create(RockOwner)); |
| 109 |
|
| 110 |
{ Note: do not put initial X too far beyond the screen, otherwise |
| 111 |
it could be too often destroyed when off-screen, which isn't impressive |
| 112 |
for the player. } |
| 113 |
Rock.Translation := Vector3(1100, RandomFloatRange(-500, 500), 100); |
| 114 |
|
| 115 |
RocksParent.Add(Rock); |
| 116 |
|
| 117 |
RockDesign.RockRigidBody.LinearVelocity := Vector3(-500, 0, 0); |
| 118 |
RockDesign.RockRigidBody.OnCollisionEnter := {$ifdef FPC}@{$endif} RockCollisionEnter; |
| 119 |
finally FreeAndNil(RockDesign) end; |
| 120 |
end; |
| 121 |
|
| 122 |
begin |
| 123 |
inherited; |
| 124 |
TimeToNextRock := TimeToNextRock - SecondsPassed; |
| 125 |
while TimeToNextRock < 0.0 do |
| 126 |
begin |
| 127 |
SpawnRock; |
| 128 |
TimeToNextRock := TimeToNextRock + RandomFloatRange(0.0, 1.0); |
| 129 |
end; |
| 130 |
end; |
| 131 |
|
| 132 |
procedure TRocksManager.RockCollisionEnter(const CollisionDetails: TPhysicsCollisionDetails); |
| 133 |
var |
| 134 |
Rock: TRockBehavior; |
| 135 |
Rocket: TRocketBehavior; |
| 136 |
begin |
| 137 |
Rock := CollisionDetails.Sender.FindBehavior(TRockBehavior) as TRockBehavior; |
| 138 |
Rocket := CollisionDetails.OtherTransform.FindBehavior(TRocketBehavior) as TRocketBehavior; |
| 139 |
// Abort if this is not a collision between rock and rocket |
| 140 |
if (Rock = nil) or (Rocket = nil) then |
| 141 |
Exit; |
| 142 |
|
| 143 |
if not Rocket.Armed then |
| 144 |
Exit; |
| 145 |
|
| 146 |
{ TODO: We could do something more interesting here, like play a sound, |
| 147 |
or play pretty rock exploding animation. |
| 148 |
For now, we just make the rock disappear. |
| 149 |
The rocket that hit the rock also disappears. } |
| 150 |
ApplicationProperties.FreeDelayed(Rock.RemoveOwner); |
| 151 |
ApplicationProperties.FreeDelayed(Rocket.RemoveOwner); |
| 152 |
|
| 153 |
Inc(FRocksDestroyed); |
| 154 |
end; |
| 155 |
|
| 156 |
end. |