0:00
/
0:00
Transcript

With Bevy, from Rust!

Made a basic 3d game

As I mentioned in the previous post, I took a detour of a week in my Rust learning journey from Databases to learn fundamentals of a popular Rust game engine Bevy.

It is a performant game engine where most of the things just work. Few important points to know about Bevy.

No UI Editor

There is no official UI editor or development environment for Bevy. Yet. I know this is a shock for many game dev who are used to design their scenes in the UI editor. Games are all about visuals and so having a editor makes sense to ease the development.

Bevy team is working hard at it and there are many ideas and interesting proposals from the top. There are many open source Editors built by the community which are really good and can help with development.

For me, the code first world was a relief as I am not into core game dev and found my way through the coding samples easily. There was no learning curve to learn a new UI tool.

ECS

You will learn a new game development paradigm here.

Entity. Component. Systems.

TLDR of ECS below.

  1. Think of your game as a large relational table.

  2. All the game components are entities which are loke rows in the table.

  3. All the attributes of entities are components. Think about columns in table.

  4. You can query, insert, update this table using systems. The changes will be rendered by the engine.

ECS is new and refreshing. Once you learn the basics, you can go through the awesome examples to dig deeper.

Slowly, you develop the ability to think in ECS terms about your game play, mechanics and model everything the ECS way. You can not escape it if you want to do games in Bevy. This is more or less similar as DOTS (Data oriented Technology stack) in Unity game engine.

Adoption

Bevy is popular in Rust community. However, there are not AAA games done in Bevy yet. It is too young and api keeps changing with each version. The current version which I used at time of writing this 0.15.1. There are lots of indie devs and hobbyists making games in Bevy and team is working hard to deliver features so that it is on parity with legacy battle tested engines like Unity, Unreal and Godot.

As far as I know, Tiny Glades is the most popular game which has used Bevy. Many upcoming games are looking promising.

About my Game

Game Repo: Cody' Escape

Corby's Escape is a 3d Third person game built in Bevy engine using Rust. It is about a robot named Corby who is imprisoned on a planet in space. His only crime was to save a human from planet earth from death. Corby wants to escape from the space prison and meet his friend who is locked on another planet. Can you help Corby ?

This is Corby, BTW.

It is my first game in Bevy engine and is also a ground for expermiments. I plan to add more game elements, proceduraly generated worlds, more enemies, game play mechanics and boss enemy.

Of course, I used the game examples assets in the game. The scene setup is from the motion blur sample and aircraft is from rotation sample in bevy examples page. The protagonist Corby is itself from a Game example ‘Alien Cake Addict’.

Next, I will spend some time every week to add more features in the game. Expect some Wave function collpase based procedural geneated worlds, better game mechanics, scoring, UI etc.

Not gonna leave Bevy after this. Yes, we can develop games in Bevy and also so many other things like simulations etc. It is useful tool to master in the Rust journey.

A Glimpse into Bevy

Here’s how you add a 3 model to a scene.

fn spawn_agent(mut commands: Commands, assets: Res<AssetServer>) {
    let agent = (
        SceneRoot(assets.load(GltfAssetLabel::Scene(0).from_asset("alien.glb"))),
        Transform::from_xyz(-2.0, -0.70, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
        Agent,
        ThirdPersonCameraTarget,
        Speed(2.5),
    );
    commands.spawn(agent);
}

Here is how you control the player movement using the keyboard.

fn agent_movement(
    time: Res<Time>,
    keys: Res<ButtonInput<KeyCode>>,
    mut player_q: Query<(&mut Transform, &Speed), With<Agent>>,
    cam_q: Query<&Transform, (With<Camera3d>, Without<Agent>)>,
) {
    for (mut player_transform, player_speed) in player_q.iter_mut() {
        let cam = match cam_q.get_single() {
            Ok(c) => c,
            Err(e) => Err(format!("Error retrieving camera: {}", e)).unwrap(),
        };

        let mut direction = Vec3::ZERO;

        // forward
        if keys.pressed(KeyCode::KeyW) {
            direction += *cam.forward();
        }

        // back
        if keys.pressed(KeyCode::KeyS) {
            direction += *cam.back();
        }

        // left
        if keys.pressed(KeyCode::KeyA) {
            direction += *cam.left();
        }

        // right
        if keys.pressed(KeyCode::KeyD) {
            direction += *cam.right();
        }

        direction.y = 0.0;
        let movement = direction.normalize_or_zero() * player_speed.0 * time.delta_secs();
        player_transform.translation += movement;

        // rotate player to face direction he is currently moving
        if direction.length_squared() > 0.0 {
            player_transform.look_to(direction, Vec3::Y);
        }
    }
}

Seems easy, right. Once you get hold of basic stuff, it is easy for developers to make progress in Bevy becaue of code first and ECS approaches.

Learning Bevy

There are many good tutorials on youtube for game design.

However, I will recommend this tutorial for understanding ECS and basics of Bevy first. Build your foundations right.You can refer to Bevy official Examples and Unofficial cheatbook for next level dive. Keep in mind that the api keeps changing and examples may not work out of the box but will need little modification if you are using latest api version.

There is a big ecosystem of crates on Bevy to help with all your requirements.

Thanks for reading!

Discussion about this video

User's avatar