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.

58 lines
1.2 KiB

  1. use glc::vector::Vector4f;
  2. use serde::{Deserialize, Serialize};
  3. use specs::{
  4. error::NoError,
  5. saveload::{ConvertSaveload, Marker},
  6. Component, Entity, HashMapStorage,
  7. };
  8. #[derive(Clone, Debug, Default, Serialize, Deserialize)]
  9. pub struct Player {
  10. pub index: usize,
  11. pub color: Vector4f,
  12. }
  13. #[derive(Copy, Clone, Debug)]
  14. pub struct Owned {
  15. pub owner: Entity,
  16. }
  17. #[derive(Serialize, Deserialize)]
  18. pub struct OwnedData<M> {
  19. pub owner: M,
  20. }
  21. impl Component for Player {
  22. type Storage = HashMapStorage<Self>;
  23. }
  24. impl Component for Owned {
  25. type Storage = HashMapStorage<Self>;
  26. }
  27. impl<M> ConvertSaveload<M> for Owned
  28. where
  29. for<'de> M: Marker + Serialize + Deserialize<'de>,
  30. {
  31. type Data = OwnedData<M>;
  32. type Error = NoError;
  33. fn convert_into<F>(&self, mut ids: F) -> Result<Self::Data, Self::Error>
  34. where
  35. F: FnMut(Entity) -> Option<M>,
  36. {
  37. let owner = ids(self.owner).unwrap();
  38. Ok(OwnedData { owner })
  39. }
  40. fn convert_from<F>(data: Self::Data, mut ids: F) -> Result<Self, Self::Error>
  41. where
  42. F: FnMut(M) -> Option<Entity>,
  43. {
  44. let owner = ids(data.owner).unwrap();
  45. Ok(Owned { owner })
  46. }
  47. }