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:flame_forge2d/flame_forge2d.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
/// {@template contact_callbacks_adder}
|
class ContactCallbacksNotifer implements ContactCallbacks {
|
||||||
///
|
final List<ContactCallbacks> _callbacks = [];
|
||||||
/// {@endtemplate}
|
|
||||||
// TODO(alestiago): Consider adding streams to [ContactCallbacks].
|
@override
|
||||||
extension ContactCallbacksAdder on ContactCallbacks {
|
@mustCallSuper
|
||||||
/// {@macro contact_callbacks_adder}
|
void beginContact(Object other, Contact contact) {
|
||||||
void add(ContactCallbacks contactCallbacks) {
|
onBeginContact?.call(other, contact);
|
||||||
if (contactCallbacks.onBeginContact != null) {
|
for (final callback in _callbacks) {
|
||||||
onBeginContact = (other, contact) {
|
callback.beginContact(other, contact);
|
||||||
onBeginContact?.call(other, contact);
|
|
||||||
contactCallbacks.beginContact(other, contact);
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (contactCallbacks.onEndContact != null) {
|
@override
|
||||||
onEndContact = (other, contact) {
|
@mustCallSuper
|
||||||
onEndContact?.call(other, contact);
|
void endContact(Object other, Contact contact) {
|
||||||
contactCallbacks.endContact(other, contact);
|
onEndContact?.call(other, contact);
|
||||||
};
|
for (final callback in _callbacks) {
|
||||||
|
callback.endContact(other, contact);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (contactCallbacks.onPreSolve != null) {
|
@override
|
||||||
onPreSolve = (other, contact, oldManifold) {
|
@mustCallSuper
|
||||||
onPreSolve?.call(other, contact, oldManifold);
|
void preSolve(Object other, Contact contact, Manifold oldManifold) {
|
||||||
contactCallbacks.preSolve(other, contact, oldManifold);
|
onPreSolve?.call(other, contact, oldManifold);
|
||||||
};
|
for (final callback in _callbacks) {
|
||||||
|
callback.preSolve(other, contact, oldManifold);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (contactCallbacks.onPostSolve != null) {
|
@override
|
||||||
onPostSolve = (other, contact, impulse) {
|
@mustCallSuper
|
||||||
onPostSolve?.call(other, contact, impulse);
|
void postSolve(Object other, Contact contact, ContactImpulse impulse) {
|
||||||
contactCallbacks.postSolve(other, contact, impulse);
|
onPostSolve?.call(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