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

Overview

Fields

Public nested const DefaultLife = 100.0;

Methods

Protected function CanAttachToParent(const NewParent: TCastleTransform; out ReasonWhyCannot: String): Boolean; override;
Public constructor Create(AOwner: TComponent); override;
Public function PropertySections(const PropertyName: String): TPropertySections; override;
Public function Alive: Boolean;
Public function Dead: Boolean;
Public procedure Hurt(const LifeLoss: Single; const AHurtDirection: TVector3; const AHurtStrength: Single = 0; const AnAttacker: TCastleLiving = nil); virtual;

Properties

Public property HurtDirection: TVector3 read FHurtDirection;
Public property HurtStrength: Single read FHurtStrength;
Public property Attacker: TCastleLiving read FAttacker write SetAttacker;
Published property Life: Single read FLife write FLife default DefaultLife;
Published property MaxLife: Single read FMaxLife write FMaxLife default DefaultLife;
Published property OnHurt: TNotifyEvent read FOnHurt write FOnHurt;

Description

Fields

Public nested const DefaultLife = 100.0;

Default value for MaxLife and Life.

Methods

Protected 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 False, it has to set also ReasonWhyCannot. When overriding this, you can use e.g. this code to make sure we are the only behavior of given class:

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;

Public constructor Create(AOwner: TComponent); override;

This item has no description.

Public 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.

Public function Alive: Boolean;

Shortcut for checking Life > 0. Always equal to not Dead.

Public function Dead: Boolean;

Shortcut for checking Life <= 0. Always equal to not Alive.

Public 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 MyCreature.Life := MyCreature.Life - 10;.

)

Parameters
AHurtDirection
Should be a normalized vector indicating direction from which the attack came, in world coordinates.

In this class, it does nothing, merely sets HurtDirection property. Which may be used by other effects.

AHurtStrength
Describes "strength" of the attack. 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 AHurtDirection parameter is non-zero.

In this class, it does nothing, merely sets HurtStrength property. Which may be used by other effects.

AnAttacker
The other living creature that caused this damage. It may be Nil if no other TCastleLiving is directly responsible for this damage. This may be useful for various purposes, for example the victim may become aware of attacker presence when it's attacked.

In this class, it does nothing, merely sets Attacker property. Which may be used by other effects.

Properties

Public 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.

Public 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.

Public property Attacker: TCastleLiving read FAttacker write SetAttacker;

Last attacker. Set by the last Hurt call, it may also be set directly.

Published 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").

Published 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.

Published 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.