You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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