Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

129 wiersze
2.8 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::{builder::MeetingPointBuilder, misc::FlaggedStorage};
  10. /// Point in the universe fleets can meet
  11. #[derive(Clone, Debug, Default)]
  12. pub struct MeetingPoint {
  13. min: f32,
  14. max: f32,
  15. fleets: Fleets,
  16. }
  17. /// Entities with this component are fleets by an meeting point
  18. #[derive(Copy, Clone, Debug)]
  19. pub struct MeetingPointOwned {
  20. owner: Entity,
  21. }
  22. #[derive(Serialize, Deserialize)]
  23. pub struct MeetingPointData<M> {
  24. pub min: f32,
  25. pub max: f32,
  26. pub fleets: Vec<Option<M>>,
  27. }
  28. #[derive(Serialize, Deserialize)]
  29. pub struct MeetingPointOwnedData<M> {
  30. pub owner: M,
  31. }
  32. type Fleets = SmallVec<[Option<Entity>; 8]>;
  33. /* MeetingPoint */
  34. impl MeetingPoint {
  35. #[inline]
  36. pub fn builder() -> MeetingPointBuilder {
  37. MeetingPointBuilder::default()
  38. }
  39. #[inline]
  40. pub fn min(&self) -> f32 {
  41. self.min
  42. }
  43. #[inline]
  44. pub fn max(&self) -> f32 {
  45. self.max
  46. }
  47. #[inline]
  48. pub fn fleets(&self) -> &Fleets {
  49. &self.fleets
  50. }
  51. #[inline]
  52. pub fn fleet(&self, index: usize) -> Option<Entity> {
  53. self.fleets.get(index).cloned().flatten()
  54. }
  55. }
  56. impl MeetingPoint {
  57. #[inline]
  58. pub(crate) fn new(min: f32, max: f32) -> Self {
  59. Self {
  60. min,
  61. max,
  62. fleets: smallvec![],
  63. }
  64. }
  65. pub(crate) fn add_fleet(&mut self, player: usize, fleet: Entity) {
  66. self.fleets.resize_with(player + 1, Default::default);
  67. self.fleets[player] = Some(fleet);
  68. }
  69. pub(crate) fn remove_fleet(&mut self, player: usize) {
  70. if let Some(fleet) = self.fleets.get_mut(player) {
  71. *fleet = None;
  72. }
  73. }
  74. }
  75. impl Component for MeetingPoint {
  76. type Storage = FlaggedStorage<Self, HashMapStorage<Self>>;
  77. }
  78. impl<M> ConvertSaveload<M> for MeetingPoint
  79. where
  80. for<'de> M: Marker + Serialize + Deserialize<'de>,
  81. {
  82. type Data = MeetingPointData<M>;
  83. type Error = NoError;
  84. fn convert_into<F>(&self, mut ids: F) -> Result<Self::Data, Self::Error>
  85. where
  86. F: FnMut(Entity) -> Option<M>,
  87. {
  88. let MeetingPoint { min, max, fleets } = self;
  89. let min = *min;
  90. let max = *max;
  91. let fleets = fleets
  92. .iter()
  93. .cloned()
  94. .map(|e| e.and_then(|e| ids(e)))
  95. .collect();
  96. Ok(MeetingPointData { min, max, fleets })
  97. }
  98. fn convert_from<F>(data: Self::Data, mut ids: F) -> Result<Self, Self::Error>
  99. where
  100. F: FnMut(M) -> Option<Entity>,
  101. {
  102. let MeetingPointData { min, max, fleets } = data;
  103. let fleets = fleets.into_iter().map(|e| e.and_then(|e| ids(e))).collect();
  104. Ok(MeetingPoint { min, max, fleets })
  105. }
  106. }