Einführung von Screens.
This commit is contained in:
parent
dde94437fc
commit
b118a5a024
|
@ -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();
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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<Background> i = Background.backgrounds.iterator();
|
||||
while (i.hasNext()) {
|
||||
Background b = i.next();
|
||||
b.paint(g);
|
||||
}
|
||||
}
|
||||
|
||||
public void drawEntities(Graphics g) {
|
||||
Iterator<Entity> i = Entity.entities.iterator();
|
||||
while (i.hasNext()) {
|
||||
Entity e = i.next();
|
||||
e.paint(g);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Entity> entities;
|
||||
|
||||
public Screen(Screen parent) {
|
||||
this.overlay = null;
|
||||
this.parent = parent;
|
||||
this.entities = new ArrayList<Entity>();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Background> i = Background.backgrounds.iterator();
|
||||
while (i.hasNext()) {
|
||||
Background b = i.next();
|
||||
b.update();
|
||||
}
|
||||
}
|
||||
|
||||
private void updateEntities() {
|
||||
Iterator<Entity> i = Entity.entities.iterator();
|
||||
while (i.hasNext()) {
|
||||
Entity e = i.next();
|
||||
e.update();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
package de.teamteamteam.spacescooter.utility;
|
||||
|
||||
public class GameConfig {
|
||||
|
||||
public static int windowHeight;
|
||||
public static int windowWidth;
|
||||
|
||||
}
|
Loading…
Reference in New Issue