122 lines
2.5 KiB
Dart
122 lines
2.5 KiB
Dart
import 'dart:async';
|
|
|
|
import '../enums/StoneColor.dart';
|
|
import 'Board.dart';
|
|
import 'IGameConsumer.dart';
|
|
import 'stone/Stone.dart';
|
|
import 'stone/StoneLocation.dart';
|
|
|
|
class Game {
|
|
IGameConsumer gameConsumer;
|
|
late int timeInSeconds;
|
|
int startTime = 60; // hardcoded
|
|
int points = 0;
|
|
bool running = false;
|
|
late Board board;
|
|
late Timer counterTimer;
|
|
StoneLocation stoneToSwap = StoneLocation(row: -1, column: -1);
|
|
|
|
Game(this.gameConsumer) {
|
|
timeInSeconds = startTime;
|
|
board = Board(this);
|
|
}
|
|
|
|
int getPoints() => points;
|
|
|
|
int getTimeInSeconds() => timeInSeconds;
|
|
|
|
void addPoints(int pointsToAdd) {
|
|
if (timeInSeconds != 0 && running) {
|
|
points += pointsToAdd;
|
|
gameConsumer.updatePoints();
|
|
}
|
|
}
|
|
|
|
void addTime(int timeToAdd) {
|
|
if (timeInSeconds != 0 && running) {
|
|
timeInSeconds += timeToAdd;
|
|
gameConsumer.updateTime();
|
|
}
|
|
}
|
|
|
|
void endGame() {
|
|
running = false;
|
|
counterTimer?.cancel();
|
|
gameConsumer.gameStopped();
|
|
}
|
|
|
|
Future<void> countDown() async {
|
|
do {
|
|
timeInSeconds--;
|
|
gameConsumer.updateTime();
|
|
if (timeInSeconds > 0) {
|
|
await Future.delayed(Duration(seconds: 1));
|
|
}
|
|
} while (timeInSeconds > 0);
|
|
if (running) {
|
|
endGame();
|
|
}
|
|
}
|
|
|
|
StoneColors? getStoneColor(StoneLocation location) {
|
|
return board.getStone(location)?.getStoneColor();
|
|
}
|
|
|
|
Stone? getStone(StoneLocation location) {
|
|
return board.getStone(location);
|
|
}
|
|
|
|
void start() {
|
|
if (!running) {
|
|
timeInSeconds = startTime;
|
|
points = 0;
|
|
board = Board(this);
|
|
gameConsumer.updateStones();
|
|
counterTimer = Timer.periodic(Duration(seconds: 1), (_) => countDown());
|
|
gameConsumer.updatePoints();
|
|
running = true;
|
|
}
|
|
}
|
|
|
|
void stop() {
|
|
if (running) {
|
|
running = false;
|
|
timeInSeconds = 0;
|
|
counterTimer.cancel();
|
|
}
|
|
}
|
|
|
|
bool swapStones(StoneLocation sl1, StoneLocation sl2) {
|
|
return board.swapStones(sl1, sl2);
|
|
}
|
|
|
|
void updateBoard() {
|
|
if (running) {
|
|
gameConsumer.updateStones();
|
|
}
|
|
}
|
|
|
|
bool isRunning() => running;
|
|
|
|
bool swap(StoneLocation sl) {
|
|
if (stoneToSwap.row == -1) {
|
|
stoneToSwap = sl;
|
|
return false;
|
|
} else {
|
|
swapStones(stoneToSwap, sl);
|
|
stoneToSwap = StoneLocation(row: -1, column: -1);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
bool performSpecialStone(StoneLocation sl) {
|
|
Stone? s = getStone(sl);
|
|
/*if (s is TriggerableSpecialStone) {
|
|
s.setActivationLocation(sl);
|
|
board.performSpecialStone(s, sl);
|
|
return true;
|
|
}*/
|
|
return false;
|
|
}
|
|
}
|