From 766c1a0105254bf3c0f541a6caf12dc0f30a70e6 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Fri, 24 Jul 2020 14:55:49 -0400 Subject: [PATCH] Start on chapter 3 --- src/components.rs | 24 +++++++++++++++++++++--- src/entities.rs | 2 +- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/components.rs b/src/components.rs index 38bb5f6..d5607d8 100644 --- a/src/components.rs +++ b/src/components.rs @@ -1,6 +1,16 @@ use specs::{Component, NullStorage, VecStorage, World, WorldExt}; -// Components +pub enum BoxColor { + Red, + Blue, +} + +impl Display for BoxColor { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + + } +} + #[derive(Debug, Component, Clone, Copy)] #[storage(VecStorage)] pub struct Position { @@ -13,6 +23,10 @@ impl Position { pub fn new(x: u8, y: u8) -> Self { Position { x, y, z: 0 } } + + pub fn with_z(self, z: u8) -> Self { + Position { z, ..self } + } } #[derive(Component)] @@ -31,11 +45,15 @@ pub struct Player {} #[derive(Component)] #[storage(VecStorage)] -pub struct Box {} +pub struct Box { + pub color: BoxColor, +} #[derive(Component)] #[storage(VecStorage)] -pub struct BoxSpot {} +pub struct BoxSpot { + pub color: BoxColor, +} #[derive(Component, Default)] #[storage(NullStorage)] diff --git a/src/entities.rs b/src/entities.rs index b240683..e089c73 100644 --- a/src/entities.rs +++ b/src/entities.rs @@ -5,7 +5,7 @@ use specs::{Builder, World, WorldExt, EntityBuilder}; fn common_entity<'w>(world: &'w mut World, position: Position, z: u8, sprite_path: &str) -> EntityBuilder<'w> { return world .create_entity() - .with(Position { z, ..position }) + .with(position.with_z(z)) .with(Renderable { path: sprite_path.to_string() }); }