10
10
11
11
namespace PerceptionVR . Portal
12
12
{
13
- public class PortalCloneSystem : PortalCollisionFilteringSystem
13
+ [ RequireComponent ( typeof ( Portal ) , typeof ( PortalVicinity ) , typeof ( PortalCollisionFilteringSystem ) ) ]
14
+ public class PortalCloneSystem : MonoBehaviour
14
15
{
15
- [ SerializeField ] private List < NearbyTeleportable > vicinity = new ( ) ;
16
+ [ SerializeField ] private List < NearbyTeleportable > objectsInVicinity = new ( ) ;
16
17
18
+ private Portal portal ;
19
+ private PortalVicinity vicinity ;
20
+ private PortalCollisionFilteringSystem collisionFilteringSystem ;
17
21
private PortalCloneSystem pairCloneSystem ;
18
22
19
- protected override void Awake ( )
23
+ private void Awake ( )
20
24
{
21
- base . Awake ( ) ;
25
+ portal = GetComponent < Portal > ( ) ;
26
+ vicinity = GetComponent < PortalVicinity > ( ) ;
27
+ collisionFilteringSystem = GetComponent < PortalCollisionFilteringSystem > ( ) ;
28
+
22
29
portal . OnPortalPairSet += portalPair =>
23
30
{
24
31
pairCloneSystem = portalPair . transform . GetComponentInChildren < PortalCloneSystem > ( ) ;
25
32
} ;
26
33
portal . OnPortalPairUnset += ( ) =>
27
34
{
28
- vicinity . ToList ( ) . ForEach ( UnregisterTeleportable ) ;
35
+ objectsInVicinity . ToList ( ) . ForEach ( UnregisterTeleportable ) ;
29
36
pairCloneSystem = null ;
30
37
} ;
31
38
32
- OnEnteredVicinity += OnVicinityEnter ;
33
- OnExitedVicinity += OnVicinityExit ;
39
+ vicinity . OnOutsideToFront += OnVicinityEnter ;
40
+ vicinity . OnFrontToOutside += OnVicinityExit ;
41
+
42
+ vicinity . OnBackToFront += OnVicinityEnter ;
43
+ vicinity . OnFrontToBack += OnVicinityExit ;
44
+
34
45
GlobalEvents . OnTeleport += OnTeleportCallback ;
35
46
}
36
47
37
48
38
49
private void OnVicinityEnter ( Collider other )
39
50
{
51
+ // Ignore if clone is coming around the portal
52
+ if ( collisionFilteringSystem . cloneGroup . Contains ( other ) )
53
+ return ;
54
+
40
55
Debugger . LogInfo ( $ "{ other } entered vicinity of { this } ") ;
41
56
var tp = other . GetComponentInParent < ITeleportable > ( ) ;
42
57
43
58
// Get nearby teleportable
44
- var nearbyTeleportable = vicinity . FirstOrDefault ( x => x . teleportable == tp ) ;
59
+ var nearbyTeleportable = objectsInVicinity . FirstOrDefault ( x => x . teleportable == tp ) ;
45
60
if ( nearbyTeleportable != null )
46
61
{
47
62
nearbyTeleportable . collidersInVicinity . Add ( other ) ;
48
63
return ;
49
64
}
50
65
51
- nearbyTeleportable = pairCloneSystem . vicinity . FirstOrDefault ( x => x . cloneTeleportable == tp ) ;
66
+ nearbyTeleportable = pairCloneSystem . objectsInVicinity . FirstOrDefault ( x => x . cloneTeleportable == tp ) ;
52
67
if ( nearbyTeleportable != null )
53
68
{
54
69
nearbyTeleportable . cloneColliderInPairVicinity . Add ( other ) ;
@@ -64,10 +79,14 @@ private void OnVicinityEnter(Collider other)
64
79
65
80
private void OnVicinityExit ( Collider other )
66
81
{
82
+ // Ignore if clone is coming around the portal
83
+ if ( collisionFilteringSystem . cloneGroup . Contains ( other ) )
84
+ return ;
85
+
67
86
Debugger . LogInfo ( $ "{ other } exited vicinity of { this } ") ;
68
87
var tp = other . GetComponentInParent < ITeleportable > ( ) ;
69
88
70
- var nearbyTeleportable = vicinity . FirstOrDefault ( x => x . teleportable == tp ) ;
89
+ var nearbyTeleportable = objectsInVicinity . FirstOrDefault ( x => x . teleportable == tp ) ;
71
90
if ( nearbyTeleportable != null )
72
91
{
73
92
nearbyTeleportable . collidersInVicinity . Remove ( other ) ;
@@ -76,7 +95,7 @@ private void OnVicinityExit(Collider other)
76
95
return ;
77
96
}
78
97
79
- nearbyTeleportable = pairCloneSystem . vicinity . FirstOrDefault ( x => x . cloneTeleportable == tp ) ;
98
+ nearbyTeleportable = pairCloneSystem . objectsInVicinity . FirstOrDefault ( x => x . cloneTeleportable == tp ) ;
80
99
if ( nearbyTeleportable != null )
81
100
nearbyTeleportable . cloneColliderInPairVicinity . Remove ( other ) ;
82
101
}
@@ -90,12 +109,12 @@ private NearbyTeleportable RegisterTeleportable(ITeleportable teleportable)
90
109
91
110
// Register teleportable
92
111
var nearbyTeleportable = new NearbyTeleportable ( teleportable , clone ) ;
93
- vicinity . Add ( nearbyTeleportable ) ;
112
+ objectsInVicinity . Add ( nearbyTeleportable ) ;
94
113
95
114
// Add clone colliders to nearbyTeleportable and pair's cloneGroup
96
115
var cloneColliders = clone . transform . GetComponentsInChildren < Collider > ( ) ;
97
116
nearbyTeleportable . cloneColliderInPairVicinity . AddRange ( cloneColliders ) ;
98
- pairCloneSystem . cloneGroup . AddRange ( cloneColliders ) ;
117
+ pairCloneSystem . collisionFilteringSystem . cloneGroup . AddRange ( cloneColliders ) ;
99
118
100
119
return nearbyTeleportable ;
101
120
}
@@ -108,15 +127,15 @@ private void UnregisterTeleportable(NearbyTeleportable teleportable)
108
127
GameObjectUtility . DestroyNotify ( teleportable . cloneTeleportable . gameObject ) ;
109
128
110
129
// Unregister teleportable
111
- vicinity . Remove ( teleportable ) ;
130
+ objectsInVicinity . Remove ( teleportable ) ;
112
131
}
113
132
114
133
115
134
private void OnTeleportCallback ( TeleportData teleportData )
116
135
{
117
136
// nt = "nearby teleportable"
118
137
119
- var nt = vicinity . FirstOrDefault ( x => x . teleportable == teleportData . teleportable && portal == teleportData . inPortal ) ;
138
+ var nt = objectsInVicinity . FirstOrDefault ( x => x . teleportable == teleportData . teleportable && portal == teleportData . inPortal ) ;
120
139
if ( nt == null )
121
140
return ;
122
141
@@ -135,8 +154,8 @@ private void OnTeleportCallback(TeleportData teleportData)
135
154
( nt . collidersInVicinity , nt . cloneColliderInPairVicinity ) = ( nt . cloneColliderInPairVicinity , nt . collidersInVicinity ) ;
136
155
137
156
// Transfer nt to pair
138
- vicinity . Remove ( nt ) ;
139
- pairCloneSystem . vicinity . Add ( nt ) ;
157
+ objectsInVicinity . Remove ( nt ) ;
158
+ pairCloneSystem . objectsInVicinity . Add ( nt ) ;
140
159
}
141
160
142
161
0 commit comments