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.6 KiB

  1. pub mod constants;
  2. pub mod debug;
  3. pub mod error;
  4. pub mod misc;
  5. pub mod render;
  6. pub mod resources;
  7. pub mod systems;
  8. use glc::matrix::Matrix4f;
  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, FleetSelect, Init, Planets, Ships};
  14. use resources::{Camera, Config, Geometry, InputState, PlayerState, Uniform};
  15. use systems::{FleetControl, 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 mut 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. camera.update(Matrix4f::scale(0.25))?;
  33. world.insert(config);
  34. world.insert(camera);
  35. world.insert(uniform);
  36. world.insert(geometry);
  37. world.insert(input_state);
  38. world.insert(player_state);
  39. let text_manager = TextManager::new(world)?;
  40. let mut dispatcher = DispatcherBuilder::new()
  41. .with(StateUpdate::new(world)?, "state_update", &[])
  42. .with(FleetControl::new(world)?, "fleet_control", &[])
  43. .with_thread_local(Init::new(world)?)
  44. .with_thread_local(Planets::new(world)?)
  45. .with_thread_local(Asteroids::new(world)?)
  46. .with_thread_local(Ships::new(world)?)
  47. .with_thread_local(FleetSelect::new(world, &text_manager)?)
  48. .with_thread_local(DebugShips::default())
  49. .with_thread_local(DebugFleets::new(&text_manager)?)
  50. .with_thread_local(DebugSummary::new(&text_manager)?)
  51. .build();
  52. dispatcher.setup(world);
  53. Ok(Self {
  54. is_running: true,
  55. events,
  56. window,
  57. dispatcher,
  58. })
  59. }
  60. pub fn is_running(&self) -> bool {
  61. self.is_running
  62. }
  63. pub fn process(&mut self, world: &World) -> Result<(), Error> {
  64. self.events.process(world, &self.window);
  65. self.dispatcher.dispatch(world);
  66. self.window.swap_buffers()?;
  67. self.is_running = !world.fetch::<InputState>().close_requested;
  68. Ok(())
  69. }
  70. }