From 1a8eeb97b5a9816c109a1051c9ca15a29cc91b24 Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Sun, 29 Sep 2024 20:01:18 +0300 Subject: [PATCH] feat(prism): impl Default --- src/lib.rs | 5 +---- src/maze/grid.rs | 17 +------------- src/maze/mod.rs | 4 ++-- src/maze/prism.rs | 56 +++++++++++++++++++++++++++++++---------------- 4 files changed, 41 insertions(+), 41 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 06307ab..d58e021 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,8 +9,6 @@ mod maze; mod screens; mod theme; -use std::f32::consts::FRAC_PI_4; - use bevy::{ asset::AssetMetaCheck, audio::{AudioPlugin, Volume}, @@ -93,10 +91,9 @@ fn spawn_camera(mut commands: Commands) { commands.spawn(( Name::new("Camera"), Camera3dBundle { - transform: Transform::from_xyz(0., 5., 10.).looking_at(Vec3::ZERO, Vec3::Y), + transform: Transform::from_xyz(0., 15., 10.).looking_at(Vec3::ZERO, Vec3::Y), ..default() }, - // Camera2dBundle::default(), // Render all UI to this camera. // Not strictly necessary since we only use one camera, // but if we don't use this component, our UI will disappear as soon diff --git a/src/maze/grid.rs b/src/maze/grid.rs index 24bab0c..98b41a1 100644 --- a/src/maze/grid.rs +++ b/src/maze/grid.rs @@ -1,4 +1,4 @@ -use std::{f32::consts::PI, usize}; +use std::usize; use bevy::{ color::palettes::css::{BLACK, GREEN, RED, SILVER}, @@ -31,21 +31,6 @@ pub(super) fn plugin(app: &mut App) { }); } -pub(super) fn spawn_light(mut commands: Commands) { - commands.spawn(( - Name::new("Light Source"), - PointLightBundle { - point_light: PointLight { - intensity: 5000., - shadows_enabled: true, - ..default() - }, - transform: Transform::from_xyz(5., 10., 5.), - ..default() - }, - )); -} - pub(super) fn spawn_hex_grid(mut commands: Commands, config: Res) { let radius = config.radius as i32; diff --git a/src/maze/mod.rs b/src/maze/mod.rs index 7f31591..6392dd4 100644 --- a/src/maze/mod.rs +++ b/src/maze/mod.rs @@ -2,7 +2,7 @@ use bevy::{ ecs::{system::RunSystemOnce, world::Command}, prelude::*, }; -use grid::{generate_maze, plugin, render_maze, spawn_hex_grid, spawn_light}; +use grid::{generate_maze, plugin, render_maze, spawn_hex_grid}; pub mod grid; pub mod prism; pub mod resource; @@ -12,6 +12,7 @@ pub struct MazePlugin; impl Plugin for MazePlugin { fn build(&self, app: &mut App) { + app.add_plugins(prism::plugin); app.add_plugins(plugin); } } @@ -21,7 +22,6 @@ impl Command for MazePlugin { // world.run_system_once(spawn_hex_grid); // world.run_system_once(generate_maze); // world.run_system_once(render_maze); - world.run_system_once(spawn_light); world.run_system_once(prism::setup); } } diff --git a/src/maze/prism.rs b/src/maze/prism.rs index 46d5cb4..9833d5d 100644 --- a/src/maze/prism.rs +++ b/src/maze/prism.rs @@ -1,6 +1,10 @@ use bevy::prelude::*; use std::f32::consts::FRAC_PI_2; +pub(super) fn plugin(app: &mut App) { + app.add_systems(Update, spawn_light); +} + #[derive(Debug, Reflect, Component)] #[reflect(Component)] struct Prism { @@ -9,11 +13,41 @@ struct Prism { } struct PrismParams { - positions: Vec3, + position: Vec3, radius: f32, height: f32, } +impl From for PrismParams { + fn from(value: Vec3) -> Self { + Self { + position: value, + ..default() + } + } +} + +impl Default for PrismParams { + fn default() -> Self { + Self { + position: Vec3::ZERO, + radius: 2., + height: 4., + } + } +} + +pub(super) fn spawn_light(mut commands: Commands) { + commands.spawn(( + Name::new("Light Source"), + PointLightBundle { + point_light: PointLight { ..default() }, + transform: Transform::from_xyz(5., 10., 5.), + ..default() + }, + )); +} + pub(super) fn setup( mut commands: Commands, mut meshes: ResMut>, @@ -21,23 +55,7 @@ pub(super) fn setup( ) { let prism_material = materials.add(Color::WHITE); - let prisms = vec![ - PrismParams { - positions: Vec3::new(-3., 0., 0.), - radius: 1., - height: 2., - }, - PrismParams { - positions: Vec3::new(0., 0., 0.), - radius: 1.5, - height: 2.5, - }, - PrismParams { - positions: Vec3::new(3., 0., 0.), - radius: 0.8, - height: 1.5, - }, - ]; + let prisms: Vec = vec![Vec3::ZERO.into()]; for params in prisms { let hexagon = RegularPolygon { @@ -51,7 +69,7 @@ pub(super) fn setup( PbrBundle { mesh: prism_mesh, material: prism_material.clone(), - transform: Transform::from_translation(params.positions) + transform: Transform::from_translation(params.position) .with_rotation(Quat::from_rotation_x(FRAC_PI_2)), ..default() },