refactor: modified ContactCallbacksGroup

pull/234/head
alestiago 3 years ago
parent 469c00c866
commit 31a92c0a38

@ -10,8 +10,13 @@ class AlienBumperContactBehavior extends Component
await super.onLoad(); await super.onLoad();
final userData = parent.body.userData; final userData = parent.body.userData;
if (userData is ContactCallbacksNotifer) { if (userData is ContactCallbacksGroup) {
userData.addCallback(this); userData.addContactCallbacks(this);
} else if (userData is ContactCallbacks) {
final notifier = ContactCallbacksGroup()
..addContactCallbacks(userData)
..addContactCallbacks(this);
parent.body.userData = notifier;
} else { } else {
parent.body.userData = this; parent.body.userData = this;
} }

@ -10,10 +10,7 @@ import 'package:pinball_flame/pinball_flame.dart';
/// ///
/// {@endtemplate} /// {@endtemplate}
class ScoringBehaviour extends Component class ScoringBehaviour extends Component
with with ContactCallbacks, HasGameRef<PinballGame>, ParentIsA<BodyComponent> {
ContactCallbacksNotifer,
HasGameRef<PinballGame>,
ParentIsA<BodyComponent> {
/// {@macro scoring_behaviour} /// {@macro scoring_behaviour}
ScoringBehaviour({ ScoringBehaviour({
required int points, required int points,
@ -26,8 +23,13 @@ class ScoringBehaviour extends Component
await super.onLoad(); await super.onLoad();
final userData = parent.body.userData; final userData = parent.body.userData;
if (userData is ContactCallbacksNotifer) { if (userData is ContactCallbacksGroup) {
userData.addCallback(this); userData.addContactCallbacks(this);
} else if (userData is ContactCallbacks) {
final notifier = ContactCallbacksGroup()
..addContactCallbacks(userData)
..addContactCallbacks(this);
parent.body.userData = notifier;
} else { } else {
parent.body.userData = this; parent.body.userData = this;
} }

@ -1,14 +1,14 @@
import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
class ContactCallbacksNotifer implements ContactCallbacks { class ContactCallbacksGroup implements ContactCallbacks {
final List<ContactCallbacks> _callbacks = []; final List<ContactCallbacks> _contactCallbacks = [];
@override @override
@mustCallSuper @mustCallSuper
void beginContact(Object other, Contact contact) { void beginContact(Object other, Contact contact) {
onBeginContact?.call(other, contact); onBeginContact?.call(other, contact);
for (final callback in _callbacks) { for (final callback in _contactCallbacks) {
callback.beginContact(other, contact); callback.beginContact(other, contact);
} }
} }
@ -17,7 +17,7 @@ class ContactCallbacksNotifer implements ContactCallbacks {
@mustCallSuper @mustCallSuper
void endContact(Object other, Contact contact) { void endContact(Object other, Contact contact) {
onEndContact?.call(other, contact); onEndContact?.call(other, contact);
for (final callback in _callbacks) { for (final callback in _contactCallbacks) {
callback.endContact(other, contact); callback.endContact(other, contact);
} }
} }
@ -26,7 +26,7 @@ class ContactCallbacksNotifer implements ContactCallbacks {
@mustCallSuper @mustCallSuper
void preSolve(Object other, Contact contact, Manifold oldManifold) { void preSolve(Object other, Contact contact, Manifold oldManifold) {
onPreSolve?.call(other, contact, oldManifold); onPreSolve?.call(other, contact, oldManifold);
for (final callback in _callbacks) { for (final callback in _contactCallbacks) {
callback.preSolve(other, contact, oldManifold); callback.preSolve(other, contact, oldManifold);
} }
} }
@ -35,13 +35,13 @@ class ContactCallbacksNotifer implements ContactCallbacks {
@mustCallSuper @mustCallSuper
void postSolve(Object other, Contact contact, ContactImpulse impulse) { void postSolve(Object other, Contact contact, ContactImpulse impulse) {
onPostSolve?.call(other, contact, impulse); onPostSolve?.call(other, contact, impulse);
for (final callback in _callbacks) { for (final callback in _contactCallbacks) {
callback.postSolve(other, contact, impulse); callback.postSolve(other, contact, impulse);
} }
} }
void addCallback(ContactCallbacks callback) { void addContactCallbacks(ContactCallbacks callback) {
_callbacks.add(callback); _contactCallbacks.add(callback);
} }
@override @override

Loading…
Cancel
Save