| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423 |
- import 'package:flame/game.dart';
- import 'package:flutter/material.dart';
- import 'package:flutter/services.dart';
- import 'package:shake/shake.dart';
- import '../l10n/app_localizations.dart';
- import '../game/zentap_game.dart';
- import '../utils/colors.dart';
- import '../utils/settings_manager.dart';
- import '../utils/haptic_utils.dart';
- import '../utils/theme_notifier.dart';
- import '../game/audio/audio_manager.dart';
- import 'components/animated_background.dart';
- import '../utils/app_lifecycle_manager.dart';
- class GameScreen extends StatefulWidget {
- final bool isZenMode;
- const GameScreen({super.key, required this.isZenMode});
- @override
- State<GameScreen> createState() => _GameScreenState();
- }
- class _GameScreenState extends State<GameScreen> {
- late ZenTapGame game;
- bool _isPaused = false;
- ShakeDetector? _shakeDetector;
- final GlobalKey _backgroundKey = GlobalKey();
- @override
- void initState() {
- super.initState();
- game = ZenTapGame();
- game.setZenMode(widget.isZenMode);
- // Register the game instance for app lifecycle management
- AppLifecycleManager.instance.setCurrentGame(game);
- // Hide system UI for immersive experience
- SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersiveSticky);
- }
- @override
- void didChangeDependencies() {
- super.didChangeDependencies();
- // Set localized strings for the game
- final l10n = AppLocalizations.of(context)!;
- game.setLocalizedStrings(
- zenMode: l10n.zenModeGame,
- relaxationPoints: l10n.relaxationPoints,
- time: l10n.time,
- );
- // Initialize shake detector only on mobile platforms
- _initializeShakeDetector();
- // Start ingame music when entering game
- WidgetsBinding.instance.addPostFrameCallback((_) {
- AudioManager().playIngameMusic();
- });
- }
- void _initializeShakeDetector() async {
- if (_shakeDetector != null) return; // Already initialized
- try {
- // Only initialize shake detector on mobile platforms
- if (Theme.of(context).platform == TargetPlatform.android ||
- Theme.of(context).platform == TargetPlatform.iOS) {
- _shakeDetector = ShakeDetector.autoStart(
- onPhoneShake: (ShakeEvent event) => _onPhoneShake(),
- minimumShakeCount: 1,
- shakeSlopTimeMS: 500,
- shakeCountResetTime: 3000,
- shakeThresholdGravity: 2.7,
- );
- }
- } catch (e) {
- // Shake detection not supported on this platform
- print('Shake detection not supported: $e');
- }
- }
- @override
- void dispose() {
- try {
- _shakeDetector?.stopListening();
- } catch (e) {
- // Shake detector might not be initialized on desktop
- }
- // Unregister the game instance from lifecycle management
- AppLifecycleManager.instance.setCurrentGame(null);
- // Restore system UI
- SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
- super.dispose();
- }
- void _onPhoneShake() async {
- if (!_isPaused) {
- // Trigger background shake animation
- final backgroundState = _backgroundKey.currentState;
- if (backgroundState != null) {
- (backgroundState as dynamic).triggerShake();
- }
- // Trigger game shake effects (spawn bubbles and shake existing ones)
- game.handleShake();
- // Haptic feedback
- if (SettingsManager.isHapticsEnabled) {
- await HapticUtils.vibrate(duration: 100);
- }
- }
- }
- @override
- Widget build(BuildContext context) {
- return ThemeAwareBuilder(
- builder: (context, theme) {
- return Scaffold(
- backgroundColor: ZenColors.currentAppBackground,
- body: OrientationBuilder(
- builder: (context, orientation) {
- return KeyboardListener(
- focusNode: FocusNode()..requestFocus(),
- onKeyEvent: (event) {
- // Add keyboard shortcut for shake simulation on desktop
- if (event.logicalKey == LogicalKeyboardKey.space &&
- event is KeyDownEvent) {
- _onPhoneShake();
- }
- },
- child: Stack(
- children: [
- // Animated Background
- AnimatedBackground(
- key: _backgroundKey,
- isZenMode: widget.isZenMode,
- onShake:
- () {}, // Background handles its own shake animation
- child: Container(), // Empty container just for background
- ),
- // Game Widget with tap detection (transparent background)
- GestureDetector(
- onTapDown: (details) async {
- // Add haptic feedback on tap
- if (SettingsManager.isHapticsEnabled) {
- await HapticUtils.vibrate(duration: 30);
- }
- // Convert screen position to game position
- final position = Vector2(
- details.localPosition.dx,
- details.localPosition.dy,
- );
- game.handleTap(position);
- },
- child: GameWidget<ZenTapGame>.controlled(
- gameFactory: () => game,
- ),
- ),
- // Top UI Overlay - adaptive to orientation
- SafeArea(
- child: Padding(
- padding: const EdgeInsets.all(16.0),
- child:
- orientation == Orientation.portrait
- ? _buildPortraitUI()
- : _buildLandscapeUI(),
- ),
- ),
- // Pause Overlay
- if (_isPaused) _buildPauseOverlay(),
- ],
- ),
- );
- },
- ),
- );
- },
- );
- }
- Widget _buildPortraitUI() {
- return Row(
- children: [
- // Back Button
- IconButton(
- onPressed: _showExitDialog,
- icon: Icon(
- Icons.arrow_back,
- color: ZenColors.currentPrimaryText,
- size: 28,
- ),
- style: IconButton.styleFrom(
- backgroundColor: ZenColors.black.withValues(alpha: 0.3),
- shape: const CircleBorder(),
- ),
- ),
- const Spacer(),
- // Mode Indicator
- Container(
- padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
- decoration: BoxDecoration(
- color: ZenColors.black.withValues(alpha: 0.3),
- borderRadius: BorderRadius.circular(20),
- ),
- child: Text(
- widget.isZenMode
- ? AppLocalizations.of(context)!.zenModeGame.toUpperCase()
- : AppLocalizations.of(context)!.playModeGame.toUpperCase(),
- style: TextStyle(
- color: ZenColors.currentPrimaryText,
- fontSize: 14,
- fontWeight: FontWeight.w600,
- letterSpacing: 1.0,
- ),
- ),
- ),
- const Spacer(),
- // Pause Button
- IconButton(
- onPressed: _togglePause,
- icon: Icon(
- _isPaused ? Icons.play_arrow : Icons.pause,
- color: ZenColors.currentPrimaryText,
- size: 28,
- ),
- style: IconButton.styleFrom(
- backgroundColor: ZenColors.black.withValues(alpha: 0.3),
- shape: const CircleBorder(),
- ),
- ),
- ],
- );
- }
- Widget _buildLandscapeUI() {
- return Row(
- children: [
- // Back Button
- IconButton(
- onPressed: _showExitDialog,
- icon: Icon(
- Icons.arrow_back,
- color: ZenColors.currentPrimaryText,
- size: 24,
- ),
- style: IconButton.styleFrom(
- backgroundColor: ZenColors.black.withValues(alpha: 0.3),
- shape: const CircleBorder(),
- ),
- ),
- const SizedBox(width: 16),
- // Mode Indicator (smaller in landscape)
- Container(
- padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
- decoration: BoxDecoration(
- color: ZenColors.black.withValues(alpha: 0.3),
- borderRadius: BorderRadius.circular(16),
- ),
- child: Text(
- widget.isZenMode
- ? AppLocalizations.of(context)!.zenModeShort
- : AppLocalizations.of(context)!.playModeShort,
- style: TextStyle(
- color: ZenColors.currentPrimaryText,
- fontSize: 12,
- fontWeight: FontWeight.w600,
- letterSpacing: 1.0,
- ),
- ),
- ),
- const Spacer(),
- // Pause Button
- IconButton(
- onPressed: _togglePause,
- icon: Icon(
- _isPaused ? Icons.play_arrow : Icons.pause,
- color: ZenColors.currentPrimaryText,
- size: 24,
- ),
- style: IconButton.styleFrom(
- backgroundColor: ZenColors.black.withValues(alpha: 0.3),
- shape: const CircleBorder(),
- ),
- ),
- ],
- );
- }
- Widget _buildPauseOverlay() {
- return Container(
- color: ZenColors.black.withValues(alpha: 0.8),
- child: Center(
- child: Container(
- margin: const EdgeInsets.all(40),
- padding: const EdgeInsets.all(30),
- decoration: BoxDecoration(
- color: ZenColors.currentUiElements,
- borderRadius: BorderRadius.circular(20),
- ),
- child: Column(
- mainAxisSize: MainAxisSize.min,
- children: [
- Text(
- AppLocalizations.of(context)!.paused,
- style: TextStyle(
- color: ZenColors.currentPrimaryText,
- fontSize: 32,
- fontWeight: FontWeight.bold,
- ),
- ),
- const SizedBox(height: 20),
- Text(
- AppLocalizations.of(context)!.takeAMomentToBreathe,
- style: TextStyle(
- color: ZenColors.currentSecondaryText,
- fontSize: 16,
- ),
- ),
- const SizedBox(height: 30),
- // Resume Button
- ElevatedButton(
- onPressed: _togglePause,
- style: ElevatedButton.styleFrom(
- backgroundColor: ZenColors.currentButtonBackground,
- foregroundColor: ZenColors.currentButtonText,
- padding: const EdgeInsets.symmetric(
- horizontal: 40,
- vertical: 15,
- ),
- shape: RoundedRectangleBorder(
- borderRadius: BorderRadius.circular(12),
- ),
- ),
- child: Text(
- AppLocalizations.of(context)!.resume,
- style: TextStyle(fontSize: 18, fontWeight: FontWeight.w600),
- ),
- ),
- ],
- ),
- ),
- ),
- );
- }
- void _togglePause() async {
- if (SettingsManager.isHapticsEnabled) {
- await HapticUtils.vibrate(duration: 50);
- }
- setState(() {
- _isPaused = !_isPaused;
- if (_isPaused) {
- game.pauseGame();
- } else {
- game.resumeGame();
- }
- });
- }
- void _showExitDialog() async {
- if (!mounted) return;
- if (SettingsManager.isHapticsEnabled) {
- await HapticUtils.vibrate(duration: 50);
- }
- if (!mounted) return;
- showDialog(
- context: context,
- builder: (BuildContext context) {
- return AlertDialog(
- backgroundColor: ZenColors.currentUiElements,
- title: Text(
- AppLocalizations.of(context)!.leaveGame,
- style: TextStyle(color: ZenColors.currentPrimaryText),
- ),
- content: Text(
- AppLocalizations.of(context)!.leaveGameConfirm,
- style: TextStyle(color: ZenColors.currentSecondaryText),
- ),
- actions: [
- TextButton(
- onPressed: () => Navigator.of(context).pop(),
- child: Text(
- AppLocalizations.of(context)!.cancel,
- style: TextStyle(color: ZenColors.currentLinks),
- ),
- ),
- ElevatedButton(
- onPressed: () {
- Navigator.of(context).pop(); // Close dialog
- Navigator.of(
- context,
- ).pop(true); // Return to main menu with result
- },
- style: ElevatedButton.styleFrom(
- backgroundColor: ZenColors.red,
- foregroundColor: ZenColors.white,
- ),
- child: Text(AppLocalizations.of(context)!.leave),
- ),
- ],
- );
- },
- );
- }
- }
|