Skip to content

Add very basic keyboard support #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 35 additions & 9 deletions game/lib/game/game.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'dart:ui';

import 'package:flame/extensions.dart';
import 'package:flame/game.dart';
import 'package:flame/input.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';

import 'analytics.dart';
import 'audio.dart';
Expand All @@ -25,7 +25,7 @@ import 'scoreboard.dart';
import 'spawner.dart';
import 'util.dart';

class MyGame extends FlameGame with TapDetector {
class MyGame extends FlameGame with TapDetector, KeyboardEvents {
static Spawner planetSpawner = Spawner(0.12);

// Setup by the flutter components to allow this game instance
Expand Down Expand Up @@ -220,13 +220,39 @@ class MyGame extends FlameGame with TapDetector {

@override
void onTapDown(TapDownInfo details) {
if (player.regularJetpack) {
player.hoverStart();
}
actionDown();
}

@override
void onTapUp(TapUpInfo details) {
actionUp();
}

@override
KeyEventResult onKeyEvent(
RawKeyEvent event,
Set<LogicalKeyboardKey> keysPressed,
) {
if (event.logicalKey == LogicalKeyboardKey.space) {
if (event is RawKeyDownEvent) {
actionDown();
return KeyEventResult.handled;
} else if (event is RawKeyUpEvent) {
actionUp();
return KeyEventResult.handled;
}
}

return super.onKeyEvent(event, keysPressed);
}

void actionDown() {
if (player.regularJetpack) {
player.hoverStart();
} else if (!player.jetpack) {
gravity *= -1;
}

if (sleeping) {
return;
}
Expand All @@ -237,14 +263,14 @@ class MyGame extends FlameGame with TapDetector {
return;
}
}
super.onTapUp(details);
if (showTutorial == 0) {
showTutorial = -1; // if the player jumps don't show the tutorial
}
}

void actionUp() {
if (player.jetpack) {
player.boost();
} else {
gravity *= -1;
}
}

Expand Down
6 changes: 5 additions & 1 deletion game/lib/screens/game_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class _GameScreenState extends State<GameScreen> {
bool _showGameOver = false;
bool _adLoading = false;

final gameFocusNode = FocusNode();

@override
void initState() {
super.initState();
Expand All @@ -50,6 +52,8 @@ class _GameScreenState extends State<GameScreen> {
_playSection = false;
_playing = true;
});

gameFocusNode.requestFocus();
}

void handleExtraLife() async {
Expand All @@ -74,7 +78,7 @@ class _GameScreenState extends State<GameScreen> {

final children = <Widget>[];

children.add(GameWidget(game: game));
children.add(GameWidget(game: game, focusNode: gameFocusNode));

if (_showGameOver) {
children.add(
Expand Down
73 changes: 50 additions & 23 deletions game/lib/widgets/game_over.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'button.dart';
import 'gr_container.dart';
Expand Down Expand Up @@ -36,7 +37,7 @@ class _Line extends StatelessWidget {
}
}

class GameOverContainer extends StatelessWidget {
class GameOverContainer extends StatefulWidget {
static const WIDTH = 350.0;
static const HEIGHT = 320.0;

Expand All @@ -56,31 +57,57 @@ class GameOverContainer extends StatelessWidget {
required this.extraLife,
});

@override
State<GameOverContainer> createState() => _GameOverContainerState();
}

class _GameOverContainerState extends State<GameOverContainer> {
late final keyboardListenerFocusNode = FocusNode();

@override
void initState() {
keyboardListenerFocusNode.requestFocus();
super.initState();
}

@override
Widget build(BuildContext context) {
return GRContainer(
width: WIDTH,
height: HEIGHT,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Label(
label: 'Game Over',
fontSize: 36,
fontColor: PaletteColors.blues.dark,
),
const SizedBox(height: 20),
_Line('Gems', '$gems'),
_Line('Distance', '$distance'),
const SizedBox(height: 20),
if (showExtraLifeButton)
PrimaryButton(
label: 'Extra life (ad)',
onPress: extraLife,
return KeyboardListener(
focusNode: keyboardListenerFocusNode,
autofocus: true,
onKeyEvent: (keyEvent) {
if (keyEvent is KeyDownEvent &&
keyEvent.logicalKey == LogicalKeyboardKey.space) {
widget.playAgain();
}
},
child: GRContainer(
width: GameOverContainer.WIDTH,
height: GameOverContainer.HEIGHT,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Label(
label: 'Game Over',
fontSize: 36,
fontColor: PaletteColors.blues.dark,
),
PrimaryButton(label: 'Play again', onPress: playAgain),
SecondaryButton(label: 'Back to menu', onPress: goToMainMenu),
],
const SizedBox(height: 20),
_Line('Gems', '${widget.gems}'),
_Line('Distance', '${widget.distance}'),
const SizedBox(height: 20),
if (widget.showExtraLifeButton)
PrimaryButton(
label: 'Extra life (ad)',
onPress: widget.extraLife,
),
PrimaryButton(label: 'Play again', onPress: widget.playAgain),
SecondaryButton(
label: 'Back to menu',
onPress: widget.goToMainMenu,
),
],
),
),
);
}
Expand Down