From 3cd196df78edcfb97da92d4249d066e0ff7867a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bokros=20B=C3=A1lint?= Date: Fri, 13 May 2016 13:56:16 +0200 Subject: [PATCH] Refactored Game following the footprints of Socc such Singleton, very software techniques, much patterns --- cicaprojekt/ApplicationFrame.java | 7 ------- cicaprojekt/Control.java | 32 +++++++++++++++---------------- cicaprojekt/FlowOfTime.java | 4 +--- cicaprojekt/Game.java | 4 +++- cicaprojekt/PlayerBase.java | 1 - 5 files changed, 19 insertions(+), 29 deletions(-) diff --git a/cicaprojekt/ApplicationFrame.java b/cicaprojekt/ApplicationFrame.java index fd25f86..d997c82 100644 --- a/cicaprojekt/ApplicationFrame.java +++ b/cicaprojekt/ApplicationFrame.java @@ -9,8 +9,6 @@ import java.io.File; public class ApplicationFrame implements Runnable { - private Game game; - private JFrame jframe; private JPanel mapselectorpanel; @@ -18,11 +16,6 @@ public class ApplicationFrame implements Runnable private JList filelist; - - public ApplicationFrame(Game game){ - this.game = game; - } - private class ListMouseHandler extends MouseAdapter{ @Override public void mouseClicked(MouseEvent e) { diff --git a/cicaprojekt/Control.java b/cicaprojekt/Control.java index f97e35c..069cb0d 100644 --- a/cicaprojekt/Control.java +++ b/cicaprojekt/Control.java @@ -5,8 +5,6 @@ import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; public class Control { - private Game game; - public class KeyHandler extends KeyAdapter{ @Override public void keyTyped(KeyEvent e) { @@ -14,49 +12,49 @@ public class Control { c = Character.toUpperCase(c); switch(c) { case 'W' : - game.moveONeill(Direction.NORTH); + Game.instance.moveONeill(Direction.NORTH); break; case 'A' : - game.moveONeill(Direction.WEST); + Game.instance.moveONeill(Direction.WEST); break; case 'S' : - game.moveONeill(Direction.SOUTH); + Game.instance.moveONeill(Direction.SOUTH); break; case 'D' : - game.moveONeill(Direction.EAST); + Game.instance.moveONeill(Direction.EAST); break; case 'I' : - game.moveJaffa(Direction.NORTH); + Game.instance.moveJaffa(Direction.NORTH); break; case 'J' : - game.moveJaffa(Direction.WEST); + Game.instance.moveJaffa(Direction.WEST); break; case 'K' : - game.moveJaffa(Direction.SOUTH); + Game.instance.moveJaffa(Direction.SOUTH); break; case 'L' : - game.moveJaffa(Direction.EAST); + Game.instance.moveJaffa(Direction.EAST); break; case 'Q' : - game.boxONeill(); + Game.instance.boxONeill(); break; case 'U' : - game.boxJaffa(); + Game.instance.boxJaffa(); break; case 'E' : - game.shootStargate(Color.YELLOW); + Game.instance.shootStargate(Color.YELLOW); break; case 'R' : - game.shootStargate(Color.BLUE); + Game.instance.shootStargate(Color.BLUE); break; case 'O' : - game.shootStargate(Color.RED); + Game.instance.shootStargate(Color.RED); break; case 'P' : - game.shootStargate(Color.GREEN); + Game.instance.shootStargate(Color.GREEN); break; } - game.updateDisplay(); + Game.instance.updateDisplay(); } } diff --git a/cicaprojekt/FlowOfTime.java b/cicaprojekt/FlowOfTime.java index aadbcaa..79427e7 100644 --- a/cicaprojekt/FlowOfTime.java +++ b/cicaprojekt/FlowOfTime.java @@ -6,14 +6,12 @@ import java.util.TimerTask; public class FlowOfTime extends Timer { private TimerTask timeup; private long gametime; - private Game game; - private class GameOver extends TimerTask { @Override public void run() { - game.stopGame(GameoverCause.TIMEOUT); + Game.instance.stopGame(GameoverCause.TIMEOUT); } } diff --git a/cicaprojekt/Game.java b/cicaprojekt/Game.java index 2f82798..e837ff0 100644 --- a/cicaprojekt/Game.java +++ b/cicaprojekt/Game.java @@ -17,8 +17,10 @@ public class Game { private FlowOfTime flowoftime; private Display display; + + public static Game instance = new Game(); - public Game() { + private Game() { dungeon = new Dungeon(); random = new Random(); flowoftime = new FlowOfTime(); diff --git a/cicaprojekt/PlayerBase.java b/cicaprojekt/PlayerBase.java index 7e25fc6..0c9ea9e 100644 --- a/cicaprojekt/PlayerBase.java +++ b/cicaprojekt/PlayerBase.java @@ -1,7 +1,6 @@ package cicaprojekt; public class PlayerBase implements Destroyable { - protected Game game; protected Tile currentTile; protected Direction facingDirection; protected String name;