Compare commits

..

No commits in common. "1d1787b8c0bedb177e7813d28e34cd97ffa0e600" and "2836b2926d5afcb92c1a8162a349da5fc7ea0ce8" have entirely different histories.

3 changed files with 95 additions and 171 deletions

View File

@ -1,6 +1,5 @@
mod debug;
mod turtle;
mod turtle_movement;
mod turtle_shapes;
use bevy::{prelude::*, window::close_on_esc};

View File

@ -1,14 +1,14 @@
use std::time::Duration;
use std::{f32::consts::PI, time::Duration};
use bevy::prelude::*;
use bevy_inspector_egui::{Inspectable, RegisterInspectable};
use bevy_prototype_lyon::{prelude::*, shapes::Line};
use bevy_prototype_lyon::prelude::*;
use bevy_tweening::{
lens::TransformPositionLens, Animator, EaseFunction, Sequence, Tween, TweenCompleted,
TweeningPlugin, TweeningType,
lens::{TransformPositionLens, TransformRotateZLens},
Animator, EaseFunction, Sequence, Tween, TweeningPlugin, TweeningType,
};
use crate::{turtle_movement::turtle_move, turtle_shapes};
use crate::turtle_shapes;
pub struct TurtlePlugin;
@ -17,7 +17,6 @@ impl Plugin for TurtlePlugin {
app.add_plugin(TweeningPlugin)
.add_startup_system(setup)
.add_system(keypresses)
.add_system(draw_lines)
.register_inspectable::<Colors>()
.register_inspectable::<TurtleCommands>();
}
@ -26,7 +25,6 @@ impl Plugin for TurtlePlugin {
pub struct Turtle {
colors: Colors,
commands: TurtleCommands,
name: Name,
}
impl Default for Turtle {
@ -36,30 +34,24 @@ impl Default for Turtle {
color: Color::DARK_GRAY,
fill_color: Color::BLACK,
},
commands: TurtleCommands::new(vec![
commands: TurtleCommands(vec![
TurtleCommand::Forward(Length(100.)),
TurtleCommand::Right(Angle(90.)),
TurtleCommand::Backward(Length(100.)),
TurtleCommand::Right(Angle(90.)),
TurtleCommand::Left(Angle(90.)),
TurtleCommand::Forward(Length(100.)),
TurtleCommand::Right(Angle(45.)),
TurtleCommand::Left(Angle(90.)),
TurtleCommand::Forward(Length(100.)),
TurtleCommand::Right(Angle(90.)),
TurtleCommand::Forward(Length(50.)),
TurtleCommand::Right(Angle(90.)),
TurtleCommand::Left(Angle(90.)),
TurtleCommand::Forward(Length(100.)),
TurtleCommand::Right(Angle(90.)),
TurtleCommand::Forward(Length(50.)),
TurtleCommand::Right(Angle(45.)),
TurtleCommand::Forward(Length(100.)),
TurtleCommand::Left(Angle(120.)),
TurtleCommand::Forward(Length(100.)),
TurtleCommand::Left(Angle(120.)),
TurtleCommand::Forward(Length(100.)),
TurtleCommand::Right(Angle(150.)),
TurtleCommand::Forward(Length(100.)),
]),
name: Name::new("Turtle"),
TurtleCommand::Left(Angle(90.)),
]), /*
shape: TurtleShape(GeometryBuilder::build_as(
&turtle_shapes::turtle(),
DrawMode::Outlined {
fill_mode: FillMode::color(Color::MIDNIGHT_BLUE),
outline_mode: StrokeMode::new(Color::BLACK, 1.0),
},
Default::default(),
)), */
}
}
}
@ -75,60 +67,61 @@ impl Turtle {
&self.colors
}
pub fn forward(&mut self) -> &mut Self {
self.commands
.commands
.push(TurtleCommand::Forward(Length(100.0)));
self.commands.0.push(TurtleCommand::Forward(Length(100.0)));
self
}
}
#[derive(Component, Inspectable)]
pub struct TurtleCommands {
commands: Vec<TurtleCommand>,
lines: Vec<TurtleGraphElement>,
}
pub struct TurtleCommands(Vec<TurtleCommand>);
impl TurtleCommands {
fn new(commands: Vec<TurtleCommand>) -> Self {
Self {
commands,
lines: vec![],
}
}
}
impl TurtleCommands {
fn generate_tweenable(&mut self) -> Sequence<Transform> {
self.lines.clear();
let mut seq = Sequence::with_capacity(self.commands.len());
fn generate_tweenable(&self) -> Sequence<Transform> {
let mut seq = Sequence::with_capacity(self.0.len());
let mut pos = Vec2::ZERO;
let mut ori: f32 = 0.;
for (index, op) in self.commands.iter().enumerate() {
for op in &self.0 {
match op {
TurtleCommand::Forward(Length(x)) => {
let (target, animation, line) = turtle_move(pos, ori, *x as f32, index as f32);
self.lines.push(line);
seq = seq.then(animation);
pos = target;
}
TurtleCommand::Backward(Length(x)) => {
let (target, animation, line) = turtle_move(pos, ori, -*x as f32, index as f32);
self.lines.push(line);
seq = seq.then(animation);
pos = target;
println!("Adding Forward");
let start = pos;
let end = pos + (Vec2::from_angle(ori) * *x as f32);
seq = seq.then(Tween::new(
// accelerate and decelerate
EaseFunction::QuadraticInOut,
// Loop animation back and forth.
TweeningType::Once,
// later to be controlled by speed
Duration::from_secs(1),
// set the start and end of the animation
TransformPositionLens {
start: start.extend(0.),
end: end.extend(0.),
},
));
pos = end;
}
TurtleCommand::Backward(_) => todo!(),
TurtleCommand::Left(Angle(x)) => {
let (nori, tween, line) = crate::turtle_movement::turtle_turn(ori, *x as f32);
ori = nori;
seq = seq.then(tween);
self.lines.push(line);
}
TurtleCommand::Right(Angle(x)) => {
let (nori, tween, line) = crate::turtle_movement::turtle_turn(ori, -*x as f32);
ori = nori;
seq = seq.then(tween);
self.lines.push(line);
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,
// Loop animation back and forth.
TweeningType::Once,
// Animation time (one way only; for ping-pong it takes 2 seconds
// to come back to start).
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.
TransformRotateZLens { start, end },
));
ori = end % (2. * PI);
}
TurtleCommand::Right(_) => todo!(),
TurtleCommand::PenUp => todo!(),
TurtleCommand::PenDown => todo!(),
TurtleCommand::Circle => todo!(),
@ -139,16 +132,6 @@ impl TurtleCommands {
}
}
#[derive(Inspectable, Default)]
pub enum TurtleGraphElement {
TurtleLine {
start: Vec2,
end: Vec2,
},
#[default]
Noop,
}
#[derive(Clone, Component, Inspectable)]
pub struct TurtleShape;
@ -164,7 +147,7 @@ pub struct Length(f64);
pub struct Angle(f64);
#[derive(Component, Inspectable, Default)]
pub enum TurtleCommand {
enum TurtleCommand {
Forward(Length),
Backward(Length),
Left(Angle),
@ -204,46 +187,48 @@ fn setup(mut commands: Commands) {
},
Transform::identity(),
))
.insert(animator)
.insert(TurtleShape);
}
fn draw_lines(
mut commands: Commands,
tcmd: Query<&TurtleCommands>,
mut query_event: EventReader<TweenCompleted>, // TODO: howto attach only to the right event?
) {
for ev in query_event.iter() {
let index = ev.user_data;
for t in tcmd.iter() {
let t = t.lines.get(index as usize).unwrap();
match t {
TurtleGraphElement::TurtleLine { start, end } => {
commands.spawn_bundle(GeometryBuilder::build_as(
&Line(*start, *end),
DrawMode::Outlined {
fill_mode: FillMode::color(Color::MIDNIGHT_BLUE),
outline_mode: StrokeMode::new(Color::BLACK, 1.0),
},
Transform::identity(),
));
}
TurtleGraphElement::Noop => (),
}
}
}
.insert(animator);
}
/// The sprite is animated by changing its translation depending on the time that has passed since
/// the last frame.
fn keypresses(
//time: Res<Time>,
keys: Res<Input<KeyCode>>,
mut qry: Query<&mut Animator<Transform>, With<TurtleShape>>,
mut tcmd: Query<&mut TurtleCommands>,
mut commands: Commands,
mut qry: Query<&mut Animator<Transform>>,
tcmd: Query<&TurtleCommands>,
) {
if keys.just_pressed(KeyCode::W) {
let mut tcmd = tcmd.single_mut();
let tcmd = tcmd.single();
let c = tcmd.generate_tweenable();
let mut shap = qry.single_mut();
shap.set_tweenable(c);
/* commands
.spawn_bundle(Turtle::default())
.insert_bundle(GeometryBuilder::build_as(
&turtle_shapes::turtle(),
DrawMode::Outlined {
fill_mode: FillMode::color(Color::RED),
outline_mode: StrokeMode::new(Color::BLACK, 1.0),
},
Transform::from_translation(Vec3::new(-100., 0., 0.)),
))
.insert(Animator::new(Tween::new(
// Use a quadratic easing on both endpoints.
EaseFunction::QuadraticInOut,
// Loop animation back and forth.
TweeningType::PingPong,
// Animation time (one way only; for ping-pong it takes 2 seconds
// to come back to start).
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.
TransformPositionLens {
start: Vec3::new(-100., 0., 0.),
end: Vec3::new(-140., 40., 0.),
},
))); */
}
}

View File

@ -1,60 +0,0 @@
use std::{f32::consts::PI, time::Duration};
use bevy::prelude::{Transform, Vec2};
use bevy_tweening::{
lens::{TransformPositionLens, TransformRotateZLens},
EaseFunction, Tween, TweeningType,
};
use crate::turtle::TurtleGraphElement;
pub fn turtle_turn(
orientation: f32,
angle_to_turn: f32,
) -> (f32, Tween<Transform>, TurtleGraphElement) {
let start = orientation;
let end = orientation + (angle_to_turn * PI / 180.);
let animation = Tween::new(
// Use a quadratic easing on both endpoints.
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).
Duration::from_millis(500),
// 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, end },
);
let line = TurtleGraphElement::Noop;
let orientation = end % (2. * PI);
(orientation, animation, line)
}
pub fn turtle_move(
position: Vec2,
orientation: f32,
length: f32,
index: f32,
) -> (Vec2, Tween<Transform>, TurtleGraphElement) {
let start = position;
let end = position + (Vec2::from_angle(orientation) * length);
let turtle_movement_animation = Tween::new(
// accelerate and decelerate
EaseFunction::QuadraticInOut,
// Loop animation back and forth.
TweeningType::Once,
// later to be controlled by speed
Duration::from_millis(500),
// set the start and end of the animation
TransformPositionLens {
start: start.extend(0.),
end: end.extend(0.),
},
)
.with_completed_event(index as u64);
let line = TurtleGraphElement::TurtleLine { start, end };
(end, turtle_movement_animation, line)
}