Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

239 řádky
4.7 KiB

  1. #![allow(dead_code)]
  2. use serde::{Deserialize, Serialize};
  3. use specs::{
  4. error::NoError,
  5. hibitset::BitSet,
  6. saveload::{ConvertSaveload, Marker},
  7. Component, Entity, HashMapStorage, VecStorage,
  8. };
  9. use crate::{
  10. builder::FleetBuilder,
  11. components::{Ship, ShipCount},
  12. misc::FlaggedStorage,
  13. };
  14. #[derive(Debug, Clone)]
  15. pub struct Fleet {
  16. owned: BitSet,
  17. count: ShipCount,
  18. }
  19. #[derive(Copy, Clone, Debug)]
  20. pub struct FleetOwned {
  21. owner: Entity,
  22. }
  23. #[derive(Debug, Clone)]
  24. pub struct FleetOrbiting {
  25. meeting_point: Entity,
  26. }
  27. #[derive(Debug, Clone)]
  28. pub struct FleetMoving {
  29. target: Entity,
  30. }
  31. #[derive(Serialize, Deserialize)]
  32. pub struct FleetOwnedData<M> {
  33. pub owner: M,
  34. }
  35. #[derive(Serialize, Deserialize)]
  36. pub struct FleetOrbitingData<M> {
  37. pub meeting_point: M,
  38. }
  39. #[derive(Serialize, Deserialize)]
  40. pub struct FleetMovingData<M> {
  41. pub target: M,
  42. }
  43. /* Fleet */
  44. impl Fleet {
  45. #[inline]
  46. pub fn builder() -> FleetBuilder {
  47. FleetBuilder::default()
  48. }
  49. #[inline]
  50. pub fn owned(&self) -> &BitSet {
  51. &self.owned
  52. }
  53. #[inline]
  54. pub fn count(&self) -> &ShipCount {
  55. &self.count
  56. }
  57. #[inline]
  58. pub fn is_empty(&self) -> bool {
  59. self.count.total() == 0
  60. }
  61. }
  62. impl Fleet {
  63. pub(crate) fn new() -> Self {
  64. Self {
  65. owned: BitSet::new(),
  66. count: ShipCount::none(),
  67. }
  68. }
  69. pub(crate) fn add_ship(&mut self, ship_id: Entity, ship: &Ship) {
  70. let type_ = ship.type_();
  71. self.count[type_] += 1;
  72. self.owned.add(ship_id.id());
  73. }
  74. pub(crate) fn remove_ship(&mut self, ship_id: Entity, ship: &Ship) {
  75. let type_ = ship.type_();
  76. self.count[type_] -= 1;
  77. self.owned.remove(ship_id.id());
  78. }
  79. }
  80. impl Component for Fleet {
  81. type Storage = HashMapStorage<Self>;
  82. }
  83. /* FleetOwned */
  84. impl FleetOwned {
  85. pub fn owner(&self) -> Entity {
  86. self.owner
  87. }
  88. }
  89. impl FleetOwned {
  90. pub(crate) fn new(owner: Entity) -> Self {
  91. Self { owner }
  92. }
  93. pub(crate) fn set_owner(&mut self, owner: Entity) {
  94. self.owner = owner;
  95. }
  96. }
  97. impl Component for FleetOwned {
  98. type Storage = FlaggedStorage<Self, VecStorage<Self>>;
  99. }
  100. impl<M> ConvertSaveload<M> for FleetOwned
  101. where
  102. for<'de> M: Marker + Serialize + Deserialize<'de>,
  103. {
  104. type Data = FleetOwnedData<M>;
  105. type Error = NoError;
  106. fn convert_into<F>(&self, mut ids: F) -> Result<Self::Data, Self::Error>
  107. where
  108. F: FnMut(Entity) -> Option<M>,
  109. {
  110. let owner = ids(self.owner).unwrap();
  111. Ok(FleetOwnedData { owner })
  112. }
  113. fn convert_from<F>(data: Self::Data, mut ids: F) -> Result<Self, Self::Error>
  114. where
  115. F: FnMut(M) -> Option<Entity>,
  116. {
  117. let owner = ids(data.owner).unwrap();
  118. Ok(FleetOwned { owner })
  119. }
  120. }
  121. /* FleetOrbiting */
  122. impl FleetOrbiting {
  123. pub fn meeting_point(&self) -> Entity {
  124. self.meeting_point
  125. }
  126. }
  127. impl FleetOrbiting {
  128. pub(crate) fn new(meeting_point: Entity) -> Self {
  129. Self { meeting_point }
  130. }
  131. }
  132. impl Component for FleetOrbiting {
  133. type Storage = FlaggedStorage<Self, HashMapStorage<Self>>;
  134. }
  135. impl<M> ConvertSaveload<M> for FleetOrbiting
  136. where
  137. for<'de> M: Marker + Serialize + Deserialize<'de>,
  138. {
  139. type Data = FleetOrbitingData<M>;
  140. type Error = NoError;
  141. fn convert_into<F>(&self, mut ids: F) -> Result<Self::Data, Self::Error>
  142. where
  143. F: FnMut(Entity) -> Option<M>,
  144. {
  145. let meeting_point = ids(self.meeting_point).unwrap();
  146. Ok(FleetOrbitingData { meeting_point })
  147. }
  148. fn convert_from<F>(data: Self::Data, mut ids: F) -> Result<Self, Self::Error>
  149. where
  150. F: FnMut(M) -> Option<Entity>,
  151. {
  152. let meeting_point = ids(data.meeting_point).unwrap();
  153. Ok(FleetOrbiting { meeting_point })
  154. }
  155. }
  156. /* FleetMoving */
  157. impl FleetMoving {
  158. pub fn target(&self) -> Entity {
  159. self.target
  160. }
  161. }
  162. impl FleetMoving {
  163. pub(crate) fn new(target: Entity) -> Self {
  164. Self { target }
  165. }
  166. }
  167. impl Component for FleetMoving {
  168. type Storage = FlaggedStorage<Self, HashMapStorage<Self>>;
  169. }
  170. impl<M> ConvertSaveload<M> for FleetMoving
  171. where
  172. for<'de> M: Marker + Serialize + Deserialize<'de>,
  173. {
  174. type Data = FleetMovingData<M>;
  175. type Error = NoError;
  176. fn convert_into<F>(&self, mut ids: F) -> Result<Self::Data, Self::Error>
  177. where
  178. F: FnMut(Entity) -> Option<M>,
  179. {
  180. let target = ids(self.target).unwrap();
  181. Ok(FleetMovingData { target })
  182. }
  183. fn convert_from<F>(data: Self::Data, mut ids: F) -> Result<Self, Self::Error>
  184. where
  185. F: FnMut(M) -> Option<Entity>,
  186. {
  187. let target = ids(data.target).unwrap();
  188. Ok(FleetMoving { target })
  189. }
  190. }