lots of new files

This commit is contained in:
2026-03-04 10:20:13 +00:00
parent b70b9fc63d
commit 7b1d9a7e04
7 changed files with 221 additions and 1 deletions

21
src/components.rs Normal file
View File

@@ -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<ColorMaterial>);
#[derive(Clone, Component)]
pub struct MeshHandle(pub Handle<Mesh>);

33
src/draw.rs Normal file
View File

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

View File

@@ -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<MaterialHandles>,
mesh_handles: Res<MeshHandles>,
) {
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);
}

30
src/material.rs Normal file
View File

@@ -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<Handle<ColorMaterial>>,
}
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<Assets<ColorMaterial>>) {
let color_handles = COLORS
.iter()
.map(|&color| materials.add(ColorMaterial::from_color(color)))
.collect();
commands.insert_resource(MaterialHandles {
color_material_handles: color_handles,
});
}

1
src/menu.rs Normal file
View File

@@ -0,0 +1 @@
// TODO: Make a menu for my game

31
src/mesh.rs Normal file
View File

@@ -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<Handle<Mesh>>,
}
pub fn create_meshes(mut commands: Commands, mut meshes: ResMut<Assets<Mesh>>) {
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,
});
}

61
src/node.rs Normal file
View File

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