| 1 |
{ |
| 2 |
Copyright 2014-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 "playing game" view, where most of the game logic takes place. } |
| 17 |
unit GameViewPlay; |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses Classes, |
| 22 |
CastleComponentSerialize, CastleUIControls, CastleControls, |
| 23 |
CastleKeysMouse, CastleViewport, CastleScene, CastleVectors, |
| 24 |
CastleNotifications, CastleTimeUtils, CastleGameService; |
| 25 |
|
| 26 |
type |
| 27 |
{ Main "playing game" view, where most of the game logic takes place. } |
| 28 |
TViewPlay = class(TCastleView) |
| 29 |
published |
| 30 |
{ Components designed using CGE editor. |
| 31 |
These fields will be automatically initialized at Start. } |
| 32 |
LabelFps: TCastleLabel; |
| 33 |
MainViewport: TCastleViewport; |
| 34 |
SceneDragon: TCastleScene; |
| 35 |
CheckboxCameraFollow: TCastleCheckbox; |
| 36 |
ButtonShowAchievements: TCastleButton; |
| 37 |
ButtonShowLeaderboardRandomScores: TCastleButton; |
| 38 |
ButtonShowLeaderboardTimes: TCastleButton; |
| 39 |
ButtonGetPlayerBestRandomScore: TCastleButton; |
| 40 |
ButtonSendLeaderboardRandomScores: TCastleButton; |
| 41 |
ButtonSendLeaderboardTimes: TCastleButton; |
| 42 |
ButtonGetPlayerBestTime: TCastleButton; |
| 43 |
ButtonSaveGame: TCastleButton; |
| 44 |
ButtonLoadGame: TCastleButton; |
| 45 |
ButtonChooseSaveGame: TCastleButton; |
| 46 |
GameNotifications: TCastleNotifications; |
| 47 |
LabelGameServiceStatus: TCastleLabel; |
| 48 |
private |
| 49 |
{ DragonFlying and DragonFlyingTarget manage currect dragon (SceneDragon) |
| 50 |
animation and it's movement. } |
| 51 |
DragonFlying: Boolean; |
| 52 |
DragonFlyingTarget: TVector2; |
| 53 |
PlayTime: TFloatTime; |
| 54 |
|
| 55 |
// UI callbacks |
| 56 |
procedure ChangeCheckboxCameraFollow(Sender: TObject); |
| 57 |
procedure ClickShowAchievements(Sender: TObject); |
| 58 |
procedure ClickShowLeaderboardRandomScores(Sender: TObject); |
| 59 |
procedure ClickShowLeaderboardTimes(Sender: TObject); |
| 60 |
procedure ClickSendLeaderboardRandomScores(Sender: TObject); |
| 61 |
procedure ClickSendLeaderboardTimes(Sender: TObject); |
| 62 |
procedure ClickGetPlayerBestRandomScore(Sender: TObject); |
| 63 |
procedure ClickGetPlayerBestTime(Sender: TObject); |
| 64 |
procedure ClickSaveGame(Sender: TObject); |
| 65 |
procedure ClickLoadGame(Sender: TObject); |
| 66 |
procedure ClickChooseSaveGame(Sender: TObject); |
| 67 |
|
| 68 |
// Game service callbacks |
| 69 |
procedure GameServiceStatusChanged(Sender: TObject); |
| 70 |
procedure PlayerBestScoreReceived(Sender: TObject; const LeaderboardId: string; const Score: Int64); |
| 71 |
procedure GameServiceSaveGameLoaded(Sender: TObject; const Success: boolean; const Content: string); |
| 72 |
procedure GameServiceSaveGameChosen(Sender: TObject; const Choice: TSaveGameChoice; const SaveGameName: string); |
| 73 |
public |
| 74 |
constructor Create(AOwner: TComponent); override; |
| 75 |
procedure Start; override; |
| 76 |
procedure Stop; override; |
| 77 |
procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override; |
| 78 |
function Press(const Event: TInputPressRelease): Boolean; override; |
| 79 |
end; |
| 80 |
|
| 81 |
var |
| 82 |
ViewPlay: TViewPlay; |
| 83 |
|
| 84 |
implementation |
| 85 |
|
| 86 |
uses SysUtils, Math, |
| 87 |
CastleConfig, CastleUtils, |
| 88 |
GameViewMenu, GameIds; |
| 89 |
|
| 90 |
{ TViewPlay ----------------------------------------------------------------- } |
| 91 |
|
| 92 |
constructor TViewPlay.Create(AOwner: TComponent); |
| 93 |
begin |
| 94 |
inherited; |
| 95 |
DesignUrl := 'castle-data:/gameviewplay.castle-user-interface'; |
| 96 |
end; |
| 97 |
|
| 98 |
procedure TViewPlay.Start; |
| 99 |
begin |
| 100 |
inherited; |
| 101 |
|
| 102 |
LabelGameServiceStatus.Caption := 'Status: ' + GameServiceStatusToStr(GameService.Status); |
| 103 |
|
| 104 |
// UI callbacks |
| 105 |
CheckboxCameraFollow.OnChange := {$ifdef FPC}@{$endif} ChangeCheckboxCameraFollow; |
| 106 |
ButtonShowAchievements.OnClick := {$ifdef FPC}@{$endif} ClickShowAchievements; |
| 107 |
ButtonShowLeaderboardRandomScores.OnClick := {$ifdef FPC}@{$endif} ClickShowLeaderboardRandomScores; |
| 108 |
ButtonShowLeaderboardTimes.OnClick := {$ifdef FPC}@{$endif} ClickShowLeaderboardTimes; |
| 109 |
ButtonSendLeaderboardRandomScores.OnClick := {$ifdef FPC}@{$endif} ClickSendLeaderboardRandomScores; |
| 110 |
ButtonSendLeaderboardTimes.OnClick := {$ifdef FPC}@{$endif} ClickSendLeaderboardTimes; |
| 111 |
ButtonGetPlayerBestRandomScore.OnClick := {$ifdef FPC}@{$endif} ClickGetPlayerBestRandomScore; |
| 112 |
ButtonGetPlayerBestTime.OnClick := {$ifdef FPC}@{$endif} ClickGetPlayerBestTime; |
| 113 |
ButtonSaveGame.OnClick := {$ifdef FPC}@{$endif} ClickSaveGame; |
| 114 |
ButtonLoadGame.OnClick := {$ifdef FPC}@{$endif} ClickLoadGame; |
| 115 |
ButtonChooseSaveGame.OnClick := {$ifdef FPC}@{$endif} ClickChooseSaveGame; |
| 116 |
|
| 117 |
// game service callbacks |
| 118 |
GameService.OnStatusChanged := {$ifdef FPC}@{$endif} GameServiceStatusChanged; |
| 119 |
GameService.OnPlayerBestScoreReceived := {$ifdef FPC}@{$endif} PlayerBestScoreReceived; |
| 120 |
GameService.OnSaveGameLoaded := {$ifdef FPC}@{$endif} GameServiceSaveGameLoaded; |
| 121 |
GameService.OnSaveGameChosen := {$ifdef FPC}@{$endif} GameServiceSaveGameChosen; |
| 122 |
end; |
| 123 |
|
| 124 |
procedure TViewPlay.Stop; |
| 125 |
begin |
| 126 |
{ GameService instance will exist throughout this application lifetime, |
| 127 |
even after this view is stopped. |
| 128 |
So unregisted our callbacks from it, to not let e.g. PlayerBestScoreReceived |
| 129 |
be called when view is stopped and UI is destroyed. } |
| 130 |
GameService.OnStatusChanged := nil; |
| 131 |
GameService.OnPlayerBestScoreReceived := nil; |
| 132 |
GameService.OnSaveGameLoaded := nil; |
| 133 |
GameService.OnSaveGameChosen := nil; |
| 134 |
inherited; |
| 135 |
end; |
| 136 |
|
| 137 |
procedure TViewPlay.Update(const SecondsPassed: Single; var HandleInput: Boolean); |
| 138 |
const |
| 139 |
DragonSpeed: TVector2 = (X: 3000; Y: 1500); |
| 140 |
var |
| 141 |
T: TVector2; |
| 142 |
CamPos: TVector3; |
| 143 |
begin |
| 144 |
inherited; |
| 145 |
{ This virtual method is executed every frame (many times per second). } |
| 146 |
|
| 147 |
LabelFps.Caption := 'FPS: ' + Container.Fps.ToString; |
| 148 |
PlayTime := PlayTime + SecondsPassed; |
| 149 |
|
| 150 |
if DragonFlying then |
| 151 |
begin |
| 152 |
{ Update SceneDragon.TranslationXY to reach DragonFlyingTarget. } |
| 153 |
T := SceneDragon.TranslationXY; |
| 154 |
if T.X < DragonFlyingTarget.X then |
| 155 |
T.X := Min(DragonFlyingTarget.X, T.X + DragonSpeed.X * SecondsPassed) |
| 156 |
else |
| 157 |
T.X := Max(DragonFlyingTarget.X, T.X - DragonSpeed.X * SecondsPassed); |
| 158 |
if T.Y < DragonFlyingTarget.Y then |
| 159 |
T.Y := Min(DragonFlyingTarget.Y, T.Y + DragonSpeed.Y * SecondsPassed) |
| 160 |
else |
| 161 |
T.Y := Max(DragonFlyingTarget.Y, T.Y - DragonSpeed.Y * SecondsPassed); |
| 162 |
SceneDragon.TranslationXY := T; |
| 163 |
|
| 164 |
{ Check did we reach the DragonFlyingTarget. Note that we can compare floats |
| 165 |
using exact "=" operator (no need to use SameValue), because |
| 166 |
our Min/Maxes above make sure that we will reach the *exact* DragonFlyingTarget |
| 167 |
value. } |
| 168 |
if (T.X = DragonFlyingTarget.X) and |
| 169 |
(T.Y = DragonFlyingTarget.Y) then |
| 170 |
begin |
| 171 |
DragonFlying := false; |
| 172 |
SceneDragon.PlayAnimation('idle', true); |
| 173 |
end else |
| 174 |
{ If we're still flying then |
| 175 |
update SceneDragon.Scale to reflect direction we're flying to. |
| 176 |
Flipping Scale.X is an easy way to flip 2D objects. } |
| 177 |
if DragonFlyingTarget.X > SceneDragon.Translation.X then |
| 178 |
SceneDragon.Scale := Vector3(-1, 1, 1) |
| 179 |
else |
| 180 |
SceneDragon.Scale := Vector3(1, 1, 1); |
| 181 |
end; |
| 182 |
|
| 183 |
if (SceneDragon.Translation.X < -10 * 1000) and not AchievementSeeLeftSubmitted then |
| 184 |
begin |
| 185 |
GameService.Achievement(AchievementSeeLeft); |
| 186 |
GameNotifications.Show('Achievement "see left" completed'); |
| 187 |
AchievementSeeLeftSubmitted := true; |
| 188 |
end; |
| 189 |
|
| 190 |
if (SceneDragon.Translation.X > 10 * 1000) and not AchievementSeeRightSubmitted then |
| 191 |
begin |
| 192 |
GameService.Achievement(AchievementSeeRight); |
| 193 |
GameNotifications.Show('Achievement "see right" completed'); |
| 194 |
AchievementSeeRightSubmitted := true; |
| 195 |
end; |
| 196 |
|
| 197 |
if CheckboxCameraFollow.Checked then |
| 198 |
begin |
| 199 |
CamPos := MainViewport.Camera.Translation; |
| 200 |
CamPos.X := SceneDragon.Translation.X; |
| 201 |
MainViewport.Camera.Translation := CamPos; |
| 202 |
end; |
| 203 |
end; |
| 204 |
|
| 205 |
function TViewPlay.Press(const Event: TInputPressRelease): Boolean; |
| 206 |
begin |
| 207 |
Result := inherited; |
| 208 |
if Result then Exit; // allow the ancestor to handle keys |
| 209 |
|
| 210 |
{ This virtual method is executed when user presses |
| 211 |
a key, a mouse button, or touches a touch-screen. |
| 212 |
|
| 213 |
Note that each UI control has also events like OnPress and OnClick. |
| 214 |
These events can be used to handle the "press", if it should do something |
| 215 |
specific when used in that UI control. |
| 216 |
The TViewPlay.Press method should be used to handle keys |
| 217 |
not handled in children controls. |
| 218 |
} |
| 219 |
|
| 220 |
if Event.IsMouseButton(buttonLeft) then |
| 221 |
begin |
| 222 |
DragonFlyingTarget := MainViewport.PositionTo2DWorld(Event.Position, true); |
| 223 |
if not DragonFlying then |
| 224 |
begin |
| 225 |
SceneDragon.PlayAnimation('flying', true); |
| 226 |
DragonFlying := true; |
| 227 |
end; |
| 228 |
|
| 229 |
GameService.Achievement(AchievementMove); |
| 230 |
// too spammy |
| 231 |
// GameNotifications.Show('Achievement "move" completed'); |
| 232 |
Exit(true); // click was handled |
| 233 |
end; |
| 234 |
|
| 235 |
if Event.IsKey(keyF5) then |
| 236 |
begin |
| 237 |
Container.SaveScreenToDefaultFile; |
| 238 |
Exit(true); |
| 239 |
end; |
| 240 |
|
| 241 |
if Event.IsKey(keyEscape) then |
| 242 |
begin |
| 243 |
Container.View := ViewMenu; |
| 244 |
Exit(true); |
| 245 |
end; |
| 246 |
end; |
| 247 |
|
| 248 |
procedure TViewPlay.ChangeCheckboxCameraFollow(Sender: TObject); |
| 249 |
begin |
| 250 |
GameService.Achievement(AchievementClickFollow); |
| 251 |
GameNotifications.Show('Achievement "click follow" completed'); |
| 252 |
end; |
| 253 |
|
| 254 |
procedure TViewPlay.ClickShowAchievements(Sender: TObject); |
| 255 |
begin |
| 256 |
GameService.ShowAchievements; |
| 257 |
end; |
| 258 |
|
| 259 |
procedure TViewPlay.ClickShowLeaderboardRandomScores(Sender: TObject); |
| 260 |
begin |
| 261 |
GameService.ShowLeaderboard(LeaderboardRandomScores); |
| 262 |
end; |
| 263 |
|
| 264 |
procedure TViewPlay.ClickShowLeaderboardTimes(Sender: TObject); |
| 265 |
begin |
| 266 |
GameService.ShowLeaderboard(LeaderboardTimes); |
| 267 |
end; |
| 268 |
|
| 269 |
procedure TViewPlay.ClickSendLeaderboardRandomScores(Sender: TObject); |
| 270 |
var |
| 271 |
Send: Int64; |
| 272 |
begin |
| 273 |
Send := Random(1000); |
| 274 |
GameService.SubmitScore(LeaderboardRandomScores, Send); |
| 275 |
GameNotifications.Show('Send score ' + IntToStr(Send) + ' to ' + LeaderboardRandomScores); |
| 276 |
end; |
| 277 |
|
| 278 |
procedure TViewPlay.ClickSendLeaderboardTimes(Sender: TObject); |
| 279 |
var |
| 280 |
Send: Int64; |
| 281 |
begin |
| 282 |
Send := Trunc(PlayTime); |
| 283 |
GameService.SubmitScore(LeaderboardTimes, Send); |
| 284 |
GameNotifications.Show('Send score ' + IntToStr(Send) + ' to ' + LeaderboardTimes); |
| 285 |
end; |
| 286 |
|
| 287 |
procedure TViewPlay.ClickGetPlayerBestRandomScore(Sender: TObject); |
| 288 |
begin |
| 289 |
GameService.RequestPlayerBestScore(LeaderboardRandomScores); |
| 290 |
end; |
| 291 |
|
| 292 |
procedure TViewPlay.ClickGetPlayerBestTime(Sender: TObject); |
| 293 |
begin |
| 294 |
GameService.RequestPlayerBestScore(LeaderboardTimes); |
| 295 |
end; |
| 296 |
|
| 297 |
procedure TViewPlay.PlayerBestScoreReceived(Sender: TObject; const LeaderboardId: string; const Score: Int64); |
| 298 |
begin |
| 299 |
GameNotifications.Show('Your best score on ' + LeaderboardId + ' is ' + IntToStr(Score)); |
| 300 |
end; |
| 301 |
|
| 302 |
procedure TViewPlay.GameServiceStatusChanged(Sender: TObject); |
| 303 |
begin |
| 304 |
LabelGameServiceStatus.Caption := 'Status: ' + GameServiceStatusToStr(GameService.Status); |
| 305 |
end; |
| 306 |
|
| 307 |
procedure TViewPlay.ClickSaveGame(Sender: TObject); |
| 308 |
var |
| 309 |
StringStream: TStringStream; |
| 310 |
UserConfigAsString: String; |
| 311 |
begin |
| 312 |
{ We could just save a String straight using GameService.SaveGameSave. |
| 313 |
But to make it a more realistic demo, we do 3 steps: |
| 314 |
- Save state to UserConfig |
| 315 |
- Then convert UserConfig into a String |
| 316 |
- And save that. |
| 317 |
} |
| 318 |
UserConfig.SetVector3('dragon/translation', SceneDragon.Translation); |
| 319 |
UserConfig.SetVector3('dragon/scale', SceneDragon.Scale); |
| 320 |
UserConfig.SetValue('camera_follows', CheckboxCameraFollow.Checked); |
| 321 |
|
| 322 |
StringStream := TStringStream.Create(''); |
| 323 |
try |
| 324 |
UserConfig.SaveToStream(StringStream); |
| 325 |
UserConfigAsString := StringStream.DataString; |
| 326 |
finally FreeAndNil(StringStream) end; |
| 327 |
|
| 328 |
GameService.SaveGameSave('default-save', UserConfigAsString, 'Default savegame.', Trunc(PlayTime)); |
| 329 |
GameNotifications.Show('Starting saving game online.'); |
| 330 |
end; |
| 331 |
|
| 332 |
procedure TViewPlay.ClickLoadGame(Sender: TObject); |
| 333 |
begin |
| 334 |
GameService.SaveGameLoad('default-save'); |
| 335 |
GameNotifications.Show('Starting loading game online.'); |
| 336 |
end; |
| 337 |
|
| 338 |
procedure TViewPlay.GameServiceSaveGameLoaded(Sender: TObject; |
| 339 |
const Success: boolean; const Content: string); |
| 340 |
var |
| 341 |
StringStream: TStringStream; |
| 342 |
begin |
| 343 |
{ Corresponding to TViewPlay.ClickSaveGame, we unpack received Content |
| 344 |
(String) into UserConfig, and then use UserConfig to restore the state. } |
| 345 |
if Success then |
| 346 |
begin |
| 347 |
StringStream := TStringStream.Create(Content); |
| 348 |
try |
| 349 |
UserConfig.LoadFromStream(StringStream, |
| 350 |
{ PretendURL; we choose to just not modify it } UserConfig.URL); |
| 351 |
finally FreeAndNil(StringStream) end; |
| 352 |
|
| 353 |
SceneDragon.Translation := UserConfig.GetVector3('dragon/translation'); |
| 354 |
SceneDragon.Scale := UserConfig.GetVector3('dragon/scale'); |
| 355 |
CheckboxCameraFollow.Checked := UserConfig.GetValue('camera_follows', true); |
| 356 |
// loading game stops dragon flying |
| 357 |
DragonFlying := false; |
| 358 |
SceneDragon.PlayAnimation('idle', true); |
| 359 |
|
| 360 |
GameNotifications.Show('Successfully loaded online game.'); |
| 361 |
end else |
| 362 |
GameNotifications.Show('Failed to load game online: ' + Content); |
| 363 |
end; |
| 364 |
|
| 365 |
procedure TViewPlay.ClickChooseSaveGame(Sender: TObject); |
| 366 |
begin |
| 367 |
GameService.ShowSaveGames('Choose save game', true, true, -1); |
| 368 |
GameNotifications.Show('Starting choosing savegame online.'); |
| 369 |
end; |
| 370 |
|
| 371 |
procedure TViewPlay.GameServiceSaveGameChosen(Sender: TObject; const Choice: TSaveGameChoice; const SaveGameName: string); |
| 372 |
begin |
| 373 |
{ We just display the user choice, without acting on it now. |
| 374 |
In a real game, you would probably want to do something |
| 375 |
depending on the user choice -- follow the ClickSaveGame or ClickLoadGame |
| 376 |
code to see how to save/load the game. } |
| 377 |
case Choice of |
| 378 |
sgCancel: GameNotifications.Show('Savegame choice cancelled.'); |
| 379 |
sgNew: GameNotifications.Show('Savegame choice: new savegame.'); |
| 380 |
sgExisting: GameNotifications.Show('Savegame choice: existing savegame.' + SaveGameName); |
| 381 |
else raise EInternalError.Create('GameServiceSaveGameChosen:Choice?'); |
| 382 |
end; |
| 383 |
end; |
| 384 |
|
| 385 |
end. |