Skip to content

Commit

Permalink
Update examples doc with top-down cam
Browse files Browse the repository at this point in the history
  • Loading branch information
theredfish committed May 20, 2024
1 parent 50edf9d commit de08756
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
7 changes: 7 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ git checkout v0.4.0
- [Assets](#assets)
- [Async Tasks](#async-tasks)
- [Audio](#audio)
- [Camera](#camera)
- [Dev tools](#dev-tools)
- [Diagnostics](#diagnostics)
- [ECS (Entity Component System)](#ecs-entity-component-system)
Expand Down Expand Up @@ -235,6 +236,12 @@ Example | Description
[Spatial Audio 2D](../examples/audio/spatial_audio_2d.rs) | Shows how to play spatial audio, and moving the emitter in 2D
[Spatial Audio 3D](../examples/audio/spatial_audio_3d.rs) | Shows how to play spatial audio, and moving the emitter in 3D

## Camera

Example | Description
--- | ---
[2D top-down camera](../examples/camera/2d_top_down_camera.rs) | A 2D top-down camera smoothly following player movements

## Dev tools

Example | Description
Expand Down
13 changes: 8 additions & 5 deletions examples/camera/2d_top_down_camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ use bevy::math::{vec2, vec3};
use bevy::prelude::*;
use bevy::sprite::{Material2d, MaterialMesh2dBundle, Mesh2dHandle};

static MOVE_SPEED: f32 = 100.;
static LERP_FACTOR: f32 = 2.;
/// Player movement speed factor.
const PLAYER_SPEED: f32 = 100.;

/// Camera lerp factor.
const CAM_LERP_FACTOR: f32 = 2.;

#[derive(Component)]
struct Player;
Expand All @@ -29,7 +32,7 @@ fn main() {
.add_plugins(default_plugins)
.insert_resource(ClearColor(Color::BLACK))
.add_systems(Startup, (scene_setup, camera_setup).chain())
.add_systems(Update, (update_camera, move_player))
.add_systems(Update, (move_player, update_camera).chain())
.run();
}

Expand Down Expand Up @@ -98,7 +101,7 @@ fn update_camera(
// Move camera with a smooth effect
camera.translation = camera
.translation
.lerp(direction, time.delta_seconds() * LERP_FACTOR);
.lerp(direction, time.delta_seconds() * CAM_LERP_FACTOR);
}

fn move_player(
Expand Down Expand Up @@ -129,6 +132,6 @@ fn move_player(
direction.x = 1.;
}

let move_delta = direction * MOVE_SPEED * time.delta_seconds();
let move_delta = direction * PLAYER_SPEED * time.delta_seconds();
player.translation += move_delta.extend(0.);
}

0 comments on commit de08756

Please sign in to comment.