From 7b1d9a7e04b7922a2297d334183fedcf46c0011e Mon Sep 17 00:00:00 2001 From: Adam French Date: Wed, 4 Mar 2026 10:20:13 +0000 Subject: [PATCH] lots of new files --- src/components.rs | 21 ++++++++++++++++ src/draw.rs | 33 +++++++++++++++++++++++++ src/main.rs | 45 +++++++++++++++++++++++++++++++++- src/material.rs | 30 +++++++++++++++++++++++ src/menu.rs | 1 + src/mesh.rs | 31 ++++++++++++++++++++++++ src/node.rs | 61 +++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 221 insertions(+), 1 deletion(-) create mode 100644 src/components.rs create mode 100644 src/draw.rs create mode 100644 src/material.rs create mode 100644 src/menu.rs create mode 100644 src/mesh.rs create mode 100644 src/node.rs diff --git a/src/components.rs b/src/components.rs new file mode 100644 index 0000000..663219c --- /dev/null +++ b/src/components.rs @@ -0,0 +1,21 @@ +use bevy::prelude::*; + +pub struct ComponentPlugin; + +#[derive(Component)] +pub struct Position(pub Vec3); + +#[derive(Component)] +pub struct Name(pub String); + +#[derive(Component)] +pub struct Scale(pub Vec3); + +#[derive(Component)] +pub struct Rotation(pub Quat); + +#[derive(Clone, Component)] +pub struct ColorHandle(pub Handle); + +#[derive(Clone, Component)] +pub struct MeshHandle(pub Handle); diff --git a/src/draw.rs b/src/draw.rs new file mode 100644 index 0000000..70ca241 --- /dev/null +++ b/src/draw.rs @@ -0,0 +1,33 @@ +use crate::components::{ColorHandle, MeshHandle, Position, Rotation, Scale}; +use bevy::prelude::*; + +pub struct DrawPlugin; +impl Plugin for DrawPlugin { + fn build(&self, app: &mut App) { + app.add_systems(Update, draw); + } +} + +pub fn draw( + mut commands: Commands, + parents: Query<( + Entity, + &MeshHandle, + &ColorHandle, + &Position, + &Scale, + &Rotation, + )>, +) { + for (_, shape, color, position, scale, rotation) in parents { + commands.spawn(( + Mesh2d(shape.0.clone()), + MeshMaterial2d(color.0.clone()), + Transform { + translation: position.0, + rotation: rotation.0, + scale: scale.0, + }, + )); + } +} diff --git a/src/main.rs b/src/main.rs index e7a11a9..5d2e52a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,46 @@ +use bevy::prelude::*; + +use crate::components::{ColorHandle, MeshHandle, Name, Position, Rotation, Scale}; +use crate::draw::DrawPlugin; +use crate::material::{MaterialHandles, MaterialPlugin}; +use crate::mesh::{MeshHandles, MeshPlugin}; + +use crate::node::TreeNodeBundle; + +mod components; +mod draw; +mod material; +mod mesh; +mod node; + +/* The bevy App + * + * The app stores three fields: + * 1. World - Stores game data + * 2. Schedule - Holds systems that operate on this data + * 3. Runner - Interprets schedule to control execution strategy + */ + fn main() { - println!("Hello, world!"); + App::new() + .insert_resource(ClearColor(Color::srgb(0.05, 0.05, 0.1))) + .add_plugins((DefaultPlugins, DrawPlugin, MeshPlugin, MaterialPlugin)) + .add_systems(PostStartup, setup) + .run(); +} + +fn setup( + mut commands: Commands, + material_handles: Res, + mesh_handles: Res, +) { + let pos = Position(Vec3::new(0.0, 0.0, 0.0)); + let scale = Scale(Vec3::new(20.0, 20.0, 20.0)); + let rotation = Rotation(Quat::from_euler(EulerRot::ZYX, 0.0, 0.0, 0.0)); + let name = Name(String::from("Hello world")); + let mat = ColorHandle(material_handles.color_material_handles[0].clone()); + let msh = MeshHandle(mesh_handles.shapes[1].clone()); + + commands.spawn(TreeNodeBundle::new(pos, name, msh, mat, rotation, scale)); + commands.spawn(Camera2d); } diff --git a/src/material.rs b/src/material.rs new file mode 100644 index 0000000..01ba765 --- /dev/null +++ b/src/material.rs @@ -0,0 +1,30 @@ +use bevy::color::palettes::basic::*; +use bevy::prelude::*; + +pub struct MaterialPlugin; +impl Plugin for MaterialPlugin { + fn build(&self, app: &mut App) { + app.add_systems(Startup, create_materials); + } +} + +#[derive(Resource)] +pub struct MaterialHandles { + pub color_material_handles: Vec>, +} + +pub const COLORS: [Srgba; 16] = [ + AQUA, BLACK, BLUE, FUCHSIA, GRAY, GREEN, LIME, MAROON, NAVY, OLIVE, PURPLE, RED, SILVER, TEAL, + WHITE, YELLOW, +]; + +pub fn create_materials(mut commands: Commands, mut materials: ResMut>) { + let color_handles = COLORS + .iter() + .map(|&color| materials.add(ColorMaterial::from_color(color))) + .collect(); + + commands.insert_resource(MaterialHandles { + color_material_handles: color_handles, + }); +} diff --git a/src/menu.rs b/src/menu.rs new file mode 100644 index 0000000..a91699f --- /dev/null +++ b/src/menu.rs @@ -0,0 +1 @@ +// TODO: Make a menu for my game diff --git a/src/mesh.rs b/src/mesh.rs new file mode 100644 index 0000000..d6545e7 --- /dev/null +++ b/src/mesh.rs @@ -0,0 +1,31 @@ +use bevy::mesh::{CircleMeshBuilder, Segment2dMeshBuilder}; +use bevy::prelude::*; + +pub struct MeshPlugin; +impl Plugin for MeshPlugin { + fn build(&self, app: &mut App) { + app.add_systems(Startup, create_meshes); + } +} + +#[derive(Resource)] +pub struct MeshHandles { + pub shapes: Vec>, +} + +pub fn create_meshes(mut commands: Commands, mut meshes: ResMut>) { + let mut shape_mesh_handle = vec![]; + + let line_handle = meshes.add(Segment2dMeshBuilder::new(Segment2d { + vertices: [Vec2 { x: 0.0, y: 0.0 }, Vec2 { x: 1.0, y: 0.0 }], + })); + + let circle_handle = meshes.add(CircleMeshBuilder::new(1.0, 20)); + + shape_mesh_handle.push(line_handle); + shape_mesh_handle.push(circle_handle); + + commands.insert_resource(MeshHandles { + shapes: shape_mesh_handle, + }); +} diff --git a/src/node.rs b/src/node.rs new file mode 100644 index 0000000..8f9d8eb --- /dev/null +++ b/src/node.rs @@ -0,0 +1,61 @@ +use crate::components::{ColorHandle, MeshHandle, Name, Position, Rotation, Scale}; +use bevy::prelude::*; + +#[derive(Component)] +pub struct Edge; + +#[derive(Component)] +pub struct TreeNode; + +#[derive(Bundle)] +pub struct TreeNodeBundle { + pub node: TreeNode, + pub name: Name, + pub color: ColorHandle, + pub shape: MeshHandle, + pub position: Position, + pub rotation: Rotation, + pub scale: Scale, +} + +impl TreeNodeBundle { + pub fn new( + position: Position, + name: Name, + shape: MeshHandle, + color: ColorHandle, + rotation: Rotation, + scale: Scale, + ) -> TreeNodeBundle { + TreeNodeBundle { + node: TreeNode, + color, + shape, + position, + name, + rotation, + scale, + } + } +} + +#[derive(Bundle)] +pub struct EdgeBundle { + pub edge: Edge, + pub name: Name, + pub color: ColorHandle, + pub shape: MeshHandle, + pub scale: Scale, +} + +impl EdgeBundle { + pub fn new(name: Name, shape: MeshHandle, color: ColorHandle, scale: Scale) -> EdgeBundle { + EdgeBundle { + edge: Edge, + name, + color, + shape, + scale, + } + } +}