| 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 |
{ Small useful behaviors (TCastleBehavior descendants) used in this game. } |
| 17 |
unit GameBehaviors; |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses Classes, |
| 22 |
CastleVectors, CastleTransform; |
| 23 |
|
| 24 |
type |
| 25 |
{ Remove the parent when it goes outside of the possibly visible space. } |
| 26 |
TAutoRemoveBehavior = class(TCastleBehavior) |
| 27 |
public |
| 28 |
RemoveOwner: TComponent; //< Remove by freeing this |
| 29 |
procedure Update(const SecondsPassed: Single; var RemoveMe: TRemoveType); override; |
| 30 |
end; |
| 31 |
|
| 32 |
{ Remove the parent when it goes outside of the @italic(left border) |
| 33 |
of possibly visible space. |
| 34 |
Compared to TAutoRemoveBehavior, this doesn't check other borders. } |
| 35 |
TAutoRemoveLeftBehavior = class(TCastleBehavior) |
| 36 |
public |
| 37 |
RemoveOwner: TComponent; //< Remove by freeing this |
| 38 |
procedure Update(const SecondsPassed: Single; var RemoveMe: TRemoveType); override; |
| 39 |
end; |
| 40 |
|
| 41 |
{ Slowly rotate around a random axis.} |
| 42 |
TRotateBehavior = class(TCastleBehavior) |
| 43 |
private |
| 44 |
Axis: TVector3; |
| 45 |
public |
| 46 |
constructor Create(AOwner: TComponent); override; |
| 47 |
procedure Update(const SecondsPassed: Single; var RemoveMe: TRemoveType); override; |
| 48 |
end; |
| 49 |
|
| 50 |
{ Behavior for rockets. |
| 51 |
You can add here any additional rocket-specific behavior. |
| 52 |
It is also useful to detect what collided with what in TRocksManager.RockCollisionEnter. } |
| 53 |
TRocketBehavior = class(TCastleBehavior) |
| 54 |
public |
| 55 |
RemoveOwner: TComponent; //< Remove by freeing this |
| 56 |
// Does the rocket explode when it hits something. |
| 57 |
Armed: Boolean; |
| 58 |
end; |
| 59 |
|
| 60 |
{ Behavior for rocks. |
| 61 |
You can add here any additional rock-specific behavior. |
| 62 |
It is also useful to detect what collided with what in TRocksManager.RockCollisionEnter. } |
| 63 |
TRockBehavior = class(TCastleBehavior) |
| 64 |
public |
| 65 |
RemoveOwner: TComponent; //< Remove by freeing this |
| 66 |
end; |
| 67 |
|
| 68 |
implementation |
| 69 |
|
| 70 |
uses Math, |
| 71 |
CastleRectangles, CastleApplicationProperties; |
| 72 |
|
| 73 |
{ TAutoRemoveBehavior -------------------------------------------------------- } |
| 74 |
|
| 75 |
procedure TAutoRemoveBehavior.Update(const SecondsPassed: Single; |
| 76 |
var RemoveMe: TRemoveType); |
| 77 |
const |
| 78 |
{ Rockets are removed when they go far away. |
| 79 |
This rectangle was chosen to be large enough to cover the whole screen. |
| 80 |
|
| 81 |
Vertically, it covers the screen for 100%: |
| 82 |
Height is 1200, for sure larger than Viewport.Camera.Orthographic.Height = 1000. |
| 83 |
|
| 84 |
Horizontally, it covers the screen as long as aspect ratio is <= 2:1. |
| 85 |
|
| 86 |
We *could* adjust it to Viewport.Camera.Orthographic.EffectiveRect |
| 87 |
sizes, and be more precise. But this would mean that game balance works |
| 88 |
a bit differently (rockets reach more or less further) depending on |
| 89 |
the window size aspect ratio. } |
| 90 |
RocketAllowedPositions: TFloatRectangle = ( |
| 91 |
Left: -1000; Bottom: -600; Width: 2000; Height: 1200 |
| 92 |
); |
| 93 |
begin |
| 94 |
inherited; |
| 95 |
if not RocketAllowedPositions.Contains(Parent.WorldTranslation.XY) then |
| 96 |
ApplicationProperties.FreeDelayed(RemoveOwner); |
| 97 |
end; |
| 98 |
|
| 99 |
{ TAutoRemoveLeftBehavior -------------------------------------------------------- } |
| 100 |
|
| 101 |
procedure TAutoRemoveLeftBehavior.Update(const SecondsPassed: Single; |
| 102 |
var RemoveMe: TRemoveType); |
| 103 |
begin |
| 104 |
inherited; |
| 105 |
if Parent.WorldTranslation.X < -1500 then |
| 106 |
ApplicationProperties.FreeDelayed(RemoveOwner); |
| 107 |
end; |
| 108 |
|
| 109 |
{ TRotateBehavior ------------------------------------------------------------ } |
| 110 |
|
| 111 |
constructor TRotateBehavior.Create(AOwner: TComponent); |
| 112 |
|
| 113 |
{ Good uniform random choice of direction. |
| 114 |
See CastleInternalSphereSampling . } |
| 115 |
function RandomHemispherePointConstXYZ: TVector3; |
| 116 |
var |
| 117 |
R1, R2, SqRoot: Single; |
| 118 |
C, S: Single; |
| 119 |
begin |
| 120 |
R1 := Random; |
| 121 |
R2 := Random; |
| 122 |
SinCos(2 * Pi * R1, S, C); |
| 123 |
SqRoot := Sqrt(1 - Sqr(R2)); |
| 124 |
|
| 125 |
Result.X := C * SqRoot; |
| 126 |
Result.Y := S * SqRoot; |
| 127 |
Result.Z := R2; |
| 128 |
end; |
| 129 |
|
| 130 |
begin |
| 131 |
inherited; |
| 132 |
Axis := RandomHemispherePointConstXYZ; |
| 133 |
end; |
| 134 |
|
| 135 |
procedure TRotateBehavior.Update(const SecondsPassed: Single; |
| 136 |
var RemoveMe: TRemoveType); |
| 137 |
const |
| 138 |
RotationSpeed = 0.5; |
| 139 |
begin |
| 140 |
inherited; |
| 141 |
Parent.Rotation := Vector4(Axis, Parent.Rotation.W + RotationSpeed * SecondsPassed); |
| 142 |
end; |
| 143 |
|
| 144 |
end. |