Changing dat visual system
This commit is contained in:
@@ -6,22 +6,15 @@ import de.siphalor.was.content.pack.FileContentPack;
|
||||
import de.siphalor.was.content.pack.JarContentPack;
|
||||
import de.siphalor.was.content.product.ProductManager;
|
||||
import de.siphalor.was.content.quest.QuestManager;
|
||||
import de.siphalor.was.state.GameState;
|
||||
import de.siphalor.was.state.NoopState;
|
||||
import de.siphalor.was.state.State;
|
||||
import de.siphalor.was.visual.layout.FixedAspectLayout;
|
||||
import de.siphalor.was.visual.layout.FulfillingLayout;
|
||||
import de.siphalor.was.visual.CanvasVisual;
|
||||
import de.siphalor.was.visual.Visual;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import java.awt.image.BufferStrategy;
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
|
||||
public class WhatAStorage {
|
||||
public static final String TITLE = "What a Storage";
|
||||
private static final WhatAStorage INSTANCE = new WhatAStorage();
|
||||
private static final double ASPECT_RATIO = 16.0 / 9.0;
|
||||
|
||||
public static WhatAStorage getInstance() {
|
||||
return INSTANCE;
|
||||
@@ -32,14 +25,9 @@ public class WhatAStorage {
|
||||
private final ProductManager productManager;
|
||||
private final QuestManager questManager;
|
||||
|
||||
private Frame frame;
|
||||
private Canvas canvas;
|
||||
private boolean fullScreen = false;
|
||||
private BufferStrategy bufferStrategy;
|
||||
private State state = new NoopState();
|
||||
private Visual visual;
|
||||
|
||||
private boolean scheduleStop;
|
||||
private long minTickTime = 1000 / 60;
|
||||
private boolean stopScheduled;
|
||||
|
||||
private WhatAStorage() {
|
||||
contentManager = new ContentManager();
|
||||
@@ -47,6 +35,8 @@ public class WhatAStorage {
|
||||
contentManager.addPack(mainPack);
|
||||
productManager = new ProductManager();
|
||||
questManager = new QuestManager();
|
||||
|
||||
visual = new CanvasVisual();
|
||||
}
|
||||
|
||||
public ContentManager getContentManager() {
|
||||
@@ -82,118 +72,18 @@ public class WhatAStorage {
|
||||
}
|
||||
|
||||
public void start() {
|
||||
frame = new Frame(TITLE);
|
||||
frame.addWindowListener(new WindowAdapter() {
|
||||
@Override
|
||||
public void windowClosing(WindowEvent e) {
|
||||
super.windowClosing(e);
|
||||
scheduleStop = true;
|
||||
}
|
||||
});
|
||||
frame.addComponentListener(new ComponentAdapter() {
|
||||
@Override
|
||||
public void componentResized(ComponentEvent e) {
|
||||
super.componentResized(e);
|
||||
state.onResize(canvas.getWidth(), canvas.getHeight());
|
||||
}
|
||||
});
|
||||
frame.addKeyListener(new KeyListener() {
|
||||
@Override
|
||||
public void keyTyped(KeyEvent e) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
if (e.getExtendedKeyCode() == KeyEvent.VK_F11) {
|
||||
GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0];
|
||||
frame.dispose();
|
||||
if (fullScreen) {
|
||||
System.out.println("End fullscreen");
|
||||
frame.setUndecorated(false);
|
||||
frame.setVisible(true);
|
||||
device.setFullScreenWindow(null);
|
||||
fullScreen = false;
|
||||
} else {
|
||||
System.out.println("Start fullscreen");
|
||||
frame.setUndecorated(true);
|
||||
frame.setVisible(true);
|
||||
device.setFullScreenWindow(frame);
|
||||
fullScreen = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyReleased(KeyEvent e) {
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
Panel panel = new Panel();
|
||||
panel.setBackground(Color.BLACK);
|
||||
frame.add(panel);
|
||||
frame.setLayout(new FulfillingLayout(panel));
|
||||
|
||||
canvas = new Canvas();
|
||||
panel.add(canvas);
|
||||
panel.setLayout(new FixedAspectLayout(ASPECT_RATIO, canvas));
|
||||
|
||||
frame.setVisible(true);
|
||||
frame.pack();
|
||||
frame.setLocationRelativeTo(null);
|
||||
|
||||
canvas.createBufferStrategy(2);
|
||||
bufferStrategy = canvas.getBufferStrategy();
|
||||
|
||||
changeState(new GameState(frame.getWidth(), frame.getHeight()));
|
||||
visual.start(this);
|
||||
}
|
||||
|
||||
public void run() {
|
||||
long time, timeDelta, tickBegin = System.nanoTime();
|
||||
|
||||
while (!scheduleStop) {
|
||||
time = System.nanoTime();
|
||||
timeDelta = time - tickBegin;
|
||||
tickBegin = System.nanoTime();
|
||||
|
||||
state.tick((int) (timeDelta / 1_000_000L));
|
||||
|
||||
do {
|
||||
do {
|
||||
Graphics graphics = bufferStrategy.getDrawGraphics();
|
||||
|
||||
state.render(graphics);
|
||||
|
||||
graphics.dispose();
|
||||
|
||||
} while(bufferStrategy.contentsRestored());
|
||||
|
||||
bufferStrategy.show();
|
||||
} while(bufferStrategy.contentsLost());
|
||||
|
||||
time = System.nanoTime();
|
||||
timeDelta = time - tickBegin;
|
||||
|
||||
if (timeDelta < minTickTime) {
|
||||
try {
|
||||
Thread.sleep((minTickTime - timeDelta) / 1_000_000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
visual.run();
|
||||
}
|
||||
|
||||
frame.setVisible(false);
|
||||
frame.dispose();
|
||||
public void scheduleStop() {
|
||||
stopScheduled = true;
|
||||
}
|
||||
|
||||
public void changeState(State newState) {
|
||||
if (state != null) {
|
||||
state.leave();
|
||||
}
|
||||
state = newState;
|
||||
state.enter();
|
||||
state.onResize(frame.getWidth(), frame.getHeight());
|
||||
public boolean isStopScheduled() {
|
||||
return stopScheduled;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,54 +0,0 @@
|
||||
package de.siphalor.was.state;
|
||||
|
||||
import de.siphalor.was.assets.AssetsManager;
|
||||
import de.siphalor.was.util.Util;
|
||||
import de.siphalor.was.visual.render.AnimatedTextureObject;
|
||||
import de.siphalor.was.visual.render.Renderable;
|
||||
import de.siphalor.was.visual.render.TextureObject;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class GameState extends State {
|
||||
private static final Image BG = AssetsManager.getImage("textures/bg.png");
|
||||
private static final Image BIN_OPEN = AssetsManager.getImage("textures/bin_open.png");
|
||||
private static final Image BIN_CLOSED = AssetsManager.getImage("textures/bin_closed.png");
|
||||
|
||||
Collection<Renderable> renderables = new LinkedList<>();
|
||||
|
||||
private boolean binOpen = false;
|
||||
|
||||
public GameState(int width, int height) {
|
||||
super(width, height);
|
||||
|
||||
renderables.add(new TextureObject(600, 700, 400, 300) {
|
||||
@Override
|
||||
public Image getTexture() {
|
||||
return binOpen ? BIN_OPEN : BIN_CLOSED;
|
||||
}
|
||||
});
|
||||
renderables.add(new AnimatedTextureObject(0, 500, 550, 400, 1000,
|
||||
AssetsManager.getImage("textures/conveyor_in_0.png"),
|
||||
AssetsManager.getImage("textures/conveyor_in_1.png"),
|
||||
AssetsManager.getImage("textures/conveyor_in_2.png")
|
||||
));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tick(int delta) {
|
||||
renderables.forEach(renderable -> renderable.tick(delta));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(Graphics graphics) {
|
||||
graphics.drawImage(BG, 0, 0, getWidth(), getHeight(), Util.dummyImageObserver());
|
||||
|
||||
renderables.forEach(renderable -> renderable.render(graphics, getWidth(), getHeight()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResize(int width, int height) {
|
||||
super.onResize(width, height);
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
package de.siphalor.was.state;
|
||||
|
||||
import de.siphalor.was.assets.AssetsManager;
|
||||
import de.siphalor.was.util.Util;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class MainMenuState extends State {
|
||||
private boolean bgDirty = true;
|
||||
|
||||
public MainMenuState(int width, int height) {
|
||||
super(width, height);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tick(int delta) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(Graphics graphics) {
|
||||
Image image = AssetsManager.getImage("assets/textures/bg.png");
|
||||
graphics.drawImage(image, 0, 0, getWidth(), getHeight(), Util.dummyImageObserver());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResize(int width, int height) {
|
||||
super.onResize(width, height);
|
||||
bgDirty = true;
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package de.siphalor.was.state;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class NoopState extends State {
|
||||
public NoopState() {
|
||||
super(0,0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tick(int delta) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(Graphics graphics) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
package de.siphalor.was.state;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public abstract class State {
|
||||
private int width, height;
|
||||
|
||||
public State(int width, int height) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
onResize(width, height);
|
||||
}
|
||||
|
||||
public int getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
public int getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
public void enter() {
|
||||
|
||||
}
|
||||
|
||||
public void leave() {
|
||||
|
||||
}
|
||||
|
||||
public abstract void tick(int delta);
|
||||
public abstract void render(Graphics graphics);
|
||||
|
||||
public void onResize(int width, int height) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
}
|
||||
122
src/main/java/de/siphalor/was/visual/CanvasVisual.java
Normal file
122
src/main/java/de/siphalor/was/visual/CanvasVisual.java
Normal file
@@ -0,0 +1,122 @@
|
||||
package de.siphalor.was.visual;
|
||||
|
||||
import de.siphalor.was.WhatAStorage;
|
||||
import de.siphalor.was.visual.canvas.layout.FixedAspectLayout;
|
||||
import de.siphalor.was.visual.canvas.layout.FulfillingLayout;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import java.awt.image.BufferStrategy;
|
||||
|
||||
public class CanvasVisual implements Visual {
|
||||
private static final double ASPECT_RATIO = 16.0 / 9.0;
|
||||
private WhatAStorage main;
|
||||
|
||||
private final Frame frame = new Frame(WhatAStorage.TITLE);
|
||||
private final Canvas canvas = new Canvas();
|
||||
private BufferStrategy bufferStrategy;
|
||||
private long minTickTime = 1000L / 60L;
|
||||
|
||||
private boolean fullScreen = false;
|
||||
|
||||
@Override
|
||||
public void start(WhatAStorage whatAStorage) {
|
||||
main = whatAStorage;
|
||||
frame.addWindowListener(new WindowAdapter() {
|
||||
@Override
|
||||
public void windowClosing(WindowEvent e) {
|
||||
super.windowClosing(e);
|
||||
main.scheduleStop();
|
||||
}
|
||||
});
|
||||
frame.addComponentListener(new ComponentAdapter() {
|
||||
@Override
|
||||
public void componentResized(ComponentEvent e) {
|
||||
super.componentResized(e);
|
||||
}
|
||||
});
|
||||
frame.addKeyListener(new KeyListener() {
|
||||
@Override
|
||||
public void keyTyped(KeyEvent e) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
if (e.getExtendedKeyCode() == KeyEvent.VK_F11) {
|
||||
GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0];
|
||||
frame.dispose();
|
||||
if (fullScreen) {
|
||||
System.out.println("End fullscreen");
|
||||
frame.setUndecorated(false);
|
||||
frame.setVisible(true);
|
||||
device.setFullScreenWindow(null);
|
||||
fullScreen = false;
|
||||
} else {
|
||||
System.out.println("Start fullscreen");
|
||||
frame.setUndecorated(true);
|
||||
frame.setVisible(true);
|
||||
device.setFullScreenWindow(frame);
|
||||
fullScreen = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyReleased(KeyEvent e) {
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
Panel panel = new Panel();
|
||||
panel.setBackground(Color.BLACK);
|
||||
frame.add(panel);
|
||||
frame.setLayout(new FulfillingLayout(panel));
|
||||
|
||||
panel.add(canvas);
|
||||
panel.setLayout(new FixedAspectLayout(ASPECT_RATIO, canvas));
|
||||
|
||||
frame.setVisible(true);
|
||||
frame.pack();
|
||||
frame.setLocationRelativeTo(null);
|
||||
|
||||
canvas.createBufferStrategy(2);
|
||||
bufferStrategy = canvas.getBufferStrategy();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
long time, timeDelta, tickBegin = System.nanoTime();
|
||||
|
||||
while (!main.isStopScheduled()) {
|
||||
time = System.nanoTime();
|
||||
timeDelta = time - tickBegin;
|
||||
tickBegin = System.nanoTime();
|
||||
|
||||
do {
|
||||
do {
|
||||
Graphics graphics = bufferStrategy.getDrawGraphics();
|
||||
|
||||
graphics.dispose();
|
||||
|
||||
} while(bufferStrategy.contentsRestored());
|
||||
|
||||
bufferStrategy.show();
|
||||
} while(bufferStrategy.contentsLost());
|
||||
|
||||
time = System.nanoTime();
|
||||
timeDelta = time - tickBegin;
|
||||
|
||||
if (timeDelta < minTickTime) {
|
||||
try {
|
||||
Thread.sleep((minTickTime - timeDelta) / 1_000_000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
frame.setVisible(false);
|
||||
frame.dispose();
|
||||
}
|
||||
}
|
||||
8
src/main/java/de/siphalor/was/visual/Visual.java
Normal file
8
src/main/java/de/siphalor/was/visual/Visual.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package de.siphalor.was.visual;
|
||||
|
||||
import de.siphalor.was.WhatAStorage;
|
||||
|
||||
public interface Visual {
|
||||
void start(WhatAStorage whatAStorage);
|
||||
void run();
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package de.siphalor.was.visual.layout;
|
||||
package de.siphalor.was.visual.canvas.layout;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package de.siphalor.was.visual.layout;
|
||||
package de.siphalor.was.visual.canvas.layout;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package de.siphalor.was.visual.render;
|
||||
package de.siphalor.was.visual.canvas.render;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package de.siphalor.was.visual.render;
|
||||
package de.siphalor.was.visual.canvas.render;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package de.siphalor.was.visual.render;
|
||||
package de.siphalor.was.visual.canvas.render;
|
||||
|
||||
import de.siphalor.was.util.Util;
|
||||
|
||||
Reference in New Issue
Block a user