Line data Source code
1 : import '../components/base_component.dart'; 2 : import 'effects.dart'; 3 : 4 : export './move_effect.dart'; 5 : export './rotate_effect.dart'; 6 : export './sequence_effect.dart'; 7 : export './size_effect.dart'; 8 : 9 : class EffectsHandler { 10 : /// The effects that should run on the component 11 : final List<ComponentEffect> _effects = []; 12 : 13 : /// The effects that should be added on the next update iteration 14 : final List<ComponentEffect> _addLater = []; 15 : 16 20 : void update(double dt) { 17 60 : _effects.addAll(_addLater); 18 40 : _addLater.clear(); 19 47 : _effects.removeWhere((e) { 20 7 : final hasCompleted = e.hasCompleted(); 21 : if (hasCompleted) { 22 6 : e.onRemove(); 23 : } 24 : return hasCompleted; 25 : }); 26 81 : _effects.where((e) => !e.isPaused).forEach((e) { 27 7 : e.update(dt); 28 7 : if (e.hasCompleted()) { 29 6 : e.setComponentToEndState(); 30 : } 31 : }); 32 : } 33 : 34 : /// Add an effect to the handler 35 7 : void add(ComponentEffect effect, BaseComponent component) { 36 21 : _addLater.add(effect..initialize(component)); 37 : } 38 : 39 : /// Mark an effect for removal 40 0 : void removeEffect(ComponentEffect effect) { 41 0 : effect.dispose(); 42 : } 43 : 44 : /// Remove all effects 45 0 : void clearEffects() { 46 0 : _addLater.forEach(removeEffect); 47 0 : _effects.forEach(removeEffect); 48 : } 49 : 50 : /// Get a list of non removed effects 51 6 : List<ComponentEffect> get effects { 52 12 : return List<ComponentEffect>.from(_effects) 53 12 : ..addAll(_addLater) 54 6 : ..where((e) => !e.hasCompleted()); 55 : } 56 : }