Compare commits

...

2 commits

Author SHA1 Message Date
2b8e04e4d6 fixed conflict 2022-08-24 15:44:52 +08:00
d2281817d0
Camera follows selected character 2022-08-24 00:45:57 +02:00
2 changed files with 49 additions and 2 deletions

View file

@ -32,9 +32,14 @@ impl Plugin for GamePlugin {
SystemSet::on_update(AppState::Game)
.with_system(crate::levels::post_setup_level)
.with_system(keyboard_input_system)
.with_system(character_particle_effect_system),
.with_system(character_particle_effect_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,
);
}
@ -296,3 +303,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;
}
}
}
}
}

View file

@ -13,6 +13,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
@ -23,6 +24,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));
}