Class TCastleLiving
Unit
Declaration
type TCastleLiving = class(TCastleBehavior)
Description
Represents a living (dead or alive at given point) creature. Tracks Life points which determine being Alive or Dead for game purposes. It is also a basis for creatures to be aware of each other.
E.g. we can remember the last Attacker. This may be used by some other logic (not implemented by this behavior) to either run away from the last attacker (e.g. when health low) or run toward it (to retaliate).
This behavior is suitable both for player characters (controlled by humans) and for creatures / NPCs / bots (controller by code). For the latter (creatures / NPCs / bots) consider adding another behavior that implements actual AI, like TCastleMoveAttack or your own logic (see "3D FPS Game" for a simple example). See https://castle-engine.io/behaviors about using and implementing behaviors.
Note that using this class in your own games is completely optional. You can implement "has hit points" functionality yourself trivially, and then make your own decisions about various details (e.g. is life a float, or integer? does life equal "precisely zero" means being still alive, or dead?). This class is just a convenience, we made some decisions trying to address a wide range of games.
Hierarchy
- TObject
- TPersistent
- TComponent
- TCastleComponent
- TCastleBehavior
- TCastleLiving
Overview
Fields
nested const DefaultLife = 100.0; |
Methods
function CanAttachToParent(const NewParent: TCastleTransform; out ReasonWhyCannot: String): Boolean; override; |
|
constructor Create(AOwner: TComponent); override; |
|
function PropertySections(const PropertyName: String): TPropertySections; override; |
|
function Alive: Boolean; |
|
function Dead: Boolean; |
|
procedure Hurt(const LifeLoss: Single; const AHurtDirection: TVector3; const AHurtStrength: Single = 0; const AnAttacker: TCastleLiving = nil); virtual; |
Properties
property HurtDirection: TVector3 read FHurtDirection; |
|
property HurtStrength: Single read FHurtStrength; |
|
property Attacker: TCastleLiving read FAttacker write SetAttacker; |
|
property Life: Single read FLife write FLife default DefaultLife; |
|
property MaxLife: Single read FMaxLife write FMaxLife default DefaultLife; |
|
property OnHurt: TNotifyEvent read FOnHurt write FOnHurt; |
Description
Fields
nested const DefaultLife = 100.0; |
|
Methods
function CanAttachToParent(const NewParent: TCastleTransform; out ReasonWhyCannot: String): Boolean; override; |
|
This item has no description. Showing description inherited from TCastleBehavior.CanAttachToParent.
Check can this behavior be added to NewParent. When this returns function TCastleBillboard.CanAttachToParent(const NewParent: TCastleTransform; out ReasonWhyCannot: String): Boolean; begin Result := inherited; if not Result then Exit; if NewParent.FindBehavior(TCastleBillboard) <> nil then begin ReasonWhyCannot := 'Only one TCastleBillboard behavior can be added to a given TCastleTransform'; Result := false; end; end; |
constructor Create(AOwner: TComponent); override; |
|
This item has no description. |
function PropertySections(const PropertyName: String): TPropertySections; override; |
|
This item has no description. Showing description inherited from TCastleComponent.PropertySections. Section where to show property in the editor. |
function Alive: Boolean; |
|
Shortcut for checking Life > 0. Always equal to |
function Dead: Boolean; |
|
Shortcut for checking Life <= 0. Always equal to |
procedure Hurt(const LifeLoss: Single; const AHurtDirection: TVector3; const AHurtStrength: Single = 0; const AnAttacker: TCastleLiving = nil); virtual; |
|
Hurt given creature, decreasing its Life by LifeLoss, also setting some additional properties that describe the damage. These additional properties do not do anything in this class – but they may be useful by other effects, e.g. "knockback", done by other behaviors. Note: If all you want to do is to decrease Life, you can also just set Life property directly, like
) Parameters
|
Properties
property HurtDirection: TVector3 read FHurtDirection; |
|
Direction from where the last attack came, set by Hurt, in world coordinates. Zero if there was no specific direction of last attack, otherwise a normalized (length 1) vector. |
property HurtStrength: Single read FHurtStrength; |
|
Strengh of the last attack, set by Hurt. What this "strengh" exactly means is not defined in this class. It may cause a "knockback" effect, in which case it may be a knockback distance, or a physical force strength, and is meaningful only when HurtDirection is non-zero. |
property Attacker: TCastleLiving read FAttacker write SetAttacker; |
|
Last |
property Life: Single read FLife write FLife default DefaultLife; |
|
Current Life (hit points). The object is considered "dead" when this is <= 0. Expressed as float, allows to represent fractional life, allows to animate life (e.g. increasing life by some "SecondsPassed * RegenerationRate" each frame, or decreasing by "SeconsPassed * PoisonSpeed"). |
property MaxLife: Single read FMaxLife write FMaxLife default DefaultLife; |
|
Maximum amount of life. Can be also used for information (to display on player HUDs and such). This is not a strict limit on Life, i.e. all the code allows the have Life > MaxLife to account for special game mechanisms, like "magic life boost to make health temporarily larger than normal". It is up to your game logic whether such situation will actually happen. |
property OnHurt: TNotifyEvent read FOnHurt write FOnHurt; |
|
Event called right after creature is hurt and properties like HurtDirection, HurtStrength and Attacker are set. |
Generated by PasDoc 0.16.0-snapshot.