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.

171 lines
4.5 KiB

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