Debian Codegraph @

tools/castle-editor/data/project_templates/3d_fps_game/files/code/gameenemy.pas

raw
1 { Enemy behavior.
2
3 Feel free to use this code as a starting point for your own projects.
4 This template code is in public domain, unlike most other CGE code which
5 is covered by BSD or LGPL (see https://castle-engine.io/license). }
6 unit GameEnemy;
7
8 interface
9
10 uses Classes, Generics.Collections,
11 CastleVectors, CastleScene, CastleTransform;
12
13 type
14 { Simple enemy intelligence.
15 It controls the parent Scene (TCastleScene): moves it, runs animations of it etc.
16
17 This is a TCastleBehavior descendant,
18 and is inserted to parent like EnemyScene.AddBehavior(...).
19 You can get the TEnemy instance of a TCastleScene,
20 by taking "Scene.FindBehavior(TEnemy)".
21
22 Other ways of making an association TCastleScene <-> TEnemy logic are possible:
23
24 - TEnemy could be an independent class (not connected to any CGE class),
25 and simply have a reference to CGE TCastleScene instance.
26
27 This makes it easy to map TEnemy->TCastleScene.
28 To map TCastleScene->TEnemy you could e.g. use TCastleScene.Tag,
29 or a dedicated map structure like TDictionary from Generics.Collections.
30
31 - You could also make TEnemy a descendant of TCastleScene.
32
33 Note that TCastleBehavior or TCastleTransform descendants could be
34 registered in the CGE editor to visually add and edit them from editor.
35 See https://castle-engine.io/manual_editor.php#section_custom_components .
36 In this unit we call RegisterSerializableComponent,
37 so you only need to add editor_units="GameEnemy" to CastleEngineManifest.xml to see it in action.
38 }
39 TEnemy = class(TCastleBehavior)
40 strict private
41 Scene: TCastleScene;
42 MoveDirection: Integer; //< Always 1 or -1
43 Dead: Boolean;
44 public
45 constructor Create(AOwner: TComponent); override;
46 procedure ParentAfterAttach; override;
47 procedure Update(const SecondsPassed: Single; var RemoveMe: TRemoveType); override;
48 procedure Hurt;
49 end;
50
51 TEnemyList = {$ifdef FPC}specialize{$endif} TObjectList<TEnemy>;
52
53 implementation
54
55 uses CastleComponentSerialize;
56
57 constructor TEnemy.Create(AOwner: TComponent);
58 begin
59 inherited;
60 MoveDirection := -1;
61 end;
62
63 procedure TEnemy.ParentAfterAttach;
64 begin
65 inherited;
66 Scene := Parent as TCastleScene; // TEnemy can only be added as behavior to TCastleScene
67 Scene.PlayAnimation('walk', true);
68 end;
69
70 procedure TEnemy.Update(const SecondsPassed: Single; var RemoveMe: TRemoveType);
71 const
72 MovingSpeed = 2;
73 begin
74 inherited;
75
76 if Dead then Exit;
77
78 // We modify the Z coordinate, responsible for enemy going forward
79 Scene.Translation := Scene.Translation +
80 Vector3(0, 0, MoveDirection * SecondsPassed * MovingSpeed);
81
82 Scene.Direction := Vector3(0, 0, MoveDirection);
83
84 // Toggle MoveDirection between 1 and -1
85 if Scene.Translation.Z > 5 then
86 MoveDirection := -1
87 else
88 if Scene.Translation.Z < -5 then
89 MoveDirection := 1;
90 end;
91
92 procedure TEnemy.Hurt;
93 begin
94 Scene.PlayAnimation('die', false);
95 // dead corpse no longer collides
96 Scene.Pickable := false;
97 Scene.Collides := false;
98 Dead := true;
99 end;
100
101 initialization
102 RegisterSerializableComponent(TEnemy, 'Enemy');
103 end.