Debian Codegraph @

examples/deprecated_to_upgrade/fixed_camera_game/code/gameviewintro.pas

raw
1 {
2 Copyright 2007-2022 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 { View showing the intro images (TViewIntro). }
17 unit GameViewIntro;
18
19 interface
20
21 uses Classes, Generics.Collections,
22 CastleGLImages, CastleUIControls, CastleTimeUtils, CastleKeysMouse,
23 CastleControls;
24
25 type
26 { View showing the intro images. }
27 TViewIntro = class(TCastleView)
28 strict private
29 type
30 TIntroPart = class
31 CorrodeDuration: TFloatTime;
32 Image: TDrawableImage;
33 ImageCorroded: TDrawableImage;
34 destructor Destroy; override;
35 end;
36 TIntroPartList = {$ifdef FPC}specialize{$endif} TObjectList<TIntroPart>;
37 var
38 IntroPart: Integer;
39 IntroPartTime: Single;
40 IntroParts: TIntroPartList;
41 InfoLabel: TCastleLabel;
42 procedure NextIntroPart;
43 public
44 constructor Create(AOwner: TComponent); override;
45 destructor Destroy; override;
46 procedure Start; override;
47 procedure Render; override;
48 function Press(const Event: TInputPressRelease): boolean; override;
49 procedure Update(const SecondsPassed: Single; var HandleInput: boolean); override;
50 end;
51
52 var
53 ViewIntro: TViewIntro;
54
55 implementation
56
57 uses SysUtils, DOM,
58 CastleFilesUtils, CastleVectors,
59 CastleGLUtils, CastleImages, CastleSoundEngine,
60 CastleStringUtils, CastleXmlUtils,
61 CastleColors, CastleApplicationProperties, CastleRenderOptions,
62 GameConfiguration, GameSound, GameViewMainMenu;
63
64 { TViewIntro.TIntroPart ----------------------------------------------------- }
65
66 destructor TViewIntro.TIntroPart.Destroy;
67 begin
68 FreeAndNil(Image);
69 FreeAndNil(ImageCorroded);
70 inherited;
71 end;
72
73 { TViewIntro ---------------------------------------------------------------- }
74
75 constructor TViewIntro.Create(AOwner: TComponent);
76
77 procedure InitializeIntroParts;
78 var
79 Element: TDOMElement;
80 ImageName: string;
81 I: TXMLElementIterator;
82 Part: TIntroPart;
83 begin
84 IntroParts := TIntroPartList.Create(true);
85
86 I := GameConfig.PathChildrenIterator('intro/parts', 'part');
87 try
88 while I.GetNext do
89 begin
90 Part := TIntroPart.Create;
91 IntroParts.Add(Part);
92
93 Element := I.Current;
94
95 { calculate Part durations }
96 Part.CorrodeDuration := Element.AttributeFloat('corrode_duration');
97
98 { calculate Part.Image and ImageCorroded }
99 ImageName := Element.AttributeString('image');
100 Part.Image := TDrawableImage.Create(
101 'castle-data:/images/' + ImageName + '.png', [TRGBImage]);
102 Part.ImageCorroded := TDrawableImage.Create(
103 'castle-data:/images/' + ImageName + '_corroded.png', [TRGBImage]);
104 end;
105 finally FreeAndNil(I) end;
106
107 if IntroParts.Count = 0 then
108 raise Exception.Create('No intro parts defined in data/index.xml');
109 end;
110
111 begin
112 inherited;
113 InitializeIntroParts;
114 end;
115
116 destructor TViewIntro.Destroy;
117 begin
118 FreeAndNil(IntroParts);
119 inherited;
120 end;
121
122 procedure TViewIntro.Start;
123 begin
124 inherited;
125 IntroPart := 0;
126 IntroPartTime := 0.0;
127
128 SoundEngine.LoopingChannel[0].Sound := AllSounds.SoundIntroMusic;
129
130 InfoLabel := TCastleLabel.Create(FreeAtStop);
131 InfoLabel.Color := White;
132 InfoLabel.Anchor(hpRight, -5);
133 InfoLabel.Anchor(vpTop, -5);
134 InsertFront(InfoLabel);
135 end;
136
137 procedure TViewIntro.NextIntroPart;
138 begin
139 Inc(IntroPart);
140 IntroPartTime := 0.0;
141
142 if IntroPart >= IntroParts.Count then
143 Container.View := ViewMainMenu;
144 end;
145
146 procedure TViewIntro.Update(const SecondsPassed: Single; var HandleInput: boolean);
147 begin
148 inherited;
149 IntroPartTime := IntroPartTime + SecondsPassed;
150 InfoLabel.Caption := Format('(%d / %d) Press [Space] or click / tap to continue',
151 [IntroPart + 1, IntroParts.Count]);
152 end;
153
154 procedure TViewIntro.Render;
155 var
156 Corrosion: Single;
157 begin
158 inherited;
159
160 if IntroPartTime >= IntroParts[IntroPart].CorrodeDuration then
161 begin
162 IntroParts[IntroPart].ImageCorroded.Draw(Container.Rect);
163 end else
164 begin
165 Corrosion := IntroPartTime / IntroParts[IntroPart].CorrodeDuration;
166
167 IntroParts[IntroPart].Image.Alpha := acBlending;
168 IntroParts[IntroPart].Image.BlendingSourceFactor := bsConstantColor;
169 IntroParts[IntroPart].Image.BlendingDestinationFactor := bdOne;
170 IntroParts[IntroPart].Image.BlendingConstantColor :=
171 Vector4(1 - Corrosion, 1 - Corrosion, 1 - Corrosion, 1);
172 IntroParts[IntroPart].Image.Draw(Container.Rect);
173
174 IntroParts[IntroPart].ImageCorroded.Alpha := acBlending;
175 IntroParts[IntroPart].ImageCorroded.BlendingSourceFactor := bsConstantColor;
176 IntroParts[IntroPart].ImageCorroded.BlendingDestinationFactor := bdOne;
177 IntroParts[IntroPart].ImageCorroded.BlendingConstantColor :=
178 Vector4(Corrosion, Corrosion, Corrosion, 1);
179 IntroParts[IntroPart].ImageCorroded.Draw(Container.Rect);
180 end;
181 end;
182
183 function TViewIntro.Press(const Event: TInputPressRelease): boolean;
184 begin
185 Result := inherited;
186
187 if Event.IsKey(CharEscape) then
188 begin
189 Container.View := ViewMainMenu;
190 Result := true;
191 end else
192 if Event.IsMouseButton(buttonLeft) or Event.IsKey(keySpace) then
193 begin
194 NextIntroPart;
195 Result := true;
196 end;
197 end;
198
199 end.