Skip to content
Snippets Groups Projects
camera.rs 933 B
use crate::system::window_bounds;
use bevy::core_pipeline::clear_color::ClearColorConfig;
use bevy::prelude::{
	Camera2d, Camera2dBundle, Color, Commands, Component, OrthographicProjection, Rect, Transform,
	Vec2,
};
use bevy::render::camera::ScalingMode;

pub fn default_projection() -> OrthographicProjection {
	let bounds = window_bounds();

	OrthographicProjection {
		area: Rect::from_center_size(Vec2::ZERO, bounds.size()),
		scaling_mode: ScalingMode::FixedVertical(bounds.height()),
		..Default::default()
	}
}

#[derive(Component)]
pub struct GameCamera;

pub fn spawn_2d_camera(mut commands: Commands) {
	let bounds = window_bounds();

	commands.spawn((
		GameCamera,
		Camera2dBundle {
			projection: default_projection(),
			camera_2d: Camera2d {
				clear_color: ClearColorConfig::Custom(Color::WHITE),
			},
			transform: Transform::from_translation(bounds.half_size().extend(0.0)),
			..Default::default()
		},
	));
}