Skip to content

Commit

Permalink
snippets and proofreading
Browse files Browse the repository at this point in the history
General proof-reading, typos etc.
Fixed some of the code snips so the project will run out of the box when you are following along the tutorial in the book.
  • Loading branch information
Somebootys committed Jun 27, 2024
1 parent 30eb6da commit 37a9bec
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 13 deletions.
19 changes: 19 additions & 0 deletions src/code/tutorials/fps/game/src/snips/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

// ANCHOR: player_imports

use crate::{player::Player};
use fyrox::{
core::pool::Handle,
core::{reflect::prelude::*, visitor::prelude::*},
plugin::{Plugin, PluginContext, PluginRegistrationContext},
scene::Scene,
event::Event,
};
use fyrox_ui::message::UiMessage;
use std::path::Path;
// ANCHOR_END: player_imports
// ANCHOR: player_mod_reg
// Add this line
pub mod player;

// ANCHOR_END: player_mod_reg
19 changes: 19 additions & 0 deletions src/code/tutorials/fps/game/src/snips/player.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// ANCHOR: player_imports
use fyrox::graph::SceneGraph;
use fyrox::{
core::{
algebra::{UnitQuaternion, UnitVector3, Vector3},
pool::Handle,
reflect::prelude::*,
type_traits::prelude::*,
variable::InheritableVariable,
visitor::prelude::*,
},
event::{DeviceEvent, ElementState, Event, MouseButton, WindowEvent},
keyboard::{KeyCode, PhysicalKey},
scene::{node::Node, rigidbody::RigidBody},
script::{ScriptContext, ScriptTrait, ScriptDeinitContext},

};

// ANCHOR_END: player_imports
46 changes: 33 additions & 13 deletions src/tutorials/fps/tutorial-1/fps-tutorial.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# First-Person Shooter Tutorial

In this tutorial we'll create a first-person shooter game.
In this tutorial, we'll create a first-person shooter game.

Before we begin, make sure you know how to create projects and run the game and the editor. Read
[this chapter](../../../beginning/scripting.md) first and let's start by creating a new project by executing the following
Expand All @@ -13,6 +13,12 @@ fyrox-template init --name=fps --style=3d
This command will create a new cargo workspace with a few projects inside, we're interested only in `game` folder
in this tutorial.

We also need to add fyrox_ui to our .toml in the fps project. The simplest way is to do it like so:

```shell
cargo add fyrox-ui --package fps
```

```text
fps
├───data
Expand All @@ -30,8 +36,8 @@ fps

## Player Prefab

Let's start by creating a [prefab](../../../scene/prefab.md) for the player. First-person shooters uses quite simple
layout for characters - usually it is just a physical capsule with a camera on top of it. Run the editor using the
Let's start by creating a [prefab](../../../scene/prefab.md) for the player. First-person shooters use quite simple
layouts for characters - usually, it is just a physical capsule with a camera on top of it. Run the editor using the
following command:

```shell
Expand All @@ -43,15 +49,15 @@ cargo run --package editor
By default, `scene.rgs` scene is loaded, and it is our main scene, but for our player prefab we need a separate scene.
Go to `File` menu and click `New Scene`. Save the scene in `data/player` folder as `player.rgs`.

Great, now we ready to create the prefab. Right-click on the `__ROOT__` node in the World Viewer and find `Replace Node`
Great, now we are ready to create the prefab. Right-click on the `__ROOT__` node in the World Viewer and find `Replace Node`
and select `Physics -> Rigid Body` there. By doing this, we've replaced the root node of the scene to be a rigid body.
This is needed, because our player will be moving.
This is needed because our player will be moving.

![replace node](editor_2.png)

Select rigid body and set the `X/Y/Z Rotation Locked` properties to `true`, `Can Sleep` - to `false`. The first three
properties prevents the rigid body from any undesired rotations and the last one prevents the rigid body to be excluded
from simulation.s
properties prevents the rigid body from any undesired rotations and the last one prevents the rigid body from being excluded
from simulations.

![rigid body props](rigid_body_props.png)

Expand All @@ -68,7 +74,7 @@ Now let's change the size of the collider, because default values are disproport

![collider properties](editor_5.png)

This way the capsule is thinner and taller, which roughly corresponds to a person of 1.8m tall. Now we need to add a
This way the capsule is thinner and taller, which roughly corresponds to a 1.8m tall person. Now we need to add a
[camera](../../../scene/camera_node.md), because without it, we couldn't see anything.

![camera](editor_6.png)
Expand All @@ -89,11 +95,15 @@ Navigate to the `fps` directory and execute the following command there:
fyrox-template script --name=player
```

This command creates a new script for our player in `game/src` folder. All you need to do now is to add the new module
to the `lib.rs` module by adding the `pub mod player;` after the imports:
This command creates a new script for our player in `game/src` folder. Next, Replace the imports in the `lib.rs` with the ones below:
```rust
{{#include ../../../code/tutorials/fps/game/src/snips/lib.rs:player_imports}}
```

and then add the new module to the `lib.rs` module by adding the `pub mod player;` after the imports:

```rust
{{#include ../../../code/tutorials/fps/game/src/lib.rs:player_mod_reg}}
{{#include ../../../code/tutorials/fps/game/src/snips/lib.rs:player_mod_reg}}
```

All scripts must be registered in the engine explicitly, otherwise they won't work. To do that, add the following
Expand All @@ -103,7 +113,15 @@ lines to the `register` method:
{{#include ../../../code/tutorials/fps/game/src/lib.rs:player_script_reg}}
```

Great, now the new script is registered, and we can start writing a basic character controller. Let's start by input
Great, now the new script is registered, we can head over to the `player.rs` module and start writing a basic character controller.
First replace the import to the following:

```rust
{{#include ../../../code/tutorials/fps/game/src/snips/player.rs:player_imports}}
```


Let's start by input
handling. At first, add the following fields to the `Player` struct:

```rust
Expand Down Expand Up @@ -155,6 +173,8 @@ this script is assigned to, then it checks if any of the WSAD keys are pressed,
the basis vectors of node. As the last step, it normalizes the vector (makes it unity length) and sets it to the rigid
body velocity.



Our script is almost ready, now all we need to do is to assign it to the player's prefab. Open the `player.rgs` prefab
in the editor, select `Player` node and assign the Player script to it. Do not forget to set Camera handle (by clicking
on the small green button and selecting Camera from the list):
Expand All @@ -163,7 +183,7 @@ on the small green button and selecting Camera from the list):

Great, now we're done with the player movement. We can test it our main scene, but at first let's create a simple level.
Open `scene.rgs` and create a rigid body with a collider. Add a cube as a child of the rigid body and squash it to some
floor-like shape. Select the collider and set it's kind to `Trimesh`, add a geometry source there and point it to the
floor-like shape. Select the collider and set its `Shape` to `Trimesh`, add a geometry source there and point it to the
floor. Select the rigid body and set its type to `Static`. You can also add some texture to the cube to make it look
much better.

Expand Down

0 comments on commit 37a9bec

Please sign in to comment.