main.rs 2.6 KB

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