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.

144 lines
3.8 KiB

  1. use glc::vector::Vector4f;
  2. use log::{error, info};
  3. use rand::random;
  4. use space_crush_app::{App, Error};
  5. use space_crush_common::{
  6. components::Player,
  7. misc::{init_logger, Vfs},
  8. Dispatcher,
  9. };
  10. use specs::{Builder, Entity, World, WorldExt};
  11. fn main() -> Result<(), Error> {
  12. let vfs = Vfs::new(&["space-crush-app"])?;
  13. init_logger(&vfs, "log4rs.yml");
  14. vfs.log_info();
  15. info!("Application started");
  16. if let Err(err) = run(vfs) {
  17. error!("Error while executing application: {}", err);
  18. return Err(err);
  19. }
  20. info!("Application exited");
  21. Ok(())
  22. }
  23. fn run(vfs: Vfs) -> Result<(), Error> {
  24. info!("Application initialization");
  25. let mut world = World::new();
  26. world.insert(vfs);
  27. let mut common = Dispatcher::new(&mut world)?;
  28. let player1 = world
  29. .create_entity()
  30. .with(Player::new(Vector4f::new(0.0, 0.5, 1.0, 0.1)))
  31. .build();
  32. let mut app = App::new(&mut world, player1)?;
  33. create_world(&mut world, player1);
  34. info!("Application initialized");
  35. while app.is_running() {
  36. world.maintain();
  37. common.process(&world);
  38. app.process(&world)?;
  39. }
  40. Ok(())
  41. }
  42. fn create_world(world: &mut World, player_id: Entity) {
  43. use glc::{matrix::Angle, vector::Vector2f};
  44. use space_crush_common::{
  45. components::{
  46. Asteroid, AsteroidType, Fleet, FleetOwned, Obstacle, Orbit, OrbitOwned, Planet,
  47. PlayerOwned, Position, Ship, ShipType, Velocity,
  48. },
  49. misc::{PersistWorld, Persistence},
  50. };
  51. use specs::saveload::MarkedBuilder;
  52. PersistWorld::setup(world);
  53. let planets = (0..3)
  54. .map(|i| {
  55. let x = 2000.0 * (i as f32 - 1.0);
  56. let y = 0.0;
  57. world
  58. .create_entity()
  59. .marked::<<PersistWorld as Persistence>::Marker>()
  60. .with(PlayerOwned::new(player_id))
  61. .with(Position::circle(Vector2f::new(x, y), 250.0))
  62. .with(Orbit::new(325.0, 425.0))
  63. .with(Obstacle {})
  64. .with(Planet {})
  65. .build()
  66. })
  67. .collect::<Vec<_>>();
  68. for i in 0..4 {
  69. let i_x: isize = if i & 1 == 0 { -1 } else { 1 };
  70. let i_y: isize = if i & 2 == 0 { -1 } else { 1 };
  71. let x = 1000.0 * i_x as f32;
  72. let y = 1000.0 * i_y as f32;
  73. world
  74. .create_entity()
  75. .marked::<<PersistWorld as Persistence>::Marker>()
  76. .with(Position::circle(Vector2f::new(x, y), 100.0))
  77. .with(Orbit::new(125.0, 175.0))
  78. .with(Obstacle {})
  79. .with(Asteroid::new(match i_x * i_y {
  80. -1 => AsteroidType::Metal,
  81. 1 => AsteroidType::Crystal,
  82. _ => unreachable!(),
  83. }))
  84. .build();
  85. }
  86. let fleet_id = world
  87. .create_entity()
  88. .marked::<<PersistWorld as Persistence>::Marker>()
  89. .with(PlayerOwned::new(player_id))
  90. .with(OrbitOwned::new(planets[1]))
  91. .with(Fleet::default())
  92. .build();
  93. for i in 0..9 {
  94. let r = 325.0 + 100.0 * random::<f32>();
  95. let a = Angle::Deg(360.0 * random::<f32>());
  96. let x = r * a.cos();
  97. let y = r * a.sin();
  98. world
  99. .create_entity()
  100. .marked::<<PersistWorld as Persistence>::Marker>()
  101. .with(PlayerOwned::new(player_id))
  102. .with(FleetOwned::new(fleet_id))
  103. .with(Position::dot(Vector2f::new(x, y)))
  104. .with(Velocity::new(
  105. Vector2f::new(random::<f32>() - 0.5, random::<f32>() - 0.5).normalize(),
  106. 100.0,
  107. ))
  108. .with(Ship::new(match i % 3 {
  109. 0 => ShipType::Fighter,
  110. 1 => ShipType::Bomber,
  111. 2 => ShipType::Transporter,
  112. _ => unreachable!(),
  113. }))
  114. .build();
  115. }
  116. }