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.

89 lines
1.8 KiB

  1. use glc::{matrix::Angle, vector::Vector2f};
  2. use serde::{Deserialize, Serialize};
  3. use specs::{
  4. error::NoError,
  5. saveload::{ConvertSaveload, Marker},
  6. Component, Entity, VecStorage,
  7. };
  8. #[derive(Clone, Debug)]
  9. pub struct Ship {
  10. pub type_: Type,
  11. pub fleet: Entity,
  12. pub agility: Angle<f32>,
  13. pub target_pos: Vector2f,
  14. pub target_dir: Vector2f,
  15. }
  16. #[derive(Clone, Debug, Serialize, Deserialize)]
  17. pub enum Type {
  18. Fighter,
  19. Bomber,
  20. Transporter,
  21. }
  22. #[derive(Serialize, Deserialize)]
  23. pub struct ShipData<M> {
  24. pub type_: Type,
  25. pub fleet: M,
  26. pub agility: Angle<f32>,
  27. pub target_pos: Vector2f,
  28. pub target_dir: Vector2f,
  29. }
  30. impl Component for Ship {
  31. type Storage = VecStorage<Self>;
  32. }
  33. impl<M> ConvertSaveload<M> for Ship
  34. where
  35. for<'de> M: Marker + Serialize + Deserialize<'de>,
  36. {
  37. type Data = ShipData<M>;
  38. type Error = NoError;
  39. fn convert_into<F>(&self, mut ids: F) -> Result<Self::Data, Self::Error>
  40. where
  41. F: FnMut(Entity) -> Option<M>,
  42. {
  43. let Ship {
  44. type_,
  45. fleet,
  46. agility,
  47. target_pos,
  48. target_dir,
  49. } = self.clone();
  50. let fleet = ids(fleet).unwrap();
  51. Ok(ShipData {
  52. type_,
  53. fleet,
  54. agility,
  55. target_pos,
  56. target_dir,
  57. })
  58. }
  59. fn convert_from<F>(data: Self::Data, mut ids: F) -> Result<Self, Self::Error>
  60. where
  61. F: FnMut(M) -> Option<Entity>,
  62. {
  63. let ShipData {
  64. type_,
  65. fleet,
  66. agility,
  67. target_pos,
  68. target_dir,
  69. } = data;
  70. let fleet = ids(fleet).unwrap();
  71. Ok(Ship {
  72. type_,
  73. fleet,
  74. agility,
  75. target_pos,
  76. target_dir,
  77. })
  78. }
  79. }