Skip to content

Commit f68a2d3

Browse files
committed
RealityViewMapper: keep track of which entity owns the comps
Because we don't have accessor from netcomp up to entity.
1 parent 63dc299 commit f68a2d3

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

Sources/AlloReality/RealityViewMapper.swift

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,34 +39,34 @@ public class RealityViewMapper
3939
}.store(in: &cancellables)
4040

4141
startSyncingOf(networkComponentType: allonet2.Transform.self, to: RealityKit.Transform.self)
42-
{ (entity, transform) in
42+
{ (entity, _, transform) in
4343
entity.setTransformMatrix(transform.matrix, relativeTo: entity.parent)
4444
}
4545

46-
startSyncingOf(networkComponentType: Relationships.self) { (entity, relationship) in
46+
startSyncingOf(networkComponentType: Relationships.self) { (entity, _, relationship) in
4747
guard entity.parent?.name != relationship.parent else { return }
4848
entity.removeFromParent()
4949
let newParent = self.guiroot.findEntity(named: relationship.parent)!
5050
newParent.addChild(entity)
51-
} remover: { (entity, relationship) in
51+
} remover: { (entity, _, relationship) in
5252
guard entity.parent != self.guiroot else { return }
5353
entity.removeFromParent()
5454
self.guiroot.addChild(entity)
5555
}
5656

57-
startSyncingModel()
57+
startSyncingOfModel()
5858

5959
startSyncingOf(networkComponentType: Collision.self, to: CollisionComponent.self)
60-
{ entity, collision in
60+
{ entity, _, collision in
6161
entity.components.set(CollisionComponent(shapes: collision.realityShapes))
6262
}
6363

6464
startSyncingOf(networkComponentType: Opacity.self, to: OpacityComponent.self)
65-
{ entity, opacity in
65+
{ entity, _, opacity in
6666
entity.components.set(OpacityComponent(opacity: opacity.opacity))
6767
}
6868
startSyncingOf(networkComponentType: Billboard.self, to: BillboardComponent.self)
69-
{ entity, billboard in
69+
{ entity, _, billboard in
7070
var reality = BillboardComponent()
7171
reality.blendFactor = billboard.blendFactor
7272
entity.components.set(reality)
@@ -75,12 +75,12 @@ public class RealityViewMapper
7575
if #available(macOS 15.0, *) {
7676
startSyncingOf(networkComponentType: InputTarget.self, to: InputTargetComponent.self)
7777
{
78-
(entity, inputTarget) in
78+
(entity, _, inputTarget) in
7979
entity.components.set(InputTargetComponent())
8080
}
8181
startSyncingOf(networkComponentType: HoverEffect.self, to: HoverEffectComponent.self)
8282
{
83-
(entity, hoverEffect) in
83+
(entity, _, hoverEffect) in
8484
entity.components.set(HoverEffectComponent(hoverEffect.realityEffect))
8585
}
8686
}
@@ -89,24 +89,25 @@ public class RealityViewMapper
8989
/// In addition to syncing the Standard Components from `startSyncing()`, also sync other/custom components with this method, called directly after `startSyncing` but before the AlloClient connects.
9090
public func startSyncingOf<T>(
9191
networkComponentType: T.Type,
92-
updater: @escaping (RealityKit.Entity, T) -> Void,
93-
remover: @escaping (RealityKit.Entity, T) -> Void
92+
updater: @escaping @MainActor (RealityKit.Entity, allonet2.EntityData, T) -> Void,
93+
remover: @escaping @MainActor (RealityKit.Entity, allonet2.EntityData, T) -> Void
9494
) where T : allonet2.Component
9595
{
9696
netstate.observers[networkComponentType.self].updated.sink { (eid, netcomp) in
9797
guard let guient = self.guiroot.findEntity(named: eid) else { return }
98-
updater(guient, netcomp)
98+
guard let netent = self.netstate.current.entities[eid] else { return }
99+
updater(guient, netent, netcomp)
99100
}.store(in: &cancellables)
100101
netstate.observers[networkComponentType.self].removed.sink { (edata, netcomp) in
101102
guard let guient = self.guiroot.findEntity(named: edata.id) else { return }
102-
remover(guient, netcomp)
103+
remover(guient, edata, netcomp)
103104
}.store(in: &cancellables)
104105
}
105106

106107
/// Convenience alternative to `startSyncingOf:updater:remover` when there's a one-to-one map between an Alloverse entity type and a RealityKit entity type.
107-
public func startSyncingOf<T, U>(networkComponentType: T.Type, to realityComponentType: U.Type, using updater: @escaping (RealityKit.Entity, T) -> Void) where T : allonet2.Component, U : RealityKit.Component
108+
public func startSyncingOf<T, U>(networkComponentType: T.Type, to realityComponentType: U.Type, using updater: @escaping (RealityKit.Entity, allonet2.EntityData, T) -> Void) where T : allonet2.Component, U : RealityKit.Component
108109
{
109-
startSyncingOf(networkComponentType: networkComponentType, updater: updater, remover: { (guient, netcomp) in
110+
startSyncingOf(networkComponentType: networkComponentType, updater: updater, remover: { (guient, _, netcomp) in
110111
guient.components[realityComponentType.self] = nil
111112
})
112113
}
@@ -118,10 +119,10 @@ public class RealityViewMapper
118119
var loadingTask: Task<Void, Error>?
119120
}
120121

121-
private func startSyncingModel()
122+
private func startSyncingOfModel()
122123
{
123124
startSyncingOf(networkComponentType: Model.self)
124-
{ (entity, model) in
125+
{ (entity, _, model) in
125126
var state = entity.components[AlloModelStateComponent.self] ?? AlloModelStateComponent()
126127
guard state.current != model else { return }
127128

@@ -154,7 +155,7 @@ public class RealityViewMapper
154155
}
155156
entity.components.set(state)
156157
}
157-
remover: { (entity, model) in
158+
remover: { (entity, _, model) in
158159
var state = entity.components[AlloModelStateComponent.self] ?? AlloModelStateComponent()
159160
state.loadingTask?.cancel()
160161
state.entity?.removeFromParent()
@@ -165,8 +166,7 @@ public class RealityViewMapper
165166
/// Stop syncing Alloverse<>RealityKit. Call this to break reference cycles, e g when your RealityView disappears (i e in `onDisappear()`).
166167
public func stopSyncing()
167168
{
168-
cancellables.forEach { $0.cancel() }
169-
cancellables.removeAll()
169+
cancellables.forEach { $0.cancel() }; cancellables.removeAll()
170170
}
171171
}
172172

0 commit comments

Comments
 (0)