fixed bug, added some minor things and added setstate, deleted game.dart

main
Oeyuu 2024-05-10 23:20:18 +02:00
parent c8c6c9bff3
commit 5a4e32992e
3 changed files with 43 additions and 75 deletions

View File

@ -1,48 +0,0 @@
import 'package:werwolf/models/role.dart';
import 'player.dart';
class Game {
List<Player> players = [];
List playernames = [];
int numWolves = 1;
Game({required this.playernames, required this.numWolves}) {
assignRoles();
}
void addPlayer(String name, Role role) {
players.add(Player(name: name, role: role));
}
List<Player> getPlayers() {
return players;
}
void assignRoles() {
List<int> indexes = List.generate(playernames.length, (index) => index)
..shuffle();
for (var player in playernames) {
//addPlayer(player, Role.dorfbewohner);
for (int i = 0; i < numWolves; i++) {
addPlayer(player[0], Role.dorfbewohner);
}
}
}
void incrementWolves() {
if (numWolves < players.length) {
numWolves++;
}
}
void decrementWolves() {
if (numWolves > 1) {
numWolves--;
}
}
// void toggleReveal(int index) {
// players[index].isRevealed = !players[index].isRevealed;
// }
}

View File

@ -1,7 +1,7 @@
import 'package:flip_card/flip_card_controller.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flip_card/flip_card.dart'; import 'package:flip_card/flip_card.dart';
import '../models/game.dart';
import '../models/player.dart'; import '../models/player.dart';
import '../models/role.dart'; import '../models/role.dart';
@ -16,20 +16,12 @@ class FlipingCard extends StatefulWidget {
class _FlipingCardState extends State<FlipingCard> { class _FlipingCardState extends State<FlipingCard> {
int index = 0; int index = 0;
String giveRole(Role role) { late FlipCardController _controller;
String playerrole = "Dorfbewohner";
switch (role) { @override
case Role.werwolf: void initState() {
playerrole = "Werwolf"; super.initState();
break; _controller = FlipCardController();
case Role.joker:
playerrole = "Joker";
break;
default:
playerrole = "Dorfbewohner";
break;
}
return playerrole;
} }
_renderContent(context) { _renderContent(context) {
@ -39,9 +31,10 @@ class _FlipingCardState extends State<FlipingCard> {
left: 15.0, right: 15.0, top: 10.0, bottom: 10.0), left: 15.0, right: 15.0, top: 10.0, bottom: 10.0),
color: const Color(0x00000000), color: const Color(0x00000000),
child: FlipCard( child: FlipCard(
controller: _controller,
direction: FlipDirection.HORIZONTAL, direction: FlipDirection.HORIZONTAL,
side: CardSide.FRONT, side: CardSide.FRONT,
speed: 400, speed: 300,
onFlipDone: (status) {}, onFlipDone: (status) {},
front: Container( front: Container(
decoration: const BoxDecoration( decoration: const BoxDecoration(
@ -71,9 +64,11 @@ class _FlipingCardState extends State<FlipingCard> {
), ),
), ),
back: Container( back: Container(
decoration: const BoxDecoration( decoration: BoxDecoration(
color: Color(0xFF006666), color: widget.players[index].role != Role.werwolf
borderRadius: BorderRadius.all(Radius.circular(8.0)), ? const Color(0xFF006666)
: Color.fromARGB(255, 100, 21, 15),
borderRadius: const BorderRadius.all(Radius.circular(8.0)),
), ),
child: Column( child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch, crossAxisAlignment: CrossAxisAlignment.stretch,
@ -82,7 +77,7 @@ class _FlipingCardState extends State<FlipingCard> {
Container( Container(
padding: const EdgeInsets.all(10), padding: const EdgeInsets.all(10),
child: Text( child: Text(
giveRole(widget.players[index].role), widget.players[index].role.stringValue,
textAlign: TextAlign.center, textAlign: TextAlign.center,
style: Theme.of(context) style: Theme.of(context)
.textTheme .textTheme
@ -123,9 +118,14 @@ class _FlipingCardState extends State<FlipingCard> {
), ),
), ),
onPressed: () { onPressed: () {
if (index > 0 && index <= widget.players.length) { setState(() {
index--; if (index > 0 && index <= widget.players.length) {
} index--;
if (!_controller.state!.isFront) {
_controller.toggleCardWithoutAnimation();
}
}
});
}, },
child: const Text("Zurück"), child: const Text("Zurück"),
), ),
@ -137,9 +137,14 @@ class _FlipingCardState extends State<FlipingCard> {
), ),
), ),
onPressed: () { onPressed: () {
if (index >= 0 && index <= widget.players.length) { setState(() {
index++; if (index >= 0 && index < widget.players.length - 1) {
} index++;
if (!_controller.state!.isFront) {
_controller.toggleCardWithoutAnimation();
}
}
});
}, },
child: const Text("Nächster Spieler"), child: const Text("Nächster Spieler"),
), ),

View File

@ -1,4 +1,5 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:werwolf/screens/flippingcards.dart';
import '../models/games.dart'; import '../models/games.dart';
import '../models/role.dart'; import '../models/role.dart';
@ -13,7 +14,6 @@ class GameSettings extends StatefulWidget {
class _GameSettingsState extends State<GameSettings> { class _GameSettingsState extends State<GameSettings> {
late Game game; late Game game;
bool joker = false;
@override @override
void initState() { void initState() {
game = Game(playernames: widget.playernames); game = Game(playernames: widget.playernames);
@ -101,6 +101,17 @@ class _GameSettingsState extends State<GameSettings> {
}, },
), ),
), ),
ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
FlipingCard(players: game.getAllPlayers()),
));
},
child: const Text('Spiel starten!'),
),
], ],
), ),
), ),