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.

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