Add a builder for the turtle plans
This commit is contained in:
parent
11b9539800
commit
f5aae7efe1
61
src/builders.rs
Normal file
61
src/builders.rs
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
use crate::{
|
||||||
|
commands::{DrawElement, TurtleSegment},
|
||||||
|
general::{angle::Angle, length::Length, Precision},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Default, Debug)]
|
||||||
|
pub struct TurtlePlan {
|
||||||
|
commands: Vec<TurtleSegment>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait WithCommands {
|
||||||
|
fn get_mut_commands(&mut self) -> &mut Vec<TurtleSegment>;
|
||||||
|
fn get_commands(self) -> Vec<TurtleSegment>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl WithCommands for TurtlePlan {
|
||||||
|
fn get_mut_commands(&mut self) -> &mut Vec<TurtleSegment> {
|
||||||
|
&mut self.commands
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_commands(self) -> Vec<TurtleSegment> {
|
||||||
|
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<Precision>) -> &mut Self {
|
||||||
|
self.get_mut_commands()
|
||||||
|
.push(TurtleSegment::Single(DrawElement::Orient(
|
||||||
|
crate::commands::OrientationCommand::Right(angle),
|
||||||
|
)));
|
||||||
|
self
|
||||||
|
}
|
||||||
|
fn left(&mut self, angle: Angle<Precision>) -> &mut Self {
|
||||||
|
self.get_mut_commands()
|
||||||
|
.push(TurtleSegment::Single(DrawElement::Orient(
|
||||||
|
crate::commands::OrientationCommand::Left(angle),
|
||||||
|
)));
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Turnable for TurtlePlan {}
|
||||||
@ -13,6 +13,7 @@ use turtle_bundle::{AnimatedTurtle, TurtleBundle};
|
|||||||
|
|
||||||
pub use commands::TurtleCommands;
|
pub use commands::TurtleCommands;
|
||||||
|
|
||||||
|
pub mod builders;
|
||||||
mod commands;
|
mod commands;
|
||||||
mod debug;
|
mod debug;
|
||||||
mod drawing;
|
mod drawing;
|
||||||
|
|||||||
@ -7,6 +7,7 @@ use bevy_prototype_lyon::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
builders::{TurtlePlan, WithCommands},
|
||||||
commands::{DrawElement, MoveCommand, TurtleCommands, TurtleSegment},
|
commands::{DrawElement, MoveCommand, TurtleCommands, TurtleSegment},
|
||||||
general::length::Length,
|
general::length::Length,
|
||||||
shapes::{self, TurtleColors},
|
shapes::{self, TurtleColors},
|
||||||
@ -39,8 +40,11 @@ impl Default for TurtleBundle {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl TurtleBundle {
|
impl TurtleBundle {
|
||||||
pub fn set_commands(&mut self, commands: Vec<TurtleSegment>) {
|
pub fn apply_plan(&mut self, plan: TurtlePlan) {
|
||||||
self.commands = TurtleCommands::new(commands);
|
self.commands = TurtleCommands::new(plan.get_commands());
|
||||||
|
}
|
||||||
|
pub fn create_plan(&self) -> TurtlePlan {
|
||||||
|
TurtlePlan::new()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user