diff options
| -rw-r--r-- | src/components.rs | 20 | ||||
| -rw-r--r-- | src/controls.rs | 14 | ||||
| -rw-r--r-- | src/resources.rs | 12 | ||||
| -rw-r--r-- | src/room.rs | 8 | ||||
| -rw-r--r-- | src/systems/controlinput.rs | 4 | ||||
| -rw-r--r-- | src/systems/draw.rs | 31 | ||||
| -rw-r--r-- | src/systems/makefloor.rs | 4 | ||||
| -rw-r--r-- | src/systems/mod.rs | 1 | ||||
| -rw-r--r-- | src/systems/moving.rs | 6 | ||||
| -rw-r--r-- | src/systems/view.rs | 22 |
10 files changed, 43 insertions, 79 deletions
diff --git a/src/components.rs b/src/components.rs index aae6453..1613b7f 100644 --- a/src/components.rs +++ b/src/components.rs @@ -11,30 +11,30 @@ use super::util::clamp; #[derive(Component, Debug, Hash, PartialEq, Eq, Clone, Copy)] -pub struct Position { +pub struct Pos { pub x: i32, pub y: i32 } -impl ops::Add<Position> for Position { - type Output = Position; +impl ops::Add<Pos> for Pos { + type Output = Pos; - fn add(self, other: Position) -> Position { - Position { + fn add(self, other: Pos) -> Pos { + Pos { x: self.x + other.x, y: self.y + other.y } } } -impl Position { +impl Pos { - pub fn new(x: i32, y: i32) -> Position { - Position {x, y} + pub fn new(x: i32, y: i32) -> Pos { + Pos {x, y} } - pub fn clamp(self, smaller: Position, larger: Position) -> Position { - Position { + pub fn clamp(self, smaller: Pos, larger: Pos) -> Pos { + Pos { x: clamp(self.x, smaller.x, larger.x), y: clamp(self.y, smaller.y, larger.y) } diff --git a/src/controls.rs b/src/controls.rs index 5cf6d4f..ae806a9 100644 --- a/src/controls.rs +++ b/src/controls.rs @@ -1,7 +1,7 @@ use serde_json::Value; -use super::components::Position; +use super::components::Pos; #[derive(Debug, Clone)] pub enum Direction { @@ -28,13 +28,13 @@ impl Direction { } } - pub fn to_position(&self) -> Position { + pub fn to_position(&self) -> Pos { match self { - Direction::North => Position::new(0, -1), - Direction::South => Position::new(0, 1), - Direction::East => Position::new(1, 0), - Direction::West => Position::new(-1, 0), - Direction::None => Position::new(0, 0) + Direction::North => Pos::new(0, -1), + Direction::South => Pos::new(0, 1), + Direction::East => Pos::new(1, 0), + Direction::West => Pos::new(-1, 0), + Direction::None => Pos::new(0, 0) } } } diff --git a/src/resources.rs b/src/resources.rs index dc8f596..35e5bdf 100644 --- a/src/resources.rs +++ b/src/resources.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use specs::Entity; -use super::components::{Position, Visible}; +use super::components::Pos; use super::controls::Action; use super::assemblages::Assemblage; use super::worldmessages::WorldMessage; @@ -25,17 +25,11 @@ pub struct Size { } #[derive(Default)] -pub struct TopView { - pub cells: HashMap<Position, Vec<Visible>> -} - - -#[derive(Default)] pub struct Floor { - pub cells: HashMap<Position, Vec<Entity>> + pub cells: HashMap<Pos, Vec<Entity>> } #[derive(Default)] pub struct NewEntities { - pub assemblages: Vec<(Position, Box<dyn Assemblage>)> + pub assemblages: Vec<(Pos, Box<dyn Assemblage>)> } diff --git a/src/room.rs b/src/room.rs index dc8c27d..df14549 100644 --- a/src/room.rs +++ b/src/room.rs @@ -11,7 +11,7 @@ use specs::{ }; use super::controls::Action; -use super::components::Position; +use super::components::Pos; use super::assemblages::Assemblage; use super::worldmessages::WorldMessage; use super::resources::{ @@ -21,7 +21,6 @@ use super::resources::{ NewEntities }; use super::systems::{ - draw::Draw, moving::Move, clearcontrols::ClearControllers, makefloor::MakeFloor, @@ -49,9 +48,8 @@ impl <'a, 'b>Room<'a, 'b> { .with(ControlInput, "controlinput", &[]) .with(MakeFloor, "makefloor", &[]) .with(Move, "move", &["makefloor", "controlinput"]) - .with(Draw, "draw", &["move"]) .with(ClearControllers, "clearcontrollers", &["move"]) - .with(View, "view", &["draw"]) + .with(View, "view", &["move"]) .build(); dispatcher.setup(&mut world); @@ -86,7 +84,7 @@ impl <'a, 'b>Room<'a, 'b> { } pub fn add_obj(&mut self, template: &dyn Assemblage, (x, y): (i32, i32)) -> Entity { - template.build(self.world.create_entity()).with(Position{x, y}).build() + template.build(self.world.create_entity()).with(Pos{x, y}).build() } } diff --git a/src/systems/controlinput.rs b/src/systems/controlinput.rs index 663174e..b672b4c 100644 --- a/src/systems/controlinput.rs +++ b/src/systems/controlinput.rs @@ -14,7 +14,7 @@ use specs::{ use super::super::components::{ Controller, Played, - Position + Pos }; use super::super::controls::{ @@ -39,7 +39,7 @@ impl <'a> System<'a> for ControlInput { let mut leaving = HashSet::new(); for action in &input.actions { match action { - Action::Join(name) => {new.assemblages.push((Position{x:10, y:10}, Box::new(Player::new(&name))));} + Action::Join(name) => {new.assemblages.push((Pos{x:10, y:10}, Box::new(Player::new(&name))));} Action::Leave(name) => {leaving.insert(name);} Action::Input(name, control) => {playercontrols.insert(name, control.clone());} } diff --git a/src/systems/draw.rs b/src/systems/draw.rs deleted file mode 100644 index 82400e3..0000000 --- a/src/systems/draw.rs +++ /dev/null @@ -1,31 +0,0 @@ - -use specs::{ - ReadStorage, - Write, - System, - Join -}; - -use super::super::components::{ - Position, - Visible -}; - -use super::super::resources::{ - TopView -}; - - -pub struct Draw; -impl <'a> System<'a> for Draw { - - type SystemData = (ReadStorage<'a, Position>, ReadStorage<'a, Visible>, Write<'a, TopView>); - - fn run(&mut self, (pos, vis, mut view): Self::SystemData) { - view.cells.clear(); - for (pos, vis) in (&pos, &vis).join(){ - view.cells.entry(*pos).or_insert(Vec::new()).push(vis.clone()); - view.cells.get_mut(pos).unwrap().sort_by(|a, b| b.height.partial_cmp(&a.height).unwrap()); - } - } -} diff --git a/src/systems/makefloor.rs b/src/systems/makefloor.rs index 864f204..ba3727d 100644 --- a/src/systems/makefloor.rs +++ b/src/systems/makefloor.rs @@ -9,7 +9,7 @@ use specs::{ }; use super::super::components::{ - Position + Pos }; use super::super::resources::{ @@ -19,7 +19,7 @@ use super::super::resources::{ pub struct MakeFloor; impl <'a> System<'a> for MakeFloor { - type SystemData = (Entities<'a>, Write<'a, Floor>, ReadStorage<'a, Position>); + type SystemData = (Entities<'a>, Write<'a, Floor>, ReadStorage<'a, Pos>); fn run(&mut self, (entities, mut floor, positions): Self::SystemData) { floor.cells.clear(); for (ent, pos) in (&entities, &positions).join() { diff --git a/src/systems/mod.rs b/src/systems/mod.rs index 678941a..0382e39 100644 --- a/src/systems/mod.rs +++ b/src/systems/mod.rs @@ -1,7 +1,6 @@ pub mod clearcontrols; pub mod controlinput; -pub mod draw; pub mod makefloor; pub mod moving; pub mod view; diff --git a/src/systems/moving.rs b/src/systems/moving.rs index c81a766..8bce8e5 100644 --- a/src/systems/moving.rs +++ b/src/systems/moving.rs @@ -8,7 +8,7 @@ use specs::{ }; use super::super::components::{ - Position, + Pos, Controller, Blocking }; @@ -26,12 +26,12 @@ use super::super::resources::{ pub struct Move; impl <'a> System<'a> for Move { - type SystemData = (ReadStorage<'a, Controller>, WriteStorage<'a, Position>, Read<'a, Size>, ReadStorage<'a, Blocking>, Read<'a, Floor>); + type SystemData = (ReadStorage<'a, Controller>, WriteStorage<'a, Pos>, Read<'a, Size>, ReadStorage<'a, Blocking>, Read<'a, Floor>); fn run(&mut self, (controller, mut pos, size, blocking, floor): Self::SystemData) { for (controller, pos) in (&controller, &mut pos).join(){ match &controller.0 { Control::Move(direction) => { - let newpos = (*pos + direction.to_position()).clamp(Position::new(0, 0), Position::new(size.width - 1, size.height - 1)); + let newpos = (*pos + direction.to_position()).clamp(Pos::new(0, 0), Pos::new(size.width - 1, size.height - 1)); let mut blocked = false; for ent in floor.cells.get(&newpos).unwrap_or(&Vec::new()) { if blocking.get(*ent).is_some(){ diff --git a/src/systems/view.rs b/src/systems/view.rs index da6d6c4..144a544 100644 --- a/src/systems/view.rs +++ b/src/systems/view.rs @@ -10,16 +10,15 @@ use specs::{ }; use super::super::components::{ - Position, + Pos, Visible, Played }; use super::super::resources::{ - TopView, Size, - Output, + Output }; use super::super::worldmessages::{ @@ -32,13 +31,18 @@ use super::super::worldmessages::{ pub struct View; impl <'a> System<'a> for View { - type SystemData = (Read<'a, TopView>, Read<'a, Size>, ReadStorage<'a, Played>, Write<'a, Output>); - fn run(&mut self, (topview, size, players, mut output): Self::SystemData) { + type SystemData = (ReadStorage<'a, Pos>, ReadStorage<'a, Visible>, Read<'a, Size>, ReadStorage<'a, Played>, Write<'a, Output>); + fn run(&mut self, (positions, visible, size, players, mut output): Self::SystemData) { + let mut cells: HashMap<Pos, Vec<Visible>> = HashMap::new(); + for (pos, vis) in (&positions, &visible).join(){ + cells.entry(*pos).or_insert(Vec::new()).push(vis.clone()); + cells.get_mut(pos).unwrap().sort_by(|a, b| b.height.partial_cmp(&a.height).unwrap()); + } let width = size.width; let height = size.height; - let (values, mapping) = draw_room(&topview.cells, (width, height)); + let (values, mapping) = draw_room(cells, (width, height)); let message = WorldMessage{updates: vec![WorldUpdate::Field(FieldMessage{ width, @@ -54,14 +58,14 @@ impl <'a> System<'a> for View { } - -fn draw_room(cells: &HashMap<Position, Vec<Visible>>, (width, height): (i32, i32)) -> (Vec<usize>, Vec<Vec<String>>){ +fn draw_room(cells: HashMap<Pos, Vec<Visible>>, (width, height): (i32, i32)) -> (Vec<usize>, Vec<Vec<String>>){ + let size = width * height; let mut values :Vec<usize> = Vec::with_capacity(size as usize); let mut mapping: Vec<Vec<String>> = Vec::new(); for y in 0..height { for x in 0..width { - let sprites: Vec<String> = match cells.get(&Position{x: x, y: y}) { + let sprites: Vec<String> = match cells.get(&Pos{x: x, y: y}) { Some(sprites) => {sprites.iter().map(|v| v.sprite.clone()).collect()} None => {vec![]} }; |
