Skip to content

Commit ffad093

Browse files
committed
fix: logic components remain active at suspended cells
1 parent ed2d36a commit ffad093

2 files changed

Lines changed: 66 additions & 20 deletions

File tree

lib/src/components/has_grid_support.dart

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ mixin HasGridSupport<G extends HasSpatialGridFramework> on PositionComponent
6767
@internal
6868
static final shapeHitboxIndex = HashMap<ShapeHitbox, int>();
6969

70-
static final componentsWithLogic = <HasGridSupport>[];
70+
static final componentsWithLogic = <HasGridSupport>{};
71+
static List<HasGridSupport> componentsWithLogicList = [];
7172
static bool componentsWithLogicChanged = true;
7273

7374
@override
@@ -111,8 +112,21 @@ mixin HasGridSupport<G extends HasSpatialGridFramework> on PositionComponent
111112
bool noVisibleChildren = false;
112113
bool noChildrenToUpdate = true;
113114
bool noUpdateAutoCheck = true;
114-
bool noUpdate = false;
115-
bool noLogic = false;
115+
116+
bool get noUpdate => _noUpdate;
117+
set noUpdate(bool value) {
118+
_noUpdate = value;
119+
}
120+
121+
bool _noUpdate = false;
122+
123+
bool get noLogic => _noLogic;
124+
set noLogic(bool value) {
125+
_noLogic = value;
126+
}
127+
128+
bool _noLogic = false;
129+
116130
bool checkOutOfCellBounds = true;
117131
bool needResize = false;
118132

@@ -283,6 +297,18 @@ mixin HasGridSupport<G extends HasSpatialGridFramework> on PositionComponent
283297
position.addListener(_onPositionChanged);
284298
final result = super.onLoad();
285299

300+
checkNoUpdate();
301+
checkNoLogic();
302+
303+
if (!noLogic && _logicPriority == 0) {
304+
_logicPriority = parent!.ancestors(includeSelf: true).length;
305+
componentsWithLogic.add(this);
306+
componentsWithLogicChanged = true;
307+
}
308+
return result;
309+
}
310+
311+
void checkNoUpdate() {
286312
noUpdate = false;
287313
try {
288314
update(0);
@@ -291,7 +317,9 @@ mixin HasGridSupport<G extends HasSpatialGridFramework> on PositionComponent
291317
} catch (error) {
292318
//suppress errors
293319
}
320+
}
294321

322+
void checkNoLogic() {
295323
noLogic = false;
296324
try {
297325
logic(0);
@@ -300,8 +328,6 @@ mixin HasGridSupport<G extends HasSpatialGridFramework> on PositionComponent
300328
} catch (error) {
301329
//suppress errors
302330
}
303-
304-
return result;
305331
}
306332

307333
int _logicPriority = 0;
@@ -325,15 +351,14 @@ mixin HasGridSupport<G extends HasSpatialGridFramework> on PositionComponent
325351
}
326352
}
327353

328-
if (!noLogic && _logicPriority == 0) {
329-
_logicPriority = parent!.ancestors(includeSelf: true).length;
330-
componentsWithLogic.add(this);
331-
componentsWithLogicChanged = true;
354+
if (_scheduledActionProvider == null) {
355+
initActionProvider(
356+
ScheduledActionProvider(
357+
scheduler: game.scheduler,
358+
actionFunction: onScheduledAction,
359+
),
360+
);
332361
}
333-
initActionProvider(ScheduledActionProvider(
334-
scheduler: game.scheduler,
335-
actionFunction: onScheduledAction,
336-
));
337362
super.onMount();
338363
}
339364

@@ -460,11 +485,21 @@ mixin HasGridSupport<G extends HasSpatialGridFramework> on PositionComponent
460485

461486
VoidCallback? onInactiveCallback;
462487

463-
void onInactive() {}
488+
void onInactive() {
489+
if (!noLogic) {
490+
HasGridSupport.componentsWithLogic.remove(this);
491+
HasGridSupport.componentsWithLogicChanged = true;
492+
}
493+
}
464494

465495
VoidCallback? onActiveCallback;
466496

467-
void onActivate() {}
497+
void onActivate() {
498+
if (!noLogic) {
499+
HasGridSupport.componentsWithLogic.add(this);
500+
HasGridSupport.componentsWithLogicChanged = true;
501+
}
502+
}
468503

469504
/// Called instead of [updateTree] when component is suspended.
470505
/// [dtElapsedWhileSuspended] accumulates all "dt" values since
@@ -473,15 +508,25 @@ mixin HasGridSupport<G extends HasSpatialGridFramework> on PositionComponent
473508

474509
/// Called when component state changes to "suspended". You should stop
475510
/// all undesired component's movements (for example) here
476-
void onSuspend() {}
511+
void onSuspend() {
512+
if (!noLogic) {
513+
HasGridSupport.componentsWithLogic.remove(this);
514+
HasGridSupport.componentsWithLogicChanged = true;
515+
}
516+
}
477517

478518
VoidCallback? onSuspendCallback;
479519

480520
/// Called when component state changes from "suspended" to active.
481521
/// [dtElapsedWhileSuspended] accumulates all "dt" values since
482522
/// component suspension. Useful to calculate next animation step as if
483523
/// the component was never suspended.
484-
void onResume(double dtElapsedWhileSuspended) {}
524+
void onResume(double dtElapsedWhileSuspended) {
525+
if (!noLogic) {
526+
HasGridSupport.componentsWithLogic.add(this);
527+
HasGridSupport.componentsWithLogicChanged = true;
528+
}
529+
}
485530

486531
Function(double dtElapsedWhileSuspended)? onResumeCallback;
487532

lib/src/core/has_spatial_grid_framework.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import 'package:flame/extensions.dart';
55
import 'package:flame/game.dart';
66
import 'package:flame_spatial_grid/flame_spatial_grid.dart';
77
import 'package:flame_spatial_grid/src/components/layers/scheduled_layer_operation.dart';
8-
import 'package:flame_spatial_grid/src/components/utility/scheduler/scheduler.dart';
98
import 'package:flutter/foundation.dart';
109
import 'package:meta/meta.dart';
1110

@@ -708,7 +707,9 @@ mixin HasSpatialGridFramework<W extends World> on FlameGame<W>
708707

709708
if (_logicUpdateDt >= logicUpdateInterval) {
710709
if (HasGridSupport.componentsWithLogicChanged) {
711-
HasGridSupport.componentsWithLogic.sort(
710+
HasGridSupport.componentsWithLogicList =
711+
HasGridSupport.componentsWithLogic.toList(growable: false);
712+
HasGridSupport.componentsWithLogicList.sort(
712713
(a, b) {
713714
if (a.logicPriority == b.logicPriority) {
714715
return 0;
@@ -738,7 +739,7 @@ mixin HasSpatialGridFramework<W extends World> on FlameGame<W>
738739

739740
void logic(double dt) {
740741
scheduler.runActions(dt, ScheduledActionType.beforeLogic);
741-
for (final component in HasGridSupport.componentsWithLogic) {
742+
for (final component in HasGridSupport.componentsWithLogicList) {
742743
component.logic(dt);
743744
}
744745
scheduler.runActions(dt, ScheduledActionType.afterLogic);

0 commit comments

Comments
 (0)