rust-sokoban/src/main.rs

100 lines
2.3 KiB
Rust
Raw Normal View History

2020-07-23 15:42:35 -04:00
use ggez;
2020-07-23 18:12:52 -04:00
use ggez::event::KeyCode;
use ggez::event::KeyMods;
2020-07-23 11:55:36 -04:00
use ggez::{conf, event, Context, GameResult};
2020-07-23 18:12:52 -04:00
use specs::{RunNow, World, WorldExt};
2020-07-23 11:55:36 -04:00
use std::path;
2020-07-23 18:12:52 -04:00
mod components;
mod constants;
mod entities;
mod map;
mod resources;
mod systems;
2020-07-23 11:55:36 -04:00
2020-07-23 18:12:52 -04:00
use crate::components::*;
use crate::map::*;
use crate::resources::*;
use crate::systems::*;
2020-07-23 15:42:35 -04:00
2020-07-23 11:55:36 -04:00
// All the game state
struct Game {
world: World,
}
impl event::EventHandler for Game {
fn update(&mut self, _context: &mut Context) -> GameResult {
2020-07-23 15:42:35 -04:00
// Run input system
{
let mut is = InputSystem {};
is.run_now(&self.world);
}
2020-07-24 11:35:21 -04:00
// Run gameplay state system
{
let mut gss = GameplayStateSystem {};
gss.run_now(&self.world);
}
2020-07-23 11:55:36 -04:00
Ok(())
}
fn draw(&mut self, context: &mut Context) -> GameResult {
// Render gaem entities
{
let mut rs = RenderingSystem { context };
rs.run_now(&self.world);
}
Ok(())
}
2020-07-23 15:42:35 -04:00
fn key_down_event(
&mut self,
_context: &mut Context,
keycode: KeyCode,
_keymod: KeyMods,
_repeat: bool,
) {
let mut input_queue = self.world.write_resource::<InputQueue>();
input_queue.keys_pressed.push(keycode);
}
2020-07-23 11:55:36 -04:00
}
2020-07-23 14:01:14 -04:00
pub fn initialize_level(world: &mut World) {
const MAP: &str = "
N N W W W W W W
W W W . . . . W
2020-07-24 17:55:38 -04:00
W . . . BB . . W
W . . RB . . . W
W . P . . . . W
W . . . . RS . W
W . . BS . . . W
2020-07-23 14:01:14 -04:00
W . . . . . . W
W W W W W W W W
";
load_map(world, MAP.to_string());
2020-07-23 11:55:36 -04:00
}
pub fn main() -> GameResult {
let mut world = World::new();
register_components(&mut world);
2020-07-23 15:42:35 -04:00
register_resources(&mut world);
2020-07-23 11:55:36 -04:00
initialize_level(&mut world);
// Create a game context and event loop
let context_builder = ggez::ContextBuilder::new("rust_sokoban", "Timothy J. Warren")
.window_setup(conf::WindowSetup::default().title("Rust Sokoban!"))
.window_mode(conf::WindowMode::default().dimensions(800.0, 600.0))
.add_resource_path(path::PathBuf::from("./resources"));
let (context, event_loop) = &mut context_builder.build()?;
// Create the game state
let game = &mut Game { world };
// Run the main event loop
event::run(context, event_loop, game)
}