|
- #![allow(dead_code)]
-
- use serde::{Deserialize, Serialize};
- use specs::{
- error::NoError,
- hibitset::BitSet,
- saveload::{ConvertSaveload, Marker},
- Component, Entity, HashMapStorage, VecStorage,
- };
-
- use crate::{
- builder::FleetBuilder,
- components::{Ship, ShipCount},
- misc::FlaggedStorage,
- };
-
- #[derive(Debug, Clone)]
- pub struct Fleet {
- owned: BitSet,
- count: ShipCount,
- }
-
- #[derive(Copy, Clone, Debug)]
- pub struct FleetOwned {
- owner: Entity,
- }
-
- #[derive(Debug, Clone)]
- pub struct FleetOrbiting {
- meeting_point: Entity,
- }
-
- #[derive(Debug, Clone)]
- pub struct FleetMoving {
- target: Entity,
- }
-
- #[derive(Serialize, Deserialize)]
- pub struct FleetOwnedData<M> {
- pub owner: M,
- }
-
- #[derive(Serialize, Deserialize)]
- pub struct FleetOrbitingData<M> {
- pub meeting_point: M,
- }
-
- #[derive(Serialize, Deserialize)]
- pub struct FleetMovingData<M> {
- pub target: M,
- }
-
- /* Fleet */
-
- impl Fleet {
- #[inline]
- pub fn builder() -> FleetBuilder {
- FleetBuilder::default()
- }
-
- #[inline]
- pub fn owned(&self) -> &BitSet {
- &self.owned
- }
-
- #[inline]
- pub fn count(&self) -> &ShipCount {
- &self.count
- }
-
- #[inline]
- pub fn is_empty(&self) -> bool {
- self.count.total() == 0
- }
- }
-
- impl Fleet {
- pub(crate) fn new() -> Self {
- Self {
- owned: BitSet::new(),
- count: ShipCount::none(),
- }
- }
-
- pub(crate) fn add_ship(&mut self, ship_id: Entity, ship: &Ship) {
- let type_ = ship.type_();
-
- self.count[type_] += 1;
- self.owned.add(ship_id.id());
- }
-
- pub(crate) fn remove_ship(&mut self, ship_id: Entity, ship: &Ship) {
- let type_ = ship.type_();
-
- self.count[type_] -= 1;
- self.owned.remove(ship_id.id());
- }
- }
-
- impl Component for Fleet {
- type Storage = HashMapStorage<Self>;
- }
-
- /* FleetOwned */
-
- impl FleetOwned {
- pub fn owner(&self) -> Entity {
- self.owner
- }
- }
-
- impl FleetOwned {
- pub(crate) fn new(owner: Entity) -> Self {
- Self { owner }
- }
-
- pub(crate) fn set_owner(&mut self, owner: Entity) {
- self.owner = owner;
- }
- }
-
- impl Component for FleetOwned {
- type Storage = FlaggedStorage<Self, VecStorage<Self>>;
- }
-
- impl<M> ConvertSaveload<M> for FleetOwned
- where
- for<'de> M: Marker + Serialize + Deserialize<'de>,
- {
- type Data = FleetOwnedData<M>;
- type Error = NoError;
-
- fn convert_into<F>(&self, mut ids: F) -> Result<Self::Data, Self::Error>
- where
- F: FnMut(Entity) -> Option<M>,
- {
- let owner = ids(self.owner).unwrap();
-
- Ok(FleetOwnedData { owner })
- }
-
- fn convert_from<F>(data: Self::Data, mut ids: F) -> Result<Self, Self::Error>
- where
- F: FnMut(M) -> Option<Entity>,
- {
- let owner = ids(data.owner).unwrap();
-
- Ok(FleetOwned { owner })
- }
- }
-
- /* FleetOrbiting */
-
- impl FleetOrbiting {
- pub fn meeting_point(&self) -> Entity {
- self.meeting_point
- }
- }
-
- impl FleetOrbiting {
- pub(crate) fn new(meeting_point: Entity) -> Self {
- Self { meeting_point }
- }
- }
-
- impl Component for FleetOrbiting {
- type Storage = FlaggedStorage<Self, HashMapStorage<Self>>;
- }
-
- impl<M> ConvertSaveload<M> for FleetOrbiting
- where
- for<'de> M: Marker + Serialize + Deserialize<'de>,
- {
- type Data = FleetOrbitingData<M>;
- type Error = NoError;
-
- fn convert_into<F>(&self, mut ids: F) -> Result<Self::Data, Self::Error>
- where
- F: FnMut(Entity) -> Option<M>,
- {
- let meeting_point = ids(self.meeting_point).unwrap();
-
- Ok(FleetOrbitingData { meeting_point })
- }
-
- fn convert_from<F>(data: Self::Data, mut ids: F) -> Result<Self, Self::Error>
- where
- F: FnMut(M) -> Option<Entity>,
- {
- let meeting_point = ids(data.meeting_point).unwrap();
-
- Ok(FleetOrbiting { meeting_point })
- }
- }
-
- /* FleetMoving */
-
- impl FleetMoving {
- pub fn target(&self) -> Entity {
- self.target
- }
- }
-
- impl FleetMoving {
- pub(crate) fn new(target: Entity) -> Self {
- Self { target }
- }
- }
-
- impl Component for FleetMoving {
- type Storage = FlaggedStorage<Self, HashMapStorage<Self>>;
- }
-
- impl<M> ConvertSaveload<M> for FleetMoving
- where
- for<'de> M: Marker + Serialize + Deserialize<'de>,
- {
- type Data = FleetMovingData<M>;
- type Error = NoError;
-
- fn convert_into<F>(&self, mut ids: F) -> Result<Self::Data, Self::Error>
- where
- F: FnMut(Entity) -> Option<M>,
- {
- let target = ids(self.target).unwrap();
-
- Ok(FleetMovingData { target })
- }
-
- fn convert_from<F>(data: Self::Data, mut ids: F) -> Result<Self, Self::Error>
- where
- F: FnMut(M) -> Option<Entity>,
- {
- let target = ids(data.target).unwrap();
-
- Ok(FleetMoving { target })
- }
- }
|