diff --git a/src/de/teamteamteam/spacescooter/Main.java b/src/de/teamteamteam/spacescooter/Main.java index d7ed32c..e34f7a3 100644 --- a/src/de/teamteamteam/spacescooter/Main.java +++ b/src/de/teamteamteam/spacescooter/Main.java @@ -2,12 +2,11 @@ package de.teamteamteam.spacescooter; import java.awt.EventQueue; -import de.teamteamteam.spacescooter.background.StarBackground; -import de.teamteamteam.spacescooter.entity.Player; -import de.teamteamteam.spacescooter.entity.TestEntity; import de.teamteamteam.spacescooter.gui.GameFrame; +import de.teamteamteam.spacescooter.screen.SuperScreen; import de.teamteamteam.spacescooter.thread.PaintThread; import de.teamteamteam.spacescooter.thread.UpdateThread; +import de.teamteamteam.spacescooter.utility.GameConfig; import de.teamteamteam.spacescooter.utility.GraphicsSettings; /** @@ -24,28 +23,31 @@ public class Main { public static void main(String[] args) { GraphicsSettings gs = new GraphicsSettings(); //Get settings - final GameFrame gf = new GameFrame(); + GameConfig.windowWidth = 800; + GameConfig.windowHeight = 600; - //Whatever. - new StarBackground(); - new Player(); - new TestEntity(); + final GameFrame gameFrame = new GameFrame(); + + //Initialize SuperScreen and add to GameFrame, so we can call doPaint() on it. + SuperScreen superScreen = new SuperScreen(null); + gameFrame.setSuperScreen(superScreen); //Initialize the GameFrame properly within the AWT EventQueue EventQueue.invokeLater(new Runnable() { public void run() { - gf.init(); - gf.draw(); //Draw nothing for the first time. + gameFrame.init(); + gameFrame.draw(); //Draw nothing for the first time. } }); //Initialize GameThread - PaintThread paintThread = new PaintThread(gf); + PaintThread paintThread = new PaintThread(gameFrame); paintThread.setHz(gs.getRefreshRate()); //This may be set depending on the system graphic settings. paintThread.start(); //Initialize UpdateThread UpdateThread updateThread = new UpdateThread(); + updateThread.setSuperScreen(superScreen); updateThread.setHz(100); //This shall remain constant across all systems. updateThread.start(); diff --git a/src/de/teamteamteam/spacescooter/entity/Player.java b/src/de/teamteamteam/spacescooter/entity/Player.java index f6ef257..654b602 100644 --- a/src/de/teamteamteam/spacescooter/entity/Player.java +++ b/src/de/teamteamteam/spacescooter/entity/Player.java @@ -8,6 +8,7 @@ import java.io.IOException; import javax.imageio.ImageIO; import de.teamteamteam.spacescooter.control.Keyboard; +import de.teamteamteam.spacescooter.utility.GameConfig; public class Player extends Entity { @@ -36,13 +37,13 @@ public class Player extends Entity { if(Keyboard.isKeyDown(KeyEvent.VK_UP) && this.y > 0) { this.y -= off; } - if(Keyboard.isKeyDown(KeyEvent.VK_DOWN) && this.y < (600 - Player.img.getHeight())) { + if(Keyboard.isKeyDown(KeyEvent.VK_DOWN) && this.y < (GameConfig.windowHeight - Player.img.getHeight())) { this.y += off; } if(Keyboard.isKeyDown(KeyEvent.VK_LEFT) && this.x > 0) { this.x -= off; } - if(Keyboard.isKeyDown(KeyEvent.VK_RIGHT) && this.x < (800 - Player.img.getWidth())) { + if(Keyboard.isKeyDown(KeyEvent.VK_RIGHT) && this.x < (GameConfig.windowWidth - Player.img.getWidth())) { this.x += off; } diff --git a/src/de/teamteamteam/spacescooter/gui/GameFrame.java b/src/de/teamteamteam/spacescooter/gui/GameFrame.java index c52b15e..c565069 100644 --- a/src/de/teamteamteam/spacescooter/gui/GameFrame.java +++ b/src/de/teamteamteam/spacescooter/gui/GameFrame.java @@ -3,13 +3,11 @@ package de.teamteamteam.spacescooter.gui; import java.awt.Graphics; import java.awt.Toolkit; import java.awt.image.BufferStrategy; -import java.util.Iterator; - import javax.swing.JFrame; -import de.teamteamteam.spacescooter.background.Background; import de.teamteamteam.spacescooter.control.Keyboard; -import de.teamteamteam.spacescooter.entity.Entity; +import de.teamteamteam.spacescooter.screen.Screen; +import de.teamteamteam.spacescooter.utility.GameConfig; /** * The game will take place in this beautiful window. @@ -20,6 +18,11 @@ public class GameFrame extends JFrame { private BufferStrategy bufferStrategy; + private Screen superScreen; + + /** + * Default constructor. + */ public GameFrame() { super(); } @@ -29,7 +32,7 @@ public class GameFrame extends JFrame { */ public void init() { this.setTitle("Unser schöner Titel"); - this.setSize(800, 600); + this.setSize(GameConfig.windowWidth, GameConfig.windowHeight); this.setUndecorated(false); this.setDefaultCloseOperation(EXIT_ON_CLOSE); @@ -47,6 +50,14 @@ public class GameFrame extends JFrame { this.bufferStrategy = this.getBufferStrategy(); } + /** + * Set the superScreen to trigger doPaint() on. + * @param superScreen + */ + public void setSuperScreen(Screen superScreen) { + this.superScreen = superScreen; + } + /** * The pain, do not underestimate it! * @@ -59,13 +70,11 @@ public class GameFrame extends JFrame { try { bufferedGraphics = this.bufferStrategy.getDrawGraphics(); - // Now we can use bufferedGraphics to actually draw stuff - this.drawBackgrounds(bufferedGraphics); - this.drawEntities(bufferedGraphics); + this.superScreen.doPaint(bufferedGraphics); } catch (Exception e) { System.out.println("Hier geht was schief"); - System.out.println(e); + e.printStackTrace(); } finally { // We are done, dispose the pen and celebrate the result! if (bufferedGraphics != null) @@ -77,19 +86,4 @@ public class GameFrame extends JFrame { Toolkit.getDefaultToolkit().sync(); //Tell the OS to update its graphics of the window. } - public void drawBackgrounds(Graphics g) { - Iterator i = Background.backgrounds.iterator(); - while (i.hasNext()) { - Background b = i.next(); - b.paint(g); - } - } - - public void drawEntities(Graphics g) { - Iterator i = Entity.entities.iterator(); - while (i.hasNext()) { - Entity e = i.next(); - e.paint(g); - } - } } diff --git a/src/de/teamteamteam/spacescooter/screen/GameScreen.java b/src/de/teamteamteam/spacescooter/screen/GameScreen.java new file mode 100644 index 0000000..123fc60 --- /dev/null +++ b/src/de/teamteamteam/spacescooter/screen/GameScreen.java @@ -0,0 +1,31 @@ +package de.teamteamteam.spacescooter.screen; + +import java.awt.Color; +import java.awt.Graphics; + +import java.awt.event.KeyEvent; + +import de.teamteamteam.spacescooter.control.Keyboard; +import de.teamteamteam.spacescooter.utility.GameConfig; + +public class GameScreen extends Screen { + + public GameScreen(Screen parent) { + super(parent); + } + + @Override + protected void paint(Graphics g) { + g.fillRect(0, 0, GameConfig.windowWidth, GameConfig.windowHeight); + g.setColor(new Color(255,0,255)); + g.drawRect(100, 200, 150, 75); + } + + @Override + protected void update() { + if(Keyboard.isKeyDown(KeyEvent.VK_ESCAPE)) { + this.parent.setOverlay(new MainMenuScreen(this.parent)); + } + } + +} diff --git a/src/de/teamteamteam/spacescooter/screen/MainMenuScreen.java b/src/de/teamteamteam/spacescooter/screen/MainMenuScreen.java new file mode 100644 index 0000000..9b2d258 --- /dev/null +++ b/src/de/teamteamteam/spacescooter/screen/MainMenuScreen.java @@ -0,0 +1,35 @@ +package de.teamteamteam.spacescooter.screen; + +import java.awt.Graphics; + +import java.awt.event.KeyEvent; + +import de.teamteamteam.spacescooter.background.StarBackground; +import de.teamteamteam.spacescooter.control.Keyboard; +import de.teamteamteam.spacescooter.entity.Entity; + +public class MainMenuScreen extends Screen { + + public MainMenuScreen(Screen parent) { + super(parent); + this.addEntity(new StarBackground()); + } + + @Override + public void paint(Graphics g) { + for(Entity entity : this.entities) { + entity.paint(g); + } + } + + @Override + public void update() { + for(Entity entity : this.entities) { + entity.update(); + } + if(Keyboard.isKeyDown(KeyEvent.VK_SPACE)) { + this.parent.setOverlay(new GameScreen(this.parent)); + } + } + +} diff --git a/src/de/teamteamteam/spacescooter/screen/Screen.java b/src/de/teamteamteam/spacescooter/screen/Screen.java new file mode 100644 index 0000000..68d67de --- /dev/null +++ b/src/de/teamteamteam/spacescooter/screen/Screen.java @@ -0,0 +1,48 @@ +package de.teamteamteam.spacescooter.screen; + +import java.awt.Graphics; +import java.util.ArrayList; + +import de.teamteamteam.spacescooter.entity.Entity; + +public abstract class Screen { + + protected Screen overlay; + protected Screen parent; + + protected ArrayList entities; + + public Screen(Screen parent) { + this.overlay = null; + this.parent = parent; + this.entities = new ArrayList(); + } + + protected void addEntity(Entity e) { + this.entities.add(e); + } + + protected void removeEntity(Entity e) { + this.entities.remove(e); + } + + protected abstract void paint(Graphics g); + protected abstract void update(); + + public void doPaint(Graphics g) { + this.paint(g); + if(this.overlay != null) this.overlay.doPaint(g); + } + + public void doUpdate() { + if(this.overlay != null) { + this.overlay.doUpdate(); + return; + } + this.update(); + } + + public void setOverlay(Screen screen) { + this.overlay = screen; + } +} diff --git a/src/de/teamteamteam/spacescooter/screen/SuperScreen.java b/src/de/teamteamteam/spacescooter/screen/SuperScreen.java new file mode 100644 index 0000000..ecaa40e --- /dev/null +++ b/src/de/teamteamteam/spacescooter/screen/SuperScreen.java @@ -0,0 +1,22 @@ +package de.teamteamteam.spacescooter.screen; + +import java.awt.Graphics; + +public class SuperScreen extends Screen { + + public SuperScreen(Screen parent) { + super(null); + this.overlay = new MainMenuScreen(this); + } + + @Override + public void paint(Graphics g) { + //nothing to paint, we're so meta meta. + } + + @Override + public void update() { + //dummy method + } + +} diff --git a/src/de/teamteamteam/spacescooter/thread/UpdateThread.java b/src/de/teamteamteam/spacescooter/thread/UpdateThread.java index 2e8df3d..cbd492f 100644 --- a/src/de/teamteamteam/spacescooter/thread/UpdateThread.java +++ b/src/de/teamteamteam/spacescooter/thread/UpdateThread.java @@ -1,35 +1,21 @@ package de.teamteamteam.spacescooter.thread; -import java.util.Iterator; - -import de.teamteamteam.spacescooter.background.Background; -import de.teamteamteam.spacescooter.entity.Entity; +import de.teamteamteam.spacescooter.screen.Screen; public class UpdateThread extends TimedThread { + private Screen superScreen; + public UpdateThread() { this.setName("UpdateThread"); } + public void setSuperScreen(Screen superScreen) { + this.superScreen = superScreen; + } + public void work() { - // Update all the entities - this.updateBackgrounds(); - this.updateEntities(); + this.superScreen.doUpdate(); } - private void updateBackgrounds() { - Iterator i = Background.backgrounds.iterator(); - while (i.hasNext()) { - Background b = i.next(); - b.update(); - } - } - - private void updateEntities() { - Iterator i = Entity.entities.iterator(); - while (i.hasNext()) { - Entity e = i.next(); - e.update(); - } - } } diff --git a/src/de/teamteamteam/spacescooter/utility/GameConfig.java b/src/de/teamteamteam/spacescooter/utility/GameConfig.java new file mode 100644 index 0000000..8f3a510 --- /dev/null +++ b/src/de/teamteamteam/spacescooter/utility/GameConfig.java @@ -0,0 +1,8 @@ +package de.teamteamteam.spacescooter.utility; + +public class GameConfig { + + public static int windowHeight; + public static int windowWidth; + +}