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.

90 lines
2.7 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::{
  12. Fleets as DebugFleets, Orbits as DebugOrbits, Ships as DebugShips, Summary as DebugSummary,
  13. };
  14. use misc::{Events, TextManager, Window};
  15. use render::{Asteroids, FleetMove, FleetSelect, Init, Planets, Ships};
  16. use resources::{Camera, Config, GameState, Geometry, InputState, Uniform};
  17. use systems::InputStateUpdate;
  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 mut camera = Camera::new()?;
  30. let uniform = Uniform::new()?;
  31. let geometry = Geometry::new(world)?;
  32. let input_state = InputState::default();
  33. let game_state = GameState::new(world, player_id);
  34. camera.update(Matrix4f::scale(0.25))?;
  35. world.insert(config);
  36. world.insert(camera);
  37. world.insert(uniform);
  38. world.insert(geometry);
  39. world.insert(input_state);
  40. world.insert(game_state);
  41. let text_manager = TextManager::new(world)?;
  42. let mut dispatcher = DispatcherBuilder::new()
  43. .with(InputStateUpdate::new(world)?, "input_state_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(FleetSelect::new(world, &text_manager)?)
  49. .with_thread_local(FleetMove::new(world)?)
  50. .with_thread_local(DebugShips::default())
  51. .with_thread_local(DebugOrbits::default())
  52. .with_thread_local(DebugFleets::new(&text_manager)?)
  53. .with_thread_local(DebugSummary::new(&text_manager)?)
  54. .build();
  55. dispatcher.setup(world);
  56. Ok(Self {
  57. is_running: true,
  58. events,
  59. window,
  60. dispatcher,
  61. })
  62. }
  63. pub fn is_running(&self) -> bool {
  64. self.is_running
  65. }
  66. pub fn process(&mut self, world: &World) -> Result<(), Error> {
  67. self.events.process(world, &self.window);
  68. self.dispatcher.dispatch(world);
  69. self.window.swap_buffers()?;
  70. self.is_running = !world.fetch::<InputState>().close_requested;
  71. Ok(())
  72. }
  73. }