summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorNao Pross <naopross@thearcway.org>2018-11-19 10:27:15 +0100
committerNao Pross <naopross@thearcway.org>2018-11-19 10:27:15 +0100
commit7a7007571699eb57bc96cef4cd053a5bd50468b3 (patch)
tree14b879b399417a59a340c6fda9daf4719a3dbebf /src
parentAdd Actor.SkillSet, remove useless members in various strucutres (diff)
downloadSubconscious-java-7a7007571699eb57bc96cef4cd053a5bd50468b3.tar.gz
Subconscious-java-7a7007571699eb57bc96cef4cd053a5bd50468b3.zip
Update Scene class (and some derivates) to be abstract
Diffstat (limited to 'src')
-rw-r--r--src/Actor.java2
-rw-r--r--src/BattleScene.java (renamed from src/Battle.java)16
-rw-r--r--src/GameWindow.java (renamed from src/Sub.java)30
-rw-r--r--src/MapEditor.java10
-rw-r--r--src/MapLoader.java14
-rw-r--r--src/MapScene.java27
-rw-r--r--src/Scene.java41
-rw-r--r--src/Subconscious.java6
8 files changed, 83 insertions, 63 deletions
diff --git a/src/Actor.java b/src/Actor.java
index b4d34aa..7b4635f 100644
--- a/src/Actor.java
+++ b/src/Actor.java
@@ -9,7 +9,7 @@ public class Actor {
private int y;
public class SkillSet {
- public int agility;
+ public int agility = 0;
// public int strenght;
// public int defense;
}
diff --git a/src/Battle.java b/src/BattleScene.java
index cde2734..1e4b633 100644
--- a/src/Battle.java
+++ b/src/BattleScene.java
@@ -22,15 +22,12 @@ import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
-// TODO: rename to BattleScene
-public class Battle extends MapScene {
+public class BattleScene extends MapScene {
private enum Mode {
NONE, ATTACK, MOVE
};
- // TODO: make sub observe this object to remove this reference
- private Sub main;
// TODO: refractor, make Points()
private int previousX = -1;
private int previousY = -1;
@@ -46,10 +43,10 @@ public class Battle extends MapScene {
private int realX = 0;
private int realY = 0;
- public Battle(JFrame frame, Sub sub) {
- super(frame, sub);
- this.main = sub;
+ public BattleScene(JFrame frame) {
+ super(frame);
+ // TODO: this should be handled in MapScene
MapLoader mapLoader = new MapLoader("../testmap.json");
this.map = mapLoader.getMap();
@@ -175,8 +172,9 @@ public class Battle extends MapScene {
}
if (enemyCounter == 0) {
- //win
- this.main.backToMenu();
+ // win
+ // TODO: reimplement on upper level
+ // this.main.backToMenu();
}
}
diff --git a/src/Sub.java b/src/GameWindow.java
index e365032..540d75b 100644
--- a/src/Sub.java
+++ b/src/GameWindow.java
@@ -10,15 +10,15 @@ import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
-public class Sub implements ActionListener {
+public class GameWindow implements ActionListener {
public static final Dimension WINDOW_SIZE = new Dimension(600, 400);
private JFrame frame;
private JPanel menu;
// TODO: remove map editor, start directly on Battle mode
- public Sub() {
- this.frame = new JFrame("Sub");
+ public GameWindow() {
+ this.frame = new JFrame("Subconscious");
this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.frame.setSize(WINDOW_SIZE);
@@ -53,19 +53,27 @@ public class Sub implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if ("editor".equals(e.getActionCommand())) {
- MapEditor test = new MapEditor(frame, this);
+ MapEditor mapEditor = new MapEditor(frame);
+
this.frame.getContentPane().removeAll();
this.frame.getContentPane().invalidate();
- this.frame.getContentPane().add(test);
+ this.frame.getContentPane().add(mapEditor);
this.frame.getContentPane().revalidate();
- test.start();
+
+ Thread mapEditorThread = new Thread(mapEditor);
+ mapEditorThread.start();
+
} else if ("battle".equals(e.getActionCommand())) {
- Battle test = new Battle(frame, this);
+ BattleScene battleScene = new BattleScene(frame);
+
this.frame.getContentPane().removeAll();
this.frame.getContentPane().invalidate();
- this.frame.getContentPane().add(test);
+ this.frame.getContentPane().add(battleScene);
this.frame.getContentPane().revalidate();
- test.start();
+
+ Thread battleThread = new Thread(battleScene);
+ battleThread.start();
+
} else if ("exit".equals(e.getActionCommand())) {
this.frame.setVisible(false);
this.frame.dispose();
@@ -78,8 +86,4 @@ public class Sub implements ActionListener {
this.frame.getContentPane().add(this.menu);
this.frame.getContentPane().revalidate();
}
-
- public static void main(String[] args) {
- Sub sub = new Sub();
- }
}
diff --git a/src/MapEditor.java b/src/MapEditor.java
index ee07d22..4ca5530 100644
--- a/src/MapEditor.java
+++ b/src/MapEditor.java
@@ -34,8 +34,8 @@ public class MapEditor extends MapScene {
private JFrame actorFrame;
private boolean nukeActor = false;
- public MapEditor(JFrame frame, Sub sub) {
- super(frame, sub);
+ public MapEditor(JFrame frame) {
+ super(frame);
this.setLayout(new BorderLayout());
JPanel bottomPanel = new JPanel();
@@ -69,6 +69,12 @@ public class MapEditor extends MapScene {
}
@Override
+ protected void update(int deltaTime) {}
+
+ @Override
+ protected void absoluteRender(Graphics2D g2d) {}
+
+ @Override
public void mouseClicked(MouseEvent e) {
int tileSize = this.maxSize/10;
Point2D p = new Point2D.Double(e.getX(), e.getY());
diff --git a/src/MapLoader.java b/src/MapLoader.java
index b55f2ac..4bd4e02 100644
--- a/src/MapLoader.java
+++ b/src/MapLoader.java
@@ -1,5 +1,6 @@
import java.lang.String;
+// TODO: use java.nio? http://tutorials.jenkov.com/java-nio/nio-vs-io.html
import java.io.File;
import java.io.PrintWriter;
import java.io.FileReader;
@@ -32,7 +33,7 @@ class MapLoader {
try {
fr = new FileReader(this.path);
bf = new BufferedReader(fr);
- // TODO: read all
+ // TODO: read all at once
while ((line = bf.readLine()) != null) {
mapText = mapText + line;
}
@@ -44,10 +45,13 @@ class MapLoader {
Gson gson = new Gson();
Map importMap = gson.fromJson(mapText, Map.class);
- //update map with new classes
- Map map = new Map(new Dimension(importMap.getSize(), importMap.getSize()));
- map.update(importMap.getActors(), importMap.getGrid());
- return map;
+ // TODO: ask @mafaldo why is there a copy?
+ // update map with new classes
+ // Map map = new Map(new Dimension(importMap.getSize(), importMap.getSize()));
+ // map.update(importMap.getActors(), importMap.getGrid());
+ // return map;
+
+ return importMap;
}
public void saveMap(Map map) {
diff --git a/src/MapScene.java b/src/MapScene.java
index 785698f..af86431 100644
--- a/src/MapScene.java
+++ b/src/MapScene.java
@@ -29,20 +29,25 @@ import java.io.IOException;
import com.google.gson.Gson;
-public class MapScene extends Scene implements ActionListener {
+public abstract class MapScene extends Scene implements ActionListener {
protected Map map;
+
protected int panX = 0;
protected int panY = 0;
+
protected boolean panning = false;
protected double zoom = 1;
+
protected int mouseX;
protected int mouseY;
+
protected boolean zooming = false;
+
protected int maxSize;
protected AffineTransform tx = new AffineTransform();
- public MapScene(JFrame frame, Sub sub) {
- super(frame, sub);
+ public MapScene(JFrame frame) {
+ super(frame);
//this.map = new Map(new Dimension(10, 10));
}
@@ -56,7 +61,10 @@ public class MapScene extends Scene implements ActionListener {
}
// TODO: negate if condition and return
- if (this.map != null) {
+ if (this.map == null) {
+ return;
+ }
+
Graphics2D g = (Graphics2D) this.buffer.getDrawGraphics();
//clear
g.setColor(Palette.BLACK);
@@ -169,18 +177,9 @@ public class MapScene extends Scene implements ActionListener {
g.dispose();
this.buffer.show();
- }
- }
-
- // TODO: make abstract
- protected void absoluteRender(Graphics2D g) {
-
}
- // TODO: remove this overridden method and leave abstract
- @Override
- protected void update(int deltaTime) {
- }
+ protected abstract void absoluteRender(Graphics2D g);
@Override
public void keyPressed(KeyEvent e) {
diff --git a/src/Scene.java b/src/Scene.java
index 08608cf..529a2f6 100644
--- a/src/Scene.java
+++ b/src/Scene.java
@@ -16,15 +16,19 @@ import javax.swing.JFrame;
import javax.swing.JPanel;
// TODO: implement pause (rendering) function
-public class Scene extends JPanel implements Runnable, KeyListener, MouseListener, MouseMotionListener, MouseWheelListener {
+public abstract class Scene extends JPanel
+ implements Runnable, KeyListener, MouseListener,
+ MouseMotionListener, MouseWheelListener {
+
+ protected final long DESIRED_FPS = 60;
+ protected final long DESIRED_DELTA_LOOP = (1000*1000*1000)/DESIRED_FPS;
+
// TODO: lowercase not constant values
protected int WIDTH;
protected int HEIGHT;
protected BufferStrategy buffer;
- // TODO: make final
- protected long desiredFPS = 60;
- // TODO: make final
- protected long desiredDeltaLoop = (1000*1000*1000)/desiredFPS;
+
+ // TODO: remove frame reference
protected JFrame frame;
protected boolean running;
protected Canvas canvas;
@@ -32,10 +36,9 @@ public class Scene extends JPanel implements Runnable, KeyListener, MouseListen
// TODO: make accessible from user settings
protected int guiSize = 2;
- // TODO: remove sub argument
- protected Scene(JFrame frame, Sub sub) {
+ protected Scene(JFrame frame) {
this.frame = frame;
- //getting window size
+ // getting window size
this.WIDTH = (int) this.frame.getSize().getWidth();
this.HEIGHT = (int) this.frame.getSize().getHeight();
@@ -50,14 +53,11 @@ public class Scene extends JPanel implements Runnable, KeyListener, MouseListen
this.canvas.addMouseWheelListener(this);
}
- // TODO: fix this weird memory shit
- public void start() {
+ public void initCanvasBuffer() {
this.canvas.createBufferStrategy(2);
this.buffer = this.canvas.getBufferStrategy();
- this.canvas.requestFocus();
- this.running = true;
- new Thread(this).start();
+ this.canvas.requestFocus();
}
public void run() {
@@ -67,6 +67,10 @@ public class Scene extends JPanel implements Runnable, KeyListener, MouseListen
long lastUpdateTime;
long deltaLoop;
+ this.initCanvasBuffer();
+
+ this.running = true;
+
while (running) {
beginLoopTime = System.nanoTime();
@@ -84,11 +88,11 @@ public class Scene extends JPanel implements Runnable, KeyListener, MouseListen
endLoopTime = System.nanoTime();
deltaLoop = endLoopTime - beginLoopTime;
- if (deltaLoop > this.desiredDeltaLoop) {
- // late => skip frame
+ if (deltaLoop > this.DESIRED_DELTA_LOOP) {
+ // TODO: late => skip frame
} else {
try {
- Thread.sleep((this.desiredDeltaLoop - deltaLoop)/(1000*1000));
+ Thread.sleep((this.DESIRED_DELTA_LOOP - deltaLoop)/(1000*1000));
} catch (InterruptedException e ) {
}
@@ -96,9 +100,8 @@ public class Scene extends JPanel implements Runnable, KeyListener, MouseListen
}
}
- // TODO: make abstract
- protected void render() {}
- protected void update(int deltaTime) {}
+ protected abstract void render();
+ protected abstract void update(int deltaNanoTime);
@Override
public void keyTyped(KeyEvent e) {}
diff --git a/src/Subconscious.java b/src/Subconscious.java
new file mode 100644
index 0000000..81bc25c
--- /dev/null
+++ b/src/Subconscious.java
@@ -0,0 +1,6 @@
+
+public class Subconscious {
+ public static void main(String[] args) {
+ GameWindow w = new GameWindow();
+ }
+} \ No newline at end of file