forked from tutorials/rust-sokoban
Load level from file #1
29
src/main.rs
29
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")
|
||||
|
Loading…
Reference in New Issue
Block a user