Load level from file #1

Merged
timw4mail merged 1 commits from develop into master 2020-07-27 14:37:21 -04:00
2 changed files with 12 additions and 17 deletions

View File

@ -3,6 +3,9 @@ use ggez::event::KeyCode;
use ggez::event::KeyMods; use ggez::event::KeyMods;
use ggez::{conf, event, timer, Context, GameResult}; use ggez::{conf, event, timer, Context, GameResult};
use specs::{RunNow, World, WorldExt}; use specs::{RunNow, World, WorldExt};
use std::io::prelude::*;
use std::fs::File;
use std::path; use std::path;
mod audio; mod audio;
@ -76,23 +79,15 @@ impl event::EventHandler for Game {
} }
} }
pub fn initialize_level(world: &mut World) { pub fn initialize_level(world: &mut World, level: u32) {
const MAP: &str = " let map_file = format!("resources/maps/{:02}.txt", level);
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
";
// @TODO: load map from file let mut file = File::open(&map_file).expect("Failed to open map file");
load_map(world, MAP.to_string()); 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 { pub fn main() -> GameResult {
@ -101,7 +96,7 @@ pub fn main() -> GameResult {
register_resources(&mut world); register_resources(&mut world);
// @TODO multiple levels // @TODO multiple levels
initialize_level(&mut world); initialize_level(&mut world, 1);
// Create a game context and event loop // Create a game context and event loop
let context_builder = ggez::ContextBuilder::new("rust_sokoban", "Timothy J. Warren") let context_builder = ggez::ContextBuilder::new("rust_sokoban", "Timothy J. Warren")