Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

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