diff --git a/src/turtle.rs b/src/turtle.rs index de5c7ff..0195ee4 100644 --- a/src/turtle.rs +++ b/src/turtle.rs @@ -1,6 +1,7 @@ use std::{f32::consts::PI, time::Duration}; use bevy::prelude::*; +use bevy_inspector_egui::{egui::plot::Orientation, Inspectable, RegisterInspectable}; use bevy_prototype_lyon::{entity::ShapeBundle, prelude::*}; use bevy_tweening::{ lens::{TransformPositionLens, TransformRotateXLens, TransformRotateZLens, TransformScaleLens}, @@ -15,7 +16,9 @@ impl Plugin for TurtlePlugin { fn build(&self, app: &mut bevy::prelude::App) { app.add_plugin(TweeningPlugin) .add_startup_system(setup) - .add_system(keypresses); + .add_system(keypresses) + .register_inspectable::() + .register_inspectable::(); } } #[derive(Bundle)] @@ -34,6 +37,12 @@ impl Default for Turtle { commands: TurtleCommands(vec![ TurtleCommand::Forward(Length(100.)), TurtleCommand::Left(Angle(90.)), + TurtleCommand::Forward(Length(100.)), + TurtleCommand::Left(Angle(90.)), + TurtleCommand::Forward(Length(100.)), + TurtleCommand::Left(Angle(90.)), + TurtleCommand::Forward(Length(100.)), + TurtleCommand::Left(Angle(90.)), ]), /* shape: TurtleShape(GeometryBuilder::build_as( &turtle_shapes::turtle(), @@ -63,36 +72,40 @@ impl Turtle { } } -#[derive(Component)] +#[derive(Component, Inspectable)] pub struct TurtleCommands(Vec); impl TurtleCommands { fn generate_tweenable(&self) -> Sequence { let mut seq = Sequence::with_capacity(self.0.len()); + let mut pos = Vec2::ZERO; + let mut ori: f32 = 0.; for op in &self.0 { match op { TurtleCommand::Forward(Length(x)) => { println!("Adding Forward"); + let start = pos; + let end = pos + (Vec2::from_angle(ori) * *x as f32); seq = seq.then(Tween::new( - // Use a quadratic easing on both endpoints. + // accelerate and decelerate EaseFunction::QuadraticInOut, // Loop animation back and forth. TweeningType::Once, - // Animation time (one way only; for ping-pong it takes 2 seconds - // to come back to start). + // later to be controlled by speed Duration::from_secs(1), - // The lens gives access to the Transform component of the Entity, - // for the Animator to animate it. It also contains the start and - // end values respectively associated with the progress ratios 0. and 1. + // set the start and end of the animation TransformPositionLens { - start: Vec3::ZERO, - end: Vec3::new(*x as f32, 40., 0.), + start: start.extend(0.), + end: end.extend(0.), }, )); + pos = end; } TurtleCommand::Backward(_) => todo!(), TurtleCommand::Left(Angle(x)) => { println!("Adding Left"); + let start = ori; + let end = ori + (*x as f32 * PI / 180.); seq = seq.then(Tween::new( // Use a quadratic easing on both endpoints. EaseFunction::QuadraticInOut, @@ -104,35 +117,36 @@ impl TurtleCommands { // The lens gives access to the Transform component of the Entity, // for the Animator to animate it. It also contains the start and // end values respectively associated with the progress ratios 0. and 1. - TransformRotateZLens { - start: *x as f32 * (PI / 180.), - end: -*x as f32 * (PI / 180.), - }, + TransformRotateZLens { start, end }, )); + ori = end % (2. * PI); } TurtleCommand::Right(_) => todo!(), TurtleCommand::PenUp => todo!(), TurtleCommand::PenDown => todo!(), TurtleCommand::Circle => todo!(), + TurtleCommand::Pause => todo!(), } } seq } } -#[derive(Clone, Component)] +#[derive(Clone, Component, Inspectable)] pub struct TurtleShape; -#[derive(Clone, Component)] +#[derive(Clone, Component, Inspectable)] pub struct Colors { color: Color, fill_color: Color, } +#[derive(Inspectable, Default)] pub struct Length(f64); +#[derive(Inspectable, Default)] pub struct Angle(f64); -#[derive(Component)] +#[derive(Component, Inspectable, Default)] enum TurtleCommand { Forward(Length), Backward(Length), @@ -140,6 +154,8 @@ enum TurtleCommand { Right(Angle), PenUp, PenDown, + #[default] + Pause, Circle, } @@ -173,7 +189,7 @@ fn setup(mut commands: Commands) { fill_mode: FillMode::color(Color::MIDNIGHT_BLUE), outline_mode: StrokeMode::new(Color::BLACK, 1.0), }, - Default::default(), + Transform::identity(), )) .insert(animator); } diff --git a/src/turtle_shapes.rs b/src/turtle_shapes.rs index 6c09675..45fc2b7 100644 --- a/src/turtle_shapes.rs +++ b/src/turtle_shapes.rs @@ -1,3 +1,5 @@ +use std::f32::consts::PI; + use bevy::prelude::Vec2; use bevy_prototype_lyon::prelude::{Path, PathBuilder}; @@ -33,9 +35,9 @@ pub fn turtle() -> Path { turtle_path.line_to(Vec2::new(-1.0, -1.0)); turtle_path.line_to(Vec2::new(1.0, -1.0)); turtle_path.close(); - turtle_path.move_to(Vec2::new(0.0, 16.0)); + turtle_path.move_to(Vec2::new(0.0, 16.0).rotate(Vec2::from_angle(-PI / 2.))); for coord in polygon { - turtle_path.line_to(Vec2::from_array(*coord)); + turtle_path.line_to(Vec2::from_array(*coord).rotate(Vec2::from_angle(-PI / 2.))); } turtle_path.close(); turtle_path.build()