main.rs 2.6 KB

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