From fa81f03509aa1300613c62e3a7b32db1183cd5c8 Mon Sep 17 00:00:00 2001 From: troido Date: Fri, 7 Feb 2020 20:28:01 +0100 Subject: added floors --- src/componentwrapper.rs | 16 +++++++++++----- src/main.rs | 5 +++-- src/resources.rs | 2 +- src/systems/makefloor.rs | 10 +++++----- src/systems/moving.rs | 25 +++++++++++++++++++------ 5 files changed, 39 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/componentwrapper.rs b/src/componentwrapper.rs index 07529fc..311becf 100644 --- a/src/componentwrapper.rs +++ b/src/componentwrapper.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use specs::{Builder, EntityBuilder}; -use crate::components::{Visible, Blocking, Player}; +use crate::components::{Visible, Blocking, Player, Floor}; use crate::hashmap; use crate::parameter::{Parameter, ParameterType}; @@ -11,7 +11,8 @@ use crate::parameter::{Parameter, ParameterType}; pub enum ComponentWrapper{ Visible(Visible), Blocking(Blocking), - Player(Player) + Player(Player), + Floor(Floor) } impl ComponentWrapper { @@ -20,7 +21,8 @@ impl ComponentWrapper { match self.clone() { Self::Visible(c) => builder.with(c), Self::Blocking(c) => builder.with(c), - Self::Player(c) => builder.with(c) + Self::Player(c) => builder.with(c), + Self::Floor(c) => builder.with(c) } } @@ -33,7 +35,8 @@ impl ComponentWrapper { ComponentType::Blocking => Some(Self::Blocking(Blocking)), ComponentType::Player => Some(Self::Player(Player::new( parameters.remove("name")?.as_str()?.to_string() - ))) + ))), + ComponentType::Floor => Some(Self::Floor(Floor)) } } } @@ -42,7 +45,8 @@ impl ComponentWrapper { pub enum ComponentType { Visible, Blocking, - Player + Player, + Floor } impl ComponentType { @@ -52,6 +56,7 @@ impl ComponentType { "Visible" => Some(ComponentType::Visible), "Blocking" => Some(ComponentType::Blocking), "Player" => Some(ComponentType::Player), + "Floor" => Some(ComponentType::Floor), _ => None } } @@ -60,6 +65,7 @@ impl ComponentType { match self { ComponentType::Visible => hashmap!("sprite" => ParameterType::String, "height" => ParameterType::Float), ComponentType::Blocking => HashMap::new(), + ComponentType::Floor => HashMap::new(), ComponentType::Player => hashmap!("name" => ParameterType::String) } } diff --git a/src/main.rs b/src/main.rs index 7db7dd9..66c6fcc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -72,7 +72,7 @@ fn gen_room<'a, 'b>() -> Room<'a, 'b> { let roomtemplate = RoomTemplate::from_json(&json!({ "width": 42, "height": 22, - "spawn": [35, 5], + "spawn": [5, 15], "field": [ "##########################################", "#,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,#", @@ -133,7 +133,8 @@ fn default_assemblages() -> Encyclopedia { ["string", "ground"] ]], "height": ["float", 0.1] - }] + }], + ["Floor", {}] ] }, "player": { diff --git a/src/resources.rs b/src/resources.rs index 630ffca..bda745f 100644 --- a/src/resources.rs +++ b/src/resources.rs @@ -31,7 +31,7 @@ pub struct Spawn { } #[derive(Default)] -pub struct Floor { +pub struct Ground { pub cells: HashMap> } diff --git a/src/systems/makefloor.rs b/src/systems/makefloor.rs index 209d965..8b550bc 100644 --- a/src/systems/makefloor.rs +++ b/src/systems/makefloor.rs @@ -11,17 +11,17 @@ use specs::{ use super::super::components::Position; use super::super::resources::{ - Floor + Ground }; pub struct MakeFloor; impl <'a> System<'a> for MakeFloor { - type SystemData = (Entities<'a>, Write<'a, Floor>, ReadStorage<'a, Position>); - fn run(&mut self, (entities, mut floor, positions): Self::SystemData) { - floor.cells.clear(); + type SystemData = (Entities<'a>, Write<'a, Ground>, ReadStorage<'a, Position>); + fn run(&mut self, (entities, mut ground, positions): Self::SystemData) { + ground.cells.clear(); for (ent, pos) in (&entities, &positions).join() { - floor.cells.entry(pos.pos).or_insert(Vec::new()).push(ent); + ground.cells.entry(pos.pos).or_insert(Vec::new()).push(ent); } } } diff --git a/src/systems/moving.rs b/src/systems/moving.rs index 2ea0650..28c3d3f 100644 --- a/src/systems/moving.rs +++ b/src/systems/moving.rs @@ -12,7 +12,8 @@ use super::super::pos::Pos; use super::super::components::{ Controller, Blocking, - Position + Position, + Floor }; use super::super::controls::{ @@ -21,27 +22,39 @@ use super::super::controls::{ use super::super::resources::{ Size, - Floor + Ground }; 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>); - fn run(&mut self, (controllers, mut positions, size, blocking, floor): Self::SystemData) { + type SystemData = ( + ReadStorage<'a, Controller>, + WriteStorage<'a, Position>, + Read<'a, Size>, + ReadStorage<'a, Blocking>, + Read<'a, Ground>, + ReadStorage<'a, Floor>, + ); + + fn run(&mut self, (controllers, mut positions, size, blocking, ground, floor): Self::SystemData) { for (controller, mut pos) in (&controllers, &mut positions.restrict_mut()).join(){ match &controller.0 { Control::Move(direction) => { let newpos = (pos.get_unchecked().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()) { + let mut on_floor = false; + for ent in ground.cells.get(&newpos).unwrap_or(&Vec::new()) { if blocking.get(*ent).is_some(){ blocked = true; break; } + if floor.get(*ent).is_some(){ + on_floor = true; + } } - if !blocked { + if !blocked && on_floor { let mut pos_mut = pos.get_mut_unchecked(); pos_mut.prev = Some(pos_mut.pos); pos_mut.pos = newpos.clone(); -- cgit