Creating a First-person Shooter 3D Game with the Castle Game Engine

These instructions are based on part of the PDF of the slide show presented by Michalis Kamburelis.

  1. Get the engine and install the package castle_components in Lazarus.
  2. Start a new form-based application in Lazarus and drop onto the form a TCastleControl from the newly created Castle tab.
  3. Extract the ZIP file and copy the data folder from the fps_game subdirectory to your application folder.
    The file bridge_final.x3dv is written by hand and inlines bridge.x3d, which was exported from Blender. (The file level.xml is already present in the data folder so you do not need to create it).
  4. Add a uses CastleLevels; clause to the implementation section:
    {$R *.lfm}
  5. Double click in the onOpen event of CastleControl1 in the Object Inspector and add this code to the CastleControl1Open procedure:
  6. Add CastleResources to the implementation uses clause and insert the line Resources.LoadFromFiles; at the start of the CastleControl1Open procedure.
    This lets the engine see the resource.xml file (which is already present in the item_medkit folder) describing the item named MedKit.
  7. Instead of changing the Blender file to add a MedKit, you can insert this code after the Background DEF tag near the beginning of bridge.x3d.
        <Transform DEF="CasMedKit_TRANSFORM"
                   translation="-10.035152 0.0 1.422754"
                   scale="1.000000 1.000000 1.000000"
                   rotation="0.000000 0.707107 0.707107 3.141593"
          <Transform DEF="CasResMedKit_ifs_TRANSFORM"
                     translation="0.000000 0.000000 0.000000"
                     scale="1.000000 1.000000 1.000000"
                     rotation="1.000000 0.000000 0.000000 0.000000"
            <Group DEF="group_ME_Cube">
                <IndexedFaceSet solid="false"
                                coordIndex="1 0 4 5 -1 5 6 2 1 -1 6 7 3 2 -1 0 3 7 4 -1 0 1 2 3 -1 7 6 5 4 -1 "
                  <Coordinate DEF="coords_ME_Cube"
                              point="-1.000000 -1.000000 -1.000000 -1.000000 1.000000 -1.000000 1.000000 1.000000
                                     -1.000000 1.000000 -1.000000 -1.000000 -1.000000 -1.000000 1.000000 -1.000000
                                      1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 -1.000000 1.000000"
    Alternatively, for the inclusion of two knights and many MedKits, you could move bridge_with_resources.x3d from the unused_extras folder to data and change the inline url code in bridge_final.x3dv to:
    Inline {
      url "bridge_with_resources.x3d"
  8. Add CastlePlayer to the interface uses clause (not to the implementation uses clause) and declare in the class private section Player: TPlayer;. Insert at the beginning of the CastleControl1Open procedure the code:
    Player := TPlayer.Create(CastleControl1.SceneManager);
    CastleControl1.SceneManager.Player := Player;

    Items are pickable now; they are added to player’s inventory but the inventory is not visualized in any way. Next add a simple 2D HUD as described in the following two steps.

  9. Add CastleUIControls to the implementation uses clause. Add to the interface section:
      TGame2DControls = class(TUIControl)
        procedure Render; override;
    procedure TGame2DControls.Render;
      Player : TPlayer;
      I, J, X: Integer;
      Player := Form1.Player;
      X := 0;
      for I :=0 to Player.Inventory.Count - 1 do
        for J := 0 to Player.Inventory[I].Quantity - 1 do
            Player.Inventory[I].Resource.GLImage.Draw(X, 0) ;
            X += 100;
  10. Insert in the CastleControl1Open procedure the code:
      Game2DControls: TGame2DControls;
      . . . 
      Game2DControls := TGame2DControls.Create(Application);
  11. Add a button on a form to spawn creatures. It is best to use a TSpeedButton (from the Additional component tab) so as not to capture focus.
  12. Add to the implementation uses clause CastleVectors and CastleCreatures.
  13. Double click on the speed button and add code to the click procedure so that it looks like this:
    procedure TForm1.SpeedButton1Click(Sender: TObject);  //Note speedbutton
      P: TVector3Single;
      Direction: TVector3Single;
      CreatureResource: TCreatureResource;
      P := Player.Position + Player.Direction * 10;
      Direction := Player.Direction;
      CreatureResource := Resources.FindName('Knight') as TCreatureResource;
      CreatureResource.CreateCreature(CastleControl1.SceneManager.Items, P, Direction);
  14. Add a speed button to kill a creature and add Castle3D to the implementation uses clause. The button click procedure is:
    procedure TForm1.SpeedButton2Click(Sender: TObject);
      I: Integer;
      Hit: TRayCollision;
      Hit := Player.Ray(Player.Middle, Player.Direction);
      if Hit <> nil then
          for I := 0 to Hit.Count - 1 do
            if Hit[I].Item is T3DAlive then
                (Hit[I].Item as T3DAlive).Hurt(100, Player.Direction, 1, Player);
  15. Compile, execute and test the program.
    You should be able to pick up the MedKit. It should appear at the bottom left of the window and stop rotating. One knight looks fine on the bridge. It should die impressively when you shoot it from a suitable position.
Programming - a skill for life!

How to use and learn from the Castle Game Engine