122 lines
2.6 KiB
Dart
122 lines
2.6 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:bubbletwist/game/stone/triggerable_special_stone.dart';
|
|
|
|
import '../enums/stone_color.dart';
|
|
import 'board.dart';
|
|
import 'i_game_consumer.dart';
|
|
import 'stone/stone.dart';
|
|
import 'stone/stone_location.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(bool gameIsCanceled) {
|
|
running = false;
|
|
counterTimer.cancel();
|
|
if (!gameIsCanceled) {
|
|
gameConsumer.gameStopped();
|
|
}
|
|
}
|
|
|
|
void countDown() {
|
|
timeInSeconds--;
|
|
gameConsumer.updateTime();
|
|
if (timeInSeconds == 0 && running) {
|
|
endGame(false);
|
|
}
|
|
}
|
|
|
|
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(const 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;
|
|
}
|
|
}
|