2016-03-27 22:27:24 +00:00
|
|
|
package cicaprojekt;
|
|
|
|
|
2016-05-13 16:23:09 +00:00
|
|
|
import javax.swing.*;
|
2016-05-07 15:41:25 +00:00
|
|
|
import java.awt.*;
|
2016-05-07 12:04:42 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2016-03-27 22:27:24 +00:00
|
|
|
public class Game {
|
2016-05-14 12:13:27 +00:00
|
|
|
private Player oneill = null;
|
|
|
|
private Player jaffa = null;
|
2016-05-07 12:04:42 +00:00
|
|
|
|
2016-05-14 12:13:27 +00:00
|
|
|
private PlayerBase replicator = null;
|
2016-04-23 15:15:19 +00:00
|
|
|
|
2016-04-24 21:01:34 +00:00
|
|
|
private Dungeon dungeon;
|
|
|
|
private FlowOfTime flowoftime;
|
2016-04-23 15:15:19 +00:00
|
|
|
|
2016-05-07 12:24:23 +00:00
|
|
|
private Display display;
|
2016-05-13 11:56:16 +00:00
|
|
|
|
|
|
|
public static Game instance = new Game();
|
2016-05-07 12:24:23 +00:00
|
|
|
|
2016-05-13 20:16:54 +00:00
|
|
|
private Game() {}
|
2016-05-13 12:06:02 +00:00
|
|
|
|
2016-05-13 20:53:19 +00:00
|
|
|
public void playerBaseDestroyed(PlayerBase caller) {
|
2016-05-14 14:16:59 +00:00
|
|
|
if (caller.getCurrentTile().isGap()) {
|
|
|
|
Field field = new Field();
|
|
|
|
Tile callertile = caller.getCurrentTile();
|
2016-05-13 20:53:19 +00:00
|
|
|
|
2016-05-14 14:16:59 +00:00
|
|
|
field.setX(callertile.getX());
|
|
|
|
field.setY(callertile.getY());
|
2016-05-13 21:17:42 +00:00
|
|
|
|
2016-05-14 14:16:59 +00:00
|
|
|
field.setAdajacentTile(caller.getCurrentTile().getAdjacentTile(Direction.NORTH), Direction.NORTH);
|
|
|
|
callertile.getAdjacentTile(Direction.NORTH).setAdajacentTile(field, Direction.SOUTH);
|
2016-05-13 20:53:19 +00:00
|
|
|
|
2016-05-14 14:16:59 +00:00
|
|
|
field.setAdajacentTile(caller.getCurrentTile().getAdjacentTile(Direction.SOUTH), Direction.SOUTH);
|
|
|
|
callertile.getAdjacentTile(Direction.SOUTH).setAdajacentTile(field, Direction.NORTH);
|
2016-05-13 20:53:19 +00:00
|
|
|
|
2016-05-14 14:16:59 +00:00
|
|
|
field.setAdajacentTile(caller.getCurrentTile().getAdjacentTile(Direction.WEST), Direction.WEST);
|
|
|
|
callertile.getAdjacentTile(Direction.WEST).setAdajacentTile(field, Direction.EAST);
|
2016-05-13 20:53:19 +00:00
|
|
|
|
2016-05-14 14:16:59 +00:00
|
|
|
field.setAdajacentTile(caller.getCurrentTile().getAdjacentTile(Direction.EAST), Direction.EAST);
|
|
|
|
callertile.getAdjacentTile(Direction.EAST).setAdajacentTile(field, Direction.WEST);
|
2016-05-13 20:53:19 +00:00
|
|
|
|
2016-05-14 14:16:59 +00:00
|
|
|
display.gapMagic((Gap) callertile, field, dungeon.getMapWidth());
|
2016-05-13 21:17:42 +00:00
|
|
|
|
2016-05-14 14:16:59 +00:00
|
|
|
Game.instance.updateDisplay();
|
|
|
|
}
|
2016-05-13 20:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void playerDestroyed(Player caller) {
|
|
|
|
dungeon.setZPMsToWin(dungeon.getZPMsToWin() - caller.getZPMCount());
|
|
|
|
Game.instance.updateDisplay();
|
2016-05-14 22:02:29 +00:00
|
|
|
if (jaffa.isDestroyed() && oneill.isDestroyed()) {
|
|
|
|
flowoftime.stopTime();
|
|
|
|
stopGame(GameoverCause.ANUBIS);
|
|
|
|
}
|
2016-05-13 20:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-05-13 12:06:02 +00:00
|
|
|
public void setDungeon(Dungeon dungeon) {
|
|
|
|
this.dungeon = dungeon;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setDisplay(Display display) {
|
|
|
|
this.display = display;
|
2016-05-07 12:04:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void startGame(File dungeonFile) throws IOException {
|
|
|
|
Stargate.init();
|
|
|
|
|
2016-05-07 17:15:19 +00:00
|
|
|
Map<String, Tile> players = dungeon.buildDungeon(dungeonFile, display);
|
2016-05-13 20:00:06 +00:00
|
|
|
oneill = new Player("O'Neill", players.get("oneill"), Direction.getRandom());
|
|
|
|
jaffa = new Player("Jaffa", players.get("jaffa"), Direction.getRandom());
|
|
|
|
replicator = new PlayerBase("Replicator", players.get("replicator"), Direction.getRandom());
|
2016-05-14 00:51:10 +00:00
|
|
|
|
|
|
|
display.setMapSize(dungeon.getMapWidth(), dungeon.getMapHeight());
|
2016-05-13 12:34:05 +00:00
|
|
|
|
|
|
|
display.addVisual(new PlayerDrawer(oneill));
|
|
|
|
display.addVisual(new PlayerDrawer(jaffa));
|
2016-05-13 19:05:34 +00:00
|
|
|
display.addVisual(new PlayerBaseDrawer(replicator));
|
|
|
|
|
2016-05-13 22:14:49 +00:00
|
|
|
display.startMusic();
|
|
|
|
|
2016-05-13 19:05:34 +00:00
|
|
|
flowoftime = new FlowOfTime();
|
2016-05-13 18:29:03 +00:00
|
|
|
flowoftime.start(dungeon.getTimeLimit());
|
2016-05-07 12:04:42 +00:00
|
|
|
}
|
|
|
|
|
2016-05-07 11:43:26 +00:00
|
|
|
public void stopGame(GameoverCause cause) {
|
2016-05-13 18:04:20 +00:00
|
|
|
switch (cause){
|
|
|
|
|
|
|
|
case TIMEOUT:
|
|
|
|
JOptionPane.showMessageDialog(null, "Time is up! Anubis has enslaved the world by now.",
|
|
|
|
"Game over",
|
|
|
|
JOptionPane.INFORMATION_MESSAGE,
|
|
|
|
new ImageIcon("Anubis.png"));
|
|
|
|
Application.frameInstance.backToMapSelection();
|
|
|
|
break;
|
|
|
|
case ONEILLWON:
|
|
|
|
JOptionPane.showMessageDialog(null, "Colonel O'Neill won!", "Game over",
|
|
|
|
JOptionPane.INFORMATION_MESSAGE,
|
|
|
|
new ImageIcon("Deal_with_iit.png"));
|
|
|
|
Application.frameInstance.backToMapSelection();
|
|
|
|
break;
|
|
|
|
case JAFFAWON:
|
|
|
|
JOptionPane.showMessageDialog(null, "Jaffa won!", "Game over",
|
|
|
|
JOptionPane.INFORMATION_MESSAGE,
|
|
|
|
new ImageIcon("Deal_with_iit.png"));
|
|
|
|
Application.frameInstance.backToMapSelection();
|
|
|
|
break;
|
2016-05-14 19:32:08 +00:00
|
|
|
case DRAW:
|
|
|
|
JOptionPane.showMessageDialog(null, "Draw! While O'Neill and the Jaffa struggled against eachother, Anubis has enslaved the world.",
|
|
|
|
"Game over",
|
|
|
|
JOptionPane.INFORMATION_MESSAGE,
|
|
|
|
new ImageIcon("Anubis.png"));
|
2016-05-14 22:02:29 +00:00
|
|
|
Application.frameInstance.backToMapSelection();
|
|
|
|
break;
|
|
|
|
case ANUBIS:
|
|
|
|
JOptionPane.showMessageDialog(null, "There is no one left to stop Anubis from enslaving the world",
|
|
|
|
"Game over",
|
|
|
|
JOptionPane.INFORMATION_MESSAGE,
|
|
|
|
new ImageIcon("Anubis.png"));
|
|
|
|
Application.frameInstance.backToMapSelection();
|
|
|
|
break;
|
2016-05-13 18:04:20 +00:00
|
|
|
}
|
2016-05-13 22:14:49 +00:00
|
|
|
|
|
|
|
display.stopMusic();
|
2016-04-24 21:01:34 +00:00
|
|
|
}
|
2016-05-07 13:19:58 +00:00
|
|
|
|
|
|
|
public void moveONeill(Direction direction) {
|
|
|
|
oneill.move(direction);
|
2016-05-13 18:54:44 +00:00
|
|
|
checkZPMStatus();
|
2016-05-07 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void moveJaffa(Direction direction) {
|
|
|
|
jaffa.move(direction);
|
2016-05-13 18:54:44 +00:00
|
|
|
checkZPMStatus();
|
2016-05-07 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
2016-05-13 20:00:06 +00:00
|
|
|
public void moveReplicator(Direction direction) {
|
|
|
|
replicator.move(direction);
|
|
|
|
}
|
|
|
|
|
2016-05-13 14:08:53 +00:00
|
|
|
public void rotateOneillLeft() {
|
|
|
|
oneill.rotateLeft();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void rotateOneillRight() {
|
|
|
|
oneill.rotateRight();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void rotateJaffaLeft() {
|
|
|
|
jaffa.rotateLeft();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void rotateJaffaRight() {
|
|
|
|
jaffa.rotateRight();
|
|
|
|
}
|
|
|
|
|
2016-05-07 13:19:58 +00:00
|
|
|
public void boxONeill() {
|
2016-05-07 14:22:59 +00:00
|
|
|
if(oneill.hasBox())
|
|
|
|
oneill.boxDrop();
|
|
|
|
else
|
|
|
|
oneill.boxLift();
|
2016-05-07 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void boxJaffa() {
|
2016-05-07 14:22:59 +00:00
|
|
|
if(jaffa.hasBox())
|
|
|
|
jaffa.boxDrop();
|
|
|
|
else
|
|
|
|
jaffa.boxLift();
|
2016-05-07 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
2016-05-07 15:41:25 +00:00
|
|
|
public void shootStargate(Color color) {
|
|
|
|
if(color == Color.YELLOW || color == Color.BLUE)
|
|
|
|
oneill.shootStargate(Stargate.get(color));
|
|
|
|
else
|
|
|
|
jaffa.shootStargate(Stargate.get(color));
|
|
|
|
}
|
|
|
|
|
2016-05-07 13:19:58 +00:00
|
|
|
public void updateDisplay() {
|
2016-05-13 16:23:09 +00:00
|
|
|
try {
|
|
|
|
display.drawVisuals();
|
|
|
|
display.repaint();
|
|
|
|
}
|
|
|
|
catch (IOException e) {
|
|
|
|
Control.ioErrorMessage();
|
|
|
|
}
|
2016-05-07 13:19:58 +00:00
|
|
|
}
|
2016-05-13 18:54:44 +00:00
|
|
|
|
2016-05-14 22:02:29 +00:00
|
|
|
private void checkZPMStatus() {
|
2016-05-14 19:32:08 +00:00
|
|
|
if (oneill.getZPMCount() + jaffa.getZPMCount() >= dungeon.getZPMsToWin()) {
|
2016-05-14 22:02:29 +00:00
|
|
|
if (oneill.getZPMCount() > jaffa.getZPMCount()) {
|
|
|
|
flowoftime.stopTime();
|
2016-05-14 19:32:08 +00:00
|
|
|
stopGame(GameoverCause.ONEILLWON);
|
2016-05-14 22:02:29 +00:00
|
|
|
}
|
|
|
|
else if (oneill.getZPMCount() < jaffa.getZPMCount()) {
|
|
|
|
flowoftime.stopTime();
|
2016-05-14 19:32:08 +00:00
|
|
|
stopGame(GameoverCause.JAFFAWON);
|
2016-05-14 22:02:29 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
flowoftime.stopTime();
|
2016-05-14 19:32:08 +00:00
|
|
|
stopGame(GameoverCause.DRAW);
|
2016-05-14 22:02:29 +00:00
|
|
|
}
|
2016-05-14 20:46:04 +00:00
|
|
|
return;
|
2016-05-14 19:32:08 +00:00
|
|
|
}
|
|
|
|
|
2016-05-13 18:54:44 +00:00
|
|
|
if (oneill.getZPMCount() >= dungeon.getZPMsToWin())
|
|
|
|
{
|
|
|
|
flowoftime.stopTime();
|
|
|
|
stopGame(GameoverCause.ONEILLWON);
|
|
|
|
}
|
|
|
|
else if (jaffa.getZPMCount() >= dungeon.getZPMsToWin())
|
|
|
|
{
|
|
|
|
flowoftime.stopTime();
|
|
|
|
stopGame(GameoverCause.JAFFAWON);
|
|
|
|
}
|
|
|
|
}
|
2016-05-14 20:37:43 +00:00
|
|
|
|
|
|
|
public void generateZPM() {
|
|
|
|
if(((jaffa.getZPMCount() + oneill.getZPMCount()) % 2) == 0) {
|
|
|
|
Tile source = oneill.getCurrentTile();
|
|
|
|
Tile compare;
|
2016-05-15 11:11:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < dungeon.getMapHeight(); i++) {
|
2016-05-14 20:37:43 +00:00
|
|
|
compare = source.getAdjacentTile(Direction.getRandom());
|
|
|
|
if (compare != null)
|
|
|
|
source = compare;
|
|
|
|
}
|
|
|
|
|
2016-05-14 21:18:29 +00:00
|
|
|
while (!source.canHazZPM()) {
|
2016-05-15 20:59:06 +00:00
|
|
|
int newSourceCounter = 0;
|
2016-05-15 11:11:18 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < dungeon.getMapHeight(); i++) {
|
|
|
|
compare = source.getAdjacentTile(Direction.getRandom());
|
|
|
|
if (compare != null)
|
2016-05-14 21:18:29 +00:00
|
|
|
source = compare;
|
2016-05-15 11:11:18 +00:00
|
|
|
}
|
|
|
|
|
2016-05-15 20:59:06 +00:00
|
|
|
while(newSourceCounter != 20) {
|
2016-05-15 11:11:18 +00:00
|
|
|
compare = source.getAdjacentTile(Direction.getRandom());
|
2016-05-15 20:59:06 +00:00
|
|
|
if (compare != null) {
|
|
|
|
source = compare;
|
|
|
|
if (!compare.hasZPM() && (compare.playerBaseOnTile == null)) {
|
|
|
|
if (source.canHazZPM()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
newSourceCounter++;
|
2016-05-15 11:11:18 +00:00
|
|
|
}
|
2016-05-14 20:37:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
source.setZPMOnTile(new ZPM());
|
2016-05-14 21:26:30 +00:00
|
|
|
dungeon.setZPMsToWin(dungeon.getZPMsToWin()+1);
|
2016-05-14 20:37:43 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-27 22:27:24 +00:00
|
|
|
}
|