@@ -17,11 +17,21 @@ var _overlapping_areas := _HistoryBuffer.new()
1717
1818## Returns the result of [method Area2D.get_overlapping_areas] at [param tick]
1919func rollback_get_overlapping_areas (tick : int ) -> Array [Area2D ]:
20- return _overlapping_areas .get_snapshot (tick ) if _overlapping_areas .has (tick ) else []
20+ if not _overlapping_areas .has (tick ):
21+ return []
22+
23+ var areas : Array [Area2D ] = []
24+ areas .assign ((_overlapping_areas .get_snapshot (tick ) as _Set ).values ())
25+ return areas
2126
2227## Returns the result of [method Area2D.get_overlapping_bodies] at [param tick]
2328func rollback_get_overlapping_bodies (tick : int ) -> Array [Node2D ]:
24- return _overlapping_bodies .get_snapshot (tick ) if _overlapping_bodies .has (tick ) else []
29+ if not _overlapping_bodies .has (tick ):
30+ return []
31+
32+ var bodies : Array [Node2D ] = []
33+ bodies .assign ((_overlapping_bodies .get_snapshot (tick ) as _Set ).values ())
34+ return bodies
2535
2636## Returns the result of [method Area2D.has_overlapping_areas] at [param tick]
2737func rollback_has_overlapping_areas (tick : int ) -> bool :
@@ -44,53 +54,59 @@ func _notification(what: int):
4454 # Use notification instead of _ready, so users can write their own _ready
4555 # callback without having to call super()
4656 if what == NOTIFICATION_READY :
47- NetworkTime . on_tick .connect (_tick )
57+ NetworkRollback . after_prepare_tick .connect (_after_prepare_tick )
4858
4959
50- func _tick ( _d : float , tick : int ):
60+ func _after_prepare_tick ( tick : int ):
5161 _update_bodies (tick )
5262 _update_areas (tick )
5363
5464
5565func _update_bodies (tick : int ):
56- var current := self .get_overlapping_bodies ()
66+ if not self .monitoring :
67+ return
68+
69+ var current := _Set .of (self .get_overlapping_bodies ())
5770 _overlapping_bodies .set_snapshot (tick , current )
5871
5972 if not _overlapping_bodies .has (tick - 1 ):
6073 for body in current :
6174 rollback_body_entered .emit (body , tick )
6275 return
6376
64- var prev : Array = _overlapping_bodies .get_snapshot (tick - 1 )
65-
66- for body in current :
67- if not prev .has (body ):
68- rollback_body_entered .emit (body , tick )
69-
70- for body in prev :
71- if not current .has (body ):
72- rollback_body_exited .emit (body , tick )
77+ var prev : _Set = _overlapping_bodies .get_snapshot (tick - 1 )
78+ if not prev . equals ( current ):
79+ for body in current :
80+ if not prev .has (body ):
81+ rollback_body_entered .emit (body , tick )
82+
83+ for body in prev :
84+ if not current .has (body ):
85+ rollback_body_exited .emit (body , tick )
7386
7487 _overlapping_bodies .trim (NetworkRollback .history_start )
7588
7689
7790func _update_areas (tick : int ):
78- var current := self .get_overlapping_areas ()
91+ if not self .monitoring :
92+ return
93+
94+ var current := _Set .of (self .get_overlapping_areas ())
7995 _overlapping_areas .set_snapshot (tick , current )
8096
8197 if not _overlapping_areas .has (tick - 1 ):
8298 for area in current :
8399 rollback_area_entered .emit (area , tick )
84100 return
85101
86- var prev : Array = _overlapping_areas .get_snapshot (tick - 1 )
87-
88- for area in current :
89- if not prev .has (area ):
90- rollback_area_entered .emit (area , tick )
91-
92- for area in prev :
93- if not current .has (area ):
94- rollback_area_exited .emit (area , tick )
102+ var prev : _Set = _overlapping_areas .get_snapshot (tick - 1 )
103+ if not prev . equals ( current ):
104+ for area in current :
105+ if not prev .has (area ):
106+ rollback_area_entered .emit (area , tick )
107+
108+ for area in prev :
109+ if not current .has (area ):
110+ rollback_area_exited .emit (area , tick )
95111
96112 _overlapping_areas .trim (NetworkRollback .history_start )
0 commit comments