circles partially working
This commit is contained in:
		
							parent
							
								
									7a543685d3
								
							
						
					
					
						commit
						a5a1be2392
					
				| @ -1,4 +1,4 @@ | |||||||
| use bevy::prelude::{Bundle, Color, Component, Name, Transform, Vec2}; | use bevy::prelude::{Bundle, Color, Component, Name, Quat, Transform, Vec2}; | ||||||
| use bevy_prototype_lyon::{ | use bevy_prototype_lyon::{ | ||||||
|     entity::ShapeBundle, |     entity::ShapeBundle, | ||||||
|     prelude::{DrawMode, FillMode, GeometryBuilder, Path, PathBuilder, ShapePath, StrokeMode}, |     prelude::{DrawMode, FillMode, GeometryBuilder, Path, PathBuilder, ShapePath, StrokeMode}, | ||||||
| @ -55,17 +55,49 @@ impl TurtleDrawLine { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| pub(crate) struct CircleAnimationLens { | pub(crate) struct CircleAnimationLens { | ||||||
|     start: Vec2, |     pub start_pos: Vec2, | ||||||
|     end: Vec2, |     pub center: Vec2, | ||||||
|  |     pub radii: Vec2, | ||||||
|  |     pub start: Angle, | ||||||
|  |     pub end: Angle, | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| impl Lens<Path> for CircleAnimationLens { | impl Lens<Path> for CircleAnimationLens { | ||||||
|     fn lerp(&mut self, target: &mut Path, ratio: f32) { |     fn lerp(&mut self, target: &mut Path, ratio: f32) { | ||||||
|         let line = shapes::Line(self.start, self.start + ((self.end - self.start) * ratio)); |         let mut path_builder = PathBuilder::new(); | ||||||
|  |         path_builder.move_to(self.start_pos); | ||||||
|  |         // The center point of the radius, then the radii in x and y direction, then the angle that will be drawn, then the x_rotation ?
 | ||||||
|  |         path_builder.arc( | ||||||
|  |             self.center, | ||||||
|  |             self.radii, | ||||||
|  |             (self.start.0 + ((self.end.0 - self.start.0) * ratio)).to_radians(), | ||||||
|  |             0., | ||||||
|  |         ); | ||||||
|  |         let line = path_builder.build(); | ||||||
|         *target = ShapePath::build_as(&line); |         *target = ShapePath::build_as(&line); | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | pub(crate) struct CircleMovementLens { | ||||||
|  |     pub center: Vec2, | ||||||
|  |     pub start: Transform, | ||||||
|  |     pub end: Angle, | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | impl Lens<Transform> for CircleMovementLens { | ||||||
|  |     fn lerp(&mut self, target: &mut Transform, ratio: f32) { | ||||||
|  |         let angle = self.end.0 * ratio; | ||||||
|  |         let mut rotated = self.start; | ||||||
|  | 
 | ||||||
|  |         rotated.rotate_around( | ||||||
|  |             self.center.extend(0.), | ||||||
|  |             Quat::from_rotation_z(angle.to_radians()), | ||||||
|  |         ); | ||||||
|  | 
 | ||||||
|  |         *target = rotated; | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
| #[derive(Bundle)] | #[derive(Bundle)] | ||||||
| 
 | 
 | ||||||
| pub(crate) struct TurtleDrawCircle { | pub(crate) struct TurtleDrawCircle { | ||||||
| @ -91,6 +123,18 @@ impl TurtleDrawCircle { | |||||||
|         path_builder.move_to(start); |         path_builder.move_to(start); | ||||||
|         // The center point of the radius, then the radii in x and y direction, then the angle that will be drawn, then the x_rotation ?
 |         // The center point of the radius, then the radii in x and y direction, then the angle that will be drawn, then the x_rotation ?
 | ||||||
|         path_builder.arc(center, radii, angle.0.to_radians(), 0.); |         path_builder.arc(center, radii, angle.0.to_radians(), 0.); | ||||||
|  | 
 | ||||||
|  |         /* println!("The radiuses: {}", radii);
 | ||||||
|  |         path_builder.move_to(Vec2::ZERO); | ||||||
|  |         path_builder.line_to(center); | ||||||
|  |         path_builder.line_to(Vec2::new(radii.x, 0.) + center); | ||||||
|  |         path_builder.move_to(center); | ||||||
|  |         path_builder.line_to(Vec2::new(0., radii.y) + center); | ||||||
|  |         path_builder.move_to(center); | ||||||
|  |         path_builder.line_to( | ||||||
|  |             center + Vec2::new(radii.x.abs(), 0.).rotate(Vec2::from_angle(angle.0.to_radians())), | ||||||
|  |         ); | ||||||
|  |         path_builder.move_to(center); */ | ||||||
|         let line = path_builder.build(); |         let line = path_builder.build(); | ||||||
|         println!("Draw Circle: {} {} {:?}", center, radii, angle); |         println!("Draw Circle: {} {} {:?}", center, radii, angle); | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -9,7 +9,7 @@ use bevy_tweening::{ | |||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| use crate::{ | use crate::{ | ||||||
|     primitives::{LineAnimationLens, TurtleDrawCircle, TurtleDrawLine}, |     primitives::{CircleAnimationLens, LineAnimationLens, TurtleDrawCircle, TurtleDrawLine}, | ||||||
|     turtle_shapes, |     turtle_shapes, | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| @ -41,27 +41,29 @@ impl Default for Turtle { | |||||||
|                 fill_color: Color::BLACK, |                 fill_color: Color::BLACK, | ||||||
|             }, |             }, | ||||||
|             commands: TurtleCommands::new(vec![ |             commands: TurtleCommands::new(vec![ | ||||||
|                 TurtleCommand::Forward(Length(100.)), |                 TurtleCommand::Forward(Length(-100.)), | ||||||
|                 TurtleCommand::Circle { |                 TurtleCommand::Circle { | ||||||
|                     radius: Length(150.), |                     radius: Length(150.), | ||||||
|                     angle: Angle(180.), |                     angle: Angle(30.), | ||||||
|                 }, |                 }, | ||||||
|                 TurtleCommand::Right(Angle(90.)), |                 TurtleCommand::Forward(Length(100.)), | ||||||
|                 TurtleCommand::Circle { |                 TurtleCommand::Circle { | ||||||
|                     radius: Length(15.), |                     radius: Length(70.), | ||||||
|                     angle: Angle(180.), |                     angle: Angle(60.), | ||||||
|                 }, |                 }, | ||||||
|                 TurtleCommand::Backward(Length(100.)), |                 TurtleCommand::Forward(Length(100.)), | ||||||
|  |                 TurtleCommand::Right(Angle(90.)), | ||||||
|  |                 //TurtleCommand::PenDown,
 | ||||||
|  |                 TurtleCommand::Circle { | ||||||
|  |                     radius: Length(30.), | ||||||
|  |                     angle: Angle(360. - 46.), | ||||||
|  |                 }, | ||||||
|  |                 /* TurtleCommand::Backward(Length(100.)),
 | ||||||
|                 TurtleCommand::Right(Angle(90.)), |                 TurtleCommand::Right(Angle(90.)), | ||||||
|                 TurtleCommand::Forward(Length(100.)), |                 TurtleCommand::Forward(Length(100.)), | ||||||
|                 TurtleCommand::Right(Angle(45.)), |                 TurtleCommand::Right(Angle(45.)), | ||||||
|                 //TurtleCommand::PenUp,
 |                 //TurtleCommand::PenUp,
 | ||||||
|                 TurtleCommand::Forward(Length(100.)), |                 TurtleCommand::Forward(Length(100.)), | ||||||
|                 //TurtleCommand::PenDown,
 |  | ||||||
|                 TurtleCommand::Circle { |  | ||||||
|                     radius: Length(-30.), |  | ||||||
|                     angle: Angle(90.), |  | ||||||
|                 }, |  | ||||||
|                 TurtleCommand::Right(Angle(90.)), |                 TurtleCommand::Right(Angle(90.)), | ||||||
|                 TurtleCommand::Forward(Length(50.)), |                 TurtleCommand::Forward(Length(50.)), | ||||||
|                 TurtleCommand::Right(Angle(90.)), |                 TurtleCommand::Right(Angle(90.)), | ||||||
| @ -75,7 +77,7 @@ impl Default for Turtle { | |||||||
|                 TurtleCommand::Left(Angle(120.)), |                 TurtleCommand::Left(Angle(120.)), | ||||||
|                 TurtleCommand::Forward(Length(100.)), |                 TurtleCommand::Forward(Length(100.)), | ||||||
|                 TurtleCommand::Right(Angle(150.)), |                 TurtleCommand::Right(Angle(150.)), | ||||||
|                 TurtleCommand::Forward(Length(100.)), |                 TurtleCommand::Forward(Length(100.)), */ | ||||||
|             ]), |             ]), | ||||||
|             name: Name::new("Turtle"), |             name: Name::new("Turtle"), | ||||||
|         } |         } | ||||||
| @ -274,15 +276,28 @@ fn draw_lines( | |||||||
|                             start, |                             start, | ||||||
|                             end, |                             end, | ||||||
|                         } => { |                         } => { | ||||||
|                             // let circle_animator = todo!();
 |                             let line_animator = Animator::new(Tween::new( | ||||||
|                             commands.spawn_bundle(TurtleDrawCircle::new( |                                 EaseFunction::QuadraticInOut, | ||||||
|  |                                 TweeningType::Once, | ||||||
|  |                                 Duration::from_millis(500), | ||||||
|  |                                 CircleAnimationLens { | ||||||
|  |                                     start_pos: start, | ||||||
|                                     center, |                                     center, | ||||||
|                                     radii, |                                     radii, | ||||||
|                                 angle, |                                     start: Angle(0.), | ||||||
|  |                                     end: angle, | ||||||
|  |                                 }, | ||||||
|  |                             )); | ||||||
|  |                             commands | ||||||
|  |                                 .spawn_bundle(TurtleDrawCircle::new( | ||||||
|  |                                     center, | ||||||
|  |                                     radii, | ||||||
|  |                                     Angle(0.), | ||||||
|                                     tcmd.state.index, |                                     tcmd.state.index, | ||||||
|                                     start, |                                     start, | ||||||
|                                     end, |                                     end, | ||||||
|                             )); |                                 )) | ||||||
|  |                                 .insert(line_animator); | ||||||
|                         } |                         } | ||||||
|                     } |                     } | ||||||
|                     return; |                     return; | ||||||
|  | |||||||
| @ -1,12 +1,15 @@ | |||||||
| use std::{f32::consts::PI, time::Duration}; | use std::{f32::consts::PI, time::Duration}; | ||||||
| 
 | 
 | ||||||
| use bevy::prelude::{Transform, Vec2}; | use bevy::prelude::{Quat, Transform, Vec2, Vec3}; | ||||||
| use bevy_tweening::{ | use bevy_tweening::{ | ||||||
|     lens::{TransformPositionLens, TransformRotateZLens}, |     lens::{TransformPositionLens, TransformRotateAxisLens, TransformRotateZLens}, | ||||||
|     EaseFunction, Tween, TweeningType, |     EaseFunction, Tween, TweeningType, | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| use crate::turtle::{Angle, TurtleGraphElement, TurtleState}; | use crate::{ | ||||||
|  |     primitives::{CircleAnimationLens, CircleMovementLens}, | ||||||
|  |     turtle::{Angle, TurtleGraphElement, TurtleState}, | ||||||
|  | }; | ||||||
| 
 | 
 | ||||||
| pub fn turtle_turn( | pub fn turtle_turn( | ||||||
|     state: &mut TurtleState, |     state: &mut TurtleState, | ||||||
| @ -66,10 +69,12 @@ pub fn turtle_circle( | |||||||
|     angle: Angle, |     angle: Angle, | ||||||
| ) -> (Option<Tween<Transform>>, Option<TurtleGraphElement>) { | ) -> (Option<Tween<Transform>>, Option<TurtleGraphElement>) { | ||||||
|     let radius_tuple = Vec2::ONE * radius.abs(); |     let radius_tuple = Vec2::ONE * radius.abs(); | ||||||
|     dbg!(state.start, radius_tuple); |     let left_right = if radius >= 0. { 90f32 } else { -90. }; | ||||||
|  |     println!("Heading: {}", state.heading); | ||||||
|     let center = state.start |     let center = state.start | ||||||
|         + (Vec2::new(radius, 0.).rotate(Vec2::from_angle((state.heading + 90.).to_radians()))); |         + (Vec2::new(radius.abs(), 0.) | ||||||
|     dbg!(center); |             .rotate(Vec2::from_angle(state.heading + left_right.to_radians()))); | ||||||
|  | 
 | ||||||
|     let turtle_movement_animation = Tween::new( |     let turtle_movement_animation = Tween::new( | ||||||
|         // accelerate and decelerate
 |         // accelerate and decelerate
 | ||||||
|         EaseFunction::QuadraticInOut, |         EaseFunction::QuadraticInOut, | ||||||
| @ -77,9 +82,14 @@ pub fn turtle_circle( | |||||||
|         // later to be controlled by speed
 |         // later to be controlled by speed
 | ||||||
|         Duration::from_millis(500), |         Duration::from_millis(500), | ||||||
|         // set the start and end of the animation
 |         // set the start and end of the animation
 | ||||||
|         TransformPositionLens { |         CircleMovementLens { | ||||||
|             start: center.extend(0.), |             start: Transform { | ||||||
|             end: center.extend(0.), |                 translation: state.start.extend(0.), | ||||||
|  |                 rotation: Quat::from_rotation_z(state.heading), | ||||||
|  |                 scale: Vec3::ONE, | ||||||
|  |             }, | ||||||
|  |             end: angle, | ||||||
|  |             center, | ||||||
|         }, |         }, | ||||||
|     ) |     ) | ||||||
|     .with_completed_event(state.index as u64); |     .with_completed_event(state.index as u64); | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user