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
3.3 KiB

  1. use serde::{Deserialize, Serialize};
  2. use smallvec::smallvec;
  3. use smallvec::SmallVec;
  4. use specs::{
  5. error::NoError,
  6. saveload::{ConvertSaveload, Marker},
  7. Component, Entity, HashMapStorage,
  8. };
  9. use crate::misc::FlaggedStorage;
  10. #[derive(Clone, Debug, Default)]
  11. pub struct Orbit {
  12. min: f32,
  13. max: f32,
  14. fleets: Fleets,
  15. }
  16. #[derive(Serialize, Deserialize)]
  17. pub struct OrbitData<M> {
  18. pub min: f32,
  19. pub max: f32,
  20. pub fleets: Vec<Option<M>>,
  21. }
  22. #[derive(Copy, Clone, Debug)]
  23. pub struct Owned {
  24. owner: Entity,
  25. }
  26. #[derive(Serialize, Deserialize)]
  27. pub struct OwnedData<M> {
  28. pub owner: M,
  29. }
  30. type Fleets = SmallVec<[Option<Entity>; 8]>;
  31. impl Orbit {
  32. #[inline]
  33. pub fn new(min: f32, max: f32) -> Self {
  34. Self {
  35. min,
  36. max,
  37. fleets: smallvec![],
  38. }
  39. }
  40. #[inline]
  41. pub fn min(&self) -> f32 {
  42. self.min
  43. }
  44. #[inline]
  45. pub fn max(&self) -> f32 {
  46. self.max
  47. }
  48. #[inline]
  49. pub fn fleets(&self) -> &Fleets {
  50. &self.fleets
  51. }
  52. #[inline]
  53. pub(crate) fn fleets_mut(&mut self) -> &mut Fleets {
  54. &mut self.fleets
  55. }
  56. #[inline]
  57. pub fn fleet(&self, index: usize) -> Option<Entity> {
  58. self.fleets.get(index).cloned().flatten()
  59. }
  60. #[inline]
  61. pub(crate) fn fleet_mut(&mut self, index: usize) -> &mut Option<Entity> {
  62. self.fleets.resize_with(index + 1, Default::default);
  63. &mut self.fleets[index]
  64. }
  65. }
  66. impl Component for Orbit {
  67. type Storage = HashMapStorage<Self>;
  68. }
  69. impl<M> ConvertSaveload<M> for Orbit
  70. where
  71. for<'de> M: Marker + Serialize + Deserialize<'de>,
  72. {
  73. type Data = OrbitData<M>;
  74. type Error = NoError;
  75. fn convert_into<F>(&self, mut ids: F) -> Result<Self::Data, Self::Error>
  76. where
  77. F: FnMut(Entity) -> Option<M>,
  78. {
  79. let Orbit { min, max, fleets } = self;
  80. let min = *min;
  81. let max = *max;
  82. let fleets = fleets
  83. .iter()
  84. .cloned()
  85. .map(|e| e.and_then(|e| ids(e)))
  86. .collect();
  87. Ok(OrbitData { min, max, fleets })
  88. }
  89. fn convert_from<F>(data: Self::Data, mut ids: F) -> Result<Self, Self::Error>
  90. where
  91. F: FnMut(M) -> Option<Entity>,
  92. {
  93. let OrbitData { min, max, fleets } = data;
  94. let fleets = fleets.into_iter().map(|e| e.and_then(|e| ids(e))).collect();
  95. Ok(Orbit { min, max, fleets })
  96. }
  97. }
  98. impl Owned {
  99. pub fn new(owner: Entity) -> Self {
  100. Self { owner }
  101. }
  102. pub fn owner(&self) -> Entity {
  103. self.owner
  104. }
  105. pub(crate) fn set_owner(&mut self, value: Entity) {
  106. self.owner = value;
  107. }
  108. }
  109. impl Component for Owned {
  110. type Storage = FlaggedStorage<Self, HashMapStorage<Self>>;
  111. }
  112. impl<M> ConvertSaveload<M> for Owned
  113. where
  114. for<'de> M: Marker + Serialize + Deserialize<'de>,
  115. {
  116. type Data = OwnedData<M>;
  117. type Error = NoError;
  118. fn convert_into<F>(&self, mut ids: F) -> Result<Self::Data, Self::Error>
  119. where
  120. F: FnMut(Entity) -> Option<M>,
  121. {
  122. let owner = ids(self.owner).unwrap();
  123. Ok(OwnedData { owner })
  124. }
  125. fn convert_from<F>(data: Self::Data, mut ids: F) -> Result<Self, Self::Error>
  126. where
  127. F: FnMut(M) -> Option<Entity>,
  128. {
  129. let owner = ids(data.owner).unwrap();
  130. Ok(Owned { owner })
  131. }
  132. }