From 6ca2dbc3f66c835826f2371c5fbda5c4d872814e Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 23 Jul 2020 11:55:36 -0400 Subject: [PATCH] Finish chapter 1 --- Cargo.toml | 11 ++ resources/images/box.png | Bin 0 -> 197 bytes resources/images/box_spot.png | Bin 0 -> 162 bytes resources/images/floor.png | Bin 0 -> 131 bytes resources/images/player.png | Bin 0 -> 264 bytes resources/images/wall.png | Bin 0 -> 167 bytes src/main.rs | 212 ++++++++++++++++++++++++++++++++++ 7 files changed, 223 insertions(+) create mode 100644 Cargo.toml create mode 100644 resources/images/box.png create mode 100644 resources/images/box_spot.png create mode 100644 resources/images/floor.png create mode 100644 resources/images/player.png create mode 100644 resources/images/wall.png create mode 100644 src/main.rs diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..3a4b628 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "rust-sokoban" +version = "0.1.0" +authors = ["Timothy Warren "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +ggez = "0.5.1" +specs = { version="0.15.0", features=["specs-derive"] } \ No newline at end of file diff --git a/resources/images/box.png b/resources/images/box.png new file mode 100644 index 0000000000000000000000000000000000000000..38f61e4d570cb407bbed637fcde228f1d337d0cf GIT binary patch literal 197 zcmeAS@N?(olHy`uVBq!ia0vp^3LwnE1|*BCs=fdz&H|6fVg?5OL=a|NVZS^RC|K_4 z;usRanY^d!tJYx$Cb0=mPo6yapZ${u5CakFnGH9xvX_ literal 0 HcmV?d00001 diff --git a/resources/images/box_spot.png b/resources/images/box_spot.png new file mode 100644 index 0000000000000000000000000000000000000000..c5f659e17b8cdb4d8fef933b45b17ebe812f82e4 GIT binary patch literal 162 zcmeAS@N?(olHy`uVBq!ia0vp^3LwnE1|*BCs=fdz&H|6fVg?5OL=a|NVZS^RC>Y}D z;usRa`S#jIUZ851K*PLKF+E3Zq^g|~ADZ45a#rv+j7sBsI_uPzopr E0CI6LDgXcg literal 0 HcmV?d00001 diff --git a/resources/images/floor.png b/resources/images/floor.png new file mode 100644 index 0000000000000000000000000000000000000000..ee2711c80c9e4487075ee9945269c921ff94da18 GIT binary patch literal 131 zcmeAS@N?(olHy`uVBq!ia0vp^3LwnE1|*BCs=fdz&H|6fVg?5OL=a|NVZS^RC}`^G z;usRa`SyY%FM|OO^Tu_5Ub3E7>vgf?YwWI>bVoC<`|bR)1_nkZ77hUg2PmT=&WCwl U#0IX9Km!;&UHx3vIVCg!0H%K-*#H0l literal 0 HcmV?d00001 diff --git a/resources/images/player.png b/resources/images/player.png new file mode 100644 index 0000000000000000000000000000000000000000..a8f3b5b43eaff750678faf0af9ce52fcda401e5a GIT binary patch literal 264 zcmeAS@N?(olHy`uVBq!ia0vp^3LwnE1|*BCs=fdz&H|6fVg?5OL=a|NVZS^RD7eeh z#W5s;^X;@iz6J#z=c5(D=ZxhK^9(hxLP1e)8;R%cU) z6NaOUC6*@XZmP)UxoyZJK z;usRa`F4sSA5b;((H|QB4)3m5t+sB%%o|T7tQ~^&Ov~>(TLf49o_DOlrtzxbgqd^y ztZ{X@sp!wZ>?SCxW6$m2A$X9%m4*2ei-yuq?k8*@(x8ALe { + context: &'a mut Context, +} + +impl<'a> System<'a> for RenderingSystem<'a> { + type SystemData = (ReadStorage<'a, Position>, ReadStorage<'a, Renderable>); + + fn run(&mut self, data: Self::SystemData) { + let (positions, renderables) = data; + + // Clear the screen/set the background + graphics::clear(self.context, graphics::Color::new(0.95, 0.95, 0.95, 1.0)); + + // Get all the renderables with their positions and sort by the position z + // This will allow us to have entities layered visually. + let mut rendering_data = (&positions, &renderables).join().collect::>(); + rendering_data.sort_by(|&a, &b| a.0.z.partial_cmp(&b.0.z).expect("expected comparison")); + + // Iterate through all paris of positions & renderables, load the image + // and draw it at the specified position. + for (position, renderable) in rendering_data.iter() { + // Load the image + let image = Image::new(self.context, renderable.path.clone()).expect("expected image"); + let x = position.x as f32 * TILE_WIDTH; + let y = position.y as f32 * TILE_WIDTH; + + // draw + let draw_params = DrawParam::new().dest(na::Point2::new(x, y)); + graphics::draw(self.context, &image, draw_params).expect("expected render"); + } + + // Finally, present the context, this will actually display everything + // on the screen. + graphics::present(self.context).expect("expected to present"); + } +} + +// All the game state +struct Game { + world: World, +} + +impl event::EventHandler for Game { + fn update(&mut self, _context: &mut Context) -> GameResult { + Ok(()) + } + + fn draw(&mut self, context: &mut Context) -> GameResult { + // Render gaem entities + { + let mut rs = RenderingSystem { context }; + rs.run_now(&self.world); + } + + Ok(()) + } +} + +pub fn register_components(world: &mut World) { + world.register::(); + world.register::(); + world.register::(); + world.register::(); + world.register::(); + world.register::(); +} + +pub fn create_wall(world: &mut World, position: Position) { + world + .create_entity() + .with(Position { z: 10, ..position }) + .with(Renderable { + path: "/images/wall.png".to_string(), + }) + .with(Wall {}) + .build(); +} + +pub fn create_floor(world: &mut World, position: Position) { + world + .create_entity() + .with(Position { z: 5, ..position }) + .with(Renderable { + path: "/images/floor.png".to_string(), + }) + .build(); +} + +pub fn create_box(world: &mut World, position: Position) { + world + .create_entity() + .with(Position { z: 10, ..position }) + .with(Renderable { + path: "/images/box.png".to_string(), + }) + .build(); +} + +pub fn create_box_spot(world: &mut World, position: Position) { + world + .create_entity() + .with(Position { z: 9, ..position }) + .with(Renderable { + path: "/images/box_spot.png".to_string(), + }) + .with(BoxSpot {}) + .build(); +} + +pub fn create_player(world: &mut World, position: Position) { + world + .create_entity() + .with(Position { z: 10, ..position }) + .with(Renderable { + path: "/images/player.png".to_string(), + }) + .with(Player {}) + .build(); +} + +pub fn initialize_level(world: &mut World) { + create_player( + world, + Position { + x: 0, + y: 0, + z: 0, // we will get the z from the factory functions + }, + ); + create_wall( + world, + Position { + x: 1, + y: 0, + z: 0, // we will get the z from the factory functions + }, + ); + create_box( + world, + Position { + x: 2, + y: 0, + z: 0, // we will get the z from the factory functions + }, + ); +} + + +pub fn main() -> GameResult { + let mut world = World::new(); + register_components(&mut world); + 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) +}