use bevy::prelude::*;
#[cfg(feature = "inspector")]
use bevy_inspector_egui::prelude::*;
use crate::enums::{LeafBillboard, TreeType};
#[cfg(feature = "inspector")]
#[derive(Resource, Reflect, InspectorOptions, Debug, Clone, PartialEq)]
#[reflect(Resource, InspectorOptions)]
pub struct GlobalTreeMeshSettings {
pub tree_type: TreeType,
pub branch: BranchParams,
pub leaves: LeafParams,
}
#[cfg(feature = "inspector")]
#[derive(Component, Reflect, InspectorOptions, Debug, Clone, PartialEq)]
#[reflect(Component, InspectorOptions)]
pub struct TreeMeshSettings {
pub tree_type: TreeType,
pub branch: BranchParams,
pub leaves: LeafParams,
}
#[cfg(not(feature = "inspector"))]
#[derive(Resource, Reflect, Debug, Clone, PartialEq)]
#[reflect(Resource)]
pub struct GlobalTreeMeshSettings {
pub tree_type: TreeType,
pub branch: BranchParams,
pub leaves: LeafParams,
}
#[cfg(not(feature = "inspector"))]
#[derive(Component, Reflect, Debug, Clone, PartialEq)]
#[reflect(Component)]
pub struct TreeMeshSettings {
pub tree_type: TreeType,
pub branch: BranchParams,
pub leaves: LeafParams,
}
impl Default for GlobalTreeMeshSettings {
fn default() -> Self {
Self {
tree_type: TreeType::Deciduous,
branch: BranchParams::default(),
leaves: LeafParams::default(),
}
}
}
impl Default for TreeMeshSettings {
fn default() -> Self {
Self {
tree_type: TreeType::Deciduous,
branch: BranchParams::default(),
leaves: LeafParams::default(),
}
}
}
impl From<GlobalTreeMeshSettings> for TreeMeshSettings {
fn from(value: GlobalTreeMeshSettings) -> Self {
Self {
tree_type: value.tree_type,
branch: value.branch,
leaves: value.leaves,
}
}
}
#[derive(Reflect, Debug, Clone, PartialEq)]
pub struct BranchForce {
pub direction: Vec3,
pub strength: f32,
pub radius_cutoff: f32,
}
impl Default for BranchForce {
fn default() -> Self {
Self {
direction: Vec3 {
x: 0.0,
y: 1.0,
z: 0.0,
},
strength: 0.05,
radius_cutoff: 0.1,
}
}
}
#[derive(Reflect, Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum BranchRecursionLevel {
Zero = 0,
One = 1,
Two = 2,
Three = 3,
}
impl TryFrom<u8> for BranchRecursionLevel {
type Error = ();
fn try_from(v: u8) -> Result<Self, Self::Error> {
match v {
0 => Ok(BranchRecursionLevel::Zero),
1 => Ok(BranchRecursionLevel::One),
2 => Ok(BranchRecursionLevel::Two),
3 => Ok(BranchRecursionLevel::Three),
_ => Err(()),
}
}
}
impl From<BranchRecursionLevel> for u8 {
fn from(z: BranchRecursionLevel) -> u8 {
z as u8
}
}
impl From<BranchRecursionLevel> for usize {
fn from(z: BranchRecursionLevel) -> usize {
z as usize
}
}
impl From<BranchRecursionLevel> for f32 {
fn from(z: BranchRecursionLevel) -> f32 {
z as usize as f32
}
}
#[derive(Reflect, Debug, Clone, PartialEq)]
pub struct BranchParams {
pub levels: BranchRecursionLevel,
pub angle: [f32; 4],
pub children: [u8; 3],
pub force: BranchForce,
pub gnarliness: [f32; 4],
pub length: [f32; 4],
pub trunk_base_radius: f32,
pub radius_factor: [f32; 4],
pub sections: [u8; 4],
pub segments: [u8; 4],
pub start: [f32; 4],
pub taper: [f32; 4],
pub twist: [f32; 4],
}
impl Default for BranchParams {
fn default() -> Self {
Self {
levels: BranchRecursionLevel::Three,
angle: [0.0, 39.0, 39.0, 59.0],
children: [7, 4, 10],
force: BranchForce::default(),
gnarliness: [-0.05, 0.20, 0.16, 0.05],
length: [4.5, 2.9, 1.5, 0.45],
trunk_base_radius: 0.2,
radius_factor: [1.0, 0.5, 0.5, 0.5],
sections: [12, 8, 6, 4],
segments: [8, 6, 4, 3],
start: [0.0, 0.32, 0.4, 0.0],
taper: [0.95, 0.8, 0.85, 0.8],
twist: [0.09, -0.07, 0.0, 0.0],
}
}
}
#[derive(Reflect, Debug, Clone, PartialEq)]
pub struct LeafParams {
pub leaf_billboard: LeafBillboard,
pub angle: f32,
pub count: u32,
pub start: f32,
pub size: f32,
pub size_variance: f32,
}
impl Default for LeafParams {
fn default() -> Self {
Self {
leaf_billboard: LeafBillboard::Double,
angle: 35.0,
count: 3,
start: 0.25,
size: 0.25,
size_variance: 0.2,
}
}
}