diff --git a/Cargo.lock b/Cargo.lock index 8ba689a..dd1d7cf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,39 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +[[package]] +name = "approx" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f2a05fd1bd10b2527e20a2cd32d8873d115b8b39fe219ee25f42a8aca6ba278" +dependencies = [ + "num-traits", +] + +[[package]] +name = "autocfg" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" + +[[package]] +name = "num-traits" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" +dependencies = [ + "autocfg", +] + +[[package]] +name = "structs" +version = "0.1.0" +dependencies = [ + "approx", +] + [[package]] name = "tuples" version = "0.1.0" +dependencies = [ + "structs", +] diff --git a/Cargo.toml b/Cargo.toml index 016dc20..5a86c4f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,4 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +structs = { path = "structs" } diff --git a/src/main.rs b/src/main.rs index e7a11a9..b746f33 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,71 @@ -fn main() { - println!("Hello, world!"); +use structs::Tuple; + +use std::fmt; + +#[derive(Debug)] +struct Environment { + gravity: Tuple, + wind: Tuple, +} + +#[derive(Debug, Clone, Copy)] +struct Projectile { + position: Tuple, + velocity: Tuple, +} + +impl Projectile { + + fn new(position: Tuple, velocity: Tuple) -> Projectile { + if !position.is_point() { + panic!("position can not be a vector"); + } + + if !velocity.is_vector() { + panic!("velocity can not be point"); + } + + Projectile { + position: position, + velocity: velocity, + } + } +} + +impl Projectile { + fn tick(&mut self, env: &Environment) { + self.position = self.position + self.velocity; + self.velocity = env.wind + env.gravity; + } +} + +impl fmt::Display for Projectile { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> fmt::Result { + write!(f, "pos: {}, vel {}", self.position, self.velocity) + } +} + +fn init_env() -> Environment { + Environment { + gravity: Tuple::vector(0.0, 0.0, -0.98), + wind: Tuple::vector(0.0, 0.0, 0.0), + } +} + +fn main() { + let env = init_env(); + + let mut ball = Projectile::new( + Tuple::point(0.0, 0.0, 1.0), + Tuple::vector(1.0, 0.0, 0.0), + ); + + let mut tick = 0; + println!("tick {} the ball {}", tick, ball); + while ball.position.z() > 0.0 { + ball.tick(&env); + + println!("tick {} the ball {}", tick, ball); + tick += 1; + } }