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.

49 lines
1.3 KiB

  1. #![allow(dead_code)]
  2. use specs::{prelude::*, ParJoin, Read, ReadStorage, System, WriteStorage};
  3. use crate::{
  4. components::{Player, PlayerOwned, Position, Ship},
  5. resources::Global,
  6. return_if_none,
  7. };
  8. #[derive(Default)]
  9. pub struct ShipMovement;
  10. #[derive(SystemData)]
  11. pub struct ShipMovementData<'a> {
  12. positions: WriteStorage<'a, Position>,
  13. ships: ReadStorage<'a, Ship>,
  14. player_owned: ReadStorage<'a, PlayerOwned>,
  15. players: ReadStorage<'a, Player>,
  16. global: Read<'a, Global>,
  17. }
  18. impl<'a> System<'a> for ShipMovement {
  19. type SystemData = ShipMovementData<'a>;
  20. fn run(&mut self, data: Self::SystemData) {
  21. let ShipMovementData {
  22. mut positions,
  23. ships,
  24. player_owned,
  25. players,
  26. global,
  27. } = data;
  28. (&mut positions, &player_owned, &ships).par_join().for_each(
  29. |(position, player_owned, ship)| {
  30. let delta = global.delta * global.world_speed;
  31. let position = position.get_mut();
  32. let type_ = ship.type_();
  33. let player_id = player_owned.owner();
  34. let player = return_if_none!(players.get(player_id));
  35. let ship_data = player.ship_data(type_);
  36. *position += ship.dir() * ship_data.speed * delta;
  37. },
  38. );
  39. }
  40. }