Async Entity Component System based on the ideas of specs (https://github.com/amethyst/specs)
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

122 righe
2.6 KiB

  1. use std::marker::PhantomData;
  2. use std::ops::{Deref, DerefMut, Not};
  3. use hibitset::BitSet;
  4. use crate::{
  5. access::{Join, ParJoin},
  6. component::Component,
  7. entity::{Entities, Entity, Index},
  8. error::Error,
  9. resource::Ref,
  10. storage::MaskedStorage,
  11. };
  12. use super::{AntiStorage, DistinctStorage, Storage};
  13. pub struct StorageWrapper<'a, T, D> {
  14. data: D,
  15. entities: Ref<'a, Entities>,
  16. phantom: PhantomData<T>,
  17. }
  18. impl<'a, T, D> StorageWrapper<'a, T, D> {
  19. pub fn new(data: D, entities: Ref<'a, Entities>) -> Self {
  20. Self {
  21. data,
  22. entities,
  23. phantom: PhantomData,
  24. }
  25. }
  26. }
  27. impl<'a, T, D> StorageWrapper<'a, T, D>
  28. where
  29. T: Component,
  30. D: DerefMut<Target = MaskedStorage<T>>,
  31. {
  32. pub fn insert(&mut self, entity: Entity, component: T) -> Result<Option<T>, Error> {
  33. if !self.entities.is_alive(entity) {
  34. return Err(Error::EntityIsNotAlive(entity));
  35. }
  36. Ok(self.data.insert(entity, component))
  37. }
  38. pub fn not(&self) -> AntiStorage<'_> {
  39. AntiStorage(&self.data.mask)
  40. }
  41. }
  42. impl<'a, T: Component, D> DistinctStorage for StorageWrapper<'a, T, D> where
  43. T::Storage: DistinctStorage
  44. {
  45. }
  46. impl<'a, 'e, T, D> Not for &'a StorageWrapper<'e, T, D>
  47. where
  48. T: Component,
  49. D: Deref<Target = MaskedStorage<T>>,
  50. {
  51. type Output = AntiStorage<'a>;
  52. fn not(self) -> Self::Output {
  53. AntiStorage(&self.data.mask)
  54. }
  55. }
  56. impl<'a, 'e, T, D> Join for &'a StorageWrapper<'e, T, D>
  57. where
  58. T: Component,
  59. D: Deref<Target = MaskedStorage<T>>,
  60. {
  61. type Mask = &'a BitSet;
  62. type Type = &'a T;
  63. type Value = &'a T::Storage;
  64. fn open(self) -> (Self::Mask, Self::Value) {
  65. (&self.data.mask, &self.data.inner)
  66. }
  67. fn get(v: &mut Self::Value, i: Index) -> &'a T {
  68. v.get(i)
  69. }
  70. }
  71. impl<'a, 'e, T, D> Join for &'a mut StorageWrapper<'e, T, D>
  72. where
  73. T: Component,
  74. D: DerefMut<Target = MaskedStorage<T>>,
  75. {
  76. type Mask = &'a BitSet;
  77. type Type = &'a mut T;
  78. type Value = &'a mut T::Storage;
  79. fn open(self) -> (Self::Mask, Self::Value) {
  80. self.data.open_mut()
  81. }
  82. fn get(v: &mut Self::Value, i: Index) -> &'a mut T {
  83. // HACK
  84. let value: *mut Self::Value = v as *mut Self::Value;
  85. unsafe { (*value).get_mut(i) }
  86. }
  87. }
  88. impl<'a, 'e, T, D> ParJoin for &'a StorageWrapper<'e, T, D>
  89. where
  90. T: Component,
  91. D: Deref<Target = MaskedStorage<T>>,
  92. T::Storage: Sync,
  93. {
  94. }
  95. impl<'a, 'e, T, D> ParJoin for &'a mut StorageWrapper<'e, T, D>
  96. where
  97. T: Component,
  98. D: DerefMut<Target = MaskedStorage<T>>,
  99. T::Storage: Sync + DistinctStorage,
  100. {
  101. }