Before Width: | Height: | Size: 306 KiB After Width: | Height: | Size: 207 KiB |
Before Width: | Height: | Size: 422 KiB After Width: | Height: | Size: 209 KiB |
Before Width: | Height: | Size: 171 KiB After Width: | Height: | Size: 124 KiB |
Before Width: | Height: | Size: 200 KiB After Width: | Height: | Size: 105 KiB |
Before Width: | Height: | Size: 298 KiB After Width: | Height: | Size: 169 KiB |
Before Width: | Height: | Size: 6.2 KiB |
@ -0,0 +1,13 @@
|
|||||||
|
// ignore_for_file: public_member_api_docs
|
||||||
|
|
||||||
|
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||||
|
import 'package:pinball_audio/pinball_audio.dart';
|
||||||
|
import 'package:pinball_flame/pinball_flame.dart';
|
||||||
|
|
||||||
|
class CowBumperNoiseBehavior extends ContactBehavior {
|
||||||
|
@override
|
||||||
|
void beginContact(Object other, Contact contact) {
|
||||||
|
super.beginContact(other, contact);
|
||||||
|
readProvider<PinballAudioPlayer>().play(PinballAudio.cowMoo);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,189 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
|
||||||
|
import 'package:flame/components.dart';
|
||||||
|
import 'package:flame/input.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:pinball/l10n/l10n.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
import 'package:pinball_flame/pinball_flame.dart';
|
||||||
|
import 'package:pinball_ui/pinball_ui.dart';
|
||||||
|
import 'package:share_repository/share_repository.dart';
|
||||||
|
|
||||||
|
/// Signature for the callback called when the user tries to share their score
|
||||||
|
/// on the [ShareDisplay].
|
||||||
|
typedef OnSocialShareTap = void Function(SharePlatform);
|
||||||
|
|
||||||
|
final _descriptionTextPaint = TextPaint(
|
||||||
|
style: const TextStyle(
|
||||||
|
fontSize: 1.6,
|
||||||
|
color: PinballColors.white,
|
||||||
|
fontFamily: PinballFonts.pixeloidSans,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
/// {@template share_display}
|
||||||
|
/// Display that allows users to share their score to social networks.
|
||||||
|
/// {@endtemplate}
|
||||||
|
class ShareDisplay extends Component with HasGameRef {
|
||||||
|
/// {@macro share_display}
|
||||||
|
ShareDisplay({
|
||||||
|
OnSocialShareTap? onShare,
|
||||||
|
}) : super(
|
||||||
|
children: [
|
||||||
|
_ShareInstructionsComponent(
|
||||||
|
onShare: onShare,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
class _ShareInstructionsComponent extends PositionComponent with HasGameRef {
|
||||||
|
_ShareInstructionsComponent({
|
||||||
|
OnSocialShareTap? onShare,
|
||||||
|
}) : super(
|
||||||
|
anchor: Anchor.center,
|
||||||
|
position: Vector2(0, -25),
|
||||||
|
children: [
|
||||||
|
_DescriptionComponent(),
|
||||||
|
_SocialNetworksComponent(
|
||||||
|
onShare: onShare,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
class _DescriptionComponent extends PositionComponent with HasGameRef {
|
||||||
|
_DescriptionComponent()
|
||||||
|
: super(
|
||||||
|
anchor: Anchor.center,
|
||||||
|
position: Vector2.zero(),
|
||||||
|
children: [
|
||||||
|
_LetEveryoneTextComponent(),
|
||||||
|
_SharingYourScoreTextComponent(),
|
||||||
|
_SocialMediaTextComponent(),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
class _LetEveryoneTextComponent extends TextComponent with HasGameRef {
|
||||||
|
_LetEveryoneTextComponent()
|
||||||
|
: super(
|
||||||
|
anchor: Anchor.center,
|
||||||
|
position: Vector2.zero(),
|
||||||
|
textRenderer: _descriptionTextPaint,
|
||||||
|
);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> onLoad() async {
|
||||||
|
await super.onLoad();
|
||||||
|
text = readProvider<AppLocalizations>().letEveryone;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class _SharingYourScoreTextComponent extends TextComponent with HasGameRef {
|
||||||
|
_SharingYourScoreTextComponent()
|
||||||
|
: super(
|
||||||
|
anchor: Anchor.center,
|
||||||
|
position: Vector2(0, 2.5),
|
||||||
|
textRenderer: _descriptionTextPaint,
|
||||||
|
);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> onLoad() async {
|
||||||
|
await super.onLoad();
|
||||||
|
text = readProvider<AppLocalizations>().bySharingYourScore;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class _SocialMediaTextComponent extends TextComponent with HasGameRef {
|
||||||
|
_SocialMediaTextComponent()
|
||||||
|
: super(
|
||||||
|
anchor: Anchor.center,
|
||||||
|
position: Vector2(0, 5),
|
||||||
|
textRenderer: _descriptionTextPaint,
|
||||||
|
);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> onLoad() async {
|
||||||
|
await super.onLoad();
|
||||||
|
text = readProvider<AppLocalizations>().socialMediaAccount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class _SocialNetworksComponent extends PositionComponent with HasGameRef {
|
||||||
|
_SocialNetworksComponent({
|
||||||
|
OnSocialShareTap? onShare,
|
||||||
|
}) : super(
|
||||||
|
anchor: Anchor.center,
|
||||||
|
position: Vector2(0, 12),
|
||||||
|
children: [
|
||||||
|
FacebookButtonComponent(onTap: onShare),
|
||||||
|
TwitterButtonComponent(onTap: onShare),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// {@template facebook_button_component}
|
||||||
|
/// Button for sharing on Facebook.
|
||||||
|
/// {@endtemplate}
|
||||||
|
class FacebookButtonComponent extends SpriteComponent
|
||||||
|
with HasGameRef, Tappable {
|
||||||
|
/// {@macro facebook_button_component}
|
||||||
|
FacebookButtonComponent({
|
||||||
|
OnSocialShareTap? onTap,
|
||||||
|
}) : _onTap = onTap,
|
||||||
|
super(
|
||||||
|
anchor: Anchor.center,
|
||||||
|
position: Vector2(-5, 0),
|
||||||
|
);
|
||||||
|
|
||||||
|
final OnSocialShareTap? _onTap;
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool onTapDown(TapDownInfo info) {
|
||||||
|
_onTap?.call(SharePlatform.facebook);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> onLoad() async {
|
||||||
|
await super.onLoad();
|
||||||
|
final sprite = Sprite(
|
||||||
|
gameRef.images.fromCache(Assets.images.backbox.button.facebook.keyName),
|
||||||
|
);
|
||||||
|
this.sprite = sprite;
|
||||||
|
size = sprite.originalSize / 25;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// {@template twitter_button_component}
|
||||||
|
/// Button for sharing on Twitter.
|
||||||
|
/// {@endtemplate}
|
||||||
|
class TwitterButtonComponent extends SpriteComponent with HasGameRef, Tappable {
|
||||||
|
/// {@macro twitter_button_component}
|
||||||
|
TwitterButtonComponent({
|
||||||
|
OnSocialShareTap? onTap,
|
||||||
|
}) : _onTap = onTap,
|
||||||
|
super(
|
||||||
|
anchor: Anchor.center,
|
||||||
|
position: Vector2(5, 0),
|
||||||
|
);
|
||||||
|
|
||||||
|
final OnSocialShareTap? _onTap;
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool onTapDown(TapDownInfo info) {
|
||||||
|
_onTap?.call(SharePlatform.twitter);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> onLoad() async {
|
||||||
|
await super.onLoad();
|
||||||
|
final sprite = Sprite(
|
||||||
|
gameRef.images.fromCache(Assets.images.backbox.button.twitter.keyName),
|
||||||
|
);
|
||||||
|
this.sprite = sprite;
|
||||||
|
size = sprite.originalSize / 25;
|
||||||
|
}
|
||||||
|
}
|
After Width: | Height: | Size: 3.1 KiB |
After Width: | Height: | Size: 3.5 KiB |
@ -0,0 +1,58 @@
|
|||||||
|
// ignore_for_file: cascade_invocations
|
||||||
|
|
||||||
|
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||||
|
import 'package:flame_test/flame_test.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:mocktail/mocktail.dart';
|
||||||
|
import 'package:pinball/game/behaviors/behaviors.dart';
|
||||||
|
import 'package:pinball_audio/pinball_audio.dart';
|
||||||
|
import 'package:pinball_flame/pinball_flame.dart';
|
||||||
|
|
||||||
|
class _TestGame extends Forge2DGame {
|
||||||
|
Future<void> pump(
|
||||||
|
_TestBodyComponent child, {
|
||||||
|
required PinballAudioPlayer audioPlayer,
|
||||||
|
}) {
|
||||||
|
return ensureAdd(
|
||||||
|
FlameProvider<PinballAudioPlayer>.value(
|
||||||
|
audioPlayer,
|
||||||
|
children: [child],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class _TestBodyComponent extends BodyComponent {
|
||||||
|
@override
|
||||||
|
Body createBody() => world.createBody(BodyDef());
|
||||||
|
}
|
||||||
|
|
||||||
|
class _MockPinballAudioPlayer extends Mock implements PinballAudioPlayer {}
|
||||||
|
|
||||||
|
class _MockContact extends Mock implements Contact {}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
TestWidgetsFlutterBinding.ensureInitialized();
|
||||||
|
|
||||||
|
group('CowBumperNoiseBehavior', () {
|
||||||
|
late PinballAudioPlayer audioPlayer;
|
||||||
|
final flameTester = FlameTester(_TestGame.new);
|
||||||
|
|
||||||
|
setUp(() {
|
||||||
|
audioPlayer = _MockPinballAudioPlayer();
|
||||||
|
});
|
||||||
|
flameTester.testGameWidget(
|
||||||
|
'plays cow moo sound on contact',
|
||||||
|
setUp: (game, _) async {
|
||||||
|
final behavior = CowBumperNoiseBehavior();
|
||||||
|
final parent = _TestBodyComponent();
|
||||||
|
await game.pump(parent, audioPlayer: audioPlayer);
|
||||||
|
await parent.ensureAdd(behavior);
|
||||||
|
behavior.beginContact(Object(), _MockContact());
|
||||||
|
},
|
||||||
|
verify: (_, __) async {
|
||||||
|
verify(() => audioPlayer.play(PinballAudio.cowMoo)).called(1);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
@ -0,0 +1,112 @@
|
|||||||
|
// ignore_for_file: cascade_invocations
|
||||||
|
|
||||||
|
import 'package:flame/game.dart';
|
||||||
|
import 'package:flame/input.dart';
|
||||||
|
import 'package:flame_bloc/flame_bloc.dart';
|
||||||
|
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||||
|
import 'package:flame_test/flame_test.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:mocktail/mocktail.dart';
|
||||||
|
import 'package:pinball/game/bloc/game_bloc.dart';
|
||||||
|
import 'package:pinball/game/components/backbox/displays/share_display.dart';
|
||||||
|
import 'package:pinball/l10n/l10n.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
import 'package:pinball_flame/pinball_flame.dart';
|
||||||
|
|
||||||
|
class _TestGame extends Forge2DGame with HasTappables {
|
||||||
|
@override
|
||||||
|
Future<void> onLoad() async {
|
||||||
|
await super.onLoad();
|
||||||
|
images.prefix = '';
|
||||||
|
await images.loadAll(
|
||||||
|
[
|
||||||
|
Assets.images.backbox.button.facebook.keyName,
|
||||||
|
Assets.images.backbox.button.twitter.keyName,
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> pump(ShareDisplay component) {
|
||||||
|
return ensureAdd(
|
||||||
|
FlameBlocProvider<GameBloc, GameState>.value(
|
||||||
|
value: GameBloc(),
|
||||||
|
children: [
|
||||||
|
FlameProvider.value(
|
||||||
|
_MockAppLocalizations(),
|
||||||
|
children: [component],
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class _MockAppLocalizations extends Mock implements AppLocalizations {
|
||||||
|
@override
|
||||||
|
String get letEveryone => '';
|
||||||
|
|
||||||
|
@override
|
||||||
|
String get bySharingYourScore => '';
|
||||||
|
|
||||||
|
@override
|
||||||
|
String get socialMediaAccount => '';
|
||||||
|
}
|
||||||
|
|
||||||
|
class _MockTapDownInfo extends Mock implements TapDownInfo {}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
TestWidgetsFlutterBinding.ensureInitialized();
|
||||||
|
|
||||||
|
final flameTester = FlameTester(_TestGame.new);
|
||||||
|
|
||||||
|
group('ShareDisplay', () {
|
||||||
|
flameTester.test(
|
||||||
|
'loads correctly',
|
||||||
|
(game) async {
|
||||||
|
final component = ShareDisplay();
|
||||||
|
await game.pump(component);
|
||||||
|
expect(game.descendants(), contains(component));
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
flameTester.test(
|
||||||
|
'calls onShare when Facebook button is tapped',
|
||||||
|
(game) async {
|
||||||
|
var tapped = false;
|
||||||
|
|
||||||
|
final tapDownInfo = _MockTapDownInfo();
|
||||||
|
final component = ShareDisplay(
|
||||||
|
onShare: (_) => tapped = true,
|
||||||
|
);
|
||||||
|
await game.pump(component);
|
||||||
|
|
||||||
|
final facebookButton =
|
||||||
|
component.descendants().whereType<FacebookButtonComponent>().first;
|
||||||
|
|
||||||
|
facebookButton.onTapDown(tapDownInfo);
|
||||||
|
|
||||||
|
expect(tapped, isTrue);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
flameTester.test(
|
||||||
|
'calls onShare when Twitter button is tapped',
|
||||||
|
(game) async {
|
||||||
|
var tapped = false;
|
||||||
|
|
||||||
|
final tapDownInfo = _MockTapDownInfo();
|
||||||
|
final component = ShareDisplay(
|
||||||
|
onShare: (_) => tapped = true,
|
||||||
|
);
|
||||||
|
await game.pump(component);
|
||||||
|
|
||||||
|
final twitterButton =
|
||||||
|
component.descendants().whereType<TwitterButtonComponent>().first;
|
||||||
|
|
||||||
|
twitterButton.onTapDown(tapDownInfo);
|
||||||
|
|
||||||
|
expect(tapped, isTrue);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|