From 6b597109645d64a5eab4b7c19c395b5104ccaf79 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Mon, 27 Jul 2020 14:30:34 -0400 Subject: [PATCH] Load level from file --- resources/maps/{00.txt => 01.txt} | 0 src/main.rs | 29 ++++++++++++----------------- 2 files changed, 12 insertions(+), 17 deletions(-) rename resources/maps/{00.txt => 01.txt} (100%) diff --git a/resources/maps/00.txt b/resources/maps/01.txt similarity index 100% rename from resources/maps/00.txt rename to resources/maps/01.txt diff --git a/src/main.rs b/src/main.rs index bd232ed..4fe8749 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,9 @@ use ggez::event::KeyCode; use ggez::event::KeyMods; use ggez::{conf, event, timer, Context, GameResult}; use specs::{RunNow, World, WorldExt}; + +use std::io::prelude::*; +use std::fs::File; use std::path; mod audio; @@ -76,23 +79,15 @@ impl event::EventHandler for Game { } } -pub fn initialize_level(world: &mut World) { - const MAP: &str = " - N N N N X X X X X N N N N N N N N N N N N N - N N N N X . . . X N N N N N N N N N N N N N - N N N N X * . . X N N N N N N N N N N N N N - N N X X X . . * X X X N N N N N N N N N N N - N N X . . * . . * . X N N N N N N N N N N N - X X X . X . X X X . X N N N N N X X X X X X - X . . . X . X X X . X X X X X X X . . % % X - X . * . . * . . . . . . . . . . . . . % % X - X X X X X . X X X X . X @ X X X X . . % % X - N N N N X . . . . . . X X X N N X X X X X X - N N N N X X X X X X X X N N N N N N N N N N - "; +pub fn initialize_level(world: &mut World, level: u32) { + let map_file = format!("resources/maps/{:02}.txt", level); - // @TODO: load map from file - load_map(world, MAP.to_string()); + let mut file = File::open(&map_file).expect("Failed to open map file"); + let mut map = String::new(); + file.read_to_string(&mut map).expect("failed to read map file"); + + + load_map(world, map); } pub fn main() -> GameResult { @@ -101,7 +96,7 @@ pub fn main() -> GameResult { register_resources(&mut world); // @TODO multiple levels - initialize_level(&mut world); + initialize_level(&mut world, 1); // Create a game context and event loop let context_builder = ggez::ContextBuilder::new("rust_sokoban", "Timothy J. Warren")