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 specs::{Component, NullStorage, VecStorage, World, WorldExt};
|
||||||
|
use std::fmt;
|
||||||
|
use std::fmt::Display;
|
||||||
|
|
||||||
pub enum BoxColor {
|
pub enum BoxColor {
|
||||||
Red,
|
Red,
|
||||||
@ -7,7 +9,12 @@ pub enum BoxColor {
|
|||||||
|
|
||||||
impl Display for BoxColor {
|
impl Display for BoxColor {
|
||||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
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 crate::components::*;
|
||||||
use specs::{Builder, World, WorldExt, EntityBuilder};
|
use specs::{Builder, EntityBuilder, World, WorldExt};
|
||||||
|
|
||||||
/// Cut down on the common syntax boilerplate
|
/// 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
|
return world
|
||||||
.create_entity()
|
.create_entity()
|
||||||
.with(position.with_z(z))
|
.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) {
|
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();
|
common_entity(world, position, 5, "/images/floor.png").build();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_box(world: &mut World, position: Position) {
|
pub fn create_box(world: &mut World, position: Position, color: BoxColor) {
|
||||||
common_entity(world, position, 10, "/images/box.png")
|
let path = format!("/images/box_{}.png", color);
|
||||||
.with(Box {})
|
common_entity(world, position, 10, &path)
|
||||||
|
.with(Box { color })
|
||||||
.with(Movable)
|
.with(Movable)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_box_spot(world: &mut World, position: Position) {
|
pub fn create_box_spot(world: &mut World, position: Position, color: BoxColor) {
|
||||||
common_entity(world, position, 9, "/images/box_spot.png")
|
let path = format!("/images/box_spot_{}.png", color);
|
||||||
.with(BoxSpot {})
|
common_entity(world, position, 9, &path)
|
||||||
|
.with(BoxSpot { color })
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
10
src/main.rs
10
src/main.rs
@ -65,11 +65,11 @@ pub fn initialize_level(world: &mut World) {
|
|||||||
const MAP: &str = "
|
const MAP: &str = "
|
||||||
N N W W W W W W
|
N N W W W W W W
|
||||||
W W W . . . . W
|
W W W . . . . W
|
||||||
W . . . B . . W
|
W . . . BB . . W
|
||||||
W . . . . . . W
|
W . . RB . . . W
|
||||||
W . P . . B . W
|
W . P . . . . W
|
||||||
W . . . . . . W
|
W . . . . RS . W
|
||||||
W . . S . S . W
|
W . . BS . . . W
|
||||||
W . . . . . . W
|
W . . . . . . W
|
||||||
W W W W W 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 crate::entities::*;
|
||||||
use specs::World;
|
use specs::World;
|
||||||
|
|
||||||
@ -24,13 +24,21 @@ pub fn load_map(world: &mut World, map_string: String) {
|
|||||||
create_floor(world, position);
|
create_floor(world, position);
|
||||||
create_player(world, position);
|
create_player(world, position);
|
||||||
}
|
}
|
||||||
"B" => {
|
"BB" => {
|
||||||
create_floor(world, position);
|
create_floor(world, position);
|
||||||
create_box(world, position);
|
create_box(world, position, BoxColor::Blue);
|
||||||
}
|
}
|
||||||
"S" => {
|
"RB" => {
|
||||||
create_floor(world, position);
|
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" => (),
|
"N" => (),
|
||||||
c => panic!("unrecognized map item {}", c),
|
c => panic!("unrecognized map item {}", c),
|
||||||
|
Loading…
Reference in New Issue
Block a user