From 941c2f6bea44cf0eddd761f2502c7ad20e70dba9 Mon Sep 17 00:00:00 2001 From: Robert Garrett Date: Thu, 28 Nov 2024 09:50:13 -0600 Subject: [PATCH] World size configuration The WorldSize is a Resource so that I can use it in the entity wrapping system. The size is pulled from a public constant in the config.rs file, and the window is made that same size. --- src/config.rs | 2 ++ src/lib.rs | 14 ++++++++++++-- src/main.rs | 12 +++++++++--- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/config.rs b/src/config.rs index f90b344..a318092 100644 --- a/src/config.rs +++ b/src/config.rs @@ -5,6 +5,8 @@ where ever they happen to be needed, I'm concentrating them here. use bevy::color::Color; +pub const WINDOW_SIZE: bevy::prelude::Vec2 = bevy::prelude::Vec2::new(800.0, 600.0); + pub(crate) const BACKGROUND_COLOR: Color = Color::srgb(0.3, 0.3, 0.3); pub(crate) const PLAYER_SHIP_COLOR: Color = Color::srgb(1.0, 1.0, 1.0); pub(crate) const SHIP_THRUSTER_COLOR_ACTIVE: Color = Color::srgb(1.0, 0.2, 0.2); diff --git a/src/lib.rs b/src/lib.rs index 2a6283c..16a4995 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,6 @@ -mod config; +pub mod config; -use crate::config::{BACKGROUND_COLOR, PLAYER_SHIP_COLOR, SHIP_ROTATION, SHIP_THRUST}; +use crate::config::{BACKGROUND_COLOR, PLAYER_SHIP_COLOR, SHIP_ROTATION, SHIP_THRUST, WINDOW_SIZE}; use bevy::{prelude::*, sprite::MaterialMesh2dBundle}; use config::{SHIP_THRUSTER_COLOR_ACTIVE, SHIP_THRUSTER_COLOR_INACTIVE}; @@ -11,6 +11,10 @@ impl Plugin for AsteroidPlugin { fn build(&self, app: &mut App) { app.add_systems(Startup, (spawn_camera, spawn_player)) .insert_resource(ClearColor(BACKGROUND_COLOR)) + .insert_resource(WorldSize { + width: WINDOW_SIZE.x, + height: WINDOW_SIZE.y, + }) .add_systems(FixedUpdate, (input_ship_thruster, input_ship_rotation)) .add_systems( FixedPostUpdate, @@ -37,6 +41,12 @@ struct Ship; #[derive(Component)] struct ThrusterColors(Handle, Handle); +#[derive(Resource)] +struct WorldSize { + width: f32, + height: f32, +} + fn spawn_camera(mut commands: Commands) { commands.spawn(Camera2dBundle::default()); } diff --git a/src/main.rs b/src/main.rs index 75686d0..dd4eb40 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,16 @@ -use bevy::prelude::*; +use bevy::{prelude::*, window::WindowResolution}; -use asteroids::AsteroidPlugin; +use asteroids::{config::WINDOW_SIZE, AsteroidPlugin}; fn main() { App::new() - .add_plugins(DefaultPlugins) + .add_plugins(DefaultPlugins.set(WindowPlugin { + primary_window: Some(Window { + resolution: WindowResolution::new(WINDOW_SIZE.x, WINDOW_SIZE.y), + ..default() + }), + ..default() + })) .add_plugins(AsteroidPlugin) .run(); }