From e354353e385ce333015d7bb530c50e171e934d31 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Fri, 24 Jul 2020 17:55:38 -0400 Subject: [PATCH] Render colored boxes and spots --- src/components.rs | 9 ++++++++- src/entities.rs | 27 ++++++++++++++++++--------- src/main.rs | 10 +++++----- src/map.rs | 18 +++++++++++++----- 4 files changed, 44 insertions(+), 20 deletions(-) diff --git a/src/components.rs b/src/components.rs index d5607d8..b456249 100644 --- a/src/components.rs +++ b/src/components.rs @@ -1,4 +1,6 @@ use specs::{Component, NullStorage, VecStorage, World, WorldExt}; +use std::fmt; +use std::fmt::Display; pub enum BoxColor { Red, @@ -7,7 +9,12 @@ pub enum BoxColor { impl Display for BoxColor { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - + fmt.write_str(match self { + BoxColor::Red => "red", + BoxColor::Blue => "blue", + })?; + + Ok(()) } } diff --git a/src/entities.rs b/src/entities.rs index e089c73..d85ff85 100644 --- a/src/entities.rs +++ b/src/entities.rs @@ -1,12 +1,19 @@ use crate::components::*; -use specs::{Builder, World, WorldExt, EntityBuilder}; +use specs::{Builder, EntityBuilder, World, WorldExt}; /// Cut down on the common syntax boilerplate -fn common_entity<'w>(world: &'w mut World, position: Position, z: u8, sprite_path: &str) -> EntityBuilder<'w> { +fn common_entity<'w>( + world: &'w mut World, + position: Position, + z: u8, + sprite_path: &str, +) -> EntityBuilder<'w> { return world .create_entity() .with(position.with_z(z)) - .with(Renderable { path: sprite_path.to_string() }); + .with(Renderable { + path: sprite_path.to_string(), + }); } pub fn create_wall(world: &mut World, position: Position) { @@ -20,16 +27,18 @@ pub fn create_floor(world: &mut World, position: Position) { common_entity(world, position, 5, "/images/floor.png").build(); } -pub fn create_box(world: &mut World, position: Position) { - common_entity(world, position, 10, "/images/box.png") - .with(Box {}) +pub fn create_box(world: &mut World, position: Position, color: BoxColor) { + let path = format!("/images/box_{}.png", color); + common_entity(world, position, 10, &path) + .with(Box { color }) .with(Movable) .build(); } -pub fn create_box_spot(world: &mut World, position: Position) { - common_entity(world, position, 9, "/images/box_spot.png") - .with(BoxSpot {}) +pub fn create_box_spot(world: &mut World, position: Position, color: BoxColor) { + let path = format!("/images/box_spot_{}.png", color); + common_entity(world, position, 9, &path) + .with(BoxSpot { color }) .build(); } diff --git a/src/main.rs b/src/main.rs index 882240c..defa079 100644 --- a/src/main.rs +++ b/src/main.rs @@ -65,11 +65,11 @@ pub fn initialize_level(world: &mut World) { const MAP: &str = " N N W W W W W W W W W . . . . W - W . . . B . . W - W . . . . . . W - W . P . . B . W - W . . . . . . W - W . . S . S . W + W . . . BB . . W + W . . RB . . . W + W . P . . . . W + W . . . . RS . W + W . . BS . . . W W . . . . . . W W W W W W W W W "; diff --git a/src/map.rs b/src/map.rs index 36c9c11..a62f2b0 100644 --- a/src/map.rs +++ b/src/map.rs @@ -1,4 +1,4 @@ -use crate::components::Position; +use crate::components::{BoxColor, Position}; use crate::entities::*; use specs::World; @@ -24,13 +24,21 @@ pub fn load_map(world: &mut World, map_string: String) { create_floor(world, position); create_player(world, position); } - "B" => { + "BB" => { create_floor(world, position); - create_box(world, position); + create_box(world, position, BoxColor::Blue); } - "S" => { + "RB" => { create_floor(world, position); - create_box_spot(world, position); + create_box(world, position, BoxColor::Red); + } + "BS" => { + create_floor(world, position); + create_box_spot(world, position, BoxColor::Blue); + } + "RS" => { + create_floor(world, position); + create_box_spot(world, position, BoxColor::Red); } "N" => (), c => panic!("unrecognized map item {}", c),