| 1 |
{ |
| 2 |
Copyright 2020-2022 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 |
|
| 18 |
This is an exact copy of enemy AI from standard CGE template "3D FPS game". |
| 19 |
It was not modified at all for the 3rd-person navigation demo. } |
| 20 |
unit GameEnemy; |
| 21 |
|
| 22 |
interface |
| 23 |
|
| 24 |
uses Classes, Generics.Collections, |
| 25 |
CastleVectors, CastleScene, CastleTransform; |
| 26 |
|
| 27 |
type |
| 28 |
{ Simple enemy intelligence. |
| 29 |
It controls the parent Scene (TCastleScene): moves it, runs animations of it etc. |
| 30 |
|
| 31 |
This is a TCastleBehavior descendant, |
| 32 |
and is inserted to parent like EnemyScene.AddBehavior(...). |
| 33 |
You can get the TEnemy instance of a TCastleScene, |
| 34 |
by taking "Scene.FindBehavior(TEnemy)". |
| 35 |
|
| 36 |
Other ways of making an association TCastleScene <-> TEnemy logic are possible: |
| 37 |
|
| 38 |
- TEnemy could be an independent class (not connected to any CGE class), |
| 39 |
and simply have a reference to CGE TCastleScene instance. |
| 40 |
|
| 41 |
This makes it easy to map TEnemy->TCastleScene. |
| 42 |
To map TCastleScene->TEnemy you could e.g. use TCastleScene.Tag, |
| 43 |
or a dedicated map structure like TDictionary from Generics.Collections. |
| 44 |
|
| 45 |
- You could also make TEnemy a descendant of TCastleScene. |
| 46 |
|
| 47 |
Note that TCastleBehavior or TCastleTransform descendants could be |
| 48 |
registered in the CGE editor to visually add and edit them from editor. |
| 49 |
See https://castle-engine.io/manual_editor.php#section_custom_components . |
| 50 |
In this unit we call RegisterSerializableComponent, |
| 51 |
so you only need to add editor_units="GameEnemy" to CastleEngineManifest.xml to see it in action. |
| 52 |
} |
| 53 |
TEnemy = class(TCastleBehavior) |
| 54 |
strict private |
| 55 |
Scene: TCastleScene; |
| 56 |
MoveDirection: Integer; //< Always 1 or -1 |
| 57 |
Dead: Boolean; |
| 58 |
public |
| 59 |
constructor Create(AOwner: TComponent); override; |
| 60 |
procedure ParentAfterAttach; override; |
| 61 |
procedure Update(const SecondsPassed: Single; var RemoveMe: TRemoveType); override; |
| 62 |
procedure Hurt; |
| 63 |
end; |
| 64 |
|
| 65 |
TEnemyList = {$ifdef FPC}specialize{$endif} TObjectList<TEnemy>; |
| 66 |
|
| 67 |
implementation |
| 68 |
|
| 69 |
uses CastleComponentSerialize; |
| 70 |
|
| 71 |
constructor TEnemy.Create(AOwner: TComponent); |
| 72 |
begin |
| 73 |
inherited; |
| 74 |
MoveDirection := -1; |
| 75 |
end; |
| 76 |
|
| 77 |
procedure TEnemy.ParentAfterAttach; |
| 78 |
begin |
| 79 |
inherited; |
| 80 |
Scene := Parent as TCastleScene; // TEnemy can only be added as behavior to TCastleScene |
| 81 |
Scene.PlayAnimation('walk', true); |
| 82 |
end; |
| 83 |
|
| 84 |
procedure TEnemy.Update(const SecondsPassed: Single; var RemoveMe: TRemoveType); |
| 85 |
const |
| 86 |
MovingSpeed = 2; |
| 87 |
begin |
| 88 |
inherited; |
| 89 |
|
| 90 |
if Dead then Exit; |
| 91 |
|
| 92 |
// We modify the Z coordinate, responsible for enemy going forward |
| 93 |
Scene.Translation := Scene.Translation + |
| 94 |
Vector3(0, 0, MoveDirection * SecondsPassed * MovingSpeed); |
| 95 |
|
| 96 |
Scene.Direction := Vector3(0, 0, MoveDirection); |
| 97 |
|
| 98 |
// Toggle MoveDirection between 1 and -1 |
| 99 |
if Scene.Translation.Z > 5 then |
| 100 |
MoveDirection := -1 |
| 101 |
else |
| 102 |
if Scene.Translation.Z < -5 then |
| 103 |
MoveDirection := 1; |
| 104 |
end; |
| 105 |
|
| 106 |
procedure TEnemy.Hurt; |
| 107 |
var |
| 108 |
RBody: TCastleRigidBody; |
| 109 |
begin |
| 110 |
Scene.PlayAnimation('die', false); |
| 111 |
|
| 112 |
// dead corpse no longer collides |
| 113 |
// old physics: |
| 114 |
Scene.Pickable := false; |
| 115 |
Scene.Collides := false; |
| 116 |
// new physics: |
| 117 |
RBody := Scene.FindBehavior(TCastleRigidBody) as TCastleRigidBody; |
| 118 |
if RBody <> nil then |
| 119 |
RBody.Exists := false; |
| 120 |
|
| 121 |
Dead := true; |
| 122 |
end; |
| 123 |
|
| 124 |
initialization |
| 125 |
RegisterSerializableComponent(TEnemy, 'Enemy'); |
| 126 |
end. |