Skip to content

Commit

Permalink
improve some stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-tennert committed Jun 2, 2024
1 parent b13475f commit 8d45d69
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 61 deletions.
94 changes: 94 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ bevy = { version = "0.13.2" }
bevy_egui = "0.27"
bevy_pancam = { version = "0.11.1", features = ["bevy_egui"] }
meval = "0.2"
bevy_vector_shapes = "0.7.0"
bevy-inspector-egui = "0.24"

[features]
block_input = ["bevy_pancam/bevy_egui"]
95 changes: 65 additions & 30 deletions src/flower.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use std::{f32::consts::PI, ops::Deref};
use bevy::{prelude::*, sprite::{Mesh2dHandle, MaterialMesh2dBundle}};
use bevy::{prelude::*, render::{render_resource::encase::rts_array::Truncate}, sprite::{MaterialMesh2dBundle, Mesh2dHandle}};
use bevy_vector_shapes::{painter::{ShapeCommands, ShapePainter}, shapes::{LinePainter, LineSpawner}};

use crate::{constants::PHI, ui::UiState};
use crate::{constants::PHI, ui::UiState, Callback, FlowerComponent};

pub struct FlowerSeedPlugin;

impl Plugin for FlowerSeedPlugin {
fn build(&self, app: &mut App) {
app
.init_resource::<SeedSettings>()
.add_systems(Startup, spawn_initial_flowers)
.add_systems(Update, animate_flowers);
.add_systems(Startup, (register_systems, spawn_flower_seeds))
.add_systems(Update, animate_flower_seeds);
}
}

Expand All @@ -25,79 +26,113 @@ pub struct SeedSettings {
pub radius: f32,
pub amount: i32,
pub color: Color,
pub material_handle: Option<Handle<ColorMaterial>>,
pub mesh_handle: Option<Mesh2dHandle>

}

impl Default for SeedSettings {

fn default() -> Self {
Self { rotation: 0., distance: 4.0, radius: 4.0, amount: 50, color: Color::ORANGE}
Self { rotation: 0., distance: 4.0, radius: 4.0, amount: 50, color: Color::ORANGE, material_handle: None, mesh_handle: None }
}

}

impl SeedSettings {

pub fn default_petal() -> Self {
Self { rotation: 0., distance: 50.0, radius: 4.0, amount: 1, color: Color::ORANGE}
Self { rotation: 0., distance: 50.0, radius: 4.0, amount: 1, color: Color::ORANGE, material_handle: None, mesh_handle: None }
}

}

#[derive(Component)]
pub struct ResetFlowerSeeds;

fn register_systems(
world: &mut World
) {
let spawn_id = world.register_system(spawn_flower_seeds);
world.spawn((Callback(spawn_id), ResetFlowerSeeds));
}

/*
fn spawn_initial_flowers(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
seed_settings: Res<SeedSettings>
) {
spawn_flowers(&mut commands, &mut meshes, &mut materials, seed_settings.deref());
}
}*/

fn animate_flowers(
fn animate_flower_seeds(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
mut seed_settings: ResMut<SeedSettings>,
ui_state: Res<UiState>,
time: Res<Time>,
flowers: Query<Entity, With<FlowerSeed>>,
reset_seeds: Query<&Callback, With<ResetFlowerSeeds>>
) {
if !ui_state.animate {
return;
}
seed_settings.rotation = seed_settings.rotation + (ui_state.step_size * time.delta_seconds());
clear_flowers(&mut commands, flowers);
spawn_flowers(&mut commands, &mut meshes, &mut materials, seed_settings.deref());
commands.run_system(reset_seeds.single().0);
}

pub fn spawn_flowers(
commands: &mut Commands,
meshes: &mut ResMut<Assets<Mesh>>,
materials: &mut ResMut<Assets<ColorMaterial>>,
settings: &SeedSettings
fn spawn_flower_seeds(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
mut settings: ResMut<SeedSettings>,
flowers: Query<Entity, With<FlowerComponent>>
) {

for e in &flowers {
commands.entity(e).despawn_recursive();
}
if settings.mesh_handle.is_none() {
settings.mesh_handle = Some(Mesh2dHandle(meshes.add(Circle { radius: settings.radius })))
}
if settings.material_handle.is_none() {
settings.material_handle = Some(materials.add(settings.color));
}
for i in 1..settings.amount+1 {
let angle = 2.0 * PI * settings.rotation * (i as f32);
let radius = 5.0 * (i as f32).sqrt();
let x = angle.cos() * radius * settings.distance;
let y = angle.sin() * radius * settings.distance;

commands.spawn(MaterialMesh2dBundle {
mesh: Mesh2dHandle(meshes.add(Circle { radius: settings.radius })),
material: materials.add(settings.color),
mesh: settings.mesh_handle.clone().unwrap().clone(),
material: settings.material_handle.clone().unwrap(),
transform: Transform::from_xyz(x, y, 0.0),
..Default::default()
})
.insert(FlowerSeed);
.insert(FlowerSeed)
.insert(FlowerComponent);
}
}

/*
pub fn draw_connections(query: Query<&Transform, With<FlowerSeed>>, mut shapes: ShapeCommands, seed_settings: Res<SeedSettings>) {
let positions: Vec<Vec3> = query.iter().map(|t| t.translation).collect();
let mut trun_pos = positions.clone();
trun_pos.reverse();
trun_pos.truncate((seed_settings.amount as f32 / 1.6) as usize);
for i in 1..trun_pos.len() {
let start = trun_pos[i - 1];
shapes.line(start, nearest_point(start, &positions));
// gizmos.linestrip_2d(positions, color)
}
}
pub fn clear_flowers(
commands: &mut Commands,
flowers: Query<Entity, With<FlowerSeed>>
) {
for entity in flowers.iter() {
commands.entity(entity).despawn_recursive();
fn nearest_point(point: Vec3, points: &Vec<Vec3>) -> Vec3 {
let mut nearest = *points.first().unwrap();
for p in points {
if p != &point && p.distance(point) < nearest.distance(point) {
nearest = p.clone();
}
}
}
return nearest
}*/
17 changes: 14 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,27 @@ mod egui_block_input;
mod petal;

use bevy::{
prelude::*,
render::{
ecs::system::SystemId, prelude::*, render::{
settings::{Backends, RenderCreation, WgpuSettings},
RenderPlugin,
}, window::PresentMode,
}, window::PresentMode
};
use bevy_egui::EguiPlugin;
use bevy_inspector_egui::quick::WorldInspectorPlugin;
use bevy_pancam::PanCamPlugin;
use bevy_vector_shapes::Shape2dPlugin;
use egui_block_input::BlockInputPlugin;
use flower::FlowerSeedPlugin;
use petal::FlowerPetalPlugin;
use setup::SetupPlugin;
use ui::UiPlugin;

#[derive(Component)]
pub struct Callback(SystemId);

#[derive(Component)]
pub struct FlowerComponent;

fn main() {
App::new()
.add_plugins(DefaultPlugins
Expand All @@ -31,9 +39,12 @@ fn main() {
..default()
})
)
.add_plugins(Shape2dPlugin::default())
// .add_plugins(WorldInspectorPlugin::new())
.add_plugins(PanCamPlugin::default())
.add_plugins(UiPlugin)
.add_plugins(SetupPlugin)
.add_plugins(FlowerPetalPlugin)
.add_plugins(FlowerSeedPlugin)
.add_plugins(EguiPlugin)
.add_plugins(BlockInputPlugin)
Expand Down
Loading

0 comments on commit 8d45d69

Please sign in to comment.