From 3d870a714348defc40cd519c00b43c87b1be6480 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Tue, 18 Jun 2024 13:28:31 +0200 Subject: load map from file --- server/src/game.rs | 44 ++------------------------------------------ server/src/main.rs | 6 ++++-- server/src/recipes.rs | 23 ++++++++++++++++++++++- 3 files changed, 28 insertions(+), 45 deletions(-) (limited to 'server/src') diff --git a/server/src/game.rs b/server/src/game.rs index f91e42d0..16f40a6d 100644 --- a/server/src/game.rs +++ b/server/src/game.rs @@ -49,49 +49,9 @@ impl Game { players: Default::default(), tiles: Default::default(), }; - for x in -5..5 { - for y in -5..5 { - g.tiles - .insert(IVec2 { x, y }, gamedata.get_tile("floor").unwrap().into()); - } + for (&p, &t) in &gamedata.initial_map { + g.tiles.insert(p, t.into()); } - for x in -5..5 { - g.tiles.insert( - IVec2 { x, y: -5 }, - gamedata.get_tile("counter").unwrap().into(), - ); - g.tiles.insert( - IVec2 { x, y: 4 }, - gamedata.get_tile("table").unwrap().into(), - ); - } - for y in -5..5 { - g.tiles.insert( - IVec2 { x: -5, y }, - gamedata.get_tile("table").unwrap().into(), - ); - g.tiles.insert( - IVec2 { x: 4, y }, - gamedata.get_tile("table").unwrap().into(), - ); - } - - g.tiles.extend( - [ - ([1, 4], "pan"), - ([2, 4], "pan"), - ([-1, 4], "oven"), - ([-2, 4], "oven"), - ([-5, 2], "sink"), - ([-5, 3], "dirty-plate-spawn"), - ([4, 0], "flour-spawn"), - ([4, 1], "tomato-spawn"), - ([4, 2], "raw-steak-spawn"), - ([4, -4], "trash"), - ] - .map(|(k, v)| (IVec2::from_array(k), gamedata.get_tile(v).unwrap().into())), - ); - g } diff --git a/server/src/main.rs b/server/src/main.rs index d747e737..5414e9fd 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -27,8 +27,10 @@ async fn main() -> Result<()> { ); info!("listening for websockets on {}", ws_listener.local_addr()?); - let data = - build_gamedata(serde_yaml::from_reader(File::open("data/recipes.yaml").unwrap()).unwrap()); + let data = build_gamedata( + serde_yaml::from_reader(File::open("data/recipes.yaml").unwrap()).unwrap(), + serde_yaml::from_reader(File::open("data/map.yaml").unwrap()).unwrap(), + ); let game = Arc::new(RwLock::new(Game::new(data.into()))); let (tx, rx) = broadcast::channel::(1024); diff --git a/server/src/recipes.rs b/server/src/recipes.rs index b99cd21e..2dcb215c 100644 --- a/server/src/recipes.rs +++ b/server/src/recipes.rs @@ -1,5 +1,8 @@ use crate::protocol::{ItemIndex, TileIndex}; +use glam::IVec2; +use log::debug; use serde::{Deserialize, Serialize}; +use std::collections::HashMap; #[derive(Debug, Deserialize, Serialize, Clone, Copy, Default)] #[serde(rename_all = "snake_case")] @@ -22,13 +25,21 @@ pub struct Recipe { pub action: Action, } +#[derive(Debug, Clone, Deserialize)] +pub struct InitialMap { + map: Vec, + tiles: HashMap, +} + #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Gamedata { pub recipes: Vec, pub item_names: Vec, pub tile_names: Vec, + #[serde(skip)] + pub initial_map: HashMap, } -pub fn build_gamedata(recipes_in: Vec>) -> Gamedata { +pub fn build_gamedata(recipes_in: Vec>, map_in: InitialMap) -> Gamedata { let mut item_names = Vec::new(); let mut tile_names = Vec::new(); let mut recipes = Vec::new(); @@ -52,8 +63,18 @@ pub fn build_gamedata(recipes_in: Vec>) -> Gamedata { }) } + let mut initial_map = HashMap::new(); + for (y, line) in map_in.map.iter().enumerate() { + for (x, tile) in line.trim().char_indices() { + debug!("{tile:?}"); + let tile = register(&mut tile_names, map_in.tiles[&tile.to_string()].clone()); + initial_map.insert(IVec2::new(x as i32, y as i32), tile); + } + } + Gamedata { recipes, + initial_map, item_names, tile_names, } -- cgit v1.2.3-70-g09d2