game_screen.dart 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. import 'package:flame/components.dart';
  2. import 'package:flame/game.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter/services.dart';
  5. import '../game/zentap_game.dart';
  6. import '../utils/colors.dart';
  7. class GameScreen extends StatefulWidget {
  8. final bool isZenMode;
  9. const GameScreen({
  10. super.key,
  11. required this.isZenMode,
  12. });
  13. @override
  14. State<GameScreen> createState() => _GameScreenState();
  15. }
  16. class _GameScreenState extends State<GameScreen> {
  17. late ZenTapGame game;
  18. bool _isPaused = false;
  19. @override
  20. void initState() {
  21. super.initState();
  22. game = ZenTapGame();
  23. game.setZenMode(widget.isZenMode);
  24. // Hide system UI for immersive experience
  25. SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersiveSticky);
  26. }
  27. @override
  28. void dispose() {
  29. // Restore system UI
  30. SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
  31. super.dispose();
  32. }
  33. @override
  34. Widget build(BuildContext context) {
  35. return Scaffold(
  36. backgroundColor: ZenColors.appBackground,
  37. body: Stack(
  38. children: [
  39. // Game Widget with tap detection
  40. GestureDetector(
  41. onTapDown: (details) {
  42. // Convert screen position to game position
  43. final position = Vector2(
  44. details.localPosition.dx,
  45. details.localPosition.dy,
  46. );
  47. game.handleTap(position);
  48. },
  49. child: GameWidget<ZenTapGame>.controlled(
  50. gameFactory: () => game,
  51. ),
  52. ),
  53. // Top UI Overlay
  54. SafeArea(
  55. child: Padding(
  56. padding: const EdgeInsets.all(16.0),
  57. child: Row(
  58. children: [
  59. // Back Button
  60. IconButton(
  61. onPressed: _showExitDialog,
  62. icon: const Icon(
  63. Icons.arrow_back,
  64. color: ZenColors.primaryText,
  65. size: 28,
  66. ),
  67. style: IconButton.styleFrom(
  68. backgroundColor: ZenColors.black.withValues(alpha: 0.3),
  69. shape: const CircleBorder(),
  70. ),
  71. ),
  72. const Spacer(),
  73. // Mode Indicator
  74. Container(
  75. padding: const EdgeInsets.symmetric(
  76. horizontal: 16,
  77. vertical: 8,
  78. ),
  79. decoration: BoxDecoration(
  80. color: ZenColors.black.withValues(alpha: 0.3),
  81. borderRadius: BorderRadius.circular(20),
  82. ),
  83. child: Text(
  84. widget.isZenMode ? 'ZEN MODE' : 'PLAY MODE',
  85. style: TextStyle(
  86. color: ZenColors.primaryText,
  87. fontSize: 14,
  88. fontWeight: FontWeight.w600,
  89. letterSpacing: 1.0,
  90. ),
  91. ),
  92. ),
  93. const Spacer(),
  94. // Pause Button
  95. IconButton(
  96. onPressed: _togglePause,
  97. icon: Icon(
  98. _isPaused ? Icons.play_arrow : Icons.pause,
  99. color: ZenColors.primaryText,
  100. size: 28,
  101. ),
  102. style: IconButton.styleFrom(
  103. backgroundColor: ZenColors.black.withValues(alpha: 0.3),
  104. shape: const CircleBorder(),
  105. ),
  106. ),
  107. ],
  108. ),
  109. ),
  110. ),
  111. // Pause Overlay
  112. if (_isPaused) _buildPauseOverlay(),
  113. ],
  114. ),
  115. );
  116. }
  117. Widget _buildPauseOverlay() {
  118. return Container(
  119. color: ZenColors.black.withValues(alpha: 0.8),
  120. child: Center(
  121. child: Container(
  122. margin: const EdgeInsets.all(40),
  123. padding: const EdgeInsets.all(30),
  124. decoration: BoxDecoration(
  125. color: ZenColors.uiElements,
  126. borderRadius: BorderRadius.circular(20),
  127. ),
  128. child: Column(
  129. mainAxisSize: MainAxisSize.min,
  130. children: [
  131. const Text(
  132. 'Paused',
  133. style: TextStyle(
  134. color: ZenColors.primaryText,
  135. fontSize: 32,
  136. fontWeight: FontWeight.bold,
  137. ),
  138. ),
  139. const SizedBox(height: 20),
  140. Text(
  141. 'Take a moment to breathe',
  142. style: TextStyle(
  143. color: ZenColors.secondaryText,
  144. fontSize: 16,
  145. ),
  146. ),
  147. const SizedBox(height: 30),
  148. // Resume Button
  149. ElevatedButton(
  150. onPressed: _togglePause,
  151. style: ElevatedButton.styleFrom(
  152. backgroundColor: ZenColors.buttonBackground,
  153. foregroundColor: ZenColors.buttonText,
  154. padding: const EdgeInsets.symmetric(
  155. horizontal: 40,
  156. vertical: 15,
  157. ),
  158. shape: RoundedRectangleBorder(
  159. borderRadius: BorderRadius.circular(12),
  160. ),
  161. ),
  162. child: const Text(
  163. 'Resume',
  164. style: TextStyle(
  165. fontSize: 18,
  166. fontWeight: FontWeight.w600,
  167. ),
  168. ),
  169. ),
  170. ],
  171. ),
  172. ),
  173. ),
  174. );
  175. }
  176. void _togglePause() {
  177. setState(() {
  178. _isPaused = !_isPaused;
  179. if (_isPaused) {
  180. game.pauseGame();
  181. } else {
  182. game.resumeGame();
  183. }
  184. });
  185. }
  186. void _showExitDialog() {
  187. showDialog(
  188. context: context,
  189. builder: (BuildContext context) {
  190. return AlertDialog(
  191. backgroundColor: ZenColors.uiElements,
  192. title: const Text(
  193. 'Leave Game?',
  194. style: TextStyle(color: ZenColors.primaryText),
  195. ),
  196. content: Text(
  197. 'Are you sure you want to return to the main menu?',
  198. style: TextStyle(color: ZenColors.secondaryText),
  199. ),
  200. actions: [
  201. TextButton(
  202. onPressed: () => Navigator.of(context).pop(),
  203. child: Text(
  204. 'Cancel',
  205. style: TextStyle(color: ZenColors.links),
  206. ),
  207. ),
  208. ElevatedButton(
  209. onPressed: () {
  210. Navigator.of(context).pop(); // Close dialog
  211. Navigator.of(context).pop(); // Return to main menu
  212. },
  213. style: ElevatedButton.styleFrom(
  214. backgroundColor: ZenColors.red,
  215. foregroundColor: ZenColors.white,
  216. ),
  217. child: const Text('Leave'),
  218. ),
  219. ],
  220. );
  221. },
  222. );
  223. }
  224. }