main.rs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #[macro_use]
  2. extern crate specs_derive;
  3. use ggez::{
  4. event::{self, EventHandler},
  5. Context, ContextBuilder, GameResult,
  6. };
  7. use specs::world::WorldExt;
  8. pub mod components;
  9. pub mod consts;
  10. pub mod game;
  11. pub mod resources;
  12. pub mod sys;
  13. pub mod types;
  14. use game::MyGame;
  15. impl EventHandler for MyGame {
  16. fn update(&mut self, _ctx: &mut Context) -> GameResult<()> {
  17. sys::input::systems(self);
  18. sys::physics::systems(self);
  19. Ok(())
  20. }
  21. fn draw(&mut self, ctx: &mut Context) -> GameResult<()> {
  22. sys::drawing::systems(self, ctx)
  23. }
  24. fn key_down_event(
  25. &mut self,
  26. ctx: &mut Context,
  27. keycode: winit::VirtualKeyCode,
  28. _keymod: ggez::event::KeyMods,
  29. _repeat: bool,
  30. ) {
  31. if keycode == winit::VirtualKeyCode::Escape {
  32. ggez::event::quit(ctx);
  33. }
  34. self.world.write_resource::<resources::KeySet>().insert(keycode);
  35. }
  36. fn key_up_event(
  37. &mut self,
  38. _ctx: &mut Context,
  39. keycode: winit::VirtualKeyCode,
  40. _keymod: ggez::event::KeyMods,
  41. ) {
  42. self.world.write_resource::<resources::KeySet>().remove(&keycode);
  43. }
  44. }
  45. fn main() -> Result<(), ggez::error::GameError> {
  46. let mut world = specs::World::new();
  47. components::register(&mut world);
  48. world.insert(ncollide2d::world::CollisionWorld::<f32, specs::Entity>::new(0.1));
  49. resources::world_from_file(&mut world, "assets/main.tmx");
  50. // Make a Context and an EventLoop.
  51. let (mut ctx, mut evloop) = ContextBuilder::new("game", "me")
  52. .add_resource_path({
  53. let base = std::env::var("CARGO_MANIFEST_DIR").unwrap();
  54. let mut path = std::path::PathBuf::from(base);
  55. path.push("assets");
  56. path
  57. })
  58. .window_mode(ggez::conf::WindowMode {
  59. width: consts::WINDOW_WIDTH,
  60. height: consts::WINDOW_HEIGHT,
  61. ..Default::default()
  62. })
  63. .build()?;
  64. let image = ggez::graphics::Image::new(&mut ctx, "/spritesheet.png")?;
  65. let mut sprites = ggez::graphics::spritebatch::SpriteBatch::new(image);
  66. sprites.set_filter(ggez::graphics::FilterMode::Nearest);
  67. world.insert(sprites);
  68. world.insert(resources::KeySet::new());
  69. let mut my_game = MyGame { world };
  70. event::run(&mut ctx, &mut evloop, &mut my_game)
  71. }