Render colored boxes and spots

This commit is contained in:
Timothy Warren 2020-07-24 17:55:38 -04:00
parent d9974f6737
commit e354353e38
4 changed files with 44 additions and 20 deletions

View File

@ -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(())
}
}

View File

@ -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();
}

View File

@ -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
";

View File

@ -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),