Last active
September 11, 2018 09:55
-
-
Save Telzhaak/aa3e97c6631d83d9df97d32f3c4440e6 to your computer and use it in GitHub Desktop.
A camera following the player in a 2D game.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 2D player following camera system. Needs to be dispatched after the Transform-Bundle, to avoid jittering, | |
/// but before the Render-Bundle. | |
pub struct FollowCameraSystem; | |
impl<'s> System<'s> for FollowCameraSystem{ | |
type SystemData = ( | |
WriteStorage<'s, Transform>, | |
ReadStorage<'s, CPlayer>, | |
ReadStorage<'s, Camera>, | |
); | |
fn run(&mut self, (mut transforms, players, camera) : Self::SystemData){ | |
let mut transform_camera = Transform::default(); | |
if let Some((transform, player)) = (&transforms, &players).join().next() { | |
// TODO: Dont use camera-offset in Player, instead make it a component of the Camera entity? | |
transform_camera.translation[0] = transform.translation[0] - player.camera_offset.0; | |
transform_camera.translation[1] = transform.translation[1] - player.camera_offset.1; | |
} | |
if let Some((transform, _)) = (&mut transforms, &camera).join().next() { | |
transform.translation[0] = transform_camera.translation[0]; | |
transform.translation[1] = transform_camera.translation[1]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment