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.

84 lines
2.4 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, Ships};
  14. use resources::{Camera, Config, Geometry, PlayerState, State, 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 state = State::default();
  28. let camera = Camera::new()?;
  29. let uniform = Uniform::new()?;
  30. let geometry = Geometry::new(world)?;
  31. let player_state = PlayerState::new(player_id);
  32. world.insert(state);
  33. world.insert(config);
  34. world.insert(camera);
  35. world.insert(uniform);
  36. world.insert(geometry);
  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(DebugShips::default())
  47. .with_thread_local(DebugFleets::default())
  48. .with_thread_local(DebugSummary::new(&text_manager)?)
  49. .build();
  50. dispatcher.setup(world);
  51. Ok(Self {
  52. is_running: true,
  53. events,
  54. window,
  55. dispatcher,
  56. })
  57. }
  58. pub fn is_running(&self) -> bool {
  59. self.is_running
  60. }
  61. pub fn process(&mut self, world: &World) -> Result<(), Error> {
  62. self.events.process(world, &self.window);
  63. self.dispatcher.dispatch(world);
  64. self.window.swap_buffers()?;
  65. self.is_running = !world.fetch::<State>().close_requested;
  66. Ok(())
  67. }
  68. }