25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

85 satır
2.5 KiB

  1. pub mod components;
  2. pub mod constants;
  3. pub mod debug;
  4. pub mod error;
  5. pub mod misc;
  6. pub mod render;
  7. pub mod resources;
  8. pub mod systems;
  9. use specs::{Dispatcher, DispatcherBuilder, Entity, World};
  10. pub use error::Error;
  11. use debug::{Fleets as DebugFleets, Ships as DebugShips, Summary as DebugSummary};
  12. use misc::{Events, TextManager, Window};
  13. use render::{Asteroids, Init, Planets, SelectFleet, Ships};
  14. use resources::{Camera, Config, Geometry, InputState, PlayerState, Uniform};
  15. use systems::{FleetInfoUpdate, StateUpdate};
  16. pub struct App<'a, 'b> {
  17. is_running: bool,
  18. events: Events,
  19. window: Window,
  20. dispatcher: Dispatcher<'a, 'b>,
  21. }
  22. impl<'a, 'b> App<'a, 'b> {
  23. pub fn new(world: &mut World, player_id: Entity) -> Result<Self, Error> {
  24. let config = Config::new(world)?;
  25. let events = Events::new(world)?;
  26. let window = Window::new(events.handle(), &config)?;
  27. let camera = Camera::new()?;
  28. let uniform = Uniform::new()?;
  29. let geometry = Geometry::new(world)?;
  30. let input_state = InputState::default();
  31. let player_state = PlayerState::new(player_id);
  32. world.insert(config);
  33. world.insert(camera);
  34. world.insert(uniform);
  35. world.insert(geometry);
  36. world.insert(input_state);
  37. world.insert(player_state);
  38. let text_manager = TextManager::new(world)?;
  39. let mut dispatcher = DispatcherBuilder::new()
  40. .with(StateUpdate::new(world)?, "state_update", &[])
  41. .with(FleetInfoUpdate::new(world), "fleet_info_update", &[])
  42. .with_thread_local(Init::new(world)?)
  43. .with_thread_local(Planets::new(world)?)
  44. .with_thread_local(Asteroids::new(world)?)
  45. .with_thread_local(Ships::new(world)?)
  46. .with_thread_local(SelectFleet::new(world, &text_manager)?)
  47. // .with_thread_local(DebugShips::default())
  48. // .with_thread_local(DebugFleets::default())
  49. .with_thread_local(DebugSummary::new(&text_manager)?)
  50. .build();
  51. dispatcher.setup(world);
  52. Ok(Self {
  53. is_running: true,
  54. events,
  55. window,
  56. dispatcher,
  57. })
  58. }
  59. pub fn is_running(&self) -> bool {
  60. self.is_running
  61. }
  62. pub fn process(&mut self, world: &World) -> Result<(), Error> {
  63. self.events.process(world, &self.window);
  64. self.dispatcher.dispatch(world);
  65. self.window.swap_buffers()?;
  66. self.is_running = !world.fetch::<InputState>().close_requested;
  67. Ok(())
  68. }
  69. }