| 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 rockets. } |
| 17 |
unit GameRocketsManager; |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses Classes, |
| 22 |
CastleVectors, CastleTransform, CastleUiControls, CastleComponentSerialize, |
| 23 |
CastleControls; |
| 24 |
|
| 25 |
type |
| 26 |
{ Manage rockets. } |
| 27 |
TRocketsManager = class(TCastleUserInterface) |
| 28 |
private |
| 29 |
RocketFactory: TCastleComponentFactory; |
| 30 |
CannonTimers: array of TCastleTimer; |
| 31 |
procedure CannonTimer(Sender: TObject); |
| 32 |
public |
| 33 |
{ Set this to the list of cannons on player's spaceship. |
| 34 |
Call InitializeCannons after setting this. } |
| 35 |
Cannons: array of TCastleTransform; |
| 36 |
|
| 37 |
{ Set this to the parent of all rockets. |
| 38 |
Should not be transformed, because (for simplicity) code here assumes |
| 39 |
that translations/rotations inside RocketsParent correspond to |
| 40 |
the translations/rotations in the world coordinates of the viewport. } |
| 41 |
RocketsParent: TCastleTransform; |
| 42 |
|
| 43 |
ShootsArmed: Boolean; |
| 44 |
|
| 45 |
constructor Create(AOwner: TComponent); override; |
| 46 |
|
| 47 |
{ Call after you set Cannons. } |
| 48 |
procedure InitializeCannons; |
| 49 |
end; |
| 50 |
|
| 51 |
implementation |
| 52 |
|
| 53 |
uses SysUtils, |
| 54 |
CastleUtils, CastleRectangles, CastleColors, CastleScene, |
| 55 |
GameBehaviors; |
| 56 |
|
| 57 |
constructor TRocketsManager.Create(AOwner: TComponent); |
| 58 |
begin |
| 59 |
inherited; |
| 60 |
RocketFactory := TCastleComponentFactory.Create(Self); |
| 61 |
RocketFactory.Url := 'castle-data:/rocket.castle-transform'; |
| 62 |
end; |
| 63 |
|
| 64 |
procedure TRocketsManager.InitializeCannons; |
| 65 |
var |
| 66 |
I: Integer; |
| 67 |
begin |
| 68 |
// make sure to clean previous timers, in case you call InitializeCannons many times |
| 69 |
for I := 0 to High(CannonTimers) do |
| 70 |
FreeAndNil(CannonTimers[I]); |
| 71 |
|
| 72 |
// create TCastleTimer for each cannon |
| 73 |
SetLength(CannonTimers, Length(Cannons)); |
| 74 |
for I := 0 to High(Cannons) do |
| 75 |
begin |
| 76 |
CannonTimers[I] := TCastleTimer.Create(Self); |
| 77 |
// for this demo, it seems nice to randomize cannons' shooting times |
| 78 |
CannonTimers[I].IntervalSeconds := RandomFloatRange(0.1, 0.3); |
| 79 |
CannonTimers[I].OnTimer := {$ifdef FPC}@{$endif} CannonTimer; |
| 80 |
CannonTimers[I].Tag := I; |
| 81 |
InsertFront(CannonTimers[I]); |
| 82 |
end; |
| 83 |
end; |
| 84 |
|
| 85 |
type |
| 86 |
TRocketDesign = class(TPersistent) |
| 87 |
published |
| 88 |
RocketRigidBody: TCastleRigidBody; |
| 89 |
RootSphere: TCastleSphere; |
| 90 |
end; |
| 91 |
|
| 92 |
procedure TRocketsManager.CannonTimer(Sender: TObject); |
| 93 |
var |
| 94 |
Rocket, Cannon: TCastleTransform; |
| 95 |
RocketDesign: TRocketDesign; |
| 96 |
RocketDirection: TVector3; |
| 97 |
RocketOwner: TComponent; |
| 98 |
RocketBehavior: TRocketBehavior; |
| 99 |
RocketAutoRemoveBehavior: TAutoRemoveBehavior; |
| 100 |
begin |
| 101 |
// create a rocket |
| 102 |
RocketDesign := TRocketDesign.Create; |
| 103 |
try |
| 104 |
RocketOwner := TComponent.Create(Self); |
| 105 |
|
| 106 |
Rocket := RocketFactory.ComponentLoad(RocketOwner, RocketDesign) as TCastleTransform; |
| 107 |
|
| 108 |
RocketAutoRemoveBehavior := TAutoRemoveBehavior.Create(RocketOwner); |
| 109 |
RocketAutoRemoveBehavior.RemoveOwner := RocketOwner; |
| 110 |
Rocket.AddBehavior(RocketAutoRemoveBehavior); |
| 111 |
|
| 112 |
RocketBehavior := TRocketBehavior.Create(RocketOwner); |
| 113 |
RocketBehavior.RemoveOwner := RocketOwner; |
| 114 |
RocketBehavior.Armed := ShootsArmed; |
| 115 |
Rocket.AddBehavior(RocketBehavior); |
| 116 |
|
| 117 |
if ShootsArmed then |
| 118 |
RocketDesign.RootSphere.Color := Red |
| 119 |
else |
| 120 |
RocketDesign.RootSphere.Color := Yellow; |
| 121 |
|
| 122 |
Cannon := Cannons[(Sender as TCastleTimer).Tag]; |
| 123 |
Rocket.Translation := Cannon.WorldTranslation; |
| 124 |
|
| 125 |
RocketsParent.Add(Rocket); |
| 126 |
|
| 127 |
{ Use Vector3(0, 1, 0) to determine the rocket direction, |
| 128 |
because we happened to arrange Cannon transformations in editor |
| 129 |
such that +Y (green arrow) points in the direction in which the cannon |
| 130 |
should shoot. |
| 131 |
|
| 132 |
Note: Do not use Cannon.Direction, which assumes typical 3D direction/up |
| 133 |
following glTF orientation, rotated by Cannon.Rotation. |
| 134 |
This is not really useful for us here, despite a tempting sounding name. } |
| 135 |
RocketDirection := Cannon.LocalToWorldDirection(Vector3(0, 1, 0)); |
| 136 |
RocketDesign.RocketRigidBody.LinearVelocity := RocketDirection * 1000; |
| 137 |
finally FreeAndNil(RocketDesign) end; |
| 138 |
end; |
| 139 |
|
| 140 |
end. |