diff options
| -rw-r--r-- | src/components.rs | 13 | ||||
| -rw-r--r-- | src/componentwrapper.rs | 6 | ||||
| -rw-r--r-- | src/main.rs | 58 | ||||
| -rw-r--r-- | src/room.rs | 26 | ||||
| -rw-r--r-- | src/roomtemplate.rs | 8 | ||||
| -rw-r--r-- | src/systems/controlinput.rs | 4 | ||||
| -rw-r--r-- | src/systems/view.rs | 4 |
7 files changed, 81 insertions, 38 deletions
diff --git a/src/components.rs b/src/components.rs index 7517af9..34dbe97 100644 --- a/src/components.rs +++ b/src/components.rs @@ -2,6 +2,7 @@ use specs::{ DenseVecStorage, VecStorage, + HashMapStorage, FlaggedStorage, Component }; @@ -41,13 +42,17 @@ pub struct Controller(pub Control); pub struct Blocking; #[derive(Component, Debug, Clone)] -pub struct Played { +pub struct Floor; + +#[derive(Component, Debug, Clone)] +#[storage(HashMapStorage)] +pub struct Player { pub name: String, pub is_new: bool } -impl Played { - pub fn new(name: String) -> Played { - Played{name, is_new: true} +impl Player { + pub fn new(name: String) -> Self { + Self{name, is_new: true} } } diff --git a/src/componentwrapper.rs b/src/componentwrapper.rs index 6da6d5a..07529fc 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, Played}; +use crate::components::{Visible, Blocking, Player}; use crate::hashmap; use crate::parameter::{Parameter, ParameterType}; @@ -11,7 +11,7 @@ use crate::parameter::{Parameter, ParameterType}; pub enum ComponentWrapper{ Visible(Visible), Blocking(Blocking), - Player(Played) + Player(Player) } impl ComponentWrapper { @@ -31,7 +31,7 @@ impl ComponentWrapper { height: parameters.remove("height")?.as_f64()? })), ComponentType::Blocking => Some(Self::Blocking(Blocking)), - ComponentType::Player => Some(Self::Player(Played::new( + ComponentType::Player => Some(Self::Player(Player::new( parameters.remove("name")?.as_str()?.to_string() ))) } diff --git a/src/main.rs b/src/main.rs index 410a9aa..bf27cdd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,6 +24,7 @@ mod encyclopedia; mod template; mod roomtemplate; +pub use self::pos::Pos; use self::gameserver::GameServer; use self::server::unixserver::UnixServer; use self::server::tcpserver::TcpServer; @@ -32,7 +33,7 @@ use self::room::Room; use self::util::ToJson; use self::encyclopedia::Encyclopedia; use self::template::Template; -pub use self::pos::Pos; +use self::roomtemplate::RoomTemplate; @@ -50,7 +51,7 @@ fn main() { let mut gameserver = GameServer::new(servers); - let mut room = gen_room(50, 40); + let mut room = gen_room(); loop { let actions = gameserver.update(); @@ -65,24 +66,45 @@ fn main() { } } -fn gen_room<'a, 'b>(width: i64, height: i64) -> Room<'a, 'b> { +fn gen_room<'a, 'b>() -> Room<'a, 'b> { let assemblages = default_assemblages(); - let mut room = Room::new(assemblages.clone(), (width, height)); - let wall = &Template::empty("wall"); - for x in 0..width { - room.add_entity(&wall, Pos::new(x, 0)).unwrap(); - room.add_entity(&wall, Pos::new(x, height - 1)).unwrap(); - } - for y in 1..height-1 { - room.add_entity(&wall, Pos::new(0, y)).unwrap(); - room.add_entity(&wall, Pos::new(width - 1, y)).unwrap(); - } - for x in 1..width-1 { - for y in 1..height-1 { - let grass = &Template::empty("grass"); - room.add_entity(&grass, Pos::new(x, y)).unwrap(); + let mut room = Room::new(assemblages.clone()); + + let roomtemplate = RoomTemplate::from_json(&json!({ + "width": 42, + "height": 22, + "spawn": [5, 5], + "field": [ + "##########################################", + "#,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,#", + "#,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,#", + "#,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,#", + "#,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,#", + "#,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,#", + "#,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,#", + "#,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,#", + "#,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,#", + "#,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,#", + "#,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,#", + "#,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,#", + "#,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,#", + "#,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,#", + "#,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,#", + "#,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,#", + "#,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,#", + "#,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,#", + "#,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,#", + "#,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,#", + "# ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,#", + "#####,,,##################################" + ], + "mapping": { + "#": "wall", + ",": "grass", + " ": [] } - } + })).unwrap(); + room.load_from_template(&roomtemplate); room } diff --git a/src/room.rs b/src/room.rs index 5daad29..8d15bde 100644 --- a/src/room.rs +++ b/src/room.rs @@ -30,6 +30,7 @@ use super::systems::{ use super::componentwrapper::ComponentWrapper; use crate::encyclopedia::Encyclopedia; use crate::template::Template; +use crate::roomtemplate::RoomTemplate; @@ -41,12 +42,8 @@ pub struct Room<'a, 'b> { impl <'a, 'b>Room<'a, 'b> { - pub fn new(encyclopedia: Encyclopedia, size: (i64, i64)) -> Room<'a, 'b> { - let (width, height) = size; + pub fn new(encyclopedia: Encyclopedia) -> Room<'a, 'b> { let mut world = World::new(); - world.insert(Size{width, height}); - world.insert(Input{actions: Vec::new()}); - world.insert(Output{output: HashMap::new()}); let mut dispatcher = DispatcherBuilder::new() .with(ControlInput, "controlinput", &[]) @@ -58,6 +55,7 @@ impl <'a, 'b>Room<'a, 'b> { dispatcher.setup(&mut world); + Room { world, dispatcher, @@ -65,6 +63,24 @@ impl <'a, 'b>Room<'a, 'b> { } } + pub fn load_from_template(&mut self, template: &RoomTemplate) { + + let (width, height) = template.size; + self.world.fetch_mut::<Size>().width = width; + self.world.fetch_mut::<Size>().height = height; + + // todo: set spawn + + for (idx, templates) in template.field.iter().enumerate() { + let x = (idx as i64) % width; + let y = (idx as i64) / width; + + for template in templates { + self.add_entity(template, Pos{x, y}); + } + } + } + pub fn view(&self) -> HashMap<String, WorldMessage> { self.world.fetch::<Output>().output.clone() } diff --git a/src/roomtemplate.rs b/src/roomtemplate.rs index 2a71341..e77fbca 100644 --- a/src/roomtemplate.rs +++ b/src/roomtemplate.rs @@ -4,10 +4,10 @@ use serde_json::Value; use crate::Pos; use crate::template::Template; -struct RoomTemplate { - size: (i64, i64), - spawn: Pos, - field: Vec<Vec<Template>> +pub struct RoomTemplate { + pub size: (i64, i64), + pub spawn: Pos, + pub field: Vec<Vec<Template>> } impl RoomTemplate { diff --git a/src/systems/controlinput.rs b/src/systems/controlinput.rs index e5f0151..b25c336 100644 --- a/src/systems/controlinput.rs +++ b/src/systems/controlinput.rs @@ -12,7 +12,7 @@ use specs::{ }; use crate::pos::Pos; -use crate::components::{Controller, Played}; +use crate::components::{Controller, Player}; use crate::controls::{Control, Action}; use crate::resources::{Input, NewEntities}; use crate::hashmap; @@ -24,7 +24,7 @@ use crate::parameter::Parameter; pub struct ControlInput; impl <'a> System<'a> for ControlInput { - type SystemData = (Entities<'a>, Read<'a, Input>, WriteStorage<'a, Controller>, ReadStorage<'a, Played>, Write<'a, NewEntities>); + type SystemData = (Entities<'a>, Read<'a, Input>, WriteStorage<'a, Controller>, ReadStorage<'a, Player>, Write<'a, NewEntities>); fn run(&mut self, (entities, input, mut controllers, players, mut new): Self::SystemData) { let mut playercontrols: HashMap<&str, Control> = HashMap::new(); let mut leaving = HashSet::new(); diff --git a/src/systems/view.rs b/src/systems/view.rs index 9ab6faf..fec5ee7 100644 --- a/src/systems/view.rs +++ b/src/systems/view.rs @@ -16,7 +16,7 @@ use specs::{ }; use super::super::pos::Pos; -use super::super::components::{Visible, Played, Position}; +use super::super::components::{Visible, Player, Position}; use super::super::resources::{Size, Output}; use super::super::worldmessages::{WorldMessage, WorldUpdate, FieldMessage}; @@ -29,7 +29,7 @@ pub struct View { } impl <'a> System<'a> for View { - type SystemData = (ReadStorage<'a, Position>, ReadStorage<'a, Visible>, Read<'a, Size>, WriteStorage<'a, Played>, Write<'a, Output>); + type SystemData = (ReadStorage<'a, Position>, ReadStorage<'a, Visible>, Read<'a, Size>, WriteStorage<'a, Player>, Write<'a, Output>); fn run(&mut self, (positions, visible, size, mut players, mut output): Self::SystemData) { let mut cells: HashMap<Pos, Vec<Visible>> = HashMap::new(); |
