cicaprojekt/cicaprojekt/Game.java

156 lines
4.5 KiB
Java

package cicaprojekt;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Random;
public class Game {
private Player oneill;
private Player jaffa;
private PlayerBase replicator;
private Dungeon dungeon;
private Random random;
private FlowOfTime flowoftime;
private Display display;
public static Game instance = new Game();
private Game() {
random = new Random();
flowoftime = new FlowOfTime();
}
public void setDungeon(Dungeon dungeon) {
this.dungeon = dungeon;
}
public void setDisplay(Display display) {
this.display = display;
}
public void allZPMsCollected(GameoverCause cause) {
this.stopGame(cause);
}
public void startGame(File dungeonFile) throws IOException {
Stargate.init();
Map<String, Tile> players = dungeon.buildDungeon(dungeonFile, display);
oneill = new Player("O'Neill", players.get("oneill"), getRandomDirection());
jaffa = new Player("Jaffa", players.get("jaffa"), getRandomDirection());
replicator = new PlayerBase("Replicator", players.get("replicator"), getRandomDirection());
display.addVisual(new PlayerDrawer(oneill));
display.addVisual(new PlayerDrawer(jaffa));
display.addVisual(new PlayerBaseDrawer(replicator));
flowoftime.start(dungeon.getTimeLimit());
}
private Direction getRandomDirection() {
return Direction.values()[random.nextInt(Direction.values().length)];
}
public void stopGame(GameoverCause cause) {
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;
}
}
public void moveONeill(Direction direction) {
oneill.move(direction);
checkZPMStatus();
}
public void moveJaffa(Direction direction) {
jaffa.move(direction);
checkZPMStatus();
}
public void rotateOneillLeft() {
oneill.rotateLeft();
}
public void rotateOneillRight() {
oneill.rotateRight();
}
public void rotateJaffaLeft() {
jaffa.rotateLeft();
}
public void rotateJaffaRight() {
jaffa.rotateRight();
}
public void boxONeill() {
if(oneill.hasBox())
oneill.boxDrop();
else
oneill.boxLift();
}
public void boxJaffa() {
if(jaffa.hasBox())
jaffa.boxDrop();
else
jaffa.boxLift();
}
public void shootStargate(Color color) {
if(color == Color.YELLOW || color == Color.BLUE)
oneill.shootStargate(Stargate.get(color));
else
jaffa.shootStargate(Stargate.get(color));
}
public void updateDisplay() {
try {
display.drawVisuals();
display.repaint();
}
catch (IOException e) {
Control.ioErrorMessage();
}
}
private void checkZPMStatus()
{
if (oneill.getZPMCount() >= dungeon.getZPMsToWin())
{
flowoftime.stopTime();
stopGame(GameoverCause.ONEILLWON);
}
else if (jaffa.getZPMCount() >= dungeon.getZPMsToWin())
{
flowoftime.stopTime();
stopGame(GameoverCause.JAFFAWON);
}
}
}