| 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 |
{ Enemy behavior. } |
| 17 |
unit GameEnemy; |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses Classes, Generics.Collections, |
| 22 |
CastleVectors, CastleScene, CastleTransform, CastleSoundEngine; |
| 23 |
|
| 24 |
type |
| 25 |
TEnemy = class(TCastleBehavior) |
| 26 |
strict private |
| 27 |
Scene: TCastleScene; |
| 28 |
RBody: TCastleRigidBody; |
| 29 |
MoveDirection: Integer; //< Always 1 or -1 |
| 30 |
Dead: Boolean; |
| 31 |
DontFallDown: Boolean; |
| 32 |
public |
| 33 |
constructor Create(AOwner: TComponent); override; |
| 34 |
procedure ParentAfterAttach; override; |
| 35 |
procedure Update(const SecondsPassed: Single; var RemoveMe: TRemoveType); override; |
| 36 |
procedure HitPlayer; |
| 37 |
procedure TakeDamageFromBullet(const Bullet: TCastleTransform); |
| 38 |
|
| 39 |
procedure CollisionEnter(const CollisionDetails: TPhysicsCollisionDetails); |
| 40 |
end; |
| 41 |
|
| 42 |
TEnemyList = {$ifdef FPC}specialize{$endif} TObjectList<TEnemy>; |
| 43 |
|
| 44 |
implementation |
| 45 |
|
| 46 |
uses |
| 47 |
CastleLog, |
| 48 |
GameSound, GameViewPlay; |
| 49 |
|
| 50 |
{ TEnemy --------------------------------------------------------------------- } |
| 51 |
|
| 52 |
constructor TEnemy.Create(AOwner: TComponent); |
| 53 |
begin |
| 54 |
inherited Create(AOwner); |
| 55 |
MoveDirection := -1; |
| 56 |
DontFallDown := true; |
| 57 |
end; |
| 58 |
|
| 59 |
procedure TEnemy.ParentAfterAttach; |
| 60 |
begin |
| 61 |
inherited; |
| 62 |
|
| 63 |
Scene := Parent as TCastleScene; // TEnemy can only be added as behavior to TCastleScene |
| 64 |
Scene.PlayAnimation('walk', true); |
| 65 |
RBody := Scene.FindBehavior(TCastleRigidBody) as TCastleRigidBody; |
| 66 |
if RBody <> nil then |
| 67 |
RBody.OnCollisionEnter := {$ifdef FPC}@{$endif}CollisionEnter; |
| 68 |
{ In editor you can change scale to -1 1 1 to change enemy inital direction } |
| 69 |
if Scene.Scale.X < 0 then |
| 70 |
MoveDirection := 1; |
| 71 |
end; |
| 72 |
|
| 73 |
procedure TEnemy.Update(const SecondsPassed: Single; var RemoveMe: TRemoveType); |
| 74 |
const |
| 75 |
MovingSpeed = 200; |
| 76 |
var |
| 77 |
EnemyOnGround: Boolean; |
| 78 |
NeedTurn: Boolean; |
| 79 |
Vel: TVector3; |
| 80 |
RayMaxDistance: Single; |
| 81 |
ObstacleAhead: TCastleTransform; |
| 82 |
begin |
| 83 |
inherited; |
| 84 |
|
| 85 |
if Dead then |
| 86 |
begin |
| 87 |
if Scene.CurrentAnimation.X3DName <> 'dead' then |
| 88 |
Scene.PlayAnimation('dead', true); |
| 89 |
Exit; |
| 90 |
end; |
| 91 |
|
| 92 |
if RBody = nil then |
| 93 |
Exit; |
| 94 |
|
| 95 |
RayMaxDistance := Scene.BoundingBox.SizeY * 0.50 + 5; |
| 96 |
EnemyOnGround := RBody.PhysicsRayCast(Scene.Translation, |
| 97 |
Vector3(0, -1, 0), RayMaxDistance).Hit; |
| 98 |
|
| 99 |
if not EnemyOnGround then |
| 100 |
begin |
| 101 |
Scene.PlayAnimation('idle', true); |
| 102 |
Exit; |
| 103 |
end else |
| 104 |
begin |
| 105 |
if Scene.CurrentAnimation.X3DName <> 'walk' then |
| 106 |
Scene.PlayAnimation('walk', true); |
| 107 |
end; |
| 108 |
|
| 109 |
if DontFallDown then |
| 110 |
begin |
| 111 |
NeedTurn := not RBody.PhysicsRayCast(Scene.Translation |
| 112 |
+ Vector3(MoveDirection * Scene.BoundingBox.SizeX * 0.50, 0, 0), |
| 113 |
Vector3(0, -1, 0), RayMaxDistance).Hit; |
| 114 |
end else |
| 115 |
NeedTurn := false; |
| 116 |
|
| 117 |
{ Check enemy must turn because he go wall. } |
| 118 |
if not NeedTurn then |
| 119 |
begin |
| 120 |
ObstacleAhead := RBody.PhysicsRayCast(Scene.Translation, |
| 121 |
Vector3(MoveDirection, 0, 0), RayMaxDistance + 5).Transform; |
| 122 |
|
| 123 |
if ObstacleAhead <> nil then |
| 124 |
begin |
| 125 |
{ Enemy should not run away from the player or change direction when |
| 126 |
there is coin } |
| 127 |
if (ObstacleAhead.Name <> 'ScenePlayer') and |
| 128 |
(Pos('GoldCoin', ObstacleAhead.Name) = 0) and |
| 129 |
(Pos('Spider', ObstacleAhead.Name) = 0) then |
| 130 |
NeedTurn := true; |
| 131 |
end; |
| 132 |
end; |
| 133 |
|
| 134 |
if NeedTurn then |
| 135 |
MoveDirection := - MoveDirection; |
| 136 |
|
| 137 |
Vel := RBody.LinearVelocity; |
| 138 |
|
| 139 |
Vel.X := MoveDirection * MovingSpeed; |
| 140 |
|
| 141 |
Scene.Scale := Vector3(-MoveDirection, 1, 1); |
| 142 |
|
| 143 |
RBody.LinearVelocity := Vel; |
| 144 |
end; |
| 145 |
|
| 146 |
procedure TEnemy.HitPlayer; |
| 147 |
begin |
| 148 |
ViewPlay.HitPlayer; |
| 149 |
Dead := true; |
| 150 |
RBody.Exists := false; |
| 151 |
end; |
| 152 |
|
| 153 |
procedure TEnemy.TakeDamageFromBullet(const Bullet: TCastleTransform); |
| 154 |
begin |
| 155 |
SoundEngine.Play(NamedSound('HitEnemy')); |
| 156 |
Bullet.Exists := false; |
| 157 |
|
| 158 |
Dead := true; |
| 159 |
RBody.Exists := false; |
| 160 |
end; |
| 161 |
|
| 162 |
procedure TEnemy.CollisionEnter(const CollisionDetails: TPhysicsCollisionDetails); |
| 163 |
begin |
| 164 |
if Dead then |
| 165 |
Exit; |
| 166 |
|
| 167 |
if CollisionDetails.OtherTransform.Name = 'ScenePlayer' then |
| 168 |
HitPlayer |
| 169 |
else |
| 170 |
if CollisionDetails.OtherTransform is TBullet then |
| 171 |
TakeDamageFromBullet(CollisionDetails.OtherTransform); |
| 172 |
end; |
| 173 |
|
| 174 |
end. |