mirror of https://github.com/flutter/pinball.git
parent
318da8c23a
commit
1420f33049
@ -0,0 +1,36 @@
|
||||
import 'package:flame/components.dart';
|
||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
import 'package:pinball_flame/pinball_flame.dart';
|
||||
|
||||
class AlienBumperContactBehavior extends Component with ContactCallbacks {
|
||||
@override
|
||||
Future<void> onLoad() async {
|
||||
await super.onLoad();
|
||||
|
||||
// TODO(alestiago): Refactor once the following is merged:
|
||||
// https://github.com/flame-engine/flame/pull/1566
|
||||
final parent = this.parent;
|
||||
if (parent is! AlienBumper) return;
|
||||
|
||||
final userData = parent.body.userData;
|
||||
if (userData is ContactCallbacksNotifer) {
|
||||
userData.addCallback(this);
|
||||
} else {
|
||||
parent.body.userData = this;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void beginContact(Object other, Contact contact) {
|
||||
super.beginContact(other, contact);
|
||||
if (other is! Ball) return;
|
||||
|
||||
// TODO(alestiago): Refactor once the following is merged:
|
||||
// https://github.com/flame-engine/flame/pull/1566
|
||||
final parent = this.parent;
|
||||
if (parent is! AlienBumper) return;
|
||||
|
||||
parent.state = AlienBumperState.inactive;
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
import 'package:flame/components.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
|
||||
/// {@template alien_bumper_sprite_behavior}
|
||||
///
|
||||
/// {@endtemplate}
|
||||
class AlienBumperSpriteBehavior extends TimerComponent {
|
||||
/// {@macro alien_bumper_sprite_behavior}
|
||||
AlienBumperSpriteBehavior()
|
||||
: super(
|
||||
period: 0.05,
|
||||
removeOnFinish: false,
|
||||
);
|
||||
|
||||
void _onNewState(AlienBumperState state) {
|
||||
switch (state) {
|
||||
case AlienBumperState.active:
|
||||
timer.stop();
|
||||
break;
|
||||
case AlienBumperState.inactive:
|
||||
timer
|
||||
..reset()
|
||||
..start();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> onLoad() async {
|
||||
await super.onLoad();
|
||||
|
||||
// TODO(alestiago): Refactor once the following is merged:
|
||||
// https://github.com/flame-engine/flame/pull/1566
|
||||
final parent = this.parent;
|
||||
if (parent is! AlienBumper) return;
|
||||
|
||||
timer.stop();
|
||||
parent.stream.listen(_onNewState);
|
||||
}
|
||||
|
||||
@override
|
||||
void onTick() {
|
||||
super.onTick();
|
||||
// TODO(alestiago): Refactor once the following is merged:
|
||||
// https://github.com/flame-engine/flame/pull/1566
|
||||
final parent = this.parent;
|
||||
if (parent is! AlienBumper) return;
|
||||
|
||||
parent.state = AlienBumperState.active;
|
||||
}
|
||||
}
|
@ -0,0 +1,2 @@
|
||||
export 'alien_bumper_contact_behavior.dart';
|
||||
export 'alien_bumper_sprite_behavior.dart';
|
@ -1,38 +1,60 @@
|
||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// {@template contact_callbacks_adder}
|
||||
///
|
||||
/// {@endtemplate}
|
||||
// TODO(alestiago): Consider adding streams to [ContactCallbacks].
|
||||
extension ContactCallbacksAdder on ContactCallbacks {
|
||||
/// {@macro contact_callbacks_adder}
|
||||
void add(ContactCallbacks contactCallbacks) {
|
||||
if (contactCallbacks.onBeginContact != null) {
|
||||
onBeginContact = (other, contact) {
|
||||
class ContactCallbacksNotifer implements ContactCallbacks {
|
||||
final List<ContactCallbacks> _callbacks = [];
|
||||
|
||||
@override
|
||||
@mustCallSuper
|
||||
void beginContact(Object other, Contact contact) {
|
||||
onBeginContact?.call(other, contact);
|
||||
contactCallbacks.beginContact(other, contact);
|
||||
};
|
||||
for (final callback in _callbacks) {
|
||||
callback.beginContact(other, contact);
|
||||
}
|
||||
}
|
||||
|
||||
if (contactCallbacks.onEndContact != null) {
|
||||
onEndContact = (other, contact) {
|
||||
@override
|
||||
@mustCallSuper
|
||||
void endContact(Object other, Contact contact) {
|
||||
onEndContact?.call(other, contact);
|
||||
contactCallbacks.endContact(other, contact);
|
||||
};
|
||||
for (final callback in _callbacks) {
|
||||
callback.endContact(other, contact);
|
||||
}
|
||||
}
|
||||
|
||||
if (contactCallbacks.onPreSolve != null) {
|
||||
onPreSolve = (other, contact, oldManifold) {
|
||||
@override
|
||||
@mustCallSuper
|
||||
void preSolve(Object other, Contact contact, Manifold oldManifold) {
|
||||
onPreSolve?.call(other, contact, oldManifold);
|
||||
contactCallbacks.preSolve(other, contact, oldManifold);
|
||||
};
|
||||
for (final callback in _callbacks) {
|
||||
callback.preSolve(other, contact, oldManifold);
|
||||
}
|
||||
}
|
||||
|
||||
if (contactCallbacks.onPostSolve != null) {
|
||||
onPostSolve = (other, contact, impulse) {
|
||||
@override
|
||||
@mustCallSuper
|
||||
void postSolve(Object other, Contact contact, ContactImpulse impulse) {
|
||||
onPostSolve?.call(other, contact, impulse);
|
||||
contactCallbacks.postSolve(other, contact, impulse);
|
||||
};
|
||||
for (final callback in _callbacks) {
|
||||
callback.postSolve(other, contact, impulse);
|
||||
}
|
||||
}
|
||||
|
||||
void addCallback(ContactCallbacks callback) {
|
||||
_callbacks.add(callback);
|
||||
}
|
||||
|
||||
@override
|
||||
void Function(Object other, Contact contact)? onBeginContact;
|
||||
|
||||
@override
|
||||
void Function(Object other, Contact contact)? onEndContact;
|
||||
|
||||
@override
|
||||
void Function(Object other, Contact contact, ContactImpulse impulse)?
|
||||
onPostSolve;
|
||||
|
||||
@override
|
||||
void Function(Object other, Contact contact, Manifold oldManifold)?
|
||||
onPreSolve;
|
||||
}
|
||||
|
Loading…
Reference in new issue