|
- pub mod constants;
- pub mod debug;
- pub mod error;
- pub mod misc;
- pub mod render;
- pub mod resources;
- pub mod systems;
-
- use glc::matrix::Matrix4f;
- use specs::{Dispatcher, DispatcherBuilder, Entity, World};
-
- pub use error::Error;
-
- use debug::{
- Fleets as DebugFleets, Orbits as DebugOrbits, Ships as DebugShips, Summary as DebugSummary,
- };
- use misc::{Events, TextManager, Window};
- use render::{Asteroids, FleetMove, FleetSelect, Init, Planets, Ships};
- use resources::{Camera, Config, GameState, Geometry, InputState, Uniform};
- use systems::InputStateUpdate;
-
- pub struct App<'a, 'b> {
- is_running: bool,
- events: Events,
- window: Window,
- dispatcher: Dispatcher<'a, 'b>,
- }
-
- impl<'a, 'b> App<'a, 'b> {
- pub fn new(world: &mut World, player_id: Entity) -> Result<Self, Error> {
- let config = Config::new(world)?;
- let events = Events::new(world)?;
- let window = Window::new(events.handle(), &config)?;
-
- let mut camera = Camera::new()?;
- let uniform = Uniform::new()?;
- let geometry = Geometry::new(world)?;
- let input_state = InputState::default();
- let game_state = GameState::new(world, player_id);
-
- camera.update(Matrix4f::scale(0.25))?;
-
- world.insert(config);
- world.insert(camera);
- world.insert(uniform);
- world.insert(geometry);
- world.insert(input_state);
- world.insert(game_state);
-
- let text_manager = TextManager::new(world)?;
-
- let mut dispatcher = DispatcherBuilder::new()
- .with(InputStateUpdate::new(world)?, "input_state_update", &[])
- .with_thread_local(Init::new(world)?)
- .with_thread_local(Planets::new(world)?)
- .with_thread_local(Asteroids::new(world)?)
- .with_thread_local(Ships::new(world)?)
- .with_thread_local(FleetSelect::new(world, &text_manager)?)
- .with_thread_local(FleetMove::new(world)?)
- .with_thread_local(DebugShips::default())
- .with_thread_local(DebugOrbits::default())
- .with_thread_local(DebugFleets::new(&text_manager)?)
- .with_thread_local(DebugSummary::new(&text_manager)?)
- .build();
- dispatcher.setup(world);
-
- Ok(Self {
- is_running: true,
- events,
- window,
- dispatcher,
- })
- }
-
- pub fn is_running(&self) -> bool {
- self.is_running
- }
-
- pub fn process(&mut self, world: &World) -> Result<(), Error> {
- self.events.process(world, &self.window);
- self.dispatcher.dispatch(world);
-
- self.window.swap_buffers()?;
-
- self.is_running = !world.fetch::<InputState>().close_requested;
-
- Ok(())
- }
- }
|