use glc::vector::Vector4f; use log::{error, info}; use rand::random; use space_crush_app::{App, Error}; use space_crush_common::{ components::Player, misc::{init_logger, Vfs}, Dispatcher, }; use specs::{Builder, Entity, World, WorldExt}; fn main() -> Result<(), Error> { let vfs = Vfs::new(&["space-crush-app"])?; init_logger(&vfs, "log4rs.yml"); vfs.log_info(); info!("Application started"); if let Err(err) = run(vfs) { error!("Error while executing application: {}", err); return Err(err); } info!("Application exited"); Ok(()) } fn run(vfs: Vfs) -> Result<(), Error> { info!("Application initialization"); let mut world = World::new(); world.insert(vfs); let mut common = Dispatcher::new(&mut world)?; let player1 = world .create_entity() .with(Player::new(Vector4f::new(0.0, 0.5, 1.0, 0.1))) .build(); let mut app = App::new(&mut world, player1)?; create_world(&mut world, player1); info!("Application initialized"); while app.is_running() { world.maintain(); common.process(&world); app.process(&world)?; } Ok(()) } fn create_world(world: &mut World, player_id: Entity) { use glc::{matrix::Angle, vector::Vector2f}; use space_crush_common::{ components::{ Asteroid, AsteroidType, Fleet, FleetOwned, Obstacle, Orbit, OrbitOwned, Planet, PlayerOwned, Position, Ship, ShipType, Velocity, }, misc::{PersistWorld, Persistence}, }; use specs::saveload::MarkedBuilder; PersistWorld::setup(world); let planets = (0..3) .map(|i| { let x = 2000.0 * (i as f32 - 1.0); let y = 0.0; world .create_entity() .marked::<::Marker>() .with(PlayerOwned::new(player_id)) .with(Position::circle(Vector2f::new(x, y), 250.0)) .with(Orbit::new(325.0, 425.0)) .with(Obstacle {}) .with(Planet {}) .build() }) .collect::>(); for i in 0..4 { let i_x: isize = if i & 1 == 0 { -1 } else { 1 }; let i_y: isize = if i & 2 == 0 { -1 } else { 1 }; let x = 1000.0 * i_x as f32; let y = 1000.0 * i_y as f32; world .create_entity() .marked::<::Marker>() .with(Position::circle(Vector2f::new(x, y), 100.0)) .with(Orbit::new(125.0, 175.0)) .with(Obstacle {}) .with(Asteroid::new(match i_x * i_y { -1 => AsteroidType::Metal, 1 => AsteroidType::Crystal, _ => unreachable!(), })) .build(); } let fleet_id = world .create_entity() .marked::<::Marker>() .with(PlayerOwned::new(player_id)) .with(OrbitOwned::new(planets[1])) .with(Fleet::default()) .build(); for i in 0..9 { let r = 325.0 + 100.0 * random::(); let a = Angle::Deg(360.0 * random::()); let x = r * a.cos(); let y = r * a.sin(); world .create_entity() .marked::<::Marker>() .with(PlayerOwned::new(player_id)) .with(FleetOwned::new(fleet_id)) .with(Position::dot(Vector2f::new(x, y))) .with(Velocity::new( Vector2f::new(random::() - 0.5, random::() - 0.5).normalize(), 100.0, )) .with(Ship::new(match i % 3 { 0 => ShipType::Fighter, 1 => ShipType::Bomber, 2 => ShipType::Transporter, _ => unreachable!(), })) .build(); } }