2020-07-23 18:12:52 -04:00
|
|
|
use crate::components::*;
|
2020-07-24 17:55:38 -04:00
|
|
|
use specs::{Builder, EntityBuilder, World, WorldExt};
|
2020-07-23 18:12:52 -04:00
|
|
|
|
2020-07-23 21:24:36 -04:00
|
|
|
/// Cut down on the common syntax boilerplate
|
2020-07-24 18:56:48 -04:00
|
|
|
fn static_entity<'w>(
|
2020-07-24 17:55:38 -04:00
|
|
|
world: &'w mut World,
|
|
|
|
position: Position,
|
|
|
|
z: u8,
|
|
|
|
sprite_path: &str,
|
|
|
|
) -> EntityBuilder<'w> {
|
2020-07-24 18:56:48 -04:00
|
|
|
let sprite_path = sprite_path.to_string();
|
|
|
|
world
|
2020-07-23 18:12:52 -04:00
|
|
|
.create_entity()
|
2020-07-24 14:55:49 -04:00
|
|
|
.with(position.with_z(z))
|
2020-07-24 18:56:48 -04:00
|
|
|
.with(Renderable::new_static(sprite_path))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Cut down on animated entity boilerplate,
|
|
|
|
/// and accept vectors of `&str` and `String`
|
|
|
|
fn animated_entity<S>(
|
|
|
|
world: &mut World,
|
|
|
|
position: Position,
|
|
|
|
z: u8,
|
|
|
|
sprites: Vec<S>,
|
|
|
|
) -> EntityBuilder
|
|
|
|
where
|
|
|
|
S: Into<String>,
|
|
|
|
{
|
|
|
|
let sprites: Vec<String> = sprites.into_iter().map(|s| s.into()).collect();
|
|
|
|
world
|
|
|
|
.create_entity()
|
|
|
|
.with(position.with_z(z))
|
|
|
|
.with(Renderable::new_animated(sprites))
|
2020-07-23 21:24:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_wall(world: &mut World, position: Position) {
|
2020-07-24 18:56:48 -04:00
|
|
|
static_entity(world, position, 10, "/images/wall.png")
|
2020-07-23 18:12:52 -04:00
|
|
|
.with(Wall {})
|
|
|
|
.with(Immovable)
|
|
|
|
.build();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_floor(world: &mut World, position: Position) {
|
2020-07-24 18:56:48 -04:00
|
|
|
static_entity(world, position, 5, "/images/floor.png").build();
|
2020-07-23 18:12:52 -04:00
|
|
|
}
|
|
|
|
|
2020-07-24 17:55:38 -04:00
|
|
|
pub fn create_box(world: &mut World, position: Position, color: BoxColor) {
|
2020-07-24 18:56:48 -04:00
|
|
|
let mut sprites: Vec<String> = vec![];
|
|
|
|
for x in 1..=2 {
|
|
|
|
sprites.push(format!("/images/box_{}_{}.png", color, x));
|
|
|
|
}
|
|
|
|
animated_entity(world, position, 10, sprites)
|
2020-07-24 17:55:38 -04:00
|
|
|
.with(Box { color })
|
2020-07-23 18:12:52 -04:00
|
|
|
.with(Movable)
|
|
|
|
.build();
|
|
|
|
}
|
|
|
|
|
2020-07-24 17:55:38 -04:00
|
|
|
pub fn create_box_spot(world: &mut World, position: Position, color: BoxColor) {
|
|
|
|
let path = format!("/images/box_spot_{}.png", color);
|
2020-07-24 18:56:48 -04:00
|
|
|
static_entity(world, position, 9, &path)
|
2020-07-24 17:55:38 -04:00
|
|
|
.with(BoxSpot { color })
|
2020-07-23 18:12:52 -04:00
|
|
|
.build();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_player(world: &mut World, position: Position) {
|
2020-07-24 18:56:48 -04:00
|
|
|
let sprites = vec![
|
|
|
|
"/images/player_1.png",
|
|
|
|
"/images/player_2.png",
|
|
|
|
"/images/player_3.png",
|
|
|
|
];
|
|
|
|
animated_entity(world, position, 10, sprites)
|
2020-07-23 18:12:52 -04:00
|
|
|
.with(Player {})
|
|
|
|
.with(Movable)
|
|
|
|
.build();
|
|
|
|
}
|