forked from tutorials/rust-sokoban
Render colored boxes and spots
This commit is contained in:
parent
d9974f6737
commit
e354353e38
@ -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(())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
|
10
src/main.rs
10
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
|
||||
";
|
||||
|
18
src/map.rs
18
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),
|
||||
|
Loading…
Reference in New Issue
Block a user