diff options
author | metamuffin <metamuffin@disroot.org> | 2024-06-18 13:28:31 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2024-06-23 19:21:22 +0200 |
commit | 3d870a714348defc40cd519c00b43c87b1be6480 (patch) | |
tree | 5e013dff20bcb41594dbe168f83b32f0f616aa26 /server | |
parent | fdcf100f756f5d2fe8550705a2a10124bfa1c021 (diff) | |
download | hurrycurry-3d870a714348defc40cd519c00b43c87b1be6480.tar hurrycurry-3d870a714348defc40cd519c00b43c87b1be6480.tar.bz2 hurrycurry-3d870a714348defc40cd519c00b43c87b1be6480.tar.zst |
load map from file
Diffstat (limited to 'server')
-rw-r--r-- | server/src/game.rs | 44 | ||||
-rw-r--r-- | server/src/main.rs | 6 | ||||
-rw-r--r-- | server/src/recipes.rs | 23 |
3 files changed, 28 insertions, 45 deletions
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::<PacketC>(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<T = TileIndex, I = ItemIndex> { pub action: Action, } +#[derive(Debug, Clone, Deserialize)] +pub struct InitialMap { + map: Vec<String>, + tiles: HashMap<String, String>, +} + #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Gamedata { pub recipes: Vec<Recipe>, pub item_names: Vec<String>, pub tile_names: Vec<String>, + #[serde(skip)] + pub initial_map: HashMap<IVec2, TileIndex>, } -pub fn build_gamedata(recipes_in: Vec<Recipe<String, String>>) -> Gamedata { +pub fn build_gamedata(recipes_in: Vec<Recipe<String, String>>, 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<Recipe<String, String>>) -> 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, } |