feat(prism): impl Default

This commit is contained in:
Kristofers Solo 2024-09-29 20:01:18 +03:00
parent 623b53c34f
commit 1a8eeb97b5
4 changed files with 41 additions and 41 deletions

View File

@ -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

View File

@ -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<MazeConfig>) {
let radius = config.radius as i32;

View File

@ -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);
}
}

View File

@ -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<Vec3> 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<Assets<Mesh>>,
@ -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<PrismParams> = 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()
},