Skip to content

Commit

Permalink
feat: F3 preparations (#98)
Browse files Browse the repository at this point in the history
* Show achievement QR-code for top players

* Fix min level

* WIP

* Upgrade to Flame 1.9.0

* Try with y-axis

* Debouncing

* Correct direction

* Doubles are not needed

* Reverse direction

* Try out good min values

* Movement in lobby

* Upgrade

* Add flag for scavenger hunt
  • Loading branch information
spydon authored Sep 22, 2023
1 parent 9153e80 commit 18637e7
Show file tree
Hide file tree
Showing 15 changed files with 187 additions and 103 deletions.
Binary file added assets/images/qr-top.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 44 additions & 2 deletions lib/end_game/view/end_game_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:async';
import 'package:flutter/material.dart';
import 'package:gamepads/gamepads.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:lightrunners/firebase/score.dart';
import 'package:lightrunners/firebase/score_calculator.dart';
import 'package:lightrunners/firebase/scores.dart';
import 'package:lightrunners/game/game.dart';
Expand Down Expand Up @@ -35,15 +36,19 @@ class _EndGamePageState extends State<EndGamePage> {
late StreamSubscription<GamepadEvent> _gamepadSubscription;
late GamepadNavigator _gamepadNavigator;

bool isScavengerHunt = true;
bool updatingFirebase = true;
bool receivedTopPlayers = false;
bool minDurationElapsed = false;
Iterable<Score> topPlayers = [];

bool get canMoveOn => !updatingFirebase && minDurationElapsed;
bool get canMoveOn =>
!updatingFirebase && minDurationElapsed && receivedTopPlayers;

@override
void initState() {
super.initState();
_updateFirebase();
_updateFirebase().whenComplete(_fetchTopPlayers);
_gamepadNavigator = GamepadNavigator(
onAny: () {
if (canMoveOn) {
Expand Down Expand Up @@ -78,6 +83,16 @@ class _EndGamePageState extends State<EndGamePage> {
setState(() => updatingFirebase = false);
}

Future<void> _fetchTopPlayers() async {
final topPlayers = await Scores.fetchTopPlayers(
playerIds: widget.points.keys.map((p) => p.playerId).whereType<int>(),
);
setState(() {
this.topPlayers = topPlayers;
receivedTopPlayers = true;
});
}

@override
Widget build(BuildContext context) {
final fontFamily = GoogleFonts.bungee().fontFamily;
Expand Down Expand Up @@ -158,6 +173,33 @@ class _EndGamePageState extends State<EndGamePage> {
),
],
),
if (isScavengerHunt && topPlayers.isNotEmpty) ...[
Text(
'Congratulations, the following players have received\n'
'an achievement and can scan the QR code with the F3 app!',
style: TextStyle(
fontFamily: fontFamily,
fontSize: 32,
color: GamePalette.yellow,
),
),
Text(
topPlayers.map((p) => p.username).join(' | '),
style: TextStyle(
fontFamily: fontFamily,
fontSize: 32,
color: GamePalette.lightBlue,
),
),
Align(
child: Image.asset(
'assets/images/qr-top.png',
width: 600,
height: 600,
fit: BoxFit.cover,
),
),
],
OpacityBlinker(
child: TextButton(
onPressed: () =>
Expand Down
26 changes: 26 additions & 0 deletions lib/firebase/scores.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,32 @@ class Scores {
}
}

static Future<Iterable<Score>> fetchTopPlayers({
required Iterable<int> playerIds,
}) async {
final firestore = _firestore;
if (firestore == null) {
print('Error: Firestore not initialized.');
return [];
} else if (playerIds.isEmpty) {
print('No registered players playing this round.');
return [];
}

final topPlayers = (await firestore
.collection('scores')
.where(
'playerId',
whereIn: playerIds.map((i) => i.toString()).toList(),
)
.where('score', isGreaterThanOrEqualTo: 10)
.get())
.map(Score.fromDocument)
.toList();
print(topPlayers);
return topPlayers;
}

static String _getServiceAccountUuid() {
return File('.secrets/uuid.conf').readAsStringSync();
}
Expand Down
1 change: 0 additions & 1 deletion lib/game/components/background.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'dart:ui';

import 'package:collection/collection.dart';
import 'package:flame/components.dart';
import 'package:flame/experimental.dart';
import 'package:flame/extensions.dart';
import 'package:lightrunners/game/lightrunners_game.dart';
import 'package:lightrunners/ui/palette.dart';
Expand Down
1 change: 0 additions & 1 deletion lib/game/components/bullet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'dart:ui';

import 'package:flame/collisions.dart';
import 'package:flame/components.dart';
import 'package:flame/experimental.dart';
import 'package:lightrunners/game/game.dart';
import 'package:lightrunners/ui/ui.dart';

Expand Down
2 changes: 1 addition & 1 deletion lib/game/components/count_down.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:flutter/painting.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:lightrunners/ui/ui.dart';

const _matchLength = 60.0;
const _matchLength = 4.0;
const _radius = Radius.circular(3.0);

class CountDown extends PositionComponent {
Expand Down
1 change: 0 additions & 1 deletion lib/game/components/powerup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'dart:math';
import 'package:flame/collisions.dart';
import 'package:flame/components.dart';
import 'package:flame/effects.dart';
import 'package:flame/experimental.dart';
import 'package:flame/extensions.dart';
import 'package:flame/geometry.dart';
import 'package:flutter/animation.dart';
Expand Down
1 change: 0 additions & 1 deletion lib/game/components/score_box.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:flame/components.dart';
import 'package:flame/experimental.dart';
import 'package:flutter/painting.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:lightrunners/game/components/ship.dart';
Expand Down
1 change: 0 additions & 1 deletion lib/game/components/score_panel.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'dart:ui';

import 'package:flame/components.dart';
import 'package:flame/experimental.dart';
import 'package:lightrunners/game/components/score_box.dart';
import 'package:lightrunners/game/lightrunners_game.dart';
import 'package:lightrunners/ui/palette.dart';
Expand Down
1 change: 0 additions & 1 deletion lib/game/components/ship.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'dart:ui';

import 'package:flame/collisions.dart';
import 'package:flame/components.dart';
import 'package:flame/experimental.dart';
import 'package:flame/extensions.dart';
import 'package:flame/geometry.dart';
import 'package:flutter/services.dart';
Expand Down
1 change: 0 additions & 1 deletion lib/game/components/spotlight.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'dart:ui';

import 'package:flame/collisions.dart';
import 'package:flame/components.dart';
import 'package:flame/experimental.dart';
import 'package:flame/palette.dart';
import 'package:lightrunners/game/components/ship.dart';
import 'package:lightrunners/game/lightrunners_game.dart';
Expand Down
18 changes: 7 additions & 11 deletions lib/game/lightrunners_game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,22 @@ class LightRunnersGame extends FlameGame
final List<Player> players;
late final Rect playArea;
late final CameraComponent cameraComponent;
late final World world;
late final Map<String, Ship> ships;

StreamSubscription<GamepadEvent>? _subscription;
final void Function(Map<Player, int>) onEndGame;

LightRunnersGame({required this.players, required this.onEndGame});
LightRunnersGame({required this.players, required this.onEndGame})
: super(
camera: CameraComponent.withFixedResolution(
width: fixedSize.x,
height: fixedSize.y,
),
);

@override
Future<void> onLoad() async {
_createShips();
world = World();

cameraComponent = CameraComponent.withFixedResolution(
world: world,
width: fixedSize.x,
height: fixedSize.y,
);
add(cameraComponent);

playArea = Rect.fromLTWH(
screenMargin - fixedSize.x / 2,
Expand All @@ -49,7 +46,6 @@ class LightRunnersGame extends FlameGame
cameraComponent.viewport.add(ScorePanel());
world.addAll([Background(), GameBorder()]);
world.addAll([Spotlight(), ...ships.values]);
add(world);

late final CountDown countDown;
add(
Expand Down
27 changes: 23 additions & 4 deletions lib/lobby/view/lobby_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ class _PlayerIdentificationState extends State<PlayerIdentification> {
var _selectedPlayerMode = false;
var _playerId = '0000';
var _cursor = 0;
int? _lastChange;
int? _lastDirection;

late StreamSubscription<GamepadEvent> _gamepadSubscription;

Expand All @@ -265,6 +267,13 @@ class _PlayerIdentificationState extends State<PlayerIdentification> {

_gamepadSubscription = Gamepads.events.listen((GamepadEvent event) {
setState(() {
final isYaxis = leftYAxis.matches(event) || rightYAxis.matches(event);
final isRight =
(leftXAxis.matches(event) || rightXAxis.matches(event)) &&
event.value > 30000;
final isLeft =
(leftXAxis.matches(event) || rightXAxis.matches(event)) &&
event.value < -30000;
if (event.gamepadId == widget.gamepadId) {
if (_selectedPlayerMode == false) {
if (aButton.matches(event)) {
Expand All @@ -275,8 +284,16 @@ class _PlayerIdentificationState extends State<PlayerIdentification> {
widget.onPlayerIdentified(widget.slotNumber, null);
}
} else {
if (aButton.matches(event) || bButton.matches(event)) {
final delta = aButton.matches(event) ? 1 : -1;
if (isYaxis) {
final delta = event.value.sign.toInt() * -1;
if ((_lastChange != null &&
event.timestamp - _lastChange! < 200 &&
delta == _lastDirection) ||
event.value.abs() < 5000) {
return;
}
_lastChange = event.timestamp;
_lastDirection = delta;
// Increment current digit
final digit =
int.parse(_playerId.substring(_cursor, _cursor + 1));
Expand All @@ -288,12 +305,14 @@ class _PlayerIdentificationState extends State<PlayerIdentification> {
'$newDigit',
);
});
} else if (r1Bumper.matches(event) && event.value > 30000) {
} else if (isRight ||
(r1Bumper.matches(event) && event.value > 30000)) {
// Move the cursor to the next digit if R1 is fully pressed
setState(() {
_cursor = (_cursor + 1) % _playerId.characters.length;
});
} else if (l1Bumper.matches(event) && event.value > 30000) {
} else if (isLeft ||
(l1Bumper.matches(event) && event.value > 30000)) {
// Move the cursor to the previous digit if L1 is fully pressed
setState(() {
_cursor = (_cursor - 1) % _playerId.characters.length;
Expand Down
Loading

0 comments on commit 18637e7

Please sign in to comment.