diff options
author | Alexander Foremny <aforemny@posteo.de> | 2024-03-24 15:55:06 +0100 |
---|---|---|
committer | Alexander Foremny <aforemny@posteo.de> | 2024-03-24 15:55:06 +0100 |
commit | b09b4a6c8854d8c253e2129b57a9637f4c362fbe (patch) | |
tree | a3f45768ea872d26b6ccacf029fee59a129f4ff9 | |
parent | 876fd9d60de9c09e8657a2cb70fd55fd041d1f97 (diff) |
feat: camera controls
-rw-r--r-- | src/client.rs | 75 |
1 files changed, 59 insertions, 16 deletions
diff --git a/src/client.rs b/src/client.rs index d536dce..bff66cd 100644 --- a/src/client.rs +++ b/src/client.rs @@ -57,6 +57,12 @@ struct MyFaction(pub Faction); #[derive(Resource)] struct Attack(Option<AbilitySlot>); +#[derive(Resource, Default)] +struct CameraControls { + pub direction: Vec2, + pub locked: bool, +} + struct ClientPlugin { pub server_addr: Option<SocketAddr>, pub client_id: u64, @@ -71,6 +77,7 @@ impl Plugin for ClientPlugin { .insert_resource(MyChampion(self.champion)) .insert_resource(MyFaction(self.faction)) .insert_resource(Attack(None)) + .insert_resource(CameraControls::default()) .add_plugins(NetworkPlugin { server_addr: self.server_addr.clone(), client_id: self.client_id, @@ -105,7 +112,14 @@ impl Plugin for ClientPlugin { .chain(), ), ) - .add_systems(Update, move_camera) + .add_systems( + Update, + ( + control_camera, + lock_camera, + move_camera.after(control_camera).after(lock_camera), + ), + ) .add_systems( FixedPreUpdate, buffer_input @@ -872,26 +886,55 @@ fn health_indicator_despawn( } } -const CAMERA_MOVEMENT_SPEED: f32 = 100.; - fn move_camera( mut cameras: Query<&mut Transform, With<Camera>>, - players: Query<(&PlayerId, &PlayerPosition, &Shape)>, - client_id: Res<ClientId>, time: Res<Time>, + camera_controls: Res<CameraControls>, + client_id: Res<ClientId>, + players: Query<(&PlayerId, &PlayerPosition, &Shape)>, ) { - let Some(player_position) = player_position(&client_id, &players) else { - return; - }; let dt = time.delta().as_secs_f32(); for mut transform in cameras.iter_mut() { - let direction = (Vec3::new( - player_position.0.x, - player_position.0.y, - transform.translation.z, - ) - transform.translation) - .normalize_or_zero(); - transform.translation += - (dt * CAMERA_MOVEMENT_SPEED).min(direction.length()) * direction.normalize_or_zero(); + if camera_controls.locked { + let Some(player_position) = player_position(&client_id, &players) else { + continue; + }; + transform.translation = Vec3::new(player_position.0.x, player_position.0.y, 0.); + continue; + } + let direction = Vec3::new(camera_controls.direction.x, camera_controls.direction.y, 0.); + transform.translation += dt * CAMERA_MOVEMENT_SPEED * direction; } } + +fn lock_camera( + mut camera_controls: ResMut<CameraControls>, + keyboard_input: Res<ButtonInput<KeyCode>>, +) { + camera_controls.locked = keyboard_input.pressed(KeyCode::Space); +} + +const CAMERA_MOVEMENT_SPEED: f32 = 150.; + +fn control_camera(mut camera_controls: ResMut<CameraControls>, windows: Query<&Window>) { + let window = windows.single(); + let Some(cursor_position) = window.cursor_position() else { + return; + }; + if cursor_position.x < 50. { + camera_controls.direction.x = -1.; + } else if window.resolution.width() - cursor_position.x < 50. { + camera_controls.direction.x = 1.; + } else { + camera_controls.direction.x = 0.; + } + if cursor_position.y < 50. { + camera_controls.direction.y = 1.; + } else if window.resolution.height() - cursor_position.y < 50. { + camera_controls.direction.y = -1.; + } else { + camera_controls.direction.y = 0.; + } + + camera_controls.direction = camera_controls.direction.normalize_or_zero(); +} |