first square

This commit is contained in:
Dietrich 2022-08-06 10:28:12 +02:00
parent ca690c2864
commit 54a1f64e60
2 changed files with 38 additions and 20 deletions

View File

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

View File

@ -1,3 +1,5 @@
use std::f32::consts::PI;
use bevy::prelude::Vec2; use bevy::prelude::Vec2;
use bevy_prototype_lyon::prelude::{Path, PathBuilder}; 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.line_to(Vec2::new(1.0, -1.0)); turtle_path.line_to(Vec2::new(1.0, -1.0));
turtle_path.close(); 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 { 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.close();
turtle_path.build() turtle_path.build()