| 1 |
{%MainUnit castlebehaviors.pas} |
| 2 |
{ |
| 3 |
Copyright 2006-2022 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{$ifdef read_interface} |
| 18 |
|
| 19 |
type |
| 20 |
{ Behavior that tracks life points, and determines being alive/dead |
| 21 |
for game purposes. |
| 22 |
|
| 23 |
Using this class in your own games is completely optional. |
| 24 |
You can implement "alive" functionality yourself trivially, |
| 25 |
and then make your own decisions about various details |
| 26 |
(e.g. is life a float, or integer? does life equal "precisely |
| 27 |
zero" means being still alive, or dead?). } |
| 28 |
TCastleAliveBehavior = class(TCastleBehavior) |
| 29 |
strict private |
| 30 |
FLife: Single; |
| 31 |
FMaxLife: Single; |
| 32 |
FHurtDirection: TVector3; |
| 33 |
FHurtStrength: Single; |
| 34 |
FAttacker: TCastleAliveBehavior; |
| 35 |
FAttackerObserver: TFreeNotificationObserver; |
| 36 |
procedure SetAttacker(const Value: TCastleAliveBehavior); |
| 37 |
procedure AttackerFreeNotification(const Sender: TFreeNotificationObserver); |
| 38 |
protected |
| 39 |
function CanAttachToParent(const NewParent: TCastleTransform; |
| 40 |
out ReasonWhyCannot: String): Boolean; override; |
| 41 |
public |
| 42 |
const |
| 43 |
{ Default value for @link(MaxLife) and @link(Life). } |
| 44 |
DefaultLife = 100.0; |
| 45 |
|
| 46 |
constructor Create(AOwner: TComponent); override; |
| 47 |
function PropertySections(const PropertyName: String): TPropertySections; override; |
| 48 |
|
| 49 |
{ Shortcut for checking Life <= 0. } |
| 50 |
function Dead: Boolean; |
| 51 |
|
| 52 |
{ Hurt given creature, decreasing its @link(Life) by LifeLoss, |
| 53 |
also setting some additional properties that describe the damage. |
| 54 |
These additional properties do not do anything in this class -- |
| 55 |
but they may be useful by other effects, e.g. "knockback", |
| 56 |
done by other behaviors. |
| 57 |
|
| 58 |
Note: If all you want to do is to decrease @link(Life), |
| 59 |
you can also just set @link(Life) property directly. |
| 60 |
|
| 61 |
@param(AHurtDirection Should be a normalized vector indicating direction |
| 62 |
from which the attack came. |
| 63 |
|
| 64 |
In this class, it does nothing, merely sets @link(HurtDirection) property. |
| 65 |
Which may be used by other effects.) |
| 66 |
|
| 67 |
@param(AHurtStrength Describes "strength" of the attack. |
| 68 |
What this "strengh" exactly means is not defined in this class. |
| 69 |
It may cause a "knockback" effect, in which case it may be a knockback |
| 70 |
distance, or a physical force strength, and is meaningful only when |
| 71 |
@code(AHurtDirection) parameter is non-zero. |
| 72 |
|
| 73 |
In this class, it does nothing, merely sets @link(HurtStrength) property. |
| 74 |
Which may be used by other effects.) |
| 75 |
|
| 76 |
@param(AnAttacker The other alive creature that caused this damage. |
| 77 |
It may be @nil if no other TCastleAliveBehavior is directly responsible |
| 78 |
for this damage. This may be useful for various purposes, |
| 79 |
for example the victim may become aware of attacker presence |
| 80 |
when it's attacked. |
| 81 |
|
| 82 |
In this class, it does nothing, merely sets @link(Attacker) property. |
| 83 |
Which may be used by other effects.) |
| 84 |
)} |
| 85 |
procedure Hurt(const LifeLoss: Single; |
| 86 |
const AHurtDirection: TVector3; |
| 87 |
const AHurtStrength: Single = 0; |
| 88 |
const AnAttacker: TCastleAliveBehavior = nil); virtual; |
| 89 |
|
| 90 |
{ Direction from where the last attack came, set by @link(Hurt). |
| 91 |
Zero if there was no specific direction of last attack, |
| 92 |
otherwise a normalized (length 1) vector. } |
| 93 |
property HurtDirection: TVector3 read FHurtDirection; |
| 94 |
|
| 95 |
{ Strengh of the last attack, set by @link(Hurt). |
| 96 |
What this "strengh" exactly means is not defined in this class. |
| 97 |
It may cause a "knockback" effect, in which case it may be a knockback |
| 98 |
distance, or a physical force strength, and is meaningful only when |
| 99 |
@link(HurtDirection) is non-zero. } |
| 100 |
property HurtStrength: Single read FHurtStrength; |
| 101 |
|
| 102 |
{ Last attacker. Set by the last @link(Hurt) call, |
| 103 |
it may also be set directly. } |
| 104 |
property Attacker: TCastleAliveBehavior read FAttacker write SetAttacker; |
| 105 |
published |
| 106 |
{ Current Life. The object is considered "dead" when this is <= 0. } |
| 107 |
property Life: Single read FLife write FLife {$ifdef FPC}default DefaultLife{$endif}; |
| 108 |
|
| 109 |
{ Maximum amount of life. |
| 110 |
Can be also used for information (to display on player HUDs and such). |
| 111 |
|
| 112 |
This is not a strict limit on @link(Life), |
| 113 |
i.e. all the code allows the have @link(Life) > @link(MaxLife) |
| 114 |
to account for special game mechanisms, |
| 115 |
like "magic life boost to make health temporarily larger than normal". |
| 116 |
It is up to your game whether such situation will actually happen. } |
| 117 |
property MaxLife: Single read FMaxLife write FMaxLife {$ifdef FPC}default DefaultLife{$endif}; |
| 118 |
end; |
| 119 |
|
| 120 |
{$endif read_interface} |
| 121 |
|
| 122 |
{$ifdef read_implementation} |
| 123 |
|
| 124 |
{ TCastleAliveBehavior ------------------------------------------------------- } |
| 125 |
|
| 126 |
constructor TCastleAliveBehavior.Create(AOwner: TComponent); |
| 127 |
begin |
| 128 |
inherited; |
| 129 |
FLife := DefaultLife; |
| 130 |
FMaxLife := DefaultLife; |
| 131 |
FAttackerObserver := TFreeNotificationObserver.Create(Self); |
| 132 |
FAttackerObserver.OnFreeNotification := {$ifdef FPC}@{$endif}AttackerFreeNotification; |
| 133 |
end; |
| 134 |
|
| 135 |
function TCastleAliveBehavior.CanAttachToParent(const NewParent: TCastleTransform; |
| 136 |
out ReasonWhyCannot: String): Boolean; |
| 137 |
begin |
| 138 |
Result := inherited; |
| 139 |
if not Result then Exit; |
| 140 |
|
| 141 |
if NewParent.FindBehavior(TCastleAliveBehavior) <> nil then |
| 142 |
begin |
| 143 |
ReasonWhyCannot := 'Only one TCastleAliveBehavior can be added to a given TCastleTransform'; |
| 144 |
Result := false; |
| 145 |
end; |
| 146 |
end; |
| 147 |
|
| 148 |
procedure TCastleAliveBehavior.AttackerFreeNotification( |
| 149 |
const Sender: TFreeNotificationObserver); |
| 150 |
begin |
| 151 |
Attacker := nil; |
| 152 |
end; |
| 153 |
|
| 154 |
procedure TCastleAliveBehavior.SetAttacker(const Value: TCastleAliveBehavior); |
| 155 |
begin |
| 156 |
if FAttacker <> Value then |
| 157 |
begin |
| 158 |
FAttackerObserver.Observed := Value; |
| 159 |
FAttacker := Value; |
| 160 |
end; |
| 161 |
end; |
| 162 |
|
| 163 |
procedure TCastleAliveBehavior.Hurt(const LifeLoss: Single; |
| 164 |
const AHurtDirection: TVector3; |
| 165 |
const AHurtStrength: Single; const AnAttacker: TCastleAliveBehavior); |
| 166 |
begin |
| 167 |
Life := Life - LifeLoss; |
| 168 |
FHurtDirection := AHurtDirection; |
| 169 |
FHurtStrength := AHurtStrength; |
| 170 |
Attacker := AnAttacker; |
| 171 |
end; |
| 172 |
|
| 173 |
function TCastleAliveBehavior.PropertySections( |
| 174 |
const PropertyName: String): TPropertySections; |
| 175 |
begin |
| 176 |
if (PropertyName = 'Life') or |
| 177 |
(PropertyName = 'MaxLife') then |
| 178 |
Result := [psBasic] |
| 179 |
else |
| 180 |
Result := inherited PropertySections(PropertyName); |
| 181 |
end; |
| 182 |
|
| 183 |
function TCastleAliveBehavior.Dead: Boolean; |
| 184 |
begin |
| 185 |
Result := Life <= 0; |
| 186 |
end; |
| 187 |
|
| 188 |
{$endif read_implementation} |