Camera follows selected character
This commit is contained in:
parent
a2bf887700
commit
d2281817d0
2 changed files with 49 additions and 2 deletions
48
src/game.rs
48
src/game.rs
|
@ -32,9 +32,14 @@ impl Plugin for GamePlugin {
|
|||
.add_system_set(
|
||||
SystemSet::on_update(AppState::Game)
|
||||
.with_system(crate::levels::post_setup_level)
|
||||
.with_system(keyboard_input_system),
|
||||
.with_system(keyboard_input_system)
|
||||
.with_system(move_camera),
|
||||
)
|
||||
.add_system_set(
|
||||
SystemSet::on_update(AppState::Win)
|
||||
.with_system(keyboard_input_system)
|
||||
.with_system(move_camera),
|
||||
)
|
||||
.add_system_set(SystemSet::on_update(AppState::Win).with_system(keyboard_input_system))
|
||||
.add_system_to_stage(CoreStage::PostUpdate, collision_event_system);
|
||||
}
|
||||
}
|
||||
|
@ -88,12 +93,14 @@ fn setup(
|
|||
mut commands: Commands,
|
||||
mut current_level: ResMut<CurrentLevel>,
|
||||
mut level_startup_event: EventWriter<LevelStartupEvent>,
|
||||
mut camera_query: Query<&mut Transform, With<Camera>>,
|
||||
) {
|
||||
let level_id = LevelId(current_level.0.map_or(0, |level_id| level_id.0 + 1));
|
||||
crate::levels::setup_level(
|
||||
&mut commands,
|
||||
&mut current_level,
|
||||
&mut level_startup_event,
|
||||
&mut camera_query,
|
||||
level_id,
|
||||
);
|
||||
}
|
||||
|
@ -343,3 +350,40 @@ fn win_setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|||
})
|
||||
.insert(Level);
|
||||
}
|
||||
|
||||
fn move_camera(
|
||||
mut camera_query: Query<(&Camera, &mut Transform, &GlobalTransform)>,
|
||||
characters: Query<(&CharacterId, &Transform), Without<Camera>>,
|
||||
level_query: Query<&SelectedCharacterId>,
|
||||
) {
|
||||
const MARGIN: f32 = 128.0;
|
||||
|
||||
if let Ok(selected_character_id) = level_query.get_single() {
|
||||
if let Some(selected_character_id) = &selected_character_id.0 {
|
||||
if let Some((_character_id, transform)) = characters
|
||||
.iter()
|
||||
.find(|(character_id, _transform)| *character_id == selected_character_id)
|
||||
{
|
||||
let (camera, mut camera_transform, camera_global_transform) =
|
||||
camera_query.single_mut();
|
||||
|
||||
let pos = camera
|
||||
.world_to_viewport(camera_global_transform, transform.translation)
|
||||
.unwrap();
|
||||
let size = camera.logical_viewport_size().unwrap();
|
||||
if pos.x < MARGIN {
|
||||
camera_transform.translation.x += pos.x - MARGIN;
|
||||
}
|
||||
if pos.x > size.x - MARGIN {
|
||||
camera_transform.translation.x += MARGIN + pos.x - size.x;
|
||||
}
|
||||
if pos.y < MARGIN {
|
||||
camera_transform.translation.y += pos.y - MARGIN;
|
||||
}
|
||||
if pos.y > size.y - MARGIN {
|
||||
camera_transform.translation.y += MARGIN + pos.y - size.y;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,6 +14,7 @@ pub fn setup_level(
|
|||
commands: &mut Commands,
|
||||
current_level: &mut ResMut<CurrentLevel>,
|
||||
level_startup_event: &mut EventWriter<LevelStartupEvent>,
|
||||
camera_query: &mut Query<&mut Transform, With<Camera>>,
|
||||
level_id: LevelId,
|
||||
) {
|
||||
let level_entity = commands
|
||||
|
@ -24,6 +25,8 @@ pub fn setup_level(
|
|||
.id();
|
||||
current_level.0 = Some(level_id);
|
||||
|
||||
camera_query.single_mut().translation = Default::default();
|
||||
|
||||
level_startup_event.send(LevelStartupEvent(level_entity));
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue