From 88d7f30b452148963c8a4c3bb739b50d907a75f1 Mon Sep 17 00:00:00 2001 From: troido Date: Tue, 28 Jan 2020 11:45:44 +0100 Subject: entities have Assemblages as template to create them --- Cargo.toml | 1 + src/room.rs | 73 ++++++++++++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 59 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 47fbf31..bbe5690 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,3 +13,4 @@ libc = "0.2" users = "0.8" json = "0.12.1" specs = { version = "0.15.1", features = ["specs-derive"] } +rand = "0.7.3" diff --git a/src/room.rs b/src/room.rs index b9614e3..5268ce7 100644 --- a/src/room.rs +++ b/src/room.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; - +use rand::Rng; use specs::{ VecStorage, Component, @@ -12,7 +12,8 @@ use specs::{ ReadStorage, DispatcherBuilder, Dispatcher, - Write + Write, + EntityBuilder }; @@ -32,12 +33,12 @@ struct Visible { height: f32 } -#[derive(Default)] -struct Size (i32, i32); - // Resources +#[derive(Default)] +struct Size (i32, i32); + #[derive(Default)] struct TopView { width: i32, @@ -85,12 +86,12 @@ impl <'a, 'b>Room<'a, 'b> { .with(Draw, "draw", &[]) .build(); - gen_world(&mut world); - - Room { + let mut room = Room { world, dispatcher - } + }; + gen_room(&mut room); + room } pub fn view(&self) -> (Vec, Vec>) { @@ -131,18 +132,60 @@ impl <'a, 'b>Room<'a, 'b> { let Size(width, height) = *self.world.fetch::(); (width, height) } + + pub fn add_obj(&mut self, template: &dyn Assemblage, (x, y): (i32, i32)) { + template.build(self.world.create_entity()).with(Position{x, y}).build(); + } } -fn gen_world(world: &mut World){ +fn gen_room(room: &mut Room){ - let Size(width, height) = *world.fetch::(); + let (width, height) = room.get_size(); for x in 0..width { - world.create_entity().with(Position{x: x, y: 0}).with(Visible{sprite: "wall".to_string(), height: 1.0}).build(); - world.create_entity().with(Position{x: x, y: height - 1}).with(Visible{sprite: "wall".to_string(), height: 1.0}).build(); + room.add_obj(&Wall, (x, 0)); + room.add_obj(&Wall, (x, height - 1)); } for y in 1..height-1 { - world.create_entity().with(Position{x: 0, y: y}).with(Visible{sprite: "wall".to_string(), height: 1.0}).build(); - world.create_entity().with(Position{x: width - 1, y: y}).with(Visible{sprite: "wall".to_string(), height: 1.0}).build(); + room.add_obj(&Wall, (0, y)); + room.add_obj(&Wall, (width - 1, y)); + } + for x in 1..width-1 { + for y in 1..height-1 { + room.add_obj(&Grass::new(), (x, y)); + } } } + + +pub trait Assemblage { + fn build<'a>(&self, builder: EntityBuilder<'a>) -> EntityBuilder<'a>; +} + +struct Wall; + +impl Assemblage for Wall { + + fn build<'a>(&self, builder: EntityBuilder<'a>) -> EntityBuilder<'a>{ + builder.with(Visible{sprite: "wall".to_string(), height: 2.0}) + } +} + +struct Grass { + sprite: String +} + +impl Grass { + fn new() -> Grass { + Grass { + sprite: ["grass1", "grass2", "grass3", "grass1", "grass2", "grass3", "ground"][rand::thread_rng().gen_range(0,7)].to_string() + } + } +} + +impl Assemblage for Grass { + + fn build<'a>(&self, builder: EntityBuilder<'a>) -> EntityBuilder<'a>{ + builder.with(Visible{sprite: self.sprite.to_string(), height: 0.1}) + } +} -- cgit