Skip to content
Snippets Groups Projects
Verified Commit fbd584c5 authored by Louis's avatar Louis :fire:
Browse files

Refactor camera & window configuration

parent 98046acd
No related branches found
No related tags found
No related merge requests found
pub mod system;
use bevy::asset::AssetServer;
use bevy::core_pipeline::clear_color::ClearColorConfig;
use bevy::math::Vec3;
use bevy::prelude::{
App, Camera2d, Camera2dBundle, Color, Commands, OrthographicProjection, PluginGroup, Rect, Res,
Transform, Vec2, Window, WindowPlugin,
};
use bevy::render::camera::ScalingMode;
use bevy::prelude::{App, Commands, PluginGroup, Res, Transform, Window, WindowPlugin};
use bevy::sprite::SpriteBundle;
use bevy::window::{WindowMode, WindowResolution};
use bevy::DefaultPlugins;
static VIEWPORT_WIDTH: f32 = 680.0;
static VIEWPORT_HEIGHT: f32 = 300.0;
fn spawn_camera(mut commands: Commands) {
commands.spawn(Camera2dBundle {
projection: OrthographicProjection {
area: Rect::from_center_size(Vec2::ZERO, Vec2::new(VIEWPORT_WIDTH, VIEWPORT_HEIGHT)),
scaling_mode: ScalingMode::FixedVertical(VIEWPORT_HEIGHT),
..Default::default()
},
camera_2d: Camera2d {
clear_color: ClearColorConfig::Custom(Color::WHITE),
},
transform: Transform::from_translation(Vec3::new(
VIEWPORT_WIDTH / 2.0,
VIEWPORT_HEIGHT / 2.0,
0.0,
)),
..Default::default()
});
}
use shoot_the_revival::system::window_bounds;
fn spawn_ship(mut commands: Commands, assets: Res<AssetServer>) {
let bounds = window_bounds();
commands.spawn(SpriteBundle {
texture: assets.load("sprites/ship.png"),
transform: Transform::from_translation(Vec3::new(
VIEWPORT_WIDTH / 2.0,
VIEWPORT_HEIGHT / 2.0,
0.0,
)),
transform: Transform::from_translation(bounds.half_size().extend(0.0)),
..Default::default()
});
}
......@@ -57,7 +28,7 @@ fn main() {
}),
..Default::default()
}))
.add_startup_system(spawn_camera)
.add_plugin(shoot_the_revival::system::SystemPlugin)
.add_startup_system(spawn_ship)
.run();
}
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()
},
));
}
mod camera;
mod window;
mod _plugin {
use bevy::app::App;
use bevy::prelude::Plugin;
pub struct SystemPlugin;
impl Plugin for SystemPlugin {
fn build(&self, app: &mut App) {
app.add_startup_system(super::camera::spawn_2d_camera);
}
}
}
pub use camera::default_projection;
pub use window::{window_bounds, VIEWPORT_HEIGHT, VIEWPORT_WIDTH};
pub use _plugin::SystemPlugin;
use bevy::prelude::{Rect, Vec2};
pub static VIEWPORT_WIDTH: f32 = 680.0;
pub static VIEWPORT_HEIGHT: f32 = 300.0;
pub fn window_bounds() -> Rect {
Rect::from_center_size(
Vec2::new(VIEWPORT_WIDTH / 2.0, VIEWPORT_HEIGHT / 2.0),
Vec2::new(VIEWPORT_WIDTH, VIEWPORT_HEIGHT),
)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment