diff options
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..aed9bcc --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,57 @@ +pub use crate::error::{Error, Result}; +pub use crate::game::Game; + +pub mod entity; +pub mod structure; + +mod error; +mod game; +mod physics; + +pub trait Render { + fn render_map(&self) -> &std::collections::HashMap<crate::physics::Point, char>; + fn render<W: std::io::Write>(&self, w: &mut W) -> crate::Result<()> { + for (p, c) in self.render_map() { + write!(w, "{}{}", termion::cursor::Goto(p.x, p.y), c)? + } + Ok(()) + } +} + +pub trait Derender { + fn derender_map(&self) -> &std::collections::HashMap<crate::physics::Point, char>; + fn derender<W: std::io::Write>(&self, w: &mut W) -> crate::Result<()> { + for p in self.derender_map().keys() { + write!(w, "{}\x08", termion::cursor::Goto(p.x, p.y))? + } + Ok(()) + } +} + +impl<T: Render> Derender for T { + fn derender_map(&self) -> &std::collections::HashMap<crate::physics::Point, char> { + self.render_map() + } +} + +pub trait Collision { + fn collision_map(&self) -> &Vec<crate::physics::Point>; + fn is_collision<P: Into<crate::physics::Point>>(&self, point: P) -> bool { + self.collision_map().contains(&point.into()) + } +} + +pub trait Position { + fn get_position(&self) -> crate::physics::Point; + fn set_position<P: Into<crate::physics::Point>>(&mut self, position: P) -> crate::Result<()>; +} + +pub trait Velocity: Position { + fn get_velocity(&self) -> crate::physics::Vector; + + fn set_velocity<V: Into<crate::physics::Vector>>(&mut self, velocity: V) -> crate::Result<()>; + + fn tick_time(&mut self) -> Result<()> { + self.set_position(self.get_position() + self.get_velocity()) + } +} |