| 1 |
{ |
| 2 |
Copyright 2020-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 |
{ Main view, where most of the application logic takes place. } |
| 17 |
unit GameViewMain; |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses SysUtils, Classes, Generics.Collections, |
| 22 |
CastleComponentSerialize, CastleUIControls, CastleControls, |
| 23 |
CastleKeysMouse, CastleViewport, CastleScene, CastleResources, CastleCreatures, |
| 24 |
CastleLevels, CastleVectors, CastleCameras; |
| 25 |
|
| 26 |
type |
| 27 |
TViewMain = class; |
| 28 |
|
| 29 |
{ Button that spawns a creature with specified TCreatureResource. } |
| 30 |
TResourceButton = class(TCastleButton) |
| 31 |
public |
| 32 |
OwnerState: TViewMain; |
| 33 |
ButtonResource: TCreatureResource; |
| 34 |
procedure DoClick; override; |
| 35 |
end; |
| 36 |
|
| 37 |
TResourceButtonList = {$ifdef FPC}specialize{$endif} TObjectList<TResourceButton>; |
| 38 |
|
| 39 |
{ Main view, where most of the application logic takes place. } |
| 40 |
TViewMain = class(TCastleView) |
| 41 |
published |
| 42 |
{ Components designed using CGE editor. |
| 43 |
These fields will be automatically initialized at Start. } |
| 44 |
LabelFps: TCastleLabel; |
| 45 |
MainViewport: TCastleViewport; |
| 46 |
ButtonLoadResourceXml: TCastleButton; |
| 47 |
CheckboxShowDebug: TCastleCheckbox; |
| 48 |
GroupResources: TCastleVerticalGroup; |
| 49 |
ExamineNavigation1: TCastleExamineNavigation; |
| 50 |
private |
| 51 |
{ Other private stuff } |
| 52 |
Level: TLevel; |
| 53 |
CurrentCreature: TCreature; |
| 54 |
CurrentResource: TCreatureResource; |
| 55 |
ResourceButtons: TResourceButtonList; //< all current TResourceButton instances |
| 56 |
{ Remember this to make repeated usage of FileDialog in ClickButtonLoadResourceXml |
| 57 |
more comfortable, it will start with the last chosen directory. } |
| 58 |
LastLoadedResourceXml: string; |
| 59 |
|
| 60 |
procedure ClickButtonLoadResourceXml(Sender: TObject); |
| 61 |
procedure ChangedCheckboxShowDebug(Sender: TObject); |
| 62 |
{ Create buttons in ResourceButtons and GroupResources to reflect current Resources. } |
| 63 |
procedure UpdateResourceButtons; |
| 64 |
procedure SetCreatureResource(const NewCreatureResource: TCreatureResource); |
| 65 |
public |
| 66 |
constructor Create(AOwner: TComponent); override; |
| 67 |
procedure Start; override; |
| 68 |
procedure Stop; override; |
| 69 |
procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override; |
| 70 |
end; |
| 71 |
|
| 72 |
var |
| 73 |
ViewMain: TViewMain; |
| 74 |
|
| 75 |
implementation |
| 76 |
|
| 77 |
uses CastleUriUtils, CastleWindow; |
| 78 |
|
| 79 |
{ TResourceButton ------------------------------------------------------------ } |
| 80 |
|
| 81 |
procedure TResourceButton.DoClick; |
| 82 |
begin |
| 83 |
OwnerState.SetCreatureResource(ButtonResource); |
| 84 |
end; |
| 85 |
|
| 86 |
{ TViewMain ----------------------------------------------------------------- } |
| 87 |
|
| 88 |
constructor TViewMain.Create(AOwner: TComponent); |
| 89 |
begin |
| 90 |
inherited; |
| 91 |
DesignUrl := 'castle-data:/gameviewmain.castle-user-interface'; |
| 92 |
end; |
| 93 |
|
| 94 |
procedure TViewMain.Start; |
| 95 |
|
| 96 |
{ An example of creating a resource (TStillCreatureResource in this case) |
| 97 |
without resource.xml file. You just create an instance of TXxxResource by hand, |
| 98 |
fill the properties you need, and add it to global Resources list. } |
| 99 |
procedure TestAddingResourceByCode; |
| 100 |
var |
| 101 |
Res: TStillCreatureResource; |
| 102 |
begin |
| 103 |
Res := TStillCreatureResource.Create(nil); |
| 104 |
Res.Name := 'KnightCreatedFromCodeTest'; |
| 105 |
Res.ModelURL := 'castle-data:/knight_single_gltf/knight.gltf'; |
| 106 |
Res.Animations.FindName('idle').AnimationName := 'Idle'; |
| 107 |
Resources.Add(Res); |
| 108 |
end; |
| 109 |
|
| 110 |
begin |
| 111 |
inherited; |
| 112 |
|
| 113 |
ButtonLoadResourceXml.OnClick := {$ifdef FPC}@{$endif}ClickButtonLoadResourceXml; |
| 114 |
CheckboxShowDebug.OnChange := {$ifdef FPC}@{$endif}ChangedCheckboxShowDebug; |
| 115 |
|
| 116 |
{ initialize global Resources list contents } |
| 117 |
Resources.LoadFromFiles; |
| 118 |
Levels.LoadFromFiles; |
| 119 |
TestAddingResourceByCode; |
| 120 |
|
| 121 |
{ Prepare (load animations) for all resources } |
| 122 |
Resources.Prepare(MainViewport.PrepareParams, 'resources'); |
| 123 |
|
| 124 |
{ Level refers to a Viewport. |
| 125 |
We need to create Level, as creature can be spawned only within a Level, |
| 126 |
using CurrentResource.CreateCreature(Level, ...), |
| 127 |
placing it inside "Viewport.Items". } |
| 128 |
Level := TLevel.Create(FreeAtStop); |
| 129 |
Level.Viewport := MainViewport; |
| 130 |
Level.Load('main_level'); |
| 131 |
|
| 132 |
{ TLevel.Load, for historic purposes, always overrides MainViewport.Navigation |
| 133 |
with a TCastleWalkNavigation (and we're not fixing it anymore, due to both |
| 134 |
TLevel and Resources being deprecated). |
| 135 |
So just bring back examine navigation manually. } |
| 136 |
MainViewport.Navigation := ExamineNavigation1; |
| 137 |
|
| 138 |
ResourceButtons := TResourceButtonList.Create(true); |
| 139 |
UpdateResourceButtons; |
| 140 |
SetCreatureResource(Resources.FindName('KnightSingleGltf') as TCreatureResource); |
| 141 |
end; |
| 142 |
|
| 143 |
procedure TViewMain.Stop; |
| 144 |
begin |
| 145 |
FreeAndNil(ResourceButtons); |
| 146 |
inherited; |
| 147 |
end; |
| 148 |
|
| 149 |
procedure TViewMain.SetCreatureResource(const NewCreatureResource: TCreatureResource); |
| 150 |
var |
| 151 |
I: Integer; |
| 152 |
begin |
| 153 |
FreeAndNil(CurrentCreature); // remove previous creature |
| 154 |
|
| 155 |
{ CreateCreature creates TCreature instance and adds it to Viewport.Items } |
| 156 |
CurrentResource := NewCreatureResource; |
| 157 |
CurrentCreature := CurrentResource.CreateCreature(Level, |
| 158 |
{ Translation } Vector3(0, 0, 0), |
| 159 |
{ Direction } Vector3(0, 0, 1)); |
| 160 |
|
| 161 |
{ update Pressed of buttons } |
| 162 |
for I := 0 to ResourceButtons.Count - 1 do |
| 163 |
ResourceButtons[I].Pressed := ResourceButtons[I].ButtonResource = CurrentResource; |
| 164 |
end; |
| 165 |
|
| 166 |
procedure TViewMain.UpdateResourceButtons; |
| 167 |
var |
| 168 |
ResButton: TResourceButton; |
| 169 |
I: Integer; |
| 170 |
begin |
| 171 |
{ easily destroy all existing buttons using the XxxButtons list, |
| 172 |
destroying them also automatically removed them from Window.Controls list } |
| 173 |
ResourceButtons.Clear; |
| 174 |
|
| 175 |
for I := 0 to Resources.Count - 1 do |
| 176 |
begin |
| 177 |
ResButton := TResourceButton.Create(nil); |
| 178 |
ResButton.ButtonResource := Resources[I] as TCreatureResource; |
| 179 |
ResButton.Caption := 'Spawn creature ' + ResButton.ButtonResource.Name; |
| 180 |
ResButton.Toggle := true; |
| 181 |
ResButton.OwnerState := Self; |
| 182 |
ResourceButtons.Add(ResButton); |
| 183 |
GroupResources.InsertFront(ResButton); |
| 184 |
end; |
| 185 |
if Resources.Count = 0 then |
| 186 |
raise Exception.CreateFmt('No resources found. Make sure we search in proper path (current data path is detected as "%s")', |
| 187 |
[ResolveCastleDataURL('castle-data:/')]); |
| 188 |
end; |
| 189 |
|
| 190 |
procedure TViewMain.ClickButtonLoadResourceXml(Sender: TObject); |
| 191 |
begin |
| 192 |
if Application.MainWindow.FileDialog('Resource file to load', |
| 193 |
LastLoadedResourceXml, true, |
| 194 |
'All Files|*|*Resource files (resource.xml)|resource.xml|') then |
| 195 |
begin |
| 196 |
Resources.AddFromFile(LastLoadedResourceXml); |
| 197 |
{ directly prepare new resource } |
| 198 |
Resources.Prepare(MainViewport.PrepareParams, 'resources'); |
| 199 |
|
| 200 |
UpdateResourceButtons; |
| 201 |
SetCreatureResource(Resources.Last as TCreatureResource); // newly added resource is the last, activate it |
| 202 |
end; |
| 203 |
end; |
| 204 |
|
| 205 |
procedure TViewMain.ChangedCheckboxShowDebug(Sender: TObject); |
| 206 |
begin |
| 207 |
// show bounding box of the creatures |
| 208 |
TCreature.RenderDebug := CheckboxShowDebug.Checked; |
| 209 |
end; |
| 210 |
|
| 211 |
procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: Boolean); |
| 212 |
begin |
| 213 |
inherited; |
| 214 |
{ This virtual method is executed every frame (many times per second). } |
| 215 |
LabelFps.Caption := 'FPS: ' + Container.Fps.ToString; |
| 216 |
end; |
| 217 |
|
| 218 |
end. |