| 1 |
{ |
| 2 |
Copyright 2006-2023 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 |
{ Player (TPlayer). } |
| 17 |
unit CastlePlayer |
| 18 |
deprecated 'define your own player class'; |
| 19 |
|
| 20 |
{$I castleconf.inc} |
| 21 |
|
| 22 |
interface |
| 23 |
|
| 24 |
uses Classes, |
| 25 |
CastleBoxes, CastleCameras, CastleItems, CastleVectors, CastleInputs, |
| 26 |
CastleKeysMouse, CastleShapes, CastleInternalMaterialProperties, CastleSoundEngine, |
| 27 |
CastleTransformExtra, CastleGLUtils, CastleColors, CastleFrustum, CastleTriangles, |
| 28 |
CastleTimeUtils, CastleScene, CastleDebugTransform, X3DNodes, CastleTransform, |
| 29 |
CastleResources, CastleThirdPersonNavigation; |
| 30 |
|
| 31 |
type |
| 32 |
TPlayerSwimming = ( |
| 33 |
{ Not swimming. } |
| 34 |
psNo, |
| 35 |
|
| 36 |
{ Swimming on the water surface. |
| 37 |
Conceptually, avatar is submerged in water, but has head above the water. } |
| 38 |
psAboveWater, |
| 39 |
|
| 40 |
{ Swimming under the water surface. |
| 41 |
Conpeptually, avatar is fully submerged in water, and may have trouble |
| 42 |
breathing. Games may simulate lack of oxygen, drowning etc. in this case. } |
| 43 |
psUnderWater |
| 44 |
); |
| 45 |
|
| 46 |
{ Player, 3D object controlling the camera, main enemy of hostile creatures, |
| 47 |
carries a backpack, may cause fadeout effects on screen and such. |
| 48 |
|
| 49 |
Note that you can operate on player even before level is loaded, |
| 50 |
before TLevel or TCastleViewport are initialized. |
| 51 |
This allows to create player before level is started |
| 52 |
(create it from scratch, or by loading from save game), |
| 53 |
and "carry" the same player instance across various loaded levels. |
| 54 |
|
| 55 |
@link(Dead) or @link(Blocked) player behaves much like alive and normal player. |
| 56 |
For example, it still has an associated @link(Navigation) |
| 57 |
that can be affected by physics |
| 58 |
(e.g. to apply physics to the dead player body, |
| 59 |
because player was killed when he was flying, or it's |
| 60 |
corpse lays on some moving object of the level --- like elevator). |
| 61 |
However, input shortcuts will be cleared, to prevent user from |
| 62 |
directly moving the player. |
| 63 |
|
| 64 |
Do not do some stuff when player is dead: |
| 65 |
@unorderedList( |
| 66 |
@item No calling PickItem, DropItem, UseItem. |
| 67 |
@item(No increasing Life (further decreasing Life is OK). |
| 68 |
This implies that once Player is Dead, (s)he cannot be alive again.) |
| 69 |
@item No changing EquippedWeapon, no calling Attack. |
| 70 |
) |
| 71 |
|
| 72 |
Note that a player has an associated and synchronized @link(Navigation) |
| 73 |
instance. See @link(UseThirdPerson). |
| 74 |
} |
| 75 |
TPlayer = class(TAliveWithInventory) |
| 76 |
private |
| 77 |
type |
| 78 |
{ Invisible box, that is added to TPlayer to make it collidable. |
| 79 |
Owner must be TPlayer. } |
| 80 |
TBox = class(TCastleScene) |
| 81 |
strict private |
| 82 |
Box: TBoxNode; |
| 83 |
Shape: TShapeNode; |
| 84 |
TransformNode: TTransformNode; |
| 85 |
procedure UpdateBox; |
| 86 |
public |
| 87 |
constructor Create(AOwner: TComponent); override; |
| 88 |
procedure Update(const SecondsPassed: Single; var RemoveMe: TRemoveType); override; |
| 89 |
end; |
| 90 |
|
| 91 |
var |
| 92 |
FBox: TBox; |
| 93 |
FDebugTransform: TDebugTransform; |
| 94 |
FRenderDebug: boolean; |
| 95 |
|
| 96 |
FEquippedWeapon: TItemWeapon; |
| 97 |
FEquippedWeaponResourceFrame: TResourceFrame; |
| 98 |
|
| 99 |
{ If Swimming = psUnderWater, then this is the time (from LifeTime) |
| 100 |
of setting Swimming to psUnderWater. } |
| 101 |
SwimBeginTime: Single; |
| 102 |
{ If Swimming = psUnderWater, this is the time of last |
| 103 |
drowning (or 0.0 if there was no drowning yet in this swimming session). } |
| 104 |
SwimLastDrownTime: Single; |
| 105 |
{ If Swimming = psUnderWater, this is the time of last stPlayerSwimming sound |
| 106 |
(or 0.0 if there was no stPlayerSwimming played yet in this |
| 107 |
swimming session). } |
| 108 |
SwimLastSoundTime: Single; |
| 109 |
FSwimming: TPlayerSwimming; |
| 110 |
|
| 111 |
{ Did last @link(Update) detected that we're on toxic ground? } |
| 112 |
IsToxic: boolean; |
| 113 |
{ Relevant if IsToxic, this is LifeTime when |
| 114 |
last time toxic damage was done. When player steps on toxic for the |
| 115 |
first time, he immediately gets damage, so ToxicLastDamageTime is |
| 116 |
always valid when IsToxic. } |
| 117 |
ToxicLastDamageTime: Single; |
| 118 |
|
| 119 |
SwimmingChangePlayingSound: TCastlePlayingSound; |
| 120 |
SwimmingPlayingSound: TCastlePlayingSound; |
| 121 |
|
| 122 |
{ Did last @link(Update) detected that we are on the ground. } |
| 123 |
IsOnTheGround: boolean; |
| 124 |
{ <> @nil if IsOnTheGround and last ground had some TMaterialProperty. } |
| 125 |
GroundProperty: TMaterialProperty; |
| 126 |
ReallyIsOnTheGroundTime: Single; |
| 127 |
|
| 128 |
FootstepsPlayingSound: TCastlePlayingSound; |
| 129 |
FootstepsSound: TCastleSound; |
| 130 |
ReallyWalkingOnTheGroundTime: Single; |
| 131 |
|
| 132 |
FInventoryCurrentItem: Integer; |
| 133 |
FInventoryVisible: boolean; |
| 134 |
FSickProjectionSpeed: Single; |
| 135 |
FBlocked: boolean; |
| 136 |
|
| 137 |
FFlying: boolean; |
| 138 |
FFlyingTimeOut: TFloatTime; |
| 139 |
{ FadeOut settings. } |
| 140 |
FFadeOutIntensity: Single; |
| 141 |
FFadeOutColor: TCastleColor; |
| 142 |
|
| 143 |
FFallMinHeightToSound: Single; |
| 144 |
FFallMinHeightToDamage: Single; |
| 145 |
FFallDamageScaleMin: Single; |
| 146 |
FFallDamageScaleMax: Single; |
| 147 |
FFallSound: TCastleSound; |
| 148 |
FHeadBobbing: Single; |
| 149 |
FSwimBreath: Single; |
| 150 |
FDrownPause: Single; |
| 151 |
FDrownDamageConst: Single; |
| 152 |
FDrownDamageRandom: Single; |
| 153 |
FSwimSoundPause: Single; |
| 154 |
FEnableNavigationDragging: boolean; |
| 155 |
FFallingEffect: boolean; |
| 156 |
FWalkNavigation: TCastleWalkNavigation; |
| 157 |
FThirdPersonNavigation: TCastleThirdPersonNavigation; |
| 158 |
FUseThirdPerson: Boolean; |
| 159 |
InsideSynchronizeFromCamera: Cardinal; |
| 160 |
|
| 161 |
procedure SetEquippedWeapon(Value: TItemWeapon); |
| 162 |
|
| 163 |
{ Update Navigation and ThirdPersonNavigation properties, including inputs. |
| 164 |
Call this always when @link(Flying) or @link(Dead) or some key values |
| 165 |
or @link(Swimming) or @link(Blocked) change. } |
| 166 |
procedure UpdateNavigation; |
| 167 |
|
| 168 |
procedure NavigationFall(const Sender: TCastleNavigation; const FallHeight: Single); |
| 169 |
|
| 170 |
{ This sets life, just like SetLife. |
| 171 |
But in case of life loss, the fadeout is done with specified |
| 172 |
Color (while SetLife always uses red color). |
| 173 |
Color's alpha doesn't matter for now. } |
| 174 |
procedure SetLifeCustomFadeOut(const Value: Single; |
| 175 |
const Color: TCastleColor); |
| 176 |
|
| 177 |
procedure SetSwimming(const Value: TPlayerSwimming); |
| 178 |
|
| 179 |
procedure SetFlying(const AValue: Boolean); |
| 180 |
procedure SetFlyingTimeOut(const AValue: TFloatTime); |
| 181 |
procedure SetEnableNavigationDragging(const AValue: Boolean); |
| 182 |
procedure SynchronizeToCamera; |
| 183 |
procedure SynchronizeFromCamera; |
| 184 |
procedure SetUseThirdPerson(const AValue: Boolean); |
| 185 |
function GetRenderOnTop: Boolean; |
| 186 |
procedure SetRenderOnTop(const Value: Boolean); |
| 187 |
protected |
| 188 |
procedure SetLife(const Value: Single); override; |
| 189 |
procedure Notification(AComponent: TComponent; Operation: TOperation); override; |
| 190 |
function LocalHeightCollision(const APosition, GravityUp: TVector3; |
| 191 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc; |
| 192 |
out AboveHeight: Single; out AboveGround: PTriangle): Boolean; override; |
| 193 |
function LocalSegmentCollision(const Pos1, Pos2: TVector3; |
| 194 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc; |
| 195 |
const ALineOfSight: Boolean): Boolean; override; |
| 196 |
procedure Fall(const FallHeight: Single); override; |
| 197 |
procedure ChangedTransform; override; |
| 198 |
public |
| 199 |
var |
| 200 |
{ Various navigation properties that may depend on loaded level. } |
| 201 |
DefaultMoveHorizontalSpeed: Single; |
| 202 |
DefaultMoveVerticalSpeed: Single; |
| 203 |
DefaultPreferredHeight: Single; |
| 204 |
|
| 205 |
const |
| 206 |
DefaultLife = 100; |
| 207 |
DefaultSickProjectionSpeed = 2.0; |
| 208 |
DefaultPlayerKnockBackSpeed = 20.0; |
| 209 |
DefaultSwimBreath = 30.0; |
| 210 |
DefaultDrownPause = 5.0; |
| 211 |
DefaultDrownDamageConst = 5.0; |
| 212 |
DefaultDrownDamageRandom = 10.0; |
| 213 |
DefaultSwimSoundPause = 3.11111111; |
| 214 |
{ TPlayer.FallMinHeightToSound is larger than |
| 215 |
TCreature.FallMinHeightToSound, |
| 216 |
to avoid making "fall" sound when player merely jumps or walks down a steep |
| 217 |
hill. No such need for creature. } |
| 218 |
DefaultFallMinHeightToSound = 4.0; |
| 219 |
DefaultFallSoundName = 'player_fall'; |
| 220 |
|
| 221 |
constructor Create(AOwner: TComponent); override; |
| 222 |
destructor Destroy; override; |
| 223 |
procedure Render(const Params: TRenderParams); override; |
| 224 |
function Press(const Event: TInputPressRelease): Boolean; override; |
| 225 |
property ListenPressRelease default true; |
| 226 |
|
| 227 |
{ Flying. |
| 228 |
How it interacts with FlyingTimeout: Setting this property |
| 229 |
to any value removes any timeout set by FlyingTimeout. |
| 230 |
That is, setting this to @true makes player fly indefinitely, |
| 231 |
and setting this to @false makes player stop flying (regardless |
| 232 |
if flying was initialized by @code(Flying := true) or @code(FlyingTimeout)). } |
| 233 |
property Flying: boolean read FFlying write SetFlying; |
| 234 |
|
| 235 |
{ Set this to something > 0 to start flying for a given number of seconds. |
| 236 |
The @link(Flying) property will also change to @true for this time. |
| 237 |
It will automatically change back to @false after given number of seconds |
| 238 |
(you can also always just manually switch @link(Flying) back to @false). |
| 239 |
|
| 240 |
Set this only with value > 0. |
| 241 |
|
| 242 |
When this is > 0 it means flying with a timeout |
| 243 |
(always @link(Flying) = @true then), |
| 244 |
otherwise it's = 0 (which means were not flying, or flying indefinitely |
| 245 |
long, depending on @link(Flying)).} |
| 246 |
property FlyingTimeOut: TFloatTime read FFlyingTimeOut write SetFlyingTimeOut; |
| 247 |
|
| 248 |
{ Add Item to inventory, updating player InventoryCurrentItem, |
| 249 |
making suitable notification and sound. } |
| 250 |
function PickItemUpdate(var Item: TInventoryItem): Integer; override; |
| 251 |
|
| 252 |
{ Drop item from inventory, updating player InventoryCurrentItem, |
| 253 |
making suitable notification and sound. |
| 254 |
@groupBegin } |
| 255 |
function DropItem(const Index: Integer): TItemOnWorld; override; |
| 256 |
function DropCurrentItem: TItemOnWorld; |
| 257 |
{ @groupEnd } |
| 258 |
|
| 259 |
{ Use an item from inventory. |
| 260 |
You can pass Index that is out of range (or call UseCurrentItem |
| 261 |
when InventoryCurrentItem = -1), it will then show |
| 262 |
a notification (by CastleGameNotifications unit) that nothing is selected. |
| 263 |
@groupBegin } |
| 264 |
procedure UseItem(const Index: Integer); override; |
| 265 |
procedure UseCurrentItem; |
| 266 |
{ @groupEnd } |
| 267 |
|
| 268 |
{ Change InventoryCurrentItem, cycling, and automatically showing |
| 269 |
the inventory afterwards (if it's not empty). |
| 270 |
Note that you can also always directly change InventoryCurrentItem property. } |
| 271 |
procedure ChangeInventoryCurrentItem(Change: Integer); |
| 272 |
|
| 273 |
{ Weapon the player is using right now, or nil if none. |
| 274 |
|
| 275 |
You can set this property only to some item existing on Inventory. |
| 276 |
When you drop the current weapon, |
| 277 |
DeleteItem will automatically set this to @nil. |
| 278 |
|
| 279 |
When setting this property (to nil or non-nil) player may get |
| 280 |
GameMessage about using/not using a weapon. } |
| 281 |
property EquippedWeapon: TItemWeapon read FEquippedWeapon write SetEquippedWeapon; |
| 282 |
|
| 283 |
procedure Update(const SecondsPassed: Single; var RemoveMe: TRemoveType); override; |
| 284 |
function Middle: TVector3; override; |
| 285 |
|
| 286 |
{ Cause a fade-out effect on the screen, tinting the screen to the given Color. |
| 287 |
The TPlayer class doesn't do the actual drawing of the fade-out effect |
| 288 |
on the screen, we merely store and animate the FadeOutColor and FadeOutIntensity |
| 289 |
properties. To draw the effect, use a procedure like GLFadeRectangle |
| 290 |
inside your 2D controls drawing code, see engine tutorial for example. } |
| 291 |
procedure FadeOut(const Color: TCastleColor); |
| 292 |
|
| 293 |
property FadeOutColor: TCastleColor read FFadeOutColor; |
| 294 |
property FadeOutIntensity: Single read FFadeOutIntensity; |
| 295 |
|
| 296 |
{ @noAutoLinkHere } |
| 297 |
procedure Attack; virtual; |
| 298 |
|
| 299 |
{ TLevel sets this property automatically, based on the water volume. |
| 300 |
@exclude } |
| 301 |
property Swimming: TPlayerSwimming read FSwimming write SetSwimming; |
| 302 |
|
| 303 |
{ Load various player properties from an XML file. |
| 304 |
Properties not specified in the indicated file will |
| 305 |
be reset to their default values. |
| 306 |
This is handy to use in a game to allow to configure player behavior |
| 307 |
by simply editing an XML file (instead of hacking code). |
| 308 |
|
| 309 |
Overloaded parameterless version reads from file |
| 310 |
@code('castle-data:/player.xml'). |
| 311 |
|
| 312 |
Note that the indicated file may not exist, and it will not cause errors. |
| 313 |
Not existing file is equivalent to a file with everything set at default |
| 314 |
values. |
| 315 |
|
| 316 |
It is Ok to call this multiple times, at any moment. |
| 317 |
This way you can make some debug command to reload player.xml file, |
| 318 |
very useful to test various player properties without restarting the game. |
| 319 |
|
| 320 |
@groupBegin } |
| 321 |
procedure LoadFromFile; overload; |
| 322 |
procedure LoadFromFile(const Url: String); overload; |
| 323 |
{ @groupEnd } |
| 324 |
|
| 325 |
function Ground: PTriangle; |
| 326 |
|
| 327 |
procedure LevelChanged; |
| 328 |
|
| 329 |
{ Currently selected inventory item. |
| 330 |
|
| 331 |
Note: while we try to always sensibly update InventoryCurrentItem, |
| 332 |
to keep the assumptions that |
| 333 |
@orderedList( |
| 334 |
@item(Inventory.Count = 0 => InventoryCurrentItem = -1) |
| 335 |
@item(Inventory.Count > 0 => |
| 336 |
InventoryCurrentItem between 0 and Inventory.Count - 1)) |
| 337 |
|
| 338 |
but you should @italic(nowhere) depend on these assuptions. |
| 339 |
That's because I want to allow myself freedom to modify Inventory |
| 340 |
in various situations, so InventoryCurrentItem can become |
| 341 |
invalid in many situations. |
| 342 |
|
| 343 |
So every code should check that |
| 344 |
@unorderedList( |
| 345 |
@item(If InventoryCurrentItem between 0 and Inventory.Count - 1 |
| 346 |
then InventoryCurrentItem is selected) |
| 347 |
@item(Else no item is selected (possibly Inventory.Count = 0, |
| 348 |
possibly not))) |
| 349 |
} |
| 350 |
property InventoryCurrentItem: Integer |
| 351 |
read FInventoryCurrentItem write FInventoryCurrentItem |
| 352 |
default -1; |
| 353 |
|
| 354 |
property InventoryVisible: boolean |
| 355 |
read FInventoryVisible write FInventoryVisible default false; |
| 356 |
|
| 357 |
property SickProjectionSpeed: Single |
| 358 |
read FSickProjectionSpeed write FSickProjectionSpeed |
| 359 |
{$ifdef FPC}default DefaultSickProjectionSpeed{$endif}; |
| 360 |
|
| 361 |
property CollidesWithMoving default true; |
| 362 |
function Sphere(out Radius: Single): boolean; override; |
| 363 |
|
| 364 |
{ Disables changing the camera by user. |
| 365 |
It's useful when you want to temporarily force camera to some specific |
| 366 |
setting (you can even use handy Player.Navigation.AnimateTo method |
| 367 |
to do this easily, see TCastleWalkNavigation.AnimateTo). } |
| 368 |
property Blocked: boolean read FBlocked write FBlocked; |
| 369 |
|
| 370 |
{ Render 3D children (like EquippedWeapon) on top of everything else. } |
| 371 |
property RenderOnTop: boolean read GetRenderOnTop write SetRenderOnTop; |
| 372 |
|
| 373 |
property FallMinHeightToSound: Single |
| 374 |
read FFallMinHeightToSound write FFallMinHeightToSound |
| 375 |
{$ifdef FPC}default DefaultFallMinHeightToSound{$endif}; |
| 376 |
property FallMinHeightToDamage: Single |
| 377 |
read FFallMinHeightToDamage write FFallMinHeightToDamage |
| 378 |
{$ifdef FPC}default DefaultFallMinHeightToDamage{$endif}; |
| 379 |
property FallDamageScaleMin: Single |
| 380 |
read FFallDamageScaleMin write FFallDamageScaleMin |
| 381 |
{$ifdef FPC}default DefaultFallDamageScaleMin{$endif}; |
| 382 |
property FallDamageScaleMax: Single |
| 383 |
read FFallDamageScaleMax write FFallDamageScaleMax |
| 384 |
{$ifdef FPC}default DefaultFallDamageScaleMax{$endif}; |
| 385 |
{ Sound when falling. |
| 386 |
The default is the sound named 'player_fall'. } |
| 387 |
property FallSound: TCastleSound |
| 388 |
read FFallSound write FFallSound; |
| 389 |
{ Controls head bobbing, but only when player is walking. |
| 390 |
See TCastleWalkNavigation.HeadBobbing for exact meaning of this. |
| 391 |
TPlayer.Navigation.HeadBobbing is automatically updated as necessary. |
| 392 |
|
| 393 |
Note that when using CastleLevels, then the headBobbing defined |
| 394 |
inside VRML/X3D file (see |
| 395 |
https://castle-engine.io/x3d_extensions.php#section_ext_head_bobbing ) |
| 396 |
is ignored. Instead, Player properties control TCastleWalkNavigation.HeadBobbing |
| 397 |
and TCastleWalkNavigation.HeadBobbingTime. } |
| 398 |
property HeadBobbing: Single |
| 399 |
read FHeadBobbing write FHeadBobbing |
| 400 |
{$ifdef FPC}default TCastleWalkNavigation.DefaultHeadBobbing{$endif}; |
| 401 |
|
| 402 |
{ How many seconds you can swin before you start to drown. } |
| 403 |
property SwimBreath: Single read FSwimBreath write FSwimBreath |
| 404 |
{$ifdef FPC}default DefaultSwimBreath{$endif}; |
| 405 |
|
| 406 |
{ How many seconds between each drown event. |
| 407 |
Drown event makes stPlayerDrowning sound and causes damage |
| 408 |
DrownDamageConst + Random * DrownDamageRandom. } |
| 409 |
property DrownPause: Single read FDrownPause write FDrownPause |
| 410 |
{$ifdef FPC}default DefaultDrownPause{$endif}; |
| 411 |
property DrownDamageConst: Single read FDrownDamageConst write FDrownDamageConst |
| 412 |
{$ifdef FPC}default DefaultDrownDamageConst{$endif}; |
| 413 |
property DrownDamageRandom: Single read FDrownDamageRandom write FDrownDamageRandom |
| 414 |
{$ifdef FPC}default DefaultDrownDamageRandom{$endif}; |
| 415 |
|
| 416 |
{ Pause, in seconds, between playing stPlayerSwimming sound. |
| 417 |
This should be something that is not easily synchronized |
| 418 |
with SwimDrownPause. } |
| 419 |
property SwimSoundPause: Single read FSwimSoundPause write FSwimSoundPause |
| 420 |
{$ifdef FPC}default DefaultSwimSoundPause{$endif}; |
| 421 |
|
| 422 |
{ Enable navigation falling down effect due to gravity. |
| 423 |
This indirectly controls @link(TCastleWalkNavigation.FallingEffect) |
| 424 |
underneath. |
| 425 |
|
| 426 |
Note: do not set @code(Navigation.FallingEffect), as it will |
| 427 |
be overridden in our update. Use only this property to turn on/off |
| 428 |
the effect. } |
| 429 |
property FallingEffect: boolean |
| 430 |
read FFallingEffect write FFallingEffect default true; |
| 431 |
|
| 432 |
{ Navigation synchronized with this player instance. |
| 433 |
This is either @link(WalkNavigation) or @link(ThirdPersonNavigation), |
| 434 |
depending on @link(UseThirdPerson). |
| 435 |
|
| 436 |
You can use this navigation as @link(TCastleViewport.Navigation) |
| 437 |
to allow user to directly control this player in first-person game. |
| 438 |
@link(TLevel.Load) sets this automatically. |
| 439 |
|
| 440 |
The view vectors (position, direction and up), @link(TCastleWalkNavigation.Gravity), |
| 441 |
and various navigation inputs are automatically adjusted based on the current |
| 442 |
player state (@link(Dead), @link(Blocked)) and global PlayerInput_Xxx |
| 443 |
values, like @link(PlayerInput_Forward). |
| 444 |
The outside code may still directly access and change some navigation |
| 445 |
properties like TCastleWalkNavigation.PreferredHeight, |
| 446 |
TCastleWalkNavigation.RotationHorizontalSpeed |
| 447 |
TCastleWalkNavigation.RotationVerticalSpeed. In fact, it's Ok to call |
| 448 |
TCastleWalkNavigation.Init. } |
| 449 |
function Navigation: TCastleMouseLookNavigation; |
| 450 |
|
| 451 |
{ Navigation synchronized with this player instance, |
| 452 |
when @link(UseThirdPerson) is @false. } |
| 453 |
property WalkNavigation: TCastleWalkNavigation read FWalkNavigation; |
| 454 |
{$ifdef FPC} |
| 455 |
property Camera: TCastleWalkNavigation read FWalkNavigation; deprecated 'use WalkNavigation'; |
| 456 |
{$endif} |
| 457 |
|
| 458 |
{ Navigation synchronized with this player instance, |
| 459 |
when @link(UseThirdPerson) is @true. |
| 460 |
|
| 461 |
The @link(TCastleThirdPersonNavigation.AvatarHierarchy) inside is initialized, don't touch it. |
| 462 |
|
| 463 |
The @link(TCastleThirdPersonNavigation.Avatar) inside is created, |
| 464 |
you can further configure it if you want to use @link(UseThirdPerson). |
| 465 |
Most of all, you can load some model there, |
| 466 |
like @code(ThirdPersonNavigation.Avatar.Load('castle-data:/avatar.gltf')). |
| 467 |
} |
| 468 |
property ThirdPersonNavigation: TCastleThirdPersonNavigation read FThirdPersonNavigation; |
| 469 |
|
| 470 |
{ Whether to use 1st-person @link(WalkNavigation) or 3rd-person @link(ThirdPersonNavigation). |
| 471 |
@bold(You have to set this before calling @link(TLevel.Load)). } |
| 472 |
property UseThirdPerson: Boolean read FUseThirdPerson write SetUseThirdPerson default false; |
| 473 |
|
| 474 |
property KnockBackSpeed {$ifdef FPC}default DefaultPlayerKnockBackSpeed{$endif}; |
| 475 |
|
| 476 |
{ Enable navigation by dragging. This results in including |
| 477 |
niMouseDragging in TCastleNavigation.Input (when player is not |
| 478 |
@link(Dead) or @link(Blocked)). } |
| 479 |
property EnableNavigationDragging: boolean |
| 480 |
read FEnableNavigationDragging write SetEnableNavigationDragging default true; |
| 481 |
{$ifdef FPC} |
| 482 |
property EnableCameraDragging: boolean |
| 483 |
read FEnableNavigationDragging write SetEnableNavigationDragging default true; |
| 484 |
deprecated 'use EnableNavigationDragging'; |
| 485 |
{$endif} |
| 486 |
|
| 487 |
{ Show the debug bounding box of the player. |
| 488 |
Warning: It looks a little confusing (since it's a box around camera). } |
| 489 |
property RenderDebug: boolean |
| 490 |
read FRenderDebug write FRenderDebug default false; |
| 491 |
end; |
| 492 |
|
| 493 |
const |
| 494 |
DefaultAutoOpenInventory = true; |
| 495 |
|
| 496 |
var |
| 497 |
{ Automatically open TCastlePlayer inventory when picking up an item. } |
| 498 |
AutoOpenInventory: boolean = DefaultAutoOpenInventory; |
| 499 |
|
| 500 |
var |
| 501 |
{ Player inputs that handle navigation. |
| 502 |
@groupBegin } |
| 503 |
PlayerInput_Forward: TInputShortcut; |
| 504 |
PlayerInput_Backward: TInputShortcut; |
| 505 |
PlayerInput_LeftRotate: TInputShortcut; |
| 506 |
PlayerInput_RightRotate: TInputShortcut; |
| 507 |
PlayerInput_LeftStrafe: TInputShortcut; |
| 508 |
PlayerInput_RightStrafe: TInputShortcut; |
| 509 |
PlayerInput_UpRotate: TInputShortcut; |
| 510 |
PlayerInput_DownRotate: TInputShortcut; |
| 511 |
PlayerInput_GravityUp: TInputShortcut; |
| 512 |
PlayerInput_Jump: TInputShortcut; |
| 513 |
PlayerInput_Crouch: TInputShortcut; |
| 514 |
{ @groupEnd } |
| 515 |
|
| 516 |
{ Player inputs that deal with items. |
| 517 |
@groupBegin } |
| 518 |
PlayerInput_Attack: TInputShortcut; |
| 519 |
PlayerInput_InventoryShow: TInputShortcut; //< No key/mouse associated by default. |
| 520 |
PlayerInput_InventoryPrevious: TInputShortcut; |
| 521 |
PlayerInput_InventoryNext: TInputShortcut; |
| 522 |
PlayerInput_UseItem: TInputShortcut; |
| 523 |
PlayerInput_DropItem: TInputShortcut; //< No key/mouse associated by default. |
| 524 |
PlayerInput_CancelFlying: TInputShortcut; //< No key/mouse associated by default. |
| 525 |
{ @groupEnd } |
| 526 |
|
| 527 |
implementation |
| 528 |
|
| 529 |
uses Math, SysUtils, |
| 530 |
CastleClassUtils, CastleUtils, CastleControls, |
| 531 |
CastleImages, CastleFilesUtils, CastleUIControls, CastleLog, |
| 532 |
CastleGameNotifications, CastleXMLConfig, |
| 533 |
CastleGLImages, CastleConfig, CastleRenderContext, CastleRenderOptions; |
| 534 |
|
| 535 |
{ TPlayer.TBox ----------------------------------------------------------------- } |
| 536 |
|
| 537 |
constructor TPlayer.TBox.Create(AOwner: TComponent); |
| 538 |
var |
| 539 |
Root: TX3DRootNode; |
| 540 |
begin |
| 541 |
inherited; |
| 542 |
Box := TBoxNode.CreateWithTransform(Shape, TransformNode); |
| 543 |
Box.Size := Vector3(1, 1, 1); // this way Transform.Scale determines the size |
| 544 |
|
| 545 |
UpdateBox; |
| 546 |
|
| 547 |
Root := TX3DRootNode.Create; |
| 548 |
Root.AddChildren(TransformNode); |
| 549 |
Load(Root, true); |
| 550 |
|
| 551 |
Visible := false; |
| 552 |
end; |
| 553 |
|
| 554 |
procedure TPlayer.TBox.Update(const SecondsPassed: Single; var RemoveMe: TRemoveType); |
| 555 |
begin |
| 556 |
if Exists then |
| 557 |
UpdateBox; |
| 558 |
inherited; |
| 559 |
end; |
| 560 |
|
| 561 |
procedure TPlayer.TBox.UpdateBox; |
| 562 |
var |
| 563 |
B: TBox3D; |
| 564 |
Navigation: TCastleWalkNavigation; |
| 565 |
begin |
| 566 |
Assert(not TPlayer(Owner).UseThirdPerson, 'TPlayer.TBox should be used only for 1st person navigation'); |
| 567 |
|
| 568 |
Navigation := TPlayer(Owner).WalkNavigation; |
| 569 |
|
| 570 |
B.Data[0].X := -Navigation.Radius; |
| 571 |
B.Data[0].Y := -Navigation.Radius; |
| 572 |
B.Data[0].Z := -Navigation.Radius; |
| 573 |
|
| 574 |
if World <> nil then |
| 575 |
B.Data[0].Data[World.GravityCoordinate] := -Navigation.PreferredHeight; |
| 576 |
|
| 577 |
B.Data[1].X := Navigation.Radius; |
| 578 |
B.Data[1].Y := Navigation.Radius; |
| 579 |
B.Data[1].Z := Navigation.Radius; |
| 580 |
|
| 581 |
{ We adjust TransformNode.Scale, not Box.Size, because this is faster: |
| 582 |
no need to rebuild box proxy. } |
| 583 |
TransformNode.Scale := B.Size; |
| 584 |
TransformNode.Translation := B.Center; |
| 585 |
end; |
| 586 |
|
| 587 |
{ TPlayer -------------------------------------------------------------------- } |
| 588 |
|
| 589 |
constructor TPlayer.Create(AOwner: TComponent); |
| 590 |
begin |
| 591 |
inherited Create(AOwner); |
| 592 |
ListenPressRelease := true; |
| 593 |
CollidesWithMoving := true; |
| 594 |
Life := DefaultLife; |
| 595 |
MaxLife := DefaultLife; |
| 596 |
DefaultMoveHorizontalSpeed := 1.0; |
| 597 |
DefaultMoveVerticalSpeed := 1.0; |
| 598 |
DefaultPreferredHeight := 0.0; |
| 599 |
RenderLayer := rlFront; // to make RenderOnTop = true |
| 600 |
FFallMinHeightToSound := DefaultFallMinHeightToSound; |
| 601 |
FFallMinHeightToDamage := DefaultFallMinHeightToDamage; |
| 602 |
FFallDamageScaleMin := DefaultFallDamageScaleMin; |
| 603 |
FFallDamageScaleMax := DefaultFallDamageScaleMax; |
| 604 |
FFallSound := SoundEngine.SoundFromName(DefaultFallSoundName, false); |
| 605 |
KnockBackSpeed := DefaultPlayerKnockBackSpeed; |
| 606 |
FSickProjectionSpeed := DefaultSickProjectionSpeed; |
| 607 |
FSwimBreath := DefaultSwimBreath; |
| 608 |
FDrownPause := DefaultDrownPause; |
| 609 |
FDrownDamageConst := DefaultDrownDamageConst; |
| 610 |
FDrownDamageRandom := DefaultDrownDamageRandom; |
| 611 |
FSwimSoundPause := DefaultSwimSoundPause; |
| 612 |
FFallingEffect := true; |
| 613 |
FEnableNavigationDragging := true; |
| 614 |
FWalkNavigation := TCastleWalkNavigation.Create(nil); |
| 615 |
|
| 616 |
{ Setup FThirdPersonNavigation and use Self as AvatarHierarchy } |
| 617 |
FThirdPersonNavigation := TCastleThirdPersonNavigation.Create(nil); |
| 618 |
FThirdPersonNavigation.AvatarHierarchy := Self; |
| 619 |
FThirdPersonNavigation.Avatar := TCastleScene.Create(Self); |
| 620 |
FThirdPersonNavigation.Avatar.PreciseCollisions := true; |
| 621 |
Add(FThirdPersonNavigation.Avatar); |
| 622 |
|
| 623 |
FInventoryCurrentItem := -1; |
| 624 |
|
| 625 |
{ turn off keys that are totally unavailable for the player } |
| 626 |
WalkNavigation.Input_MoveSpeedInc.MakeClear; |
| 627 |
WalkNavigation.Input_MoveSpeedDec.MakeClear; |
| 628 |
WalkNavigation.Input_IncreasePreferredHeight.MakeClear; |
| 629 |
WalkNavigation.Input_DecreasePreferredHeight.MakeClear; |
| 630 |
|
| 631 |
WalkNavigation.CheckModsDown := false; |
| 632 |
WalkNavigation.OnFall := {$ifdef FPC}@{$endif}NavigationFall; |
| 633 |
|
| 634 |
{ Although it will be called in every OnUpdate anyway, |
| 635 |
we also call it here to be sure that right after TPlayer constructor |
| 636 |
finished, Navigation has already good values. } |
| 637 |
UpdateNavigation; |
| 638 |
|
| 639 |
// once Navigation is initialized, initialize TBox |
| 640 |
FBox := TBox.Create(Self); |
| 641 |
Add(FBox); |
| 642 |
|
| 643 |
FDebugTransform := TDebugTransform.Create(Self); |
| 644 |
FDebugTransform.Parent := Self; |
| 645 |
|
| 646 |
FEquippedWeaponResourceFrame := TResourceFrame.Create(Self); |
| 647 |
Add(FEquippedWeaponResourceFrame); |
| 648 |
|
| 649 |
{ We allocate 1 TCastlePlayingSound instance for each of these sounds, |
| 650 |
which reflects the usage: at most 1 footsteps sound is played at a given time, |
| 651 |
at most 1 swimming sound etc. } |
| 652 |
|
| 653 |
FootstepsPlayingSound := TCastlePlayingSound.Create(nil); |
| 654 |
FootstepsPlayingSound.Loop := true; |
| 655 |
//FootstepsPlayingSound.Sound := ... // will be assigned based on ground type |
| 656 |
|
| 657 |
SwimmingChangePlayingSound := TCastlePlayingSound.Create(nil); |
| 658 |
{$warnings off} // just to keep deprecated working |
| 659 |
SwimmingChangePlayingSound.Sound := stPlayerSwimmingChange; |
| 660 |
{$warnings on} |
| 661 |
|
| 662 |
SwimmingPlayingSound := TCastlePlayingSound.Create(nil); |
| 663 |
{$warnings off} // just to keep deprecated working |
| 664 |
SwimmingPlayingSound.Sound := stPlayerSwimming; |
| 665 |
{$warnings on} |
| 666 |
end; |
| 667 |
|
| 668 |
destructor TPlayer.Destroy; |
| 669 |
begin |
| 670 |
EquippedWeapon := nil; { unregister free notification } |
| 671 |
|
| 672 |
FreeAndNil(FootstepsPlayingSound); |
| 673 |
FreeAndNil(SwimmingChangePlayingSound); |
| 674 |
FreeAndNil(SwimmingPlayingSound); |
| 675 |
FreeAndNil(FWalkNavigation); |
| 676 |
FreeAndNil(FThirdPersonNavigation); |
| 677 |
inherited; |
| 678 |
end; |
| 679 |
|
| 680 |
function TPlayer.Navigation: TCastleMouseLookNavigation; |
| 681 |
begin |
| 682 |
if UseThirdPerson then |
| 683 |
Result := ThirdPersonNavigation |
| 684 |
else |
| 685 |
Result := WalkNavigation; |
| 686 |
end; |
| 687 |
|
| 688 |
procedure TPlayer.SetFlying(const AValue: boolean); |
| 689 |
begin |
| 690 |
FFlyingTimeOut := 0; |
| 691 |
FFlying := AValue; |
| 692 |
end; |
| 693 |
|
| 694 |
procedure TPlayer.SetFlyingTimeOut(const AValue: TFloatTime); |
| 695 |
begin |
| 696 |
Assert(AValue > 0, 'Only call FlyingTimeOut with TimeOut > 0'); |
| 697 |
{ It's possible that this is called when timeout is already > 0. |
| 698 |
In this case, we set FFlyingTimeOut to maximum |
| 699 |
--- the effect that will allow player to fly longer wins. } |
| 700 |
FFlyingTimeOut := Max(FFlyingTimeOut, AValue); |
| 701 |
FFlying := true; |
| 702 |
end; |
| 703 |
|
| 704 |
function TPlayer.PickItemUpdate(var Item: TInventoryItem): Integer; |
| 705 |
var |
| 706 |
S: string; |
| 707 |
begin |
| 708 |
S := Format('You pick "%s"', [Item.Resource.Caption]); |
| 709 |
if Item.Quantity <> 1 then |
| 710 |
S := S + Format(' (quantity %d)', [Item.Quantity]); |
| 711 |
Notifications.Show(S); |
| 712 |
|
| 713 |
{$warnings off} // just to keep deprecated working |
| 714 |
SoundEngine.Play(stPlayerPickItem); |
| 715 |
{$warnings on} |
| 716 |
|
| 717 |
Result := inherited PickItemUpdate(Item); |
| 718 |
|
| 719 |
{ Automatically equip the weapon. } |
| 720 |
if (Item is TItemWeapon) and (EquippedWeapon = nil) then |
| 721 |
EquippedWeapon := TItemWeapon(Item); |
| 722 |
|
| 723 |
{ Update InventoryCurrentItem. } |
| 724 |
if not Between(InventoryCurrentItem, 0, Inventory.Count - 1) then |
| 725 |
InventoryCurrentItem := Result; |
| 726 |
|
| 727 |
if AutoOpenInventory then |
| 728 |
InventoryVisible := true; |
| 729 |
end; |
| 730 |
|
| 731 |
function TPlayer.DropItem(const Index: Integer): TItemOnWorld; |
| 732 |
var |
| 733 |
S: string; |
| 734 |
begin |
| 735 |
Result := inherited DropItem(Index); |
| 736 |
|
| 737 |
if Result <> nil then |
| 738 |
begin |
| 739 |
if Result.Item = EquippedWeapon then |
| 740 |
EquippedWeapon := nil; |
| 741 |
|
| 742 |
S := Format('You drop "%s"', [Result.Item.Resource.Caption]); |
| 743 |
if Result.Item.Quantity <> 1 then |
| 744 |
S := S + Format(' (quantity %d)', [Result.Item.Quantity]); |
| 745 |
Notifications.Show(S); |
| 746 |
|
| 747 |
{$warnings off} // just to keep deprecated working |
| 748 |
SoundEngine.Play(stPlayerDropItem); |
| 749 |
{$warnings on} |
| 750 |
|
| 751 |
{ update InventoryCurrentItem. |
| 752 |
Note that if Inventory.Count = 0 now, then this will |
| 753 |
correctly set InventoryCurrentItem to -1. } |
| 754 |
if InventoryCurrentItem >= Inventory.Count then |
| 755 |
InventoryCurrentItem := Inventory.Count - 1; |
| 756 |
end else |
| 757 |
begin |
| 758 |
{ Dropping item failed. } |
| 759 |
if Between(Index, 0, Inventory.Count - 1) then |
| 760 |
Notifications.Show('Not enough room here to drop this item') else |
| 761 |
Notifications.Show('Nothing to drop - select some item first'); |
| 762 |
end; |
| 763 |
end; |
| 764 |
|
| 765 |
function TPlayer.DropCurrentItem: TItemOnWorld; |
| 766 |
begin |
| 767 |
Result := DropItem(InventoryCurrentItem); |
| 768 |
end; |
| 769 |
|
| 770 |
procedure TPlayer.UseItem(const Index: Integer); |
| 771 |
begin |
| 772 |
if Between(Index, 0, Inventory.Count - 1) then |
| 773 |
begin |
| 774 |
inherited UseItem(Index); |
| 775 |
{ update InventoryCurrentItem, because item potentially disappeared. |
| 776 |
Note that if Inventory.Count = 0 now, then this will |
| 777 |
correctly set InventoryCurrentItem to -1. } |
| 778 |
if InventoryCurrentItem >= Inventory.Count then |
| 779 |
InventoryCurrentItem := Inventory.Count - 1; |
| 780 |
end else |
| 781 |
Notifications.Show('Nothing to use - select some item first'); |
| 782 |
end; |
| 783 |
|
| 784 |
procedure TPlayer.UseCurrentItem; |
| 785 |
begin |
| 786 |
UseItem(InventoryCurrentItem); |
| 787 |
end; |
| 788 |
|
| 789 |
procedure TPlayer.ChangeInventoryCurrentItem(Change: Integer); |
| 790 |
begin |
| 791 |
if Inventory.Count = 0 then |
| 792 |
InventoryCurrentItem := -1 else |
| 793 |
if InventoryCurrentItem >= Inventory.Count then |
| 794 |
InventoryCurrentItem := Inventory.Count - 1 else |
| 795 |
if InventoryCurrentItem < 0 then |
| 796 |
InventoryCurrentItem := 0 else |
| 797 |
InventoryCurrentItem := ChangeIntCycle( |
| 798 |
InventoryCurrentItem, Change, Inventory.Count - 1); |
| 799 |
|
| 800 |
if Inventory.Count <> 0 then |
| 801 |
InventoryVisible := true; |
| 802 |
end; |
| 803 |
|
| 804 |
procedure TPlayer.SetEquippedWeapon(Value: TItemWeapon); |
| 805 |
begin |
| 806 |
if Value <> FEquippedWeapon then |
| 807 |
begin |
| 808 |
if FEquippedWeapon <> nil then |
| 809 |
FEquippedWeapon.RemoveFreeNotification(Self); |
| 810 |
|
| 811 |
FEquippedWeapon := Value; |
| 812 |
|
| 813 |
if FEquippedWeapon <> nil then |
| 814 |
begin |
| 815 |
Notifications.Show(Format('You''re using weapon "%s" now', |
| 816 |
[EquippedWeapon.Resource.Caption])); |
| 817 |
FEquippedWeapon.Equip; |
| 818 |
FEquippedWeapon.FreeNotification(Self); |
| 819 |
end else |
| 820 |
{ This may be causes by EquippedWeapon := nil in destructor, |
| 821 |
do not make any notification in this case. } |
| 822 |
if not (csDestroying in ComponentState) then |
| 823 |
Notifications.Show('You''re no longer using your weapon'); |
| 824 |
end; |
| 825 |
end; |
| 826 |
|
| 827 |
procedure TPlayer.SynchronizeToCamera; |
| 828 |
var |
| 829 |
P, D, U: TVector3; |
| 830 |
begin |
| 831 |
// avoid recursive calls between SynchronizeToCamera and SynchronizeFromCamera |
| 832 |
if InsideSynchronizeFromCamera <> 0 then Exit; |
| 833 |
|
| 834 |
// synchronize Position, Direction, Up *to* Navigation |
| 835 |
if not UseThirdPerson then |
| 836 |
try |
| 837 |
GetView(P, D, U); |
| 838 |
if Parent <> nil then |
| 839 |
begin |
| 840 |
P := Parent.LocalToWorld(P); |
| 841 |
D := Parent.LocalToWorldDirection(D); |
| 842 |
U := Parent.LocalToWorldDirection(U); |
| 843 |
end; |
| 844 |
Navigation.Camera.SetView(P, D, U); |
| 845 |
except |
| 846 |
on EViewportNotAssigned do |
| 847 |
WritelnWarning('Changing TCastlePlayer transformation (like position) before the TCastlePlayer.Navigation is assigned as TCastleViewport.Navigation is ignored'); |
| 848 |
end; |
| 849 |
end; |
| 850 |
|
| 851 |
procedure TPlayer.SynchronizeFromCamera; |
| 852 |
var |
| 853 |
P, D, U: TVector3; |
| 854 |
begin |
| 855 |
// synchronize Position, Direction, Up *from* Navigation |
| 856 |
if not UseThirdPerson then |
| 857 |
try |
| 858 |
Navigation.Camera.GetView(P, D, U); |
| 859 |
if Parent <> nil then |
| 860 |
begin |
| 861 |
P := Parent.WorldToLocal(P); |
| 862 |
D := Parent.WorldToLocalDirection(D); |
| 863 |
U := Parent.WorldToLocalDirection(U); |
| 864 |
end; |
| 865 |
Inc(InsideSynchronizeFromCamera); |
| 866 |
SetView(P, D, U); |
| 867 |
Dec(InsideSynchronizeFromCamera); |
| 868 |
except |
| 869 |
on EViewportNotAssigned do |
| 870 |
WritelnWarning('Adjusting TCastlePlayer transformation (like position) in PrepareResources is aborted if TCastleViewport.Navigation is not yet associated with TCastlePlayer.Navigation'); |
| 871 |
end; |
| 872 |
end; |
| 873 |
|
| 874 |
procedure TPlayer.ChangedTransform; |
| 875 |
begin |
| 876 |
inherited; |
| 877 |
{ If something called Player.Translation := xxx, update Navigation } |
| 878 |
SynchronizeToCamera; |
| 879 |
end; |
| 880 |
|
| 881 |
procedure TPlayer.UpdateNavigation; |
| 882 |
var |
| 883 |
NormalNavigationInput: TNavigationInputs; |
| 884 |
begin |
| 885 |
WalkNavigation.Gravity := (not Blocked) and (not Flying); |
| 886 |
{ Note that when not Navigation.Gravity then FallingEffect will not |
| 887 |
work anyway. } |
| 888 |
WalkNavigation.FallingEffect := FallingEffect and (FSwimming = psNo); |
| 889 |
|
| 890 |
if Blocked then |
| 891 |
begin |
| 892 |
{ When Blocked, we navigate navigation by code. } |
| 893 |
WalkNavigation.Input := []; |
| 894 |
end else |
| 895 |
begin |
| 896 |
NormalNavigationInput := [niNormal, ni3dMouse]; |
| 897 |
if EnableNavigationDragging and not Dead then |
| 898 |
Include(NormalNavigationInput, niMouseDragging); |
| 899 |
|
| 900 |
WalkNavigation.Input := NormalNavigationInput; |
| 901 |
|
| 902 |
{ Rotation keys work always, even when player is dead. |
| 903 |
Initially I disabled them, but after some thought: |
| 904 |
let them work. They work a little strangely (because Up |
| 905 |
is orthogonal to GravityUp), but they still work and player |
| 906 |
can figure it out. } |
| 907 |
WalkNavigation.Input_LeftRotate.Assign(PlayerInput_LeftRotate, false); |
| 908 |
WalkNavigation.Input_RightRotate.Assign(PlayerInput_RightRotate, false); |
| 909 |
WalkNavigation.Input_UpRotate.Assign(PlayerInput_UpRotate, false); |
| 910 |
WalkNavigation.Input_DownRotate.Assign(PlayerInput_DownRotate, false); |
| 911 |
WalkNavigation.Input_GravityUp.Assign(PlayerInput_GravityUp, false); |
| 912 |
end; |
| 913 |
|
| 914 |
if Blocked then |
| 915 |
begin |
| 916 |
{ PreferGravityUpXxx should be ignored actually, because rotations |
| 917 |
don't work now. } |
| 918 |
WalkNavigation.PreferGravityUpForMoving := true; |
| 919 |
WalkNavigation.PreferGravityUpForRotations := false; |
| 920 |
|
| 921 |
{ No need to do MakeClear now on any inputs, as we already set |
| 922 |
Input := []. } |
| 923 |
|
| 924 |
WalkNavigation.FallSpeedStart := TCastleWalkNavigation.DefaultFallSpeedStart; |
| 925 |
WalkNavigation.FallSpeedIncrease := TCastleWalkNavigation.DefaultFallSpeedIncrease; |
| 926 |
WalkNavigation.HeadBobbing := 0.0; |
| 927 |
WalkNavigation.PreferredHeight := WalkNavigation.Radius * 1.01; |
| 928 |
|
| 929 |
WalkNavigation.MoveHorizontalSpeed := DefaultMoveHorizontalSpeed; |
| 930 |
WalkNavigation.MoveVerticalSpeed := DefaultMoveVerticalSpeed; |
| 931 |
end else |
| 932 |
if Dead then |
| 933 |
begin |
| 934 |
WalkNavigation.PreferGravityUpForMoving := true; |
| 935 |
{ This is the only case when PreferGravityUpForRotations := false |
| 936 |
is sensible. } |
| 937 |
WalkNavigation.PreferGravityUpForRotations := false; |
| 938 |
|
| 939 |
WalkNavigation.Input_Jump.MakeClear; |
| 940 |
WalkNavigation.Input_Crouch.MakeClear; |
| 941 |
|
| 942 |
WalkNavigation.Input_Forward.MakeClear; |
| 943 |
WalkNavigation.Input_Backward.MakeClear; |
| 944 |
WalkNavigation.Input_LeftStrafe.MakeClear; |
| 945 |
WalkNavigation.Input_RightStrafe.MakeClear; |
| 946 |
|
| 947 |
WalkNavigation.FallSpeedStart := TCastleWalkNavigation.DefaultFallSpeedStart; |
| 948 |
WalkNavigation.FallSpeedIncrease := TCastleWalkNavigation.DefaultFallSpeedIncrease; |
| 949 |
WalkNavigation.HeadBobbing := 0.0; |
| 950 |
WalkNavigation.PreferredHeight := WalkNavigation.Radius * 1.01; |
| 951 |
|
| 952 |
WalkNavigation.MoveHorizontalSpeed := DefaultMoveHorizontalSpeed; |
| 953 |
WalkNavigation.MoveVerticalSpeed := DefaultMoveVerticalSpeed; |
| 954 |
end else |
| 955 |
begin |
| 956 |
if Flying then |
| 957 |
begin |
| 958 |
WalkNavigation.PreferGravityUpForMoving := false; |
| 959 |
WalkNavigation.PreferGravityUpForRotations := true; |
| 960 |
|
| 961 |
{ WalkNavigation.HeadBobbing and |
| 962 |
WalkNavigation.PreferredHeight and |
| 963 |
WalkNavigation.FallSpeedStart and |
| 964 |
WalkNavigation.FallSpeedIncrease |
| 965 |
... don't matter here, because Gravity is false. } |
| 966 |
|
| 967 |
WalkNavigation.MoveHorizontalSpeed := DefaultMoveHorizontalSpeed; |
| 968 |
WalkNavigation.MoveVerticalSpeed := DefaultMoveVerticalSpeed; |
| 969 |
end else |
| 970 |
if FSwimming <> psNo then |
| 971 |
begin |
| 972 |
WalkNavigation.PreferGravityUpForMoving := false; |
| 973 |
WalkNavigation.PreferGravityUpForRotations := true; |
| 974 |
|
| 975 |
WalkNavigation.FallSpeedStart := TCastleWalkNavigation.DefaultFallSpeedStart / 6; |
| 976 |
WalkNavigation.FallSpeedIncrease := 1.0; |
| 977 |
WalkNavigation.HeadBobbing := 0.0; |
| 978 |
WalkNavigation.PreferredHeight := WalkNavigation.Radius * 1.01; |
| 979 |
|
| 980 |
WalkNavigation.MoveHorizontalSpeed := DefaultMoveHorizontalSpeed / 2; |
| 981 |
WalkNavigation.MoveVerticalSpeed := DefaultMoveVerticalSpeed / 2; |
| 982 |
end else |
| 983 |
begin |
| 984 |
WalkNavigation.PreferGravityUpForMoving := true; |
| 985 |
WalkNavigation.PreferGravityUpForRotations := true; |
| 986 |
|
| 987 |
WalkNavigation.FallSpeedStart := TCastleWalkNavigation.DefaultFallSpeedStart; |
| 988 |
WalkNavigation.FallSpeedIncrease := TCastleWalkNavigation.DefaultFallSpeedIncrease; |
| 989 |
WalkNavigation.HeadBobbing := HeadBobbing; |
| 990 |
WalkNavigation.PreferredHeight := DefaultPreferredHeight; |
| 991 |
|
| 992 |
WalkNavigation.MoveHorizontalSpeed := DefaultMoveHorizontalSpeed; |
| 993 |
WalkNavigation.MoveVerticalSpeed := DefaultMoveVerticalSpeed; |
| 994 |
end; |
| 995 |
|
| 996 |
WalkNavigation.Input_Jump.Assign(PlayerInput_Jump, false); |
| 997 |
WalkNavigation.Input_Crouch.Assign(PlayerInput_Crouch, false); |
| 998 |
WalkNavigation.Input_Forward.Assign(PlayerInput_Forward, false); |
| 999 |
WalkNavigation.Input_Backward.Assign(PlayerInput_Backward, false); |
| 1000 |
WalkNavigation.Input_LeftStrafe.Assign(PlayerInput_LeftStrafe, false); |
| 1001 |
WalkNavigation.Input_RightStrafe.Assign(PlayerInput_RightStrafe, false); |
| 1002 |
end; |
| 1003 |
end; |
| 1004 |
|
| 1005 |
procedure TPlayer.Update(const SecondsPassed: Single; var RemoveMe: TRemoveType); |
| 1006 |
|
| 1007 |
{ Perform various things related to player swimming. } |
| 1008 |
procedure UpdateSwimming; |
| 1009 |
begin |
| 1010 |
{ Swimming possibly was changed by TLevel update } |
| 1011 |
if FSwimming = psUnderWater then |
| 1012 |
begin |
| 1013 |
{ Take care of drowning. } |
| 1014 |
if not Dead then |
| 1015 |
begin |
| 1016 |
if LifeTime - SwimBeginTime > SwimBreath then |
| 1017 |
begin |
| 1018 |
if (SwimLastDrownTime = 0.0) or |
| 1019 |
(LifeTime - SwimLastDrownTime > DrownPause) then |
| 1020 |
begin |
| 1021 |
if SwimLastDrownTime = 0.0 then |
| 1022 |
Notifications.Show('You''re drowning'); |
| 1023 |
SwimLastDrownTime := LifeTime; |
| 1024 |
Life := Life - (DrownDamageConst + Random * DrownDamageRandom); |
| 1025 |
{$warnings off} // just to keep deprecated working |
| 1026 |
SoundEngine.Play(stPlayerDrowning); |
| 1027 |
{$warnings on} |
| 1028 |
end; |
| 1029 |
end; |
| 1030 |
end; |
| 1031 |
|
| 1032 |
{ Take care of playing stPlayerSwimming } |
| 1033 |
if (not SwimmingPlayingSound.Playing) and |
| 1034 |
( (SwimLastSoundTime = 0.0) or |
| 1035 |
(LifeTime - SwimLastSoundTime > SwimSoundPause) ) then |
| 1036 |
begin |
| 1037 |
SwimLastSoundTime := LifeTime; |
| 1038 |
SoundEngine.Play(SwimmingPlayingSound); |
| 1039 |
end; |
| 1040 |
end; |
| 1041 |
end; |
| 1042 |
|
| 1043 |
{ Update IsOnTheGround and related variables. } |
| 1044 |
procedure UpdateIsOnTheGround; |
| 1045 |
const |
| 1046 |
{ TimeToChangeOnTheGround and ReallyIsOnTheGroundTime play here the |
| 1047 |
analogous role as ReallyWalkingOnTheGroundTime and |
| 1048 |
TimeToChangeFootstepsSound, see UpdateFootstepsSound. } |
| 1049 |
TimeToChangeIsOnTheGround = 0.5; |
| 1050 |
begin |
| 1051 |
if WalkNavigation.IsOnTheGround then |
| 1052 |
begin |
| 1053 |
ReallyIsOnTheGroundTime := LifeTime; |
| 1054 |
IsOnTheGround := true; |
| 1055 |
if Ground <> nil then |
| 1056 |
GroundProperty := Ground^.Shape.InternalMaterialProperty |
| 1057 |
else |
| 1058 |
GroundProperty := nil; |
| 1059 |
end else |
| 1060 |
if LifeTime - ReallyIsOnTheGroundTime > TimeToChangeIsOnTheGround then |
| 1061 |
begin |
| 1062 |
GroundProperty := nil; |
| 1063 |
IsOnTheGround := false; |
| 1064 |
end; { else leave GroundProperty and IsOnTheGround unchanged. } |
| 1065 |
end; |
| 1066 |
|
| 1067 |
{ Update IsToxic and related variables, hurt player if on toxic. |
| 1068 |
Must be called after UpdateIsOnTheGround (depends on GroundProperty). } |
| 1069 |
procedure UpdateToxic; |
| 1070 |
var |
| 1071 |
NewIsToxic: boolean; |
| 1072 |
begin |
| 1073 |
{$warnings off} // using deprecated material props in deprecated CastlePlayer |
| 1074 |
NewIsToxic := (GroundProperty <> nil) and GroundProperty.Toxic; |
| 1075 |
if NewIsToxic then |
| 1076 |
begin |
| 1077 |
if (not IsToxic) or |
| 1078 |
(LifeTime - ToxicLastDamageTime > GroundProperty.ToxicDamageTime) then |
| 1079 |
begin |
| 1080 |
ToxicLastDamageTime := LifeTime; |
| 1081 |
if not Dead then |
| 1082 |
begin |
| 1083 |
SoundEngine.Play(stPlayerToxicPain); |
| 1084 |
SetLifeCustomFadeOut(Life - (GroundProperty.ToxicDamageConst + |
| 1085 |
Random * GroundProperty.ToxicDamageRandom), Green); |
| 1086 |
end; |
| 1087 |
end; |
| 1088 |
end; |
| 1089 |
IsToxic := NewIsToxic; |
| 1090 |
{$warnings on} |
| 1091 |
end; |
| 1092 |
|
| 1093 |
{ Update FootstepsSound and related variables. |
| 1094 |
Must be called after UpdateIsOnTheGround (depends on GroundProperty). } |
| 1095 |
procedure UpdateFootstepsSound; |
| 1096 |
const |
| 1097 |
TimeToChangeFootstepsSound = 0.5; |
| 1098 |
var |
| 1099 |
NewFootstepsSound: TCastleSound; |
| 1100 |
begin |
| 1101 |
{ The meaning of ReallyWalkingOnTheGroundTime and |
| 1102 |
TimeToChangeFootstepsSound: |
| 1103 |
WalkNavigation.IsWalkingOnTheGround can change quite rapidly |
| 1104 |
(when player quickly presses and releases up/down keys, |
| 1105 |
or when he're walking up the stairs, or when he's walking |
| 1106 |
on un-flat terrain --- then WalkNavigation.IsWalkingOnTheGround |
| 1107 |
switches between @true and @false quite often). |
| 1108 |
But it is undesirable to change FootstepsSound |
| 1109 |
so often, as this causes footsteps to suddenly stop, then play, |
| 1110 |
then stop again etc. --- this doesn't sound good. |
| 1111 |
|
| 1112 |
So I use ReallyWalkingOnTheGroundTime to mark myself |
| 1113 |
the time when WalkNavigation.IsWalkingOnTheGround was true. |
| 1114 |
In normal situation I would set NewFootstepsSound to nil |
| 1115 |
when WalkNavigation.IsWalkingOnTheGround = @false. |
| 1116 |
But now I set NewFootstepsSound to nil only if |
| 1117 |
WalkNavigation.IsWalkingOnTheGround = @false |
| 1118 |
for at least TimeToChangeFootstepsSound seconds. } |
| 1119 |
|
| 1120 |
{ calculate NewFootstepsSound } |
| 1121 |
if WalkNavigation.IsWalkingOnTheGround then |
| 1122 |
begin |
| 1123 |
ReallyWalkingOnTheGroundTime := LifeTime; |
| 1124 |
{ Since WalkNavigation.IsWalkingOnTheGroundm then for sure |
| 1125 |
WalkNavigation.IsOnTheGround, so UpdateIsOnTheGround updated |
| 1126 |
GroundProperty field. } |
| 1127 |
{$warnings off} // just to keep deprecated working |
| 1128 |
if (GroundProperty <> nil) and |
| 1129 |
(GroundProperty.FootstepsSound <> nil) then |
| 1130 |
NewFootstepsSound := GroundProperty.FootstepsSound |
| 1131 |
else |
| 1132 |
NewFootstepsSound := stPlayerFootstepsDefault; |
| 1133 |
{$warnings on} |
| 1134 |
end else |
| 1135 |
if LifeTime - ReallyWalkingOnTheGroundTime > |
| 1136 |
TimeToChangeFootstepsSound then |
| 1137 |
NewFootstepsSound := nil |
| 1138 |
else |
| 1139 |
NewFootstepsSound := FootstepsSound; |
| 1140 |
|
| 1141 |
if FootstepsSound <> NewFootstepsSound then |
| 1142 |
begin |
| 1143 |
FootstepsPlayingSound.Stop; |
| 1144 |
FootstepsPlayingSound.Sound := NewFootstepsSound; |
| 1145 |
if NewFootstepsSound <> nil then |
| 1146 |
SoundEngine.Play(FootstepsPlayingSound); |
| 1147 |
FootstepsSound := NewFootstepsSound; |
| 1148 |
end; |
| 1149 |
end; |
| 1150 |
|
| 1151 |
const |
| 1152 |
FadeOutSpeed = 2.0; |
| 1153 |
begin |
| 1154 |
inherited; |
| 1155 |
if not Exists then Exit; |
| 1156 |
|
| 1157 |
SynchronizeFromCamera; |
| 1158 |
|
| 1159 |
if FFlyingTimeOut > 0 then |
| 1160 |
begin |
| 1161 |
FFlyingTimeOut := FFlyingTimeOut - SecondsPassed; |
| 1162 |
if FFlyingTimeOut <= 0 then |
| 1163 |
begin |
| 1164 |
FFlyingTimeOut := 0; |
| 1165 |
FFlying := false; |
| 1166 |
end; |
| 1167 |
end; |
| 1168 |
|
| 1169 |
UpdateNavigation; |
| 1170 |
|
| 1171 |
UpdateSwimming; |
| 1172 |
|
| 1173 |
if FFadeOutIntensity > 0 then |
| 1174 |
FFadeOutIntensity := FFadeOutIntensity - FadeOutSpeed * SecondsPassed; |
| 1175 |
|
| 1176 |
if (EquippedWeapon <> nil) and |
| 1177 |
(InternalLevel <> nil) then |
| 1178 |
begin |
| 1179 |
FEquippedWeaponResourceFrame.Exists := true; |
| 1180 |
EquippedWeapon.EquippedUpdate(InternalLevel, SecondsPassed, FEquippedWeaponResourceFrame); |
| 1181 |
end else |
| 1182 |
FEquippedWeaponResourceFrame.Exists := false; |
| 1183 |
|
| 1184 |
UpdateIsOnTheGround; |
| 1185 |
UpdateToxic; |
| 1186 |
UpdateFootstepsSound; |
| 1187 |
|
| 1188 |
FDebugTransform.Exists := RenderDebug; |
| 1189 |
end; |
| 1190 |
|
| 1191 |
procedure TPlayer.FadeOut(const Color: TCastleColor); |
| 1192 |
begin |
| 1193 |
FFadeOutColor := Color; |
| 1194 |
FFadeOutIntensity := 1; |
| 1195 |
end; |
| 1196 |
|
| 1197 |
procedure TPlayer.NavigationFall(const Sender: TCastleNavigation; const FallHeight: Single); |
| 1198 |
begin |
| 1199 |
Fall(FallHeight); |
| 1200 |
end; |
| 1201 |
|
| 1202 |
procedure TPlayer.Fall(const FallHeight: Single); |
| 1203 |
begin |
| 1204 |
inherited; |
| 1205 |
|
| 1206 |
if (FSwimming = psNo) and |
| 1207 |
(FallHeight > FallMinHeightToSound) then |
| 1208 |
SoundEngine.Play(FallSound); |
| 1209 |
|
| 1210 |
if (FSwimming = psNo) and (FallHeight > FallMinHeightToDamage) then |
| 1211 |
Life := Life - Max(0, FallHeight * |
| 1212 |
MapRange(Random, 0.0, 1.0, FallDamageScaleMin, FallDamageScaleMax)); |
| 1213 |
end; |
| 1214 |
|
| 1215 |
procedure TPlayer.SetLifeCustomFadeOut(const Value: Single; |
| 1216 |
const Color: TCastleColor); |
| 1217 |
begin |
| 1218 |
if (Life > 0) and (Value <= 0) then |
| 1219 |
begin |
| 1220 |
Notifications.Show('You die'); |
| 1221 |
{$warnings off} // just to keep deprecated working |
| 1222 |
SoundEngine.Play(stPlayerDies); |
| 1223 |
{$warnings on} |
| 1224 |
WalkNavigation.FallOnTheGround; |
| 1225 |
end else |
| 1226 |
if (Life - Value) > 1 then |
| 1227 |
begin |
| 1228 |
FadeOut(Color); |
| 1229 |
{$warnings off} // just to keep deprecated working |
| 1230 |
SoundEngine.Play(stPlayerSuddenPain); |
| 1231 |
{$warnings on} |
| 1232 |
end; |
| 1233 |
inherited SetLife(Value); |
| 1234 |
end; |
| 1235 |
|
| 1236 |
procedure TPlayer.SetLife(const Value: Single); |
| 1237 |
begin |
| 1238 |
SetLifeCustomFadeOut(Value, Red); |
| 1239 |
{ cancel flying when dead } |
| 1240 |
if Dead then |
| 1241 |
Flying := false; |
| 1242 |
end; |
| 1243 |
|
| 1244 |
procedure TPlayer.Attack; |
| 1245 |
begin |
| 1246 |
if (EquippedWeapon <> nil) and |
| 1247 |
(InternalLevel <> nil) then |
| 1248 |
EquippedWeapon.EquippedAttack(InternalLevel) |
| 1249 |
else |
| 1250 |
{ Cannot attack without weapon equipped. |
| 1251 |
If the game will want to have some "always owned weapon" |
| 1252 |
(like footkick in Duke, crowbar in HalfLife) |
| 1253 |
that game will have to assign it to EquippedWeapon. } |
| 1254 |
Notifications.Show('No weapon equipped'); |
| 1255 |
end; |
| 1256 |
|
| 1257 |
procedure TPlayer.SetSwimming(const Value: TPlayerSwimming); |
| 1258 |
begin |
| 1259 |
if Value <> FSwimming then |
| 1260 |
begin |
| 1261 |
{ If "Swimming = psUnderWater" state changed then play a sound. } |
| 1262 |
if (FSwimming = psUnderWater) <> |
| 1263 |
(Value = psUnderWater) then |
| 1264 |
begin |
| 1265 |
if not SwimmingChangePlayingSound.Playing then |
| 1266 |
SoundEngine.Play(SwimmingChangePlayingSound); // play once |
| 1267 |
end; |
| 1268 |
|
| 1269 |
if (FSwimming = psNo) and (Value <> psNo) then |
| 1270 |
begin |
| 1271 |
{ Cancel falling down, otherwise he will fall down into the water |
| 1272 |
with the high speed (because in the air FallSpeedStart |
| 1273 |
is high and it's increased, but in the water it's much lower |
| 1274 |
and not increased at all right now). } |
| 1275 |
WalkNavigation.CancelFalling; |
| 1276 |
end; |
| 1277 |
|
| 1278 |
FSwimming := Value; |
| 1279 |
|
| 1280 |
if FSwimming = psUnderWater then |
| 1281 |
begin |
| 1282 |
SwimBeginTime := LifeTime; |
| 1283 |
SwimLastDrownTime := 0.0; |
| 1284 |
SwimLastSoundTime := 0.0; |
| 1285 |
end; |
| 1286 |
|
| 1287 |
{ Although UpdateNavigation will be called in nearest Player.Update anyway, |
| 1288 |
I want to call it *now*. That's because I want to set |
| 1289 |
Navigation.FallSpeedStart to low speed (suitable for moving |
| 1290 |
under the water) before next falling down will happen. |
| 1291 |
Why ? See comments about Navigation.CancelFalling above. |
| 1292 |
|
| 1293 |
And next falling down will happen... actually SetSwimming |
| 1294 |
is called from OnMatrixChanged that may be called |
| 1295 |
from TryFalling ! So next falling down definitely *can* |
| 1296 |
happen before next Player.Update. Actually we may be in the middle |
| 1297 |
of falling down right now. Fortunately Navigation.Update |
| 1298 |
and Navigation.CancelFalling are implemented (or rather fixed :) |
| 1299 |
to honour calling CancelFalling and setting FallSpeedStart now. |
| 1300 |
|
| 1301 |
So the safeguard below is needed. } |
| 1302 |
UpdateNavigation; |
| 1303 |
end; |
| 1304 |
end; |
| 1305 |
|
| 1306 |
procedure TPlayer.LoadFromFile(const Url: String); |
| 1307 |
var |
| 1308 |
Config: TCastleConfig; |
| 1309 |
begin |
| 1310 |
Config := TCastleConfig.Create(nil); |
| 1311 |
try |
| 1312 |
Config.RootName := 'player'; |
| 1313 |
Config.NotModified; { otherwise changing RootName makes it modified, and saved back at freeing } |
| 1314 |
Config.Url := Url; |
| 1315 |
|
| 1316 |
KnockBackSpeed := Config.GetFloat('knockback_speed', DefaultPlayerKnockBackSpeed); |
| 1317 |
WalkNavigation.JumpMaxHeight := Config.GetFloat('jump/max_height', TCastleWalkNavigation.DefaultJumpMaxHeight); |
| 1318 |
WalkNavigation.JumpHorizontalSpeedMultiply := Config.GetFloat('jump/horizontal_speed_multiply', TCastleWalkNavigation.DefaultJumpHorizontalSpeedMultiply); |
| 1319 |
WalkNavigation.JumpTime := Config.GetFloat('jump/time', TCastleWalkNavigation.DefaultJumpTime); |
| 1320 |
WalkNavigation.HeadBobbingTime := Config.GetFloat('head_bobbing_time', TCastleWalkNavigation.DefaultHeadBobbingTime); |
| 1321 |
HeadBobbing := Config.GetFloat('head_bobbing', TCastleWalkNavigation.DefaultHeadBobbing); |
| 1322 |
SickProjectionSpeed := Config.GetFloat('sick_projection_speed', DefaultSickProjectionSpeed); |
| 1323 |
FallMinHeightToSound := Config.GetFloat('fall/sound/min_height', DefaultFallMinHeightToSound); |
| 1324 |
FallMinHeightToDamage := Config.GetFloat('fall/damage/min_height', DefaultFallMinHeightToDamage); |
| 1325 |
FallDamageScaleMin := Config.GetFloat('fall/damage/scale_min', DefaultFallDamageScaleMin); |
| 1326 |
FallDamageScaleMax := Config.GetFloat('fall/damage/scale_max', DefaultFallDamageScaleMax); |
| 1327 |
{$warnings off} // using deprecated SoundFromName in deprecated |
| 1328 |
FallSound := SoundEngine.SoundFromName(Config.GetValue('fall/sound/name', DefaultFallSoundName), false); |
| 1329 |
{$warnings on} |
| 1330 |
FSwimBreath := Config.GetFloat('swim/breath', DefaultSwimBreath); |
| 1331 |
FSwimSoundPause := Config.GetFloat('swim/sound_pause', DefaultSwimSoundPause); |
| 1332 |
FDrownPause := Config.GetFloat('drown/pause', DefaultDrownPause); |
| 1333 |
FDrownDamageConst := Config.GetFloat('drown/damage/const', DefaultDrownDamageConst); |
| 1334 |
FDrownDamageRandom := Config.GetFloat('drown/damage/random', DefaultDrownDamageRandom); |
| 1335 |
finally FreeAndNil(Config); end; |
| 1336 |
end; |
| 1337 |
|
| 1338 |
procedure TPlayer.LoadFromFile; |
| 1339 |
begin |
| 1340 |
LoadFromFile('castle-data:/player.xml'); |
| 1341 |
end; |
| 1342 |
|
| 1343 |
procedure TPlayer.LevelChanged; |
| 1344 |
begin |
| 1345 |
{ Without this, ReallyWalkingOnTheGroundTime could pretend that |
| 1346 |
player is walking on the ground, while in fact the player is just |
| 1347 |
standing still after new level loaded. } |
| 1348 |
ReallyWalkingOnTheGroundTime := -1000.0; |
| 1349 |
|
| 1350 |
FootstepsPlayingSound.Stop; |
| 1351 |
FootstepsPlayingSound.Sound := nil; |
| 1352 |
FootstepsSound := nil; |
| 1353 |
|
| 1354 |
ReallyIsOnTheGroundTime := -1000; |
| 1355 |
IsOnTheGround := false; |
| 1356 |
GroundProperty := nil; |
| 1357 |
|
| 1358 |
IsToxic := false; |
| 1359 |
end; |
| 1360 |
|
| 1361 |
function TPlayer.Ground: PTriangle; |
| 1362 |
begin |
| 1363 |
if UseThirdPerson then |
| 1364 |
Result := nil |
| 1365 |
else |
| 1366 |
Result := WalkNavigation.AboveGround; |
| 1367 |
end; |
| 1368 |
|
| 1369 |
function TPlayer.LocalSegmentCollision(const Pos1, Pos2: TVector3; |
| 1370 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc; |
| 1371 |
const ALineOfSight: boolean): boolean; |
| 1372 |
begin |
| 1373 |
if ALineOfSight then |
| 1374 |
{ Player box is collidable (creatures cannot enter on player), |
| 1375 |
but is not visible, so ALineOfSight ignores it. |
| 1376 |
This allows creatures to see player's middle point. } |
| 1377 |
Result := false |
| 1378 |
else |
| 1379 |
Result := inherited; |
| 1380 |
end; |
| 1381 |
|
| 1382 |
function TPlayer.Sphere(out Radius: Single): boolean; |
| 1383 |
begin |
| 1384 |
{ We need to use Sphere for Player for collisions and gravity to make sense. |
| 1385 |
So we always return true. |
| 1386 |
When CollisionSphereRadius <> 0 we return it (just like inherited), |
| 1387 |
otherwise we use Navigation.Radius. } |
| 1388 |
if CollisionSphereRadius <> 0 then |
| 1389 |
Radius := CollisionSphereRadius |
| 1390 |
else |
| 1391 |
Radius := Navigation.Radius; |
| 1392 |
Result := true; |
| 1393 |
end; |
| 1394 |
|
| 1395 |
procedure TPlayer.Notification(AComponent: TComponent; Operation: TOperation); |
| 1396 |
begin |
| 1397 |
inherited; |
| 1398 |
|
| 1399 |
if (Operation = opRemove) and (AComponent = FEquippedWeapon) then |
| 1400 |
FEquippedWeapon := nil; |
| 1401 |
end; |
| 1402 |
|
| 1403 |
function TPlayer.LocalHeightCollision(const APosition, GravityUp: TVector3; |
| 1404 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc; |
| 1405 |
out AboveHeight: Single; out AboveGround: PTriangle): boolean; |
| 1406 |
begin |
| 1407 |
{ instead of allowing inherited to do the work (and allow other stuff |
| 1408 |
like items and creatures to stand on player's head), for now just |
| 1409 |
make player non-collidable for Height. Otherwise, when trying really |
| 1410 |
hard to walk "into" a creature, sometimes the creature may start to |
| 1411 |
raise up and stand on your own (player's) head. |
| 1412 |
This doesn't have any adverse effects for castle, after all |
| 1413 |
items can fall down through us (they are immediately picked up then), |
| 1414 |
and creature never fall down because of gravity on our head. } |
| 1415 |
|
| 1416 |
Result := false; |
| 1417 |
AboveHeight := MaxSingle; |
| 1418 |
AboveGround := nil; |
| 1419 |
end; |
| 1420 |
|
| 1421 |
function TPlayer.Press(const Event: TInputPressRelease): Boolean; |
| 1422 |
begin |
| 1423 |
Result := inherited; |
| 1424 |
if Result then Exit; |
| 1425 |
|
| 1426 |
if not (Blocked or Dead) then |
| 1427 |
begin |
| 1428 |
if PlayerInput_Attack.IsEvent(Event) then |
| 1429 |
begin |
| 1430 |
Attack; |
| 1431 |
Exit(true); |
| 1432 |
end; |
| 1433 |
|
| 1434 |
if PlayerInput_CancelFlying.IsEvent(Event) then |
| 1435 |
begin |
| 1436 |
Flying := false; |
| 1437 |
Exit(true); |
| 1438 |
end; |
| 1439 |
|
| 1440 |
if PlayerInput_InventoryShow.IsEvent(Event) then |
| 1441 |
begin |
| 1442 |
InventoryVisible := not InventoryVisible; |
| 1443 |
Exit(true); |
| 1444 |
end; |
| 1445 |
|
| 1446 |
if PlayerInput_InventoryPrevious.IsEvent(Event) then |
| 1447 |
begin |
| 1448 |
ChangeInventoryCurrentItem(-1); |
| 1449 |
Exit(true); |
| 1450 |
end; |
| 1451 |
|
| 1452 |
if PlayerInput_InventoryNext.IsEvent(Event) then |
| 1453 |
begin |
| 1454 |
ChangeInventoryCurrentItem(+1); |
| 1455 |
Exit(true); |
| 1456 |
end; |
| 1457 |
|
| 1458 |
if PlayerInput_DropItem.IsEvent(Event) then |
| 1459 |
begin |
| 1460 |
DropCurrentItem; |
| 1461 |
Exit(true); |
| 1462 |
end; |
| 1463 |
|
| 1464 |
if PlayerInput_UseItem.IsEvent(Event) then |
| 1465 |
begin |
| 1466 |
UseCurrentItem; |
| 1467 |
Exit(true); |
| 1468 |
end; |
| 1469 |
end; |
| 1470 |
end; |
| 1471 |
|
| 1472 |
procedure TPlayer.Render(const Params: TRenderParams); |
| 1473 |
begin |
| 1474 |
{ Do this before rendering, otherwise we could display weapon in unsynchronized |
| 1475 |
position/orientation. |
| 1476 |
That's because Navigation could be changed after our Update, |
| 1477 |
but before rendering. |
| 1478 |
(Testcase: move/rotate using touch control |
| 1479 |
in fps_game when you have shooting_eye.) } |
| 1480 |
SynchronizeFromCamera; |
| 1481 |
|
| 1482 |
inherited; |
| 1483 |
end; |
| 1484 |
|
| 1485 |
function TPlayer.Middle: TVector3; |
| 1486 |
begin |
| 1487 |
if UseThirdPerson then |
| 1488 |
Result := inherited |
| 1489 |
else |
| 1490 |
begin |
| 1491 |
{ For 1st-person player, our Translation is already the suitable "eye position" |
| 1492 |
above the ground. |
| 1493 |
|
| 1494 |
Note that Player.Gravity remains false for now (only Player.Navigation.Gravity |
| 1495 |
is true), so the player is not affected by simple gravity implemented in |
| 1496 |
CastleTransform unit, so there's no point in overriding methods like PreferredHeight. |
| 1497 |
TCastleWalkNavigation.Gravity does all the work now. } |
| 1498 |
|
| 1499 |
Result := Translation; |
| 1500 |
end; |
| 1501 |
end; |
| 1502 |
|
| 1503 |
procedure TPlayer.SetEnableNavigationDragging(const AValue: Boolean); |
| 1504 |
begin |
| 1505 |
if FEnableNavigationDragging <> AValue then |
| 1506 |
begin |
| 1507 |
FEnableNavigationDragging := AValue; |
| 1508 |
UpdateNavigation; |
| 1509 |
end; |
| 1510 |
end; |
| 1511 |
|
| 1512 |
procedure TPlayer.SetUseThirdPerson(const AValue: Boolean); |
| 1513 |
begin |
| 1514 |
if World <> nil then |
| 1515 |
raise Exception.Create('TODO: For now you cannot change TPlayer.UseThirdPerson once the TLevel (that refers to this TPlayer) has been Loaded'); |
| 1516 |
FUseThirdPerson := AValue; |
| 1517 |
FBox.Exists := not UseThirdPerson; |
| 1518 |
end; |
| 1519 |
|
| 1520 |
function TPlayer.GetRenderOnTop: Boolean; |
| 1521 |
begin |
| 1522 |
Result := RenderLayer = rlFront; |
| 1523 |
end; |
| 1524 |
|
| 1525 |
procedure TPlayer.SetRenderOnTop(const Value: Boolean); |
| 1526 |
begin |
| 1527 |
if Value then |
| 1528 |
RenderLayer := rlFront |
| 1529 |
else |
| 1530 |
RenderLayer := rlParent; |
| 1531 |
end; |
| 1532 |
|
| 1533 |
initialization |
| 1534 |
{ Order of creation below is significant: it determines the order |
| 1535 |
of menu entries in "Configure controls". } |
| 1536 |
|
| 1537 |
PlayerInput_Forward := TInputShortcut.Create(nil, 'Move forward', 'move_forward', igBasic); |
| 1538 |
PlayerInput_Forward.Assign(keyW, keyArrowUp); |
| 1539 |
PlayerInput_Backward := TInputShortcut.Create(nil, 'Move backward', 'move_backward', igBasic); |
| 1540 |
PlayerInput_Backward.Assign(keyS, keyArrowDown); |
| 1541 |
PlayerInput_LeftRotate := TInputShortcut.Create(nil, 'Turn left', 'turn_left', igBasic); |
| 1542 |
PlayerInput_LeftRotate.Assign(keyArrowLeft); |
| 1543 |
PlayerInput_RightRotate := TInputShortcut.Create(nil, 'Turn right', 'turn_right', igBasic); |
| 1544 |
PlayerInput_RightRotate.Assign(keyArrowRight); |
| 1545 |
PlayerInput_LeftStrafe := TInputShortcut.Create(nil, 'Move left', 'move_left', igBasic); |
| 1546 |
PlayerInput_LeftStrafe.Assign(keyA); |
| 1547 |
PlayerInput_RightStrafe := TInputShortcut.Create(nil, 'Move right', 'move_right', igBasic); |
| 1548 |
PlayerInput_RightStrafe.Assign(keyD); |
| 1549 |
PlayerInput_UpRotate := TInputShortcut.Create(nil, 'Look up', 'look_up', igBasic); |
| 1550 |
PlayerInput_UpRotate.Assign(keyNone); |
| 1551 |
PlayerInput_DownRotate := TInputShortcut.Create(nil, 'Look down', 'look_down', igBasic); |
| 1552 |
PlayerInput_DownRotate.Assign(keyNone); |
| 1553 |
PlayerInput_GravityUp := TInputShortcut.Create(nil, 'Look straight', 'look_straight', igBasic); |
| 1554 |
PlayerInput_GravityUp.Assign(keyNone); |
| 1555 |
PlayerInput_Jump := TInputShortcut.Create(nil, 'Jump (or fly/swim up)', 'move_up', igBasic); |
| 1556 |
PlayerInput_Jump.Assign(keySpace); |
| 1557 |
PlayerInput_Crouch := TInputShortcut.Create(nil, 'Crouch (or fly/swim down)', 'move_down', igBasic); |
| 1558 |
PlayerInput_Crouch.Assign(keyC); |
| 1559 |
|
| 1560 |
PlayerInput_Attack := TInputShortcut.Create(nil, 'Attack', 'attack', igBasic); |
| 1561 |
PlayerInput_Attack.Assign(keyCtrl, keyNone, '', false, buttonLeft); |
| 1562 |
PlayerInput_Attack.GroupOrder := -100; { before other (player) shortcuts } |
| 1563 |
PlayerInput_InventoryShow := TInputShortcut.Create(nil, 'Inventory show / hide', 'inventory_toggle', igItems); |
| 1564 |
PlayerInput_InventoryShow.Assign(keyNone, keyNone, '', false, buttonLeft); |
| 1565 |
PlayerInput_InventoryPrevious := TInputShortcut.Create(nil, 'Select previous item', 'inventory_previous', igItems); |
| 1566 |
PlayerInput_InventoryPrevious.Assign(keyLeftBracket, keyNone, '', false, buttonLeft, mwUp); |
| 1567 |
PlayerInput_InventoryNext := TInputShortcut.Create(nil, 'Select next item', 'inventory_next', igItems); |
| 1568 |
PlayerInput_InventoryNext.Assign(keyRightBracket, keyNone, '', false, buttonLeft, mwDown); |
| 1569 |
PlayerInput_UseItem := TInputShortcut.Create(nil, 'Use (or equip) selected item', 'item_use', igItems); |
| 1570 |
PlayerInput_UseItem.Assign(keyEnter, keyNone, '', false, buttonLeft); |
| 1571 |
PlayerInput_DropItem := TInputShortcut.Create(nil, 'Drop selected item', 'item_drop', igItems); |
| 1572 |
PlayerInput_DropItem.Assign(keyNone, keyNone, '', false, buttonLeft); |
| 1573 |
PlayerInput_CancelFlying := TInputShortcut.Create(nil, 'Cancel flying spell', 'cancel_flying', igOther); |
| 1574 |
PlayerInput_CancelFlying.Assign(keyNone, keyNone, '', false, buttonLeft); |
| 1575 |
end. |