Make camera follow player

This commit is contained in:
Daniel Lynn 2021-07-17 02:50:05 -05:00
parent e816846d19
commit ef4a98627e
Signed by: daniel
GPG Key ID: 28496A140E180A9D
2 changed files with 21 additions and 1 deletions

View File

@ -20,5 +20,6 @@ fn main() {
.add_startup_system(setup.system())
.add_startup_system(add_player.system())
.add_system(player_movement.system())
.add_system(follow_player.system())
.run();
}

View File

@ -1,4 +1,5 @@
use crate::prelude::*;
use bevy::render::camera::Camera;
pub fn setup(
mut commands: Commands,
@ -10,7 +11,7 @@ pub fn setup(
// Map
let grass_material = materials.add(asset_server.load("grass.png").into());
let tile_size = Vec2::new(32.0, 32.0);
let tile_size = Vec2::new(64.0, 64.0);
let sprite = Sprite::new(tile_size);
let map_size = Vec2::new(80.0, 50.0);
let half_x = (map_size.x / 2.0) as i32;
@ -79,6 +80,24 @@ pub fn player_movement(
}
}
pub fn follow_player(
mut q: QuerySet<(
Query<&Transform, With<Player>>,
Query<&mut Transform, With<Camera>>,
)>,
) {
if let Ok(player_transform) = q.q0().single() {
let player_translation = player_transform.translation;
if let Ok(mut camera_transform) = q.q1_mut().single_mut() {
let camera_translation = &mut camera_transform.translation;
camera_translation.x = player_translation.x;
camera_translation.y = player_translation.y;
}
}
}
pub fn greet_player(
query: Query<&Name, With<Player>>,
time: Res<Time>,