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.

163 lines
4.1 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, Planet, Player, PlayerOwned, Position, Ship,
  46. 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. world
  62. .create_entity()
  63. .marked::<<PersistWorld as Persistence>::Marker>()
  64. .with(Position {
  65. pos: Vector2f::new(500.0, -500.0),
  66. size: 100.0,
  67. })
  68. .with(Fleet {
  69. orbit_min: 125.0,
  70. orbit_max: 175.0,
  71. })
  72. .with(FleetInfo::default())
  73. .with(Asteroid {
  74. type_: AsteroidType::Metal,
  75. })
  76. .build();
  77. world
  78. .create_entity()
  79. .marked::<<PersistWorld as Persistence>::Marker>()
  80. .with(Position {
  81. pos: Vector2f::new(500.0, 500.0),
  82. size: 100.0,
  83. })
  84. .with(Fleet {
  85. orbit_min: 125.0,
  86. orbit_max: 175.0,
  87. })
  88. .with(FleetInfo::default())
  89. .with(Asteroid {
  90. type_: AsteroidType::Crystal,
  91. })
  92. .build();
  93. let planet = world
  94. .create_entity()
  95. .marked::<<PersistWorld as Persistence>::Marker>()
  96. .with(PlayerOwned { owner: player_id })
  97. .with(Position {
  98. pos: Vector2f::default(),
  99. size: 250.0,
  100. })
  101. .with(Fleet {
  102. orbit_min: 325.0,
  103. orbit_max: 425.0,
  104. })
  105. .with(FleetInfo::default())
  106. .with(Planet {})
  107. .build();
  108. for i in 0..10 {
  109. world
  110. .create_entity()
  111. .marked::<<PersistWorld as Persistence>::Marker>()
  112. .with(PlayerOwned { owner: player_id })
  113. .with(Position {
  114. pos: Vector2f::new(
  115. 500.0 * random::<f32>() - 250.0,
  116. 500.0 * random::<f32>() - 250.0,
  117. ),
  118. size: 15.0,
  119. })
  120. .with(Velocity {
  121. dir: Vector2f::new(random::<f32>() - 0.5, random::<f32>() - 0.5).normalize(),
  122. speed: 100.0,
  123. })
  124. .with(FleetOwned { owner: planet })
  125. .with(Ship {
  126. type_: match i % 3 {
  127. 0 => ShipType::Fighter,
  128. 1 => ShipType::Bomber,
  129. 2 => ShipType::Transporter,
  130. _ => unreachable!(),
  131. },
  132. agility: Angle::Deg(360.0),
  133. target_pos: Default::default(),
  134. target_dir: Default::default(),
  135. })
  136. .build();
  137. }
  138. }