From 33828d6a8526a482075837a2e8be1ce751313551 Mon Sep 17 00:00:00 2001 From: Robert Garrett Date: Tue, 12 Aug 2025 23:34:48 -0500 Subject: [PATCH] Rewrite `WorldSize` as a newtype around Vec2 Closes #16 Bevy provides Deref and DerefMut derives, so that's nice. I'm not sure it makes any sense to keep the field private since those derefs expose the Vec2 anyway. I added an `impl Default` anyway, though. --- src/lib.rs | 5 +---- src/machinery.rs | 2 +- src/physics.rs | 4 ++-- src/resources.rs | 15 +++++++++------ 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 38b0352..d3d9799 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,10 +40,7 @@ impl Plugin for AsteroidPlugin { RapierDebugRenderPlugin::default(), )) .insert_resource(ClearColor(BACKGROUND_COLOR)) - .insert_resource(WorldSize { - width: WINDOW_SIZE.x, - height: WINDOW_SIZE.y, - }) + .insert_resource(WorldSize::default()) .insert_resource(Lives(3)) .register_type::() .insert_resource(Score(0)) diff --git a/src/machinery.rs b/src/machinery.rs index 89bb816..eef9110 100644 --- a/src/machinery.rs +++ b/src/machinery.rs @@ -55,7 +55,7 @@ pub fn tick_asteroid_manager( let spawn_angle = rng.random_range(0.0..(std::f32::consts::PI * 2.0)); // Rho will be the radius of a circle bordering the viewport, multiplied by 1.2 // TODO: Use view diagonal to get a minimally sized circle around the play area - let spawn_distance = play_area.width.max(play_area.height) / 2.0; + let spawn_distance = play_area.x.max(play_area.y) / 2.0; // Convert polar to Cartesian, use as position let pos = Vec2::new( diff --git a/src/physics.rs b/src/physics.rs index efb57cd..11d9174 100644 --- a/src/physics.rs +++ b/src/physics.rs @@ -56,9 +56,9 @@ pub(crate) fn wrap_entities( mut query: Query<&mut Transform, With>, world_size: Res, ) { - let right = world_size.width / 2.0; + let right = world_size.x / 2.0; let left = -right; - let top = world_size.height / 2.0; + let top = world_size.y / 2.0; let bottom = -top; for mut pos in query.iter_mut() { diff --git a/src/resources.rs b/src/resources.rs index dfff0d0..a3001e6 100644 --- a/src/resources.rs +++ b/src/resources.rs @@ -10,7 +10,7 @@ use bevy::{ Vec2, primitives::{Circle, Triangle2d}, }, - prelude::{Deref, Reflect, ReflectResource}, + prelude::{Deref, DerefMut, Reflect, ReflectResource}, render::mesh::Mesh, sprite::ColorMaterial, }; @@ -19,7 +19,7 @@ use bevy_inspector_egui::inspector_options::ReflectInspectorOptions; use crate::{ ASTEROID_SMALL_COLOR, BULLET_COLOR, PLAYER_SHIP_COLOR, SHIP_THRUSTER_COLOR_ACTIVE, - SHIP_THRUSTER_COLOR_INACTIVE, + SHIP_THRUSTER_COLOR_INACTIVE, config::WINDOW_SIZE, }; #[derive(Resource, Debug, Deref, Clone, Copy)] @@ -41,10 +41,13 @@ impl From for String { } } -#[derive(Resource)] -pub struct WorldSize { - pub width: f32, - pub height: f32, +#[derive(Deref, DerefMut, Resource)] +pub struct WorldSize(Vec2); + +impl Default for WorldSize { + fn default() -> Self { + WorldSize(Vec2::new(WINDOW_SIZE.x, WINDOW_SIZE.y)) + } } #[derive(Resource)]