cpd_David_und_Yusuf/lib/screens/gameboard.dart

226 lines
7.1 KiB
Dart
Raw Permalink Normal View History

import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import '../models/player.dart';
import '../models/role.dart';
2024-06-18 01:46:57 +02:00
// Main widget for displaying the player grid
class PlayerGridView extends StatefulWidget {
final List<Player> players;
const PlayerGridView({required this.players, Key? key}) : super(key: key);
@override
2024-06-07 20:08:07 +02:00
// ignore: library_private_types_in_public_api
_PlayerGridViewState createState() => _PlayerGridViewState();
}
class _PlayerGridViewState extends State<PlayerGridView> {
2024-06-18 01:46:57 +02:00
bool isNight = true; // Variable to track whether it is night or day
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
isNight ? Icons.nights_stay : Icons.wb_sunny,
size: 24,
color: isNight ? Colors.grey[300] : Colors.yellow,
),
2024-06-07 20:08:07 +02:00
const SizedBox(width: 10),
Text(
2024-06-18 01:46:57 +02:00
isNight ? 'Nacht' : 'Tag', // Display whether it is night or day
),
],
),
centerTitle: true,
actions: [
IconButton(
2024-06-07 20:08:07 +02:00
icon: const Icon(Icons.info),
onPressed: () {
2024-06-18 01:46:57 +02:00
_showRolesDialog(); // Show dialog with player roles
},
),
],
leading: IconButton(
icon: const Icon(FontAwesomeIcons.xmark),
onPressed: () {
2024-06-18 01:46:57 +02:00
Navigator.popUntil(context, ModalRoute.withName('/')); // Return to main screen
},
),
),
body: Container(
color: isNight
? const Color(0xff2d2d2d)
2024-06-18 01:46:57 +02:00
: const Color.fromARGB(255, 194, 216, 225), // Set background color based on night/day
padding: const EdgeInsets.only(left: 15, right: 15),
child: Column(
children: [
Expanded(
child: GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount:
2024-06-18 01:46:57 +02:00
MediaQuery.of(context).size.shortestSide < 600 ? 2 : 4, // Adjust grid based on screen size
mainAxisSpacing: 10,
crossAxisSpacing: 10,
),
itemCount: widget.players.length,
itemBuilder: (context, index) {
widget.players[index];
return GestureDetector(
onTap: () {
setState(() {
if (!widget.players[index].isDead) {
2024-06-18 01:46:57 +02:00
_killPlayer(widget.players[index]); // Mark player as dead
}
});
},
child: Card(
color: widget.players[index].isDead
? Colors.grey
2024-06-18 01:46:57 +02:00
: Theme.of(context).colorScheme.primary, // Change card color based on player status
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
widget.players[index].name,
2024-06-07 20:08:07 +02:00
style: const TextStyle(fontSize: 24),
textAlign: TextAlign.center,
maxLines: 3,
overflow: TextOverflow.ellipsis,
),
if (widget.players[index].isDead)
const Icon(Icons.close,
2024-06-18 01:46:57 +02:00
color: Colors.red, size: 48), // Show icon if player is dead
],
),
),
);
},
),
),
2024-06-07 20:08:07 +02:00
const Divider(
height: 1,
color: Colors.grey,
),
Padding(
padding: const EdgeInsets.only(bottom: 60, top: 10),
child: ElevatedButton(
2024-06-18 01:46:57 +02:00
onPressed: _changePhase, // Change phase between night and day
child: Text(isNight ? 'Tag skippen' : 'Nacht skippen'),
),
),
],
),
),
);
}
void _killPlayer(Player player) {
if (isNight) {
if (player.role != Role.werwolf) {
player.isDead = true;
2024-06-18 01:46:57 +02:00
_checkWinCondition(); // Check win condition after killing player
isNight = false;
}
} else {
player.isDead = true;
2024-06-18 01:46:57 +02:00
_checkWinCondition(); // Check win condition after killing player
if (player.role == Role.joker) {
2024-06-18 01:46:57 +02:00
_showWinDialog('Der Joker hat gewonnen!'); // Show win dialog for Joker
}
isNight = true;
}
setState(() {});
}
void _changePhase() {
2024-06-18 01:46:57 +02:00
isNight = !isNight; // Toggle between night and day
setState(() {});
}
void _checkWinCondition() {
int countWolves = widget.players
.where((player) => !player.isDead && player.role == Role.werwolf)
.length;
int countVillagers = widget.players
.where((player) => !player.isDead && player.role != Role.werwolf)
.length;
if (countWolves == 0) {
2024-06-18 01:46:57 +02:00
_showWinDialog('Die Dorfbewohner haben gewonnen!'); // Show win dialog for villagers
} else if (countWolves >= countVillagers) {
2024-06-18 01:46:57 +02:00
_showWinDialog('Die Werwölfe haben gewonnen!'); // Show win dialog for werewolves
}
}
void _showWinDialog(String message) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
2024-06-07 20:08:07 +02:00
title: const Text('Spielende'),
content: Text(message),
actions: <Widget>[
TextButton(
2024-06-07 20:08:07 +02:00
child: const Text('Spiel beenden'),
onPressed: () {
2024-06-18 01:46:57 +02:00
Navigator.popUntil(context, ModalRoute.withName('/')); // Return to main screen
},
),
],
);
},
);
}
void _showRolesDialog() {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
2024-06-07 20:08:07 +02:00
title: const Text('Spielerrollen'),
content: SizedBox(
width: double.maxFinite,
child: ListView.builder(
shrinkWrap: true,
itemCount: widget.players.length,
itemBuilder: (context, index) {
final player = widget.players[index];
return ListTile(
title: Text(
player.name,
style: Theme.of(context).textTheme.labelLarge,
),
subtitle: Text(
player.role.name,
style: TextStyle(
color: Theme.of(context).colorScheme.primary,
),
),
);
},
),
),
actions: <Widget>[
TextButton(
2024-06-07 20:08:07 +02:00
child: const Text('OK'),
onPressed: () {
2024-06-18 01:46:57 +02:00
Navigator.of(context).pop(); // Close dialog
},
),
],
);
},
);
}
}