main.rs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. world.insert(ncollide2d::world::CollisionWorld::<f32, Option<specs::Entity>>::new(0.1));
  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 mut my_game = MyGame { world };
  75. event::run(&mut ctx, &mut evloop, &mut my_game)
  76. }