|
- use log::{error, info};
- use rand::random;
- use space_crush_app::{App, Error};
- use space_crush_common::{
- misc::{init_logger, Vfs},
- Dispatcher,
- };
- use specs::{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> {
- let mut world = World::new();
- world.insert(vfs);
-
- let mut common = Dispatcher::new(&mut world);
- let mut app = App::new(&mut world)?;
-
- if !load_world(&mut world, WORLD_FILEPATH)? {
- create_world(&mut world);
- }
-
- while app.is_running() {
- world.maintain();
-
- common.process(&world);
- app.process(&world)?;
- }
-
- save_world(&mut world, WORLD_FILEPATH)?;
-
- Ok(())
- }
-
- fn create_world(world: &mut World) {
- use glc::{
- matrix::Angle,
- vector::{Vector2f, Vector4f},
- };
- use space_crush_common::{
- components::{Fleet, Owned, Planet, Player, Position, Ship, ShipType, Velocity},
- misc::{PersistWorld, Persistence},
- };
- use specs::{saveload::MarkedBuilder, Builder};
-
- let player1 = world
- .create_entity()
- .with(Player {
- color: Vector4f::new(0.0, 0.5, 1.0, 0.1),
- })
- .build();
-
- let planet = world
- .create_entity()
- .marked::<<PersistWorld as Persistence>::Marker>()
- .with(Owned { owner: player1 })
- .with(Position {
- pos: Vector2f::default(),
- size: 250.0,
- })
- .with(Fleet {
- orbit_min: 325.0,
- orbit_max: 425.0,
- })
- .with(Planet {})
- .build();
-
- for i in 0..10 {
- world
- .create_entity()
- .marked::<<PersistWorld as Persistence>::Marker>()
- .with(Owned { owner: player1 })
- .with(Position {
- pos: Vector2f::new(
- 500.0 * random::<f32>() - 250.0,
- 500.0 * random::<f32>() - 250.0,
- ),
- size: 15.0,
- })
- .with(Velocity {
- dir: Vector2f::new(random::<f32>() - 0.5, random::<f32>() - 0.5).normalize(),
- speed: 100.0,
- })
- .with(Ship {
- type_: match i % 3 {
- 0 => ShipType::Fighter,
- 1 => ShipType::Bomber,
- 2 => ShipType::Transporter,
- _ => unreachable!(),
- },
- fleet: planet,
- agility: Angle::Deg(360.0),
- target_pos: Default::default(),
- target_dir: Default::default(),
- })
- .build();
- }
- }
-
- fn load_world(world: &mut World, path: &str) -> Result<bool, Error> {
- use serde_json::de::{Deserializer, IoRead};
- use space_crush_common::misc::{PersistWorld, Persistence, WorldHelper};
-
- PersistWorld::setup(world);
-
- let vfs = world.resource::<Vfs>()?;
- let path = vfs.join(path)?;
- if !path.exists() {
- return Ok(false);
- }
-
- let mut file = path.open_file()?;
- let mut read = IoRead::new(&mut file);
- let mut deserializer = Deserializer::new(&mut read);
- world.deserialize(PersistWorld, &mut deserializer)?;
-
- Ok(true)
- }
-
- fn save_world(world: &mut World, path: &str) -> Result<(), Error> {
- use serde_json::Serializer;
- use space_crush_common::misc::{PersistWorld, WorldHelper};
-
- let vfs = world.resource::<Vfs>()?;
- let mut file = vfs.join(path)?.create_file()?;
- let mut serializer = Serializer::new(&mut file);
- world.serialize(PersistWorld, &mut serializer)?;
-
- Ok(())
- }
-
- const WORLD_FILEPATH: &str = "resources/world.json";
|