Skip to content

Instantly share code, notes, and snippets.

@michaliskambi
Last active September 16, 2023 23:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaliskambi/fadf1604f69756391c50f657f3188221 to your computer and use it in GitHub Desktop.
Save michaliskambi/fadf1604f69756391c50f657f3188221 to your computer and use it in GitHub Desktop.
{
Copyright 2022-2023 Michalis Kamburelis.
This file is part of "Castle Game Engine".
"Castle Game Engine" is free software; see the file COPYING.txt,
included in this distribution, for details about the copyright.
"Castle Game Engine" is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
----------------------------------------------------------------------------
}
{ Main view, where most of the application logic takes place. }
unit GameViewMain;
interface
uses Classes,
CastleVectors, CastleComponentSerialize,
CastleUIControls, CastleControls, CastleKeysMouse, CastleTerrain, CastleCameras;
type
{ Main view, where most of the application logic takes place. }
TViewMain = class(TCastleView)
published
{ Components designed using CGE editor.
These fields will be automatically initialized at Start. }
LabelFps: TCastleLabel;
WalkNavigation1: TCastleWalkNavigation;
Terrain1: TCastleTerrain;
TerrainNoise1: TCastleTerrainNoise;
private
ChangeCount: Integer;
public
constructor Create(AOwner: TComponent); override;
procedure Start; override;
procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override;
function Press(const Event: TInputPressRelease): Boolean; override;
end;
var
ViewMain: TViewMain;
implementation
uses SysUtils,
CastleLog, CastleTransform;
{ TViewMain ----------------------------------------------------------------- }
constructor TViewMain.Create(AOwner: TComponent);
begin
inherited;
DesignUrl := 'castle-data:/gameviewmain.castle-user-interface';
end;
procedure TViewMain.Start;
var
Body: TCastleRigidBody;
Collider: TCastleMeshCollider;
begin
inherited;
Body := TCastleRigidBody.Create(Self);
Terrain1.AddBehavior(Body);
Collider := TCastleMeshCollider.Create(Self);
Collider.Mesh := Terrain1;
Collider.Restitution := 0.3;
Terrain1.AddBehavior(Collider);
end;
procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: Boolean);
begin
inherited;
{ This virtual method is executed every frame (many times per second). }
LabelFps.Caption := 'FPS: ' + Container.Fps.ToString;
WalkNavigation1.MouseLook := buttonRight in Container.MousePressed;
// Changing Smoothness (see TCastleTerrainNoise.SetSmoothness implementation)
// calls TCastleTerrain.UpdateGeometry each time.
TerrainNoise1.Smoothness := TerrainNoise1.Smoothness + SecondsPassed * 0.1;
Inc(ChangeCount);
WritelnLog('Smoothness change: %f (change num %d)', [
TerrainNoise1.Smoothness,
ChangeCount
]);
end;
function TViewMain.Press(const Event: TInputPressRelease): Boolean;
begin
Result := inherited;
if Result then Exit; // allow the ancestor to handle keys
{ This virtual method is executed when user presses
a key, a mouse button, or touches a touch-screen.
Note that each UI control has also events like OnPress and OnClick.
These events can be used to handle the "press", if it should do something
specific when used in that UI control.
The TViewMain.Press method should be used to handle keys
not handled in children controls.
}
// Use this to handle keys:
{
if Event.IsKey(keyXxx) then
begin
// DoSomething;
Exit(true); // key was handled
end;
}
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment