diff options
| author | troido <troido@protonmail.com> | 2020-01-27 23:01:26 +0100 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-01-27 23:01:26 +0100 |
| commit | fde8695bbca220e5af85344a7da23bea0f6513b6 (patch) | |
| tree | 3d971adb4111eed756bcd2d4e30a8919f4df093d | |
| parent | 599d7dc27df5e5ba37ef622d520399d7ca331425 (diff) | |
cleanup!
| -rw-r--r-- | src/doublemap.rs | 17 | ||||
| -rw-r--r-- | src/main.rs | 53 | ||||
| -rw-r--r-- | src/room.rs | 79 | ||||
| -rw-r--r-- | src/simpleworld.rs | 79 |
4 files changed, 14 insertions, 214 deletions
diff --git a/src/doublemap.rs b/src/doublemap.rs deleted file mode 100644 index add0803..0000000 --- a/src/doublemap.rs +++ /dev/null @@ -1,17 +0,0 @@ - -use std::Collections::HashMap; - -struct DoubleMap<K, V> { - keytoval: HashMap<K, V>, - valtokey: HashMap<V, K> -} - -impl DoubleMap<K, V> { - - pub fn new() -> DoubleMap<K, V> { - DoubleMap { - keytoval: HashMap::new(), - valtokey: HashMap::new() - - -} diff --git a/src/main.rs b/src/main.rs index b6ec8b7..97283d0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,18 +3,15 @@ use std::thread::sleep; use std::time::Duration; use std::path::Path; -// use std::net::SocketAddr; pub mod server; pub mod gameserver; -// pub mod simpleworld; pub mod room; use self::gameserver::GameServer; use self::server::unixserver::UnixServer; use self::server::tcpserver::TcpServer; use self::server::Server; -// use self::simpleworld::{Room, Pos, GameObject}; use json; @@ -32,57 +29,20 @@ fn main() { let servers: Vec<Box<dyn Server>> = vec![Box::new(unixserver), Box::new(inetserver)]; let mut gameserver = GameServer::new(servers); -// println!("listening on {:?}", addr); - -// let mut players: HashMap<usize, String> = HashMap::new(); - -// let (mut world, dispatcher) = room::make_room((32, 32)); - let mut room = room::Room::new((32, 32)); -// dispatcher.dispatch(&mut world); -// world.maintain(); - -// let mut world = generate_world(32, 32); loop { let _actions = gameserver.update(); room.update(); let (field, mapping) = room.view(); let updatemsg = create_update_message(room.get_size(), field, mapping); -// dispatcher.dispatch(&mut world); -// world.maintain(); -// let topview = *world.fetch::<TopView>(); let _ = gameserver.broadcast_json(updatemsg); -// update(&mut gameserver, &mut world); sleep(Duration::from_millis(100)); } } -// fn generate_world(width: i32, height: i32) -> Room { -// -// let mut world = Room::new((Pos(0,0), Pos(width, height))); -// let grass = GameObject::new("grass1"); -// let wall = GameObject::new("wall"); -// for x in 0..width { -// world.add_obj(Pos(x, 0), wall.clone()); -// world.add_obj(Pos(x, height -1), wall.clone()); -// } -// for y in 1..height { -// world.add_obj(Pos(0, y), wall.clone()); -// world.add_obj(Pos(width -1, y), wall.clone()); -// } -// for x in 10..20 { -// for y in 15 .. 25 { -// let pos = Pos(x, y); -// world.add_obj(pos, grass.clone()); -// } -// } -// world -// } - - fn create_update_message((width, height): (i32, i32), field: Vec<usize>, mapping: Vec<Vec<String>>) -> json::JsonValue { let mut updatemsg: json::JsonValue = json::array![ "world", @@ -92,8 +52,6 @@ fn create_update_message((width, height): (i32, i32), field: Vec<usize>, mapping json::object!{ "width" => width, "height" => height, -// "field" => jfield, -// "mapping" => json::from(mapping) } ] ] @@ -102,14 +60,3 @@ fn create_update_message((width, height): (i32, i32), field: Vec<usize>, mapping updatemsg[1][0][1]["mapping"] = json::from(mapping); updatemsg } - -// fn update(gameserver: &mut GameServer, world: &mut Room) { -// let actions = gameserver.update(); -// for action in actions { -// println!("a {:?}", action); -// } -// let (_start, Pos(width, height)) = world.area; -// let (field, mapping) = world.draw(); -// // let jfield = json::from(field); -// let _ = gameserver.broadcast_json(updatemsg); -// } diff --git a/src/room.rs b/src/room.rs index 4c1b291..b9614e3 100644 --- a/src/room.rs +++ b/src/room.rs @@ -1,7 +1,5 @@ use std::collections::HashMap; -// use std::collections::HashSet; -// use std::ops::Deref; use specs::{ VecStorage, @@ -17,7 +15,8 @@ use specs::{ Write }; -struct EntityId(usize); + +// Components #[derive(Component, Debug, Hash, PartialEq, Eq, Clone, Copy)] #[storage(VecStorage)] @@ -37,6 +36,17 @@ struct Visible { struct Size (i32, i32); +// Resources + +#[derive(Default)] +struct TopView { + width: i32, + height: i32, + cells: HashMap<Position, Vec<String>> +} + +// Systems + struct Draw; impl <'a> System<'a> for Draw { @@ -53,13 +63,8 @@ impl <'a> System<'a> for Draw { } } -#[derive(Default)] -struct TopView { - width: i32, - height: i32, - cells: HashMap<Position, Vec<String>> -} +// Higher level stuff pub struct Room<'a, 'b> { world: World, @@ -92,13 +97,11 @@ impl <'a, 'b>Room<'a, 'b> { let tv = &*self.world.fetch::<TopView>(); let width = tv.width; let height = tv.height; -// let TopView{width: width, height: height, cells: cells} = ; let size = width * height; let mut values :Vec<usize> = Vec::with_capacity(size as usize); let mut mapping: Vec<Vec<String>> = Vec::with_capacity(size as usize); for y in 0..height { for x in 0..width { -// values.push(mapping.len()); let sprites = match tv.cells.get(&Position{x: x, y: y}) { Some(sprites) => {sprites.to_vec()} None => {vec![]} @@ -143,57 +146,3 @@ fn gen_world(world: &mut World){ } } -// pub fn make_room<'a, 'b>(size: (i32, i32)) -> (World, Dispatcher<'a, 'b>){ -// let (width, height) = size; -// let mut world = World::new(); -// world.register::<Position>(); -// world.register::<Visible>(); -// world.insert(Size(width, height)); -// world.insert(TopView{width: width, height: height, cells: HashMap::new()}); -// -// -// -// let mut dispatcher = DispatcherBuilder::new() -// .with(Draw, "draw", &[]) -// .build(); -// -// -// gen_world(&mut world); -// -// (world, dispatcher) -// } - -// pub fn view_room(world :&World) -> (Vec<usize>, Vec<Vec<String>>) { -// let TopView{width: width, height: height, cells: cells} = *world.fetch::<TopView>(); -// let size = width * height; -// let mut values :Vec<usize> = Vec::with_capacity(size as usize); -// let mut mapping = Vec::with_capacity(size as usize); -// for y in 0..height { -// for x in 0..width { -// values.push(mapping.len()); -// mapping.push(match cells.get(Position{x: x, y: y}) { -// Some(sprites) => {sprites} -// None => {vec![]} -// }); -// } -// } -// return (values, mapping) -// -// } - -// struct Room { -// entities: Entities, -// width: u32, -// height: u32, -// ground: HashMap<(u32, u32), GroundTile> -// } -// -// struct Entities { -// components: HashMap<EntityId, Box<Component>>, -// systems: HashMap<EntityId, Box<System>> -// } -// -// -// struct GroundTile { -// entities: HashSet<EntityId> -// } diff --git a/src/simpleworld.rs b/src/simpleworld.rs deleted file mode 100644 index 5b89339..0000000 --- a/src/simpleworld.rs +++ /dev/null @@ -1,79 +0,0 @@ - -use std::collections::HashMap; - -#[derive(PartialEq, Eq, Hash, Clone, Copy)] -pub struct Pos(pub i32, pub i32); - - -pub struct Room { - objects :HashMap<Pos, Vec<GameObject>>, - pub area :(Pos, Pos), - players :HashMap<String, GameObject> -} - - -impl Room { - - pub fn new(area :(Pos, Pos)) -> Room { - Room { - objects: HashMap::new(), - area: area, - players: HashMap::new() - } - } - - - pub fn get_sprites(&self, pos :&Pos) -> Vec<String> { - match self.objects.get(pos) { - Some(objs) => {objs.iter().map(|o| o.sprite.clone()).collect()} - None => {Vec::new()} - } - } - - pub fn add_obj(&mut self, pos :Pos, obj :GameObject) { - let place = self.objects.entry(pos).or_insert(Vec::new()); - place.push(obj); - } - -// let mut sprites :Vec<String> = Vec::new(); -// for maybe_obj in self.objects.get((x, y)) -// sprites.push - - - pub fn draw(&self) -> (Vec<usize>, Vec<Vec<String>>) { - let (minp, maxp) = &self.area; - let Pos(xmin, ymin) = *minp; - let Pos(xmax, ymax) = *maxp; - let width = xmax - xmin; - let height = ymax - ymin; - let size = width * height; - let mut values :Vec<usize> = Vec::with_capacity(size as usize); - let mut mapping = Vec::with_capacity(size as usize); - for y in ymin..ymax { - for x in xmin..xmax { - values.push(mapping.len()); - mapping.push(self.get_sprites(&Pos(x, y))); - } - } - return (values, mapping) - } -} - - -#[derive(Clone)] -pub struct GameObject { -// pos :Pos, -// name :&str, - sprite :String -} - -impl GameObject { - - pub fn new(sprite :&str) -> GameObject { - GameObject { -// pos: pos, - sprite: sprite.to_string() - } - } - -} |
