Load level from file

This commit is contained in:
Timothy Warren 2020-07-27 14:30:34 -04:00
parent 9e0b822dfc
commit 6b59710964
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::{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")