| 1 |
{ |
| 2 |
Copyright 2023-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 Classes, |
| 22 |
CastleVectors, CastleComponentSerialize, CastleTransform, |
| 23 |
CastleUIControls, CastleControls, CastleKeysMouse, CastleScene, |
| 24 |
GameSubAnimations; |
| 25 |
|
| 26 |
type |
| 27 |
{ Main view, where most of the application logic takes place. } |
| 28 |
TViewMain = class(TCastleView) |
| 29 |
published |
| 30 |
{ Components designed using CGE editor. |
| 31 |
These fields will be automatically initialized at Start. } |
| 32 |
LabelFps: TCastleLabel; |
| 33 |
ButtonLoadGranger, ButtonLoadDretch, ButtonLoadBasilisk, |
| 34 |
ButtonLoadMarauder, ButtonLoadDragoon, ButtonLoadTyrant: TCastleButton; |
| 35 |
SceneHalloweenPumpkinLanternKnight1: TCastleScene; |
| 36 |
VerticalGroupAnimations: TCastleVerticalGroup; |
| 37 |
CheckboxLoop: TCastleCheckbox; |
| 38 |
private |
| 39 |
SubAnimations: TSubAnimations; |
| 40 |
procedure ClickPlayAnimation(Sender: TObject); |
| 41 |
public |
| 42 |
constructor Create(AOwner: TComponent); override; |
| 43 |
procedure Start; override; |
| 44 |
procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override; |
| 45 |
end; |
| 46 |
|
| 47 |
var |
| 48 |
ViewMain: TViewMain; |
| 49 |
|
| 50 |
implementation |
| 51 |
|
| 52 |
uses SysUtils; |
| 53 |
|
| 54 |
{ TViewMain ----------------------------------------------------------------- } |
| 55 |
|
| 56 |
constructor TViewMain.Create(AOwner: TComponent); |
| 57 |
begin |
| 58 |
inherited; |
| 59 |
DesignUrl := 'castle-data:/gameviewmain.castle-user-interface'; |
| 60 |
end; |
| 61 |
|
| 62 |
procedure TViewMain.Start; |
| 63 |
|
| 64 |
{ Create SubAnimations behavior to SceneHalloweenPumpkinLanternKnight1. |
| 65 |
Define subanimations specific to it. } |
| 66 |
procedure LoadModelPumpkin; |
| 67 |
begin |
| 68 |
{ Do not play long animation using TCastleScene.PlayAnimation / AutoAnimation |
| 69 |
(AutoAnimation could be set in CGE editor). |
| 70 |
We want SubAnimations to control model state, to reflect current subanimation. } |
| 71 |
SceneHalloweenPumpkinLanternKnight1.StopAnimation; |
| 72 |
|
| 73 |
SubAnimations := TSubAnimations.Create(FreeAtStop); |
| 74 |
SceneHalloweenPumpkinLanternKnight1.AddBehavior(SubAnimations); |
| 75 |
SubAnimations.Add('idle', 'Take 001', 0, 5.0); |
| 76 |
SubAnimations.Add('head_bounce', 'Take 001', 7.4, 2.0); |
| 77 |
end; |
| 78 |
|
| 79 |
procedure UpdateSubAnimationButtons; |
| 80 |
var |
| 81 |
I: Integer; |
| 82 |
NewButton: TCastleButton; |
| 83 |
SubAnimName: String; |
| 84 |
begin |
| 85 |
// clear VerticalGroupAnimations |
| 86 |
for I := VerticalGroupAnimations.ControlsCount - 1 downto 0 do |
| 87 |
if VerticalGroupAnimations.Controls[I] is TCastleButton then |
| 88 |
VerticalGroupAnimations.Controls[I].Free; |
| 89 |
|
| 90 |
// add VerticalGroupAnimations buttons based on ModelScene.SubAnimations |
| 91 |
for SubAnimName in SubAnimations.List.Keys do |
| 92 |
begin |
| 93 |
NewButton := TCastleButton.Create(FreeAtStop); |
| 94 |
NewButton.Caption := SubAnimName; |
| 95 |
NewButton.OnClick := {$ifdef FPC}@{$endif} ClickPlayAnimation; |
| 96 |
VerticalGroupAnimations.InsertFront(NewButton); |
| 97 |
end; |
| 98 |
end; |
| 99 |
|
| 100 |
begin |
| 101 |
inherited; |
| 102 |
LoadModelPumpkin; |
| 103 |
UpdateSubAnimationButtons; |
| 104 |
end; |
| 105 |
|
| 106 |
procedure TViewMain.ClickPlayAnimation(Sender: TObject); |
| 107 |
var |
| 108 |
SubAnimationName: String; |
| 109 |
begin |
| 110 |
SubAnimationName := (Sender as TCastleButton).Caption; |
| 111 |
SubAnimations.Play(SubAnimationName, CheckboxLoop.Checked); |
| 112 |
end; |
| 113 |
|
| 114 |
procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: Boolean); |
| 115 |
begin |
| 116 |
inherited; |
| 117 |
{ This virtual method is executed every frame (many times per second). } |
| 118 |
Assert(LabelFps <> nil, 'If you remove LabelFps from the design, remember to remove also the assignment "LabelFps.Caption := ..." from code'); |
| 119 |
LabelFps.Caption := 'FPS: ' + Container.Fps.ToString; |
| 120 |
end; |
| 121 |
|
| 122 |
end. |