Make camera follow player
This commit is contained in:
parent
e816846d19
commit
ef4a98627e
@ -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();
|
||||
}
|
||||
|
@ -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>,
|
||||
|
Loading…
x
Reference in New Issue
Block a user