| 1 |
{ |
| 2 |
Copyright 2007-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 |
{ Creatures and (as a special creature descendant) player. } |
| 17 |
unit GameCreatures; |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses SysUtils, Classes, Generics.Collections, |
| 22 |
CastleUtils, CastleClassUtils, CastleScene, |
| 23 |
CastleVectors, CastleTransform, CastleFrustum, CastleApplicationProperties, |
| 24 |
CastleTimeUtils, X3DNodes, CastleColors, CastleDebugTransform, |
| 25 |
CastleComponentSerialize; |
| 26 |
|
| 27 |
type |
| 28 |
TCreatureState = (csIdle, csBored, csWalk); |
| 29 |
|
| 30 |
type |
| 31 |
TCreatureKind = class |
| 32 |
private |
| 33 |
FName: string; |
| 34 |
Template: TSerializedComponent; |
| 35 |
Url: String; |
| 36 |
Loaded: boolean; |
| 37 |
public |
| 38 |
constructor Create(const AName: string); |
| 39 |
destructor Destroy; override; |
| 40 |
|
| 41 |
{ Create things necessary for playing (displaying this creature). } |
| 42 |
procedure Load(const PrepareParams: TPrepareParams); |
| 43 |
|
| 44 |
{ Creature short name, must be simple (needs to be valid XML element |
| 45 |
name and X3D node name, for easy data reading). } |
| 46 |
property Name: string read FName; |
| 47 |
|
| 48 |
{ Loads creature properties from GameConfig file. |
| 49 |
This is normally called by constructor, so you don't have to call it. |
| 50 |
However, it may be useful for debugging purposes to "reload index.xml" |
| 51 |
file, and then you should call this (and probably you should reload |
| 52 |
this creature kind, if it's already loaded; unless you know what |
| 53 |
you're doing :) ). } |
| 54 |
procedure LoadFromConfig; |
| 55 |
end; |
| 56 |
|
| 57 |
TCreatureKindList = class({$ifdef FPC}specialize{$endif} TObjectList<TCreatureKind>) |
| 58 |
public |
| 59 |
PlayerKind: TCreatureKind; |
| 60 |
|
| 61 |
{ Create creatures and set their parameters from GameConfig. } |
| 62 |
constructor Create; |
| 63 |
end; |
| 64 |
|
| 65 |
TCreature = class(TCastleTransform) |
| 66 |
private |
| 67 |
FKind: TCreatureKind; |
| 68 |
FDebugTransform: TDebugTransform; |
| 69 |
FState: TCreatureState; |
| 70 |
LifeTime: TFloatTime; |
| 71 |
Scene: TCastleScene; |
| 72 |
procedure SetState(const Value: TCreatureState); |
| 73 |
public |
| 74 |
constructor Create(AKind: TCreatureKind); reintroduce; |
| 75 |
property Kind: TCreatureKind read FKind; |
| 76 |
property State: TCreatureState read FState write SetState; |
| 77 |
procedure Update(const SecondsPassed: Single; var RemoveMe: TRemoveType); override; |
| 78 |
end; |
| 79 |
|
| 80 |
TPlayer = class(TCreature) |
| 81 |
private |
| 82 |
{ This is set and used only if csWalk } |
| 83 |
WantsToWalkPos, WantsToWalkDir: TVector3; |
| 84 |
FTargetVisualize: TTransformNode; |
| 85 |
FTargetVisualizeShape: TShapeNode; |
| 86 |
public |
| 87 |
constructor Create(AKind: TCreatureKind); |
| 88 |
destructor Destroy; override; |
| 89 |
procedure Update(const SecondsPassed: Single; var RemoveMe: TRemoveType); override; |
| 90 |
procedure LocationChanged; |
| 91 |
procedure WantsToWalk(const Value: TVector3); |
| 92 |
end; |
| 93 |
|
| 94 |
var |
| 95 |
CreatureKinds: TCreatureKindList; |
| 96 |
|
| 97 |
implementation |
| 98 |
|
| 99 |
uses Math, |
| 100 |
CastleLog, CastleGLUtils, CastleUIControls, CastleSceneCore, |
| 101 |
GameConfiguration, CastleViewport; |
| 102 |
|
| 103 |
const |
| 104 |
Debug = true; |
| 105 |
|
| 106 |
CreatureAnimationName: array [TCreatureState] of string = |
| 107 |
( 'idle', 'bored', 'walk' ); |
| 108 |
|
| 109 |
{ TCreatureKind -------------------------------------------------------------- } |
| 110 |
|
| 111 |
constructor TCreatureKind.Create(const AName: string); |
| 112 |
begin |
| 113 |
inherited Create; |
| 114 |
FName := AName; |
| 115 |
LoadFromConfig; |
| 116 |
end; |
| 117 |
|
| 118 |
destructor TCreatureKind.Destroy; |
| 119 |
begin |
| 120 |
FreeAndNil(Template); |
| 121 |
inherited; |
| 122 |
end; |
| 123 |
|
| 124 |
procedure TCreatureKind.LoadFromConfig; |
| 125 |
begin |
| 126 |
Url := GameConfig.GetURL('creatures/' + Name + '/url'); |
| 127 |
end; |
| 128 |
|
| 129 |
procedure TCreatureKind.Load(const PrepareParams: TPrepareParams); |
| 130 |
begin |
| 131 |
if Loaded then Exit; |
| 132 |
Loaded := true; |
| 133 |
Template := TSerializedComponent.Create(Url); |
| 134 |
end; |
| 135 |
|
| 136 |
{ TCreature ------------------------------------------------------------------ } |
| 137 |
|
| 138 |
constructor TCreature.Create(AKind: TCreatureKind); |
| 139 |
begin |
| 140 |
inherited Create(nil); |
| 141 |
|
| 142 |
{ initialize state fields } |
| 143 |
FState := csIdle; |
| 144 |
LifeTime := 0; |
| 145 |
|
| 146 |
FKind := AKind; |
| 147 |
|
| 148 |
FDebugTransform := TDebugTransform.Create(Self); |
| 149 |
FDebugTransform.Parent := Self; |
| 150 |
|
| 151 |
Scene := AKind.Template.TransformLoad(Self) as TCastleScene; |
| 152 |
Scene.AutoAnimation := CreatureAnimationName[FState]; |
| 153 |
Add(Scene); |
| 154 |
end; |
| 155 |
|
| 156 |
procedure TCreature.SetState(const Value: TCreatureState); |
| 157 |
begin |
| 158 |
if FState <> Value then |
| 159 |
begin |
| 160 |
FState := Value; |
| 161 |
Scene.AutoAnimation := CreatureAnimationName[FState]; |
| 162 |
end; |
| 163 |
end; |
| 164 |
|
| 165 |
procedure TCreature.Update(const SecondsPassed: Single; var RemoveMe: TRemoveType); |
| 166 |
begin |
| 167 |
inherited; |
| 168 |
// TODO: Switch to csBored (and back to csIdle) from time to time |
| 169 |
LifeTime := LifeTime + SecondsPassed; |
| 170 |
FDebugTransform.Exists := Debug; |
| 171 |
end; |
| 172 |
|
| 173 |
{ TCreatureKindList ---------------------------------------------------------- } |
| 174 |
|
| 175 |
constructor TCreatureKindList.Create; |
| 176 |
begin |
| 177 |
inherited Create(true); |
| 178 |
PlayerKind := TCreatureKind.Create('player'); |
| 179 |
Add(PlayerKind); |
| 180 |
end; |
| 181 |
|
| 182 |
{ TPlayer -------------------------------------------------------------------- } |
| 183 |
|
| 184 |
constructor TPlayer.Create(AKind: TCreatureKind); |
| 185 |
|
| 186 |
procedure CreateTargetVisualize; |
| 187 |
var |
| 188 |
Sphere: TSphereNode; |
| 189 |
Material: TMaterialNode; |
| 190 |
Appearance: TAppearanceNode; |
| 191 |
begin |
| 192 |
Sphere := TSphereNode.Create; |
| 193 |
Sphere.Radius := 0.1; |
| 194 |
|
| 195 |
Material := TMaterialNode.Create; |
| 196 |
Material.DiffuseColor := RedRGB; |
| 197 |
|
| 198 |
Appearance := TAppearanceNode.Create; |
| 199 |
Appearance.Material := Material; |
| 200 |
|
| 201 |
FTargetVisualizeShape := TShapeNode.Create; |
| 202 |
FTargetVisualizeShape.Appearance := Appearance; |
| 203 |
FTargetVisualizeShape.Geometry := Sphere; |
| 204 |
|
| 205 |
FTargetVisualize := TTransformNode.Create; |
| 206 |
FTargetVisualize.AddChildren(FTargetVisualizeShape); |
| 207 |
end; |
| 208 |
|
| 209 |
begin |
| 210 |
inherited; |
| 211 |
CreateTargetVisualize; |
| 212 |
FDebugTransform.ParentSpace.AddChildren(FTargetVisualize); |
| 213 |
FDebugTransform.ChangedScene; |
| 214 |
end; |
| 215 |
|
| 216 |
destructor TPlayer.Destroy; |
| 217 |
begin |
| 218 |
inherited; |
| 219 |
end; |
| 220 |
|
| 221 |
procedure TPlayer.Update(const SecondsPassed: Single; var RemoveMe: TRemoveType); |
| 222 |
var |
| 223 |
RotationAxis: TVector3; |
| 224 |
AngleToTarget: Single; |
| 225 |
|
| 226 |
procedure Rotate; |
| 227 |
const |
| 228 |
RotationSpeed = 10.0; //< radians per second |
| 229 |
var |
| 230 |
AngleRotate: Single; |
| 231 |
begin |
| 232 |
{ first adjust direction } |
| 233 |
AngleRotate := RotationSpeed * SecondsPassed; |
| 234 |
if AngleToTarget < 0 then |
| 235 |
AngleRotate := Max(-AngleRotate, AngleToTarget) else |
| 236 |
AngleRotate := Min( AngleRotate, AngleToTarget); |
| 237 |
Direction := RotatePointAroundAxisRad(AngleRotate, Direction, RotationAxis); |
| 238 |
end; |
| 239 |
|
| 240 |
procedure Move; |
| 241 |
const |
| 242 |
MoveSpeed = 1; |
| 243 |
var |
| 244 |
MoveDirectionCurrent, MoveDirectionMax: TVector3; |
| 245 |
MoveDirectionCurrentScale: Single; |
| 246 |
begin |
| 247 |
{ since Position <> WantsToWalkPos, we know that |
| 248 |
MoveDirectionMax is non-zero } |
| 249 |
MoveDirectionMax := WantsToWalkPos - Translation; |
| 250 |
MoveDirectionCurrentScale := MoveSpeed * SecondsPassed / MoveDirectionMax.Length; |
| 251 |
if MoveDirectionCurrentScale >= 1.0 then |
| 252 |
begin |
| 253 |
{ This means that |
| 254 |
Translation + MoveDirectionMax * MoveDirectionCurrentScale |
| 255 |
would actually get us too far. So we instead just go to target and |
| 256 |
stop there. } |
| 257 |
Translation := WantsToWalkPos; |
| 258 |
State := csIdle; |
| 259 |
end else |
| 260 |
begin |
| 261 |
MoveDirectionCurrent := MoveDirectionMax * MoveDirectionCurrentScale; |
| 262 |
Translation := Translation + MoveDirectionCurrent; |
| 263 |
end; |
| 264 |
end; |
| 265 |
|
| 266 |
var |
| 267 |
IsTargetPos, IsTargetDir: boolean; |
| 268 |
begin |
| 269 |
if State = csWalk then |
| 270 |
begin |
| 271 |
{ Do walking. If walking ends (because target reached, or can't reach target), |
| 272 |
change to csIdle. } |
| 273 |
|
| 274 |
{ TODO: check collisions with the scene before changing Translation } |
| 275 |
|
| 276 |
IsTargetPos := TVector3.Equals(Translation, WantsToWalkPos); |
| 277 |
|
| 278 |
{ When WantsToWalkDir is zero, consider IsTargetDir always true. |
| 279 |
This avoids crash at |
| 280 |
|
| 281 |
RotationAngleRadBetweenVectors(Direction, WantsToWalkDir, ...) |
| 282 |
|
| 283 |
when WantsToWalkDir is zero. } |
| 284 |
IsTargetDir := |
| 285 |
WantsToWalkDir.IsZero or |
| 286 |
TVector3.Equals(Direction, WantsToWalkDir); |
| 287 |
|
| 288 |
if not IsTargetDir then |
| 289 |
begin |
| 290 |
{ Calculate RotationAxis and AngleToTarget used by Rotate. |
| 291 |
By the way, compare Direction and WantsToWalkDir with more tolerance, |
| 292 |
so possibly IsTargetDir changes to true. } |
| 293 |
RotationAxis := //TVector3.CrossProduct(Direction, WantsToWalkDir); |
| 294 |
Vector3(0, 1, 0); |
| 295 |
AngleToTarget := RotationAngleRadBetweenVectors(Direction, WantsToWalkDir, RotationAxis); |
| 296 |
if Abs(AngleToTarget) < 0.01 then |
| 297 |
IsTargetDir := true; |
| 298 |
end; |
| 299 |
|
| 300 |
if IsTargetPos and IsTargetDir then |
| 301 |
State := csIdle |
| 302 |
else |
| 303 |
if not IsTargetDir then |
| 304 |
Rotate |
| 305 |
else |
| 306 |
Move; |
| 307 |
end; |
| 308 |
|
| 309 |
FTargetVisualizeShape.Render := Debug and (State = csWalk); |
| 310 |
FTargetVisualize.Translation := WantsToWalkPos; |
| 311 |
|
| 312 |
{ Update children 3D objects *after* updating our own transformation, |
| 313 |
this way they are always synchronized when displaying. |
| 314 |
Otherwise, the FTargetVisualize would have a bit of "shaking", |
| 315 |
because the TDebugTransform transformation would be updated with 1-frame delay. } |
| 316 |
inherited; |
| 317 |
end; |
| 318 |
|
| 319 |
procedure TPlayer.LocationChanged; |
| 320 |
begin |
| 321 |
State := csIdle; |
| 322 |
end; |
| 323 |
|
| 324 |
procedure TPlayer.WantsToWalk(const Value: TVector3); |
| 325 |
begin |
| 326 |
WantsToWalkPos := Value; |
| 327 |
WantsToWalkDir := WantsToWalkPos - Translation; |
| 328 |
WantsToWalkDir.Y := 0; // do not move in Y axis, only move horizontally |
| 329 |
WantsToWalkDir := WantsToWalkDir.Normalize; |
| 330 |
State := csWalk; |
| 331 |
end; |
| 332 |
|
| 333 |
initialization |
| 334 |
finalization |
| 335 |
FreeAndNil(CreatureKinds); |
| 336 |
end. |