Skip to content

Instantly share code, notes, and snippets.

@Telzhaak
Last active September 11, 2018 09:55
Show Gist options
  • Save Telzhaak/aa3e97c6631d83d9df97d32f3c4440e6 to your computer and use it in GitHub Desktop.
Save Telzhaak/aa3e97c6631d83d9df97d32f3c4440e6 to your computer and use it in GitHub Desktop.
A camera following the player in a 2D game.
/// 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