diff options
| author | troido <troido@protonmail.com> | 2020-01-29 00:13:03 +0100 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-01-29 00:13:03 +0100 |
| commit | 88f275bc427033b7981e0dc2fc5cb4b711fd5fb1 (patch) | |
| tree | bbac6dc2a928cd121d3c45d0506aad9c32afe169 /src | |
| parent | b3356eabcea09f599ad99c1332450e4d9570161b (diff) | |
refactored control
Diffstat (limited to 'src')
| -rw-r--r-- | src/assemblages.rs | 4 | ||||
| -rw-r--r-- | src/components.rs | 2 | ||||
| -rw-r--r-- | src/room.rs | 9 | ||||
| -rw-r--r-- | src/systems.rs | 32 |
4 files changed, 31 insertions, 16 deletions
diff --git a/src/assemblages.rs b/src/assemblages.rs index 4705ae4..2a74030 100644 --- a/src/assemblages.rs +++ b/src/assemblages.rs @@ -6,7 +6,7 @@ use specs::{ EntityBuilder }; -use super::components::{Visible, Controller}; +use super::components::{Visible}; @@ -56,6 +56,6 @@ impl Player { impl Assemblage for Player { fn build<'a>(&self, builder: EntityBuilder<'a>) -> EntityBuilder<'a>{ - builder.with(Visible{sprite: "player".to_string(), height: 1.0}).with(Controller(None)) + builder.with(Visible{sprite: "player".to_string(), height: 1.0}) } } diff --git a/src/components.rs b/src/components.rs index 51469cc..07de1d5 100644 --- a/src/components.rs +++ b/src/components.rs @@ -23,4 +23,4 @@ pub struct Visible { #[derive(Component, Debug)] #[storage(VecStorage)] -pub struct Controller(pub Option<Control>); +pub struct Controller(pub Control); diff --git a/src/room.rs b/src/room.rs index 8190d63..c19af37 100644 --- a/src/room.rs +++ b/src/room.rs @@ -13,7 +13,7 @@ use super::controls::Control; use super::components::{Position, Visible, Controller}; use super::assemblages::Assemblage; use super::resources::{Size, TopView}; -use super::systems::{Draw, Move}; +use super::systems::{Draw, Move, ClearControllers}; @@ -36,8 +36,9 @@ impl <'a, 'b>Room<'a, 'b> { world.insert(TopView{width: width, height: height, cells: HashMap::new()}); let dispatcher = DispatcherBuilder::new() - .with(Draw, "draw", &[]) - .with(Move, "move", &["draw"]) + .with(Move, "move", &[]) + .with(Draw, "draw", &["move"]) + .with(ClearControllers, "clearcontrollers", &["move"]) .build(); Room { @@ -108,7 +109,7 @@ impl <'a, 'b>Room<'a, 'b> { pub fn control(&mut self, name: String, control: Control){ if let Some(ent) = self.players.get(&name){ - self.world.write_component::<Controller>().get_mut(*ent).unwrap().0 = Some(control);//.insert(*ent, Controller(control)); + let _ = self.world.write_component::<Controller>().insert(*ent, Controller(control)); } } } diff --git a/src/systems.rs b/src/systems.rs index 322ad23..d00abc8 100644 --- a/src/systems.rs +++ b/src/systems.rs @@ -3,6 +3,7 @@ use specs::{ ReadStorage, WriteStorage, Write, + Entities, System, Join }; @@ -47,17 +48,30 @@ impl <'a> System<'a> for Move { type SystemData = (WriteStorage<'a, Controller>, WriteStorage<'a, Position>); fn run(&mut self, (mut controller, mut pos): Self::SystemData) { for (controller, pos) in (&mut controller, &mut pos).join(){ - if let Some(control) = &controller.0 { - match control { - Control::Move(direction) => { - let (dx, dy) = direction.to_position(); - pos.x += dx; - pos.y += dy; - } - _ => {} + match &controller.0 { + Control::Move(direction) => { + let (dx, dy) = direction.to_position(); + pos.x += dx; + pos.y += dy; } - controller.0 = None + _ => {} } } } } + + +pub struct ClearControllers; +impl <'a> System<'a> for ClearControllers { + type SystemData = (Entities<'a>, WriteStorage<'a, Controller>); + fn run(&mut self, (entities, mut controllers): Self::SystemData) { + let mut ents = Vec::new(); + for (ent, _controller) in (&*entities, &controllers).join() { + ents.push(ent); + } + for ent in ents { + controllers.remove(ent); + } + } +} + |
