From f5aae7efe16a136406bef18cf5d419ac0f555ac6 Mon Sep 17 00:00:00 2001 From: Dietrich Date: Wed, 7 Dec 2022 13:54:15 +0100 Subject: [PATCH] Add a builder for the turtle plans --- src/builders.rs | 61 ++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + src/turtle_bundle.rs | 8 ++++-- 3 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 src/builders.rs diff --git a/src/builders.rs b/src/builders.rs new file mode 100644 index 0000000..b7cc503 --- /dev/null +++ b/src/builders.rs @@ -0,0 +1,61 @@ +use crate::{ + commands::{DrawElement, TurtleSegment}, + general::{angle::Angle, length::Length, Precision}, +}; + +#[derive(Default, Debug)] +pub struct TurtlePlan { + commands: Vec, +} + +pub trait WithCommands { + fn get_mut_commands(&mut self) -> &mut Vec; + fn get_commands(self) -> Vec; +} + +impl WithCommands for TurtlePlan { + fn get_mut_commands(&mut self) -> &mut Vec { + &mut self.commands + } + + fn get_commands(self) -> Vec { + self.commands + } +} + +impl TurtlePlan { + pub fn new() -> TurtlePlan { + TurtlePlan { commands: vec![] } + } + pub fn forward(&mut self, length: Length) { + self.get_mut_commands() + .push(TurtleSegment::Single(DrawElement::Draw( + crate::commands::MoveCommand::Forward(length), + ))) + } + pub fn backward(&mut self, length: Precision) { + self.get_mut_commands() + .push(TurtleSegment::Single(DrawElement::Draw( + crate::commands::MoveCommand::Backward(Length(length)), + ))) + } +} + +pub trait Turnable: WithCommands { + fn right(&mut self, angle: Angle) -> &mut Self { + self.get_mut_commands() + .push(TurtleSegment::Single(DrawElement::Orient( + crate::commands::OrientationCommand::Right(angle), + ))); + self + } + fn left(&mut self, angle: Angle) -> &mut Self { + self.get_mut_commands() + .push(TurtleSegment::Single(DrawElement::Orient( + crate::commands::OrientationCommand::Left(angle), + ))); + self + } +} + +impl Turnable for TurtlePlan {} diff --git a/src/lib.rs b/src/lib.rs index 11d9f11..61d0ea0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,6 +13,7 @@ use turtle_bundle::{AnimatedTurtle, TurtleBundle}; pub use commands::TurtleCommands; +pub mod builders; mod commands; mod debug; mod drawing; diff --git a/src/turtle_bundle.rs b/src/turtle_bundle.rs index 9f8ee99..b5bea0f 100644 --- a/src/turtle_bundle.rs +++ b/src/turtle_bundle.rs @@ -7,6 +7,7 @@ use bevy_prototype_lyon::{ }; use crate::{ + builders::{TurtlePlan, WithCommands}, commands::{DrawElement, MoveCommand, TurtleCommands, TurtleSegment}, general::length::Length, shapes::{self, TurtleColors}, @@ -39,8 +40,11 @@ impl Default for TurtleBundle { } impl TurtleBundle { - pub fn set_commands(&mut self, commands: Vec) { - self.commands = TurtleCommands::new(commands); + pub fn apply_plan(&mut self, plan: TurtlePlan) { + self.commands = TurtleCommands::new(plan.get_commands()); + } + pub fn create_plan(&self) -> TurtlePlan { + TurtlePlan::new() } }