From 7a7007571699eb57bc96cef4cd053a5bd50468b3 Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Mon, 19 Nov 2018 10:27:15 +0100 Subject: Update Scene class (and some derivates) to be abstract --- Makefile | 2 +- jar/Subconcious.jar | Bin 270058 -> 0 bytes src/Actor.java | 2 +- src/Battle.java | 324 -------------------------------------------------- src/BattleScene.java | 322 +++++++++++++++++++++++++++++++++++++++++++++++++ src/GameWindow.java | 89 ++++++++++++++ src/MapEditor.java | 10 +- src/MapLoader.java | 14 ++- src/MapScene.java | 27 ++--- src/Scene.java | 41 ++++--- src/Sub.java | 85 ------------- src/Subconscious.java | 6 + 12 files changed, 471 insertions(+), 451 deletions(-) delete mode 100644 jar/Subconcious.jar delete mode 100644 src/Battle.java create mode 100644 src/BattleScene.java create mode 100644 src/GameWindow.java delete mode 100644 src/Sub.java create mode 100644 src/Subconscious.java diff --git a/Makefile b/Makefile index 935fb32..3955d69 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -MAINCLASS := Sub +MAINCLASS := Subconscious JAVAC_ARGS := -Xlint:all .PHONY: all run pack build dirs clean diff --git a/jar/Subconcious.jar b/jar/Subconcious.jar deleted file mode 100644 index 13b9cc9..0000000 Binary files a/jar/Subconcious.jar and /dev/null differ 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/Battle.java deleted file mode 100644 index cde2734..0000000 --- a/src/Battle.java +++ /dev/null @@ -1,324 +0,0 @@ -import java.util.ArrayList; -import java.util.Collections; - -import java.awt.Canvas; -import java.awt.Graphics2D; -import java.awt.Dimension; -import java.awt.BorderLayout; -import java.awt.GridLayout; - -import java.awt.event.KeyEvent; -import java.awt.event.MouseEvent; -import java.awt.event.MouseWheelEvent; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; - -import java.awt.image.BufferStrategy; - -import java.awt.geom.Point2D; -import java.awt.geom.NoninvertibleTransformException; - -import javax.swing.JFrame; -import javax.swing.JPanel; -import javax.swing.JButton; - -// TODO: rename to BattleScene -public class Battle 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; - // TODO: what the fuck is the difference between these two? - private Actor selectedActor; - private Actor lastActor; - - private Mode mode; - - private boolean actorClicked = false; - - // TODO: these are the coordinates of the mouse => refractor names - private int realX = 0; - private int realY = 0; - - public Battle(JFrame frame, Sub sub) { - super(frame, sub); - this.main = sub; - - MapLoader mapLoader = new MapLoader("../testmap.json"); - this.map = mapLoader.getMap(); - - this.selectedActor = this.map.getNextActor(); - - this.setLayout(new BorderLayout()); - - // TODO: make a method buildUi() ? - JPanel bottomPanel = new JPanel(); - bottomPanel.setLayout(new GridLayout(1,4)); - - JButton moveButton = new JButton("Move"); - moveButton.setActionCommand("move"); - JButton attackButton = new JButton("Attack"); - attackButton.setActionCommand("attack"); - JButton nextButton = new JButton("Next"); - nextButton.setActionCommand("next"); - JButton passButton = new JButton("Pass"); - passButton.setActionCommand("pass"); - - moveButton.addActionListener(this); - attackButton.addActionListener(this); - nextButton.addActionListener(this); - passButton.addActionListener(this); - - bottomPanel.add(moveButton); - bottomPanel.add(attackButton); - bottomPanel.add(nextButton); - bottomPanel.add(passButton); - - this.add(bottomPanel, BorderLayout.PAGE_END); - this.add(this.canvas, BorderLayout.CENTER); - } - - @Override - protected void absoluteRender(Graphics2D g) { - //draw cursor - //g.setColor(Palette.BLUE); - //g.fillOval(this.realX-10*this.guiSize, this.realY-10*this.guiSize, 20*this.guiSize, 20*this.guiSize); - //g.setColor(Palette.ORANGE); - //g.drawOval(this.realX-10*this.guiSize, this.realY-10*this.guiSize, 20*this.guiSize, 20*this.guiSize); - - //draw panels - g.setColor(Palette.WHITE_T); - g.fillRect(this.WIDTH-100*this.guiSize, this.HEIGHT-100*this.guiSize, - 100*this.guiSize, 100*this.guiSize); - g.fillRect(0, this.HEIGHT-100*this.guiSize, 150*this.guiSize, 100*this.guiSize); - - g.setColor(Palette.BLACK); - g.setFont(g.getFont().deriveFont(12.0F*this.guiSize)); - Tile tile = null; - try{ - tile = this.map.getTile(this.previousX, this.previousY); - switch (tile.getType()) { - case CLEAR: - break; - case GRASS: - g.drawString("Grass", this.WIDTH-90*this.guiSize, this.HEIGHT-80*this.guiSize); - break; - case WATER: - g.drawString("Water", this.WIDTH-90*this.guiSize, this.HEIGHT-80*this.guiSize); - break; - case MOUNTAIN: - g.drawString("Mountain", this.WIDTH-90*this.guiSize, this.HEIGHT-80*this.guiSize); - break; - } - - g.setFont(g.getFont().deriveFont(8.0F*this.guiSize)); - if (this.actorClicked && this.lastActor != null) { - g.setColor(Palette.WHITE_T); - g.fillRect(0, 0, 100*this.guiSize, 150*this.guiSize); - g.fillRect(this.WIDTH-100*this.guiSize, 0, 100*this.guiSize, 100*this.guiSize); - - g.setColor(Palette.BLACK); - g.drawString(this.lastActor.getName(), 5*this.guiSize, 15*this.guiSize); - - g.drawString("HP", 5*this.guiSize, 30*this.guiSize); - g.setColor(Palette.RED); - g.fillRect(20*this.guiSize, 22*this.guiSize, 70*this.guiSize, 10*this.guiSize); - g.setColor(Palette.DARKGREEN); - g.fillRect(20*this.guiSize, 22*this.guiSize, 70*this.guiSize*this.lastActor.getHP()/10, 10*this.guiSize); - - g.setColor(Palette.BLACK); - g.drawString("Agility: " + Integer.toString(this.lastActor.getSkills().agility), - 5*this.guiSize, 45*this.guiSize); - } else if (this.actorClicked) { - g.setColor(Palette.WHITE_T); - g.fillRect(0, 0, 100*this.guiSize, 150*this.guiSize); - g.fillRect(this.WIDTH-100*this.guiSize, 0, 100*this.guiSize, 100*this.guiSize); - - g.setColor(Palette.BLACK); - g.drawString("None left", 5*this.guiSize, 15*this.guiSize); - } - } catch (ArrayIndexOutOfBoundsException ex) { - } catch (NullPointerException ext) { - System.out.println("map non existent"); - ext.printStackTrace(); - } - } - - @Override - protected void update(int deltaTime) { - ArrayList actorsToRemove = new ArrayList<>(); - int enemyCounter = 0; - - for (Actor actor : this.map.getActors()) { - if (!actor.isAlive()) { - actorsToRemove.add(actor); - } - if (actor.isEnemy()) { - enemyCounter++; - } - } - - for (Actor actor : actorsToRemove) { - this.map.removeActor(actor); - } - - if (selectedActor != null) { - this.map.setCursor(selectedActor.getX(), selectedActor.getY()); - } else { - this.map.setCursor(-1, -1); - } - - if (enemyCounter == 0) { - //win - this.main.backToMenu(); - } - } - - private void aiPlay() { - // TODO: write AI code - // give back control back to the player - this.map.resetActors(); - this.selectedActor = this.map.getNextActor(); - this.lastActor = this.selectedActor; - this.actorClicked = true; - } - - @Override - public void mouseClicked(MouseEvent e) { - - int tileSize = this.maxSize/10; - Point2D p = new Point2D.Double(e.getX(), e.getY()); - - try { - p = this.tx.inverseTransform(p, null); - } catch (NoninvertibleTransformException ex) {} - - try { - // find actor under cursor - int x = (int) (p.getX()/tileSize); - int y = (int) (p.getY()/tileSize); - Tile tile = this.map.getTile(x, y); - Actor actor = null; - - for (Actor i : this.map.getActors()) { - if (x == i.getX() && y == i.getY()) { - actor = i; - break; - } - } - - if (actor != null) { - this.actorClicked = true; - this.lastActor = actor; - } else { - this.actorClicked = false; - } - - if (this.mode == Mode.MOVE) { - if (tile.isSelected()) { - this.selectedActor.move(x, y); - this.mode = Mode.NONE; - this.map.clearSelected(); - if (this.selectedActor.getActionsLeft() <= 0) { - this.selectedActor = this.map.getNextActor(); - this.lastActor = this.selectedActor; - this.actorClicked = true; - } - } - } else if (this.mode == Mode.ATTACK) { - if (actor != null) { - this.selectedActor.hit(actor, this.map); - if (this.selectedActor.getActionsLeft() <= 0) { - this.selectedActor = this.map.getNextActor(); - this.lastActor = this.selectedActor; - this.actorClicked = true; - } - } - this.mode = Mode.NONE; - this.map.clearSelected(); - } - } catch (ArrayIndexOutOfBoundsException ex) { - System.out.println("no tile clicked"); - } catch (NullPointerException ext) { - //ext.printStackTrace(); - System.out.println("map non existent"); - } - } - - @Override - public void mouseMoved(MouseEvent e) { - this.realX = e.getX(); - this.realY = e.getY(); - int tileSize = this.maxSize/10; - Point2D p = new Point2D.Double(e.getX(), e.getY()); - try { - p = this.tx.inverseTransform(p, null); - } catch (NoninvertibleTransformException ex) {} - int x = (int) (p.getX()/tileSize); - int y = (int) (p.getY()/tileSize); - try { - Tile tile = this.map.getTile(x, y); - if (x != this.previousX || y != this.previousY) { - if (this.mode == Mode.MOVE) { - this.map.clearSelected(); - ArrayList path = this.map.getPath(this.selectedActor, x, y); - Collections.reverse(path); - for (int i=0; i=0 && - x+r < this.map.getSize() && - y+c >= 0 && - y+c < this.map.getSize()) { - this.map.getTile(x+r, y+c).setSelected(true); - } - } - } - this.mode = Mode.ATTACK; - } else if ("next".equals(e.getActionCommand())) { - this.map.clearSelected(); - this.selectedActor = this.map.getNextActor(); - this.lastActor = this.selectedActor; - this.actorClicked = true; - } - - } -} diff --git a/src/BattleScene.java b/src/BattleScene.java new file mode 100644 index 0000000..1e4b633 --- /dev/null +++ b/src/BattleScene.java @@ -0,0 +1,322 @@ +import java.util.ArrayList; +import java.util.Collections; + +import java.awt.Canvas; +import java.awt.Graphics2D; +import java.awt.Dimension; +import java.awt.BorderLayout; +import java.awt.GridLayout; + +import java.awt.event.KeyEvent; +import java.awt.event.MouseEvent; +import java.awt.event.MouseWheelEvent; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import java.awt.image.BufferStrategy; + +import java.awt.geom.Point2D; +import java.awt.geom.NoninvertibleTransformException; + +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.JButton; + +public class BattleScene extends MapScene { + + private enum Mode { + NONE, ATTACK, MOVE + }; + + // TODO: refractor, make Points() + private int previousX = -1; + private int previousY = -1; + // TODO: what the fuck is the difference between these two? + private Actor selectedActor; + private Actor lastActor; + + private Mode mode; + + private boolean actorClicked = false; + + // TODO: these are the coordinates of the mouse => refractor names + private int realX = 0; + private int realY = 0; + + public BattleScene(JFrame frame) { + super(frame); + + // TODO: this should be handled in MapScene + MapLoader mapLoader = new MapLoader("../testmap.json"); + this.map = mapLoader.getMap(); + + this.selectedActor = this.map.getNextActor(); + + this.setLayout(new BorderLayout()); + + // TODO: make a method buildUi() ? + JPanel bottomPanel = new JPanel(); + bottomPanel.setLayout(new GridLayout(1,4)); + + JButton moveButton = new JButton("Move"); + moveButton.setActionCommand("move"); + JButton attackButton = new JButton("Attack"); + attackButton.setActionCommand("attack"); + JButton nextButton = new JButton("Next"); + nextButton.setActionCommand("next"); + JButton passButton = new JButton("Pass"); + passButton.setActionCommand("pass"); + + moveButton.addActionListener(this); + attackButton.addActionListener(this); + nextButton.addActionListener(this); + passButton.addActionListener(this); + + bottomPanel.add(moveButton); + bottomPanel.add(attackButton); + bottomPanel.add(nextButton); + bottomPanel.add(passButton); + + this.add(bottomPanel, BorderLayout.PAGE_END); + this.add(this.canvas, BorderLayout.CENTER); + } + + @Override + protected void absoluteRender(Graphics2D g) { + //draw cursor + //g.setColor(Palette.BLUE); + //g.fillOval(this.realX-10*this.guiSize, this.realY-10*this.guiSize, 20*this.guiSize, 20*this.guiSize); + //g.setColor(Palette.ORANGE); + //g.drawOval(this.realX-10*this.guiSize, this.realY-10*this.guiSize, 20*this.guiSize, 20*this.guiSize); + + //draw panels + g.setColor(Palette.WHITE_T); + g.fillRect(this.WIDTH-100*this.guiSize, this.HEIGHT-100*this.guiSize, + 100*this.guiSize, 100*this.guiSize); + g.fillRect(0, this.HEIGHT-100*this.guiSize, 150*this.guiSize, 100*this.guiSize); + + g.setColor(Palette.BLACK); + g.setFont(g.getFont().deriveFont(12.0F*this.guiSize)); + Tile tile = null; + try{ + tile = this.map.getTile(this.previousX, this.previousY); + switch (tile.getType()) { + case CLEAR: + break; + case GRASS: + g.drawString("Grass", this.WIDTH-90*this.guiSize, this.HEIGHT-80*this.guiSize); + break; + case WATER: + g.drawString("Water", this.WIDTH-90*this.guiSize, this.HEIGHT-80*this.guiSize); + break; + case MOUNTAIN: + g.drawString("Mountain", this.WIDTH-90*this.guiSize, this.HEIGHT-80*this.guiSize); + break; + } + + g.setFont(g.getFont().deriveFont(8.0F*this.guiSize)); + if (this.actorClicked && this.lastActor != null) { + g.setColor(Palette.WHITE_T); + g.fillRect(0, 0, 100*this.guiSize, 150*this.guiSize); + g.fillRect(this.WIDTH-100*this.guiSize, 0, 100*this.guiSize, 100*this.guiSize); + + g.setColor(Palette.BLACK); + g.drawString(this.lastActor.getName(), 5*this.guiSize, 15*this.guiSize); + + g.drawString("HP", 5*this.guiSize, 30*this.guiSize); + g.setColor(Palette.RED); + g.fillRect(20*this.guiSize, 22*this.guiSize, 70*this.guiSize, 10*this.guiSize); + g.setColor(Palette.DARKGREEN); + g.fillRect(20*this.guiSize, 22*this.guiSize, 70*this.guiSize*this.lastActor.getHP()/10, 10*this.guiSize); + + g.setColor(Palette.BLACK); + g.drawString("Agility: " + Integer.toString(this.lastActor.getSkills().agility), + 5*this.guiSize, 45*this.guiSize); + } else if (this.actorClicked) { + g.setColor(Palette.WHITE_T); + g.fillRect(0, 0, 100*this.guiSize, 150*this.guiSize); + g.fillRect(this.WIDTH-100*this.guiSize, 0, 100*this.guiSize, 100*this.guiSize); + + g.setColor(Palette.BLACK); + g.drawString("None left", 5*this.guiSize, 15*this.guiSize); + } + } catch (ArrayIndexOutOfBoundsException ex) { + } catch (NullPointerException ext) { + System.out.println("map non existent"); + ext.printStackTrace(); + } + } + + @Override + protected void update(int deltaTime) { + ArrayList actorsToRemove = new ArrayList<>(); + int enemyCounter = 0; + + for (Actor actor : this.map.getActors()) { + if (!actor.isAlive()) { + actorsToRemove.add(actor); + } + if (actor.isEnemy()) { + enemyCounter++; + } + } + + for (Actor actor : actorsToRemove) { + this.map.removeActor(actor); + } + + if (selectedActor != null) { + this.map.setCursor(selectedActor.getX(), selectedActor.getY()); + } else { + this.map.setCursor(-1, -1); + } + + if (enemyCounter == 0) { + // win + // TODO: reimplement on upper level + // this.main.backToMenu(); + } + } + + private void aiPlay() { + // TODO: write AI code + // give back control back to the player + this.map.resetActors(); + this.selectedActor = this.map.getNextActor(); + this.lastActor = this.selectedActor; + this.actorClicked = true; + } + + @Override + public void mouseClicked(MouseEvent e) { + + int tileSize = this.maxSize/10; + Point2D p = new Point2D.Double(e.getX(), e.getY()); + + try { + p = this.tx.inverseTransform(p, null); + } catch (NoninvertibleTransformException ex) {} + + try { + // find actor under cursor + int x = (int) (p.getX()/tileSize); + int y = (int) (p.getY()/tileSize); + Tile tile = this.map.getTile(x, y); + Actor actor = null; + + for (Actor i : this.map.getActors()) { + if (x == i.getX() && y == i.getY()) { + actor = i; + break; + } + } + + if (actor != null) { + this.actorClicked = true; + this.lastActor = actor; + } else { + this.actorClicked = false; + } + + if (this.mode == Mode.MOVE) { + if (tile.isSelected()) { + this.selectedActor.move(x, y); + this.mode = Mode.NONE; + this.map.clearSelected(); + if (this.selectedActor.getActionsLeft() <= 0) { + this.selectedActor = this.map.getNextActor(); + this.lastActor = this.selectedActor; + this.actorClicked = true; + } + } + } else if (this.mode == Mode.ATTACK) { + if (actor != null) { + this.selectedActor.hit(actor, this.map); + if (this.selectedActor.getActionsLeft() <= 0) { + this.selectedActor = this.map.getNextActor(); + this.lastActor = this.selectedActor; + this.actorClicked = true; + } + } + this.mode = Mode.NONE; + this.map.clearSelected(); + } + } catch (ArrayIndexOutOfBoundsException ex) { + System.out.println("no tile clicked"); + } catch (NullPointerException ext) { + //ext.printStackTrace(); + System.out.println("map non existent"); + } + } + + @Override + public void mouseMoved(MouseEvent e) { + this.realX = e.getX(); + this.realY = e.getY(); + int tileSize = this.maxSize/10; + Point2D p = new Point2D.Double(e.getX(), e.getY()); + try { + p = this.tx.inverseTransform(p, null); + } catch (NoninvertibleTransformException ex) {} + int x = (int) (p.getX()/tileSize); + int y = (int) (p.getY()/tileSize); + try { + Tile tile = this.map.getTile(x, y); + if (x != this.previousX || y != this.previousY) { + if (this.mode == Mode.MOVE) { + this.map.clearSelected(); + ArrayList path = this.map.getPath(this.selectedActor, x, y); + Collections.reverse(path); + for (int i=0; i=0 && + x+r < this.map.getSize() && + y+c >= 0 && + y+c < this.map.getSize()) { + this.map.getTile(x+r, y+c).setSelected(true); + } + } + } + this.mode = Mode.ATTACK; + } else if ("next".equals(e.getActionCommand())) { + this.map.clearSelected(); + this.selectedActor = this.map.getNextActor(); + this.lastActor = this.selectedActor; + this.actorClicked = true; + } + + } +} diff --git a/src/GameWindow.java b/src/GameWindow.java new file mode 100644 index 0000000..540d75b --- /dev/null +++ b/src/GameWindow.java @@ -0,0 +1,89 @@ +// TODO: package + +import java.awt.Dimension; +import java.awt.GridLayout; + +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.JButton; + +import java.awt.event.ActionEvent; +import java.awt.event.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 GameWindow() { + this.frame = new JFrame("Subconscious"); + + this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + this.frame.setSize(WINDOW_SIZE); + this.frame.setPreferredSize(WINDOW_SIZE); + this.frame.setLocationRelativeTo(null); + + JPanel menu = new JPanel(); + menu.setLayout(new GridLayout(3, 1)); + JButton editor = new JButton("Editor"); + editor.setActionCommand("editor"); + JButton battle = new JButton("Battle"); + battle.setActionCommand("battle"); + JButton exit = new JButton("Exit"); + exit.setActionCommand("exit"); + + editor.addActionListener(this); + battle.addActionListener(this); + exit.addActionListener(this); + + menu.add(editor); + menu.add(battle); + menu.add(exit); + + this.menu = menu; + + this.frame.add(this.menu); + this.frame.pack(); + + this.frame.setVisible(true); + } + + @Override + public void actionPerformed(ActionEvent e) { + if ("editor".equals(e.getActionCommand())) { + MapEditor mapEditor = new MapEditor(frame); + + this.frame.getContentPane().removeAll(); + this.frame.getContentPane().invalidate(); + this.frame.getContentPane().add(mapEditor); + this.frame.getContentPane().revalidate(); + + Thread mapEditorThread = new Thread(mapEditor); + mapEditorThread.start(); + + } else if ("battle".equals(e.getActionCommand())) { + BattleScene battleScene = new BattleScene(frame); + + this.frame.getContentPane().removeAll(); + this.frame.getContentPane().invalidate(); + this.frame.getContentPane().add(battleScene); + this.frame.getContentPane().revalidate(); + + Thread battleThread = new Thread(battleScene); + battleThread.start(); + + } else if ("exit".equals(e.getActionCommand())) { + this.frame.setVisible(false); + this.frame.dispose(); + } + } + + public void backToMenu() { + this.frame.getContentPane().removeAll(); + this.frame.getContentPane().invalidate(); + this.frame.getContentPane().add(this.menu); + this.frame.getContentPane().revalidate(); + } +} 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(); @@ -68,6 +68,12 @@ public class MapEditor extends MapScene { this.add(this.canvas, BorderLayout.CENTER); } + @Override + protected void update(int deltaTime) {} + + @Override + protected void absoluteRender(Graphics2D g2d) {} + @Override public void mouseClicked(MouseEvent e) { int tileSize = this.maxSize/10; 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/Sub.java b/src/Sub.java deleted file mode 100644 index e365032..0000000 --- a/src/Sub.java +++ /dev/null @@ -1,85 +0,0 @@ -// TODO: package - -import java.awt.Dimension; -import java.awt.GridLayout; - -import javax.swing.JFrame; -import javax.swing.JPanel; -import javax.swing.JButton; - -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; - -public class Sub 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"); - - this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - this.frame.setSize(WINDOW_SIZE); - this.frame.setPreferredSize(WINDOW_SIZE); - this.frame.setLocationRelativeTo(null); - - JPanel menu = new JPanel(); - menu.setLayout(new GridLayout(3, 1)); - JButton editor = new JButton("Editor"); - editor.setActionCommand("editor"); - JButton battle = new JButton("Battle"); - battle.setActionCommand("battle"); - JButton exit = new JButton("Exit"); - exit.setActionCommand("exit"); - - editor.addActionListener(this); - battle.addActionListener(this); - exit.addActionListener(this); - - menu.add(editor); - menu.add(battle); - menu.add(exit); - - this.menu = menu; - - this.frame.add(this.menu); - this.frame.pack(); - - this.frame.setVisible(true); - } - - @Override - public void actionPerformed(ActionEvent e) { - if ("editor".equals(e.getActionCommand())) { - MapEditor test = new MapEditor(frame, this); - this.frame.getContentPane().removeAll(); - this.frame.getContentPane().invalidate(); - this.frame.getContentPane().add(test); - this.frame.getContentPane().revalidate(); - test.start(); - } else if ("battle".equals(e.getActionCommand())) { - Battle test = new Battle(frame, this); - this.frame.getContentPane().removeAll(); - this.frame.getContentPane().invalidate(); - this.frame.getContentPane().add(test); - this.frame.getContentPane().revalidate(); - test.start(); - } else if ("exit".equals(e.getActionCommand())) { - this.frame.setVisible(false); - this.frame.dispose(); - } - } - - public void backToMenu() { - this.frame.getContentPane().removeAll(); - this.frame.getContentPane().invalidate(); - this.frame.getContentPane().add(this.menu); - this.frame.getContentPane().revalidate(); - } - - public static void main(String[] args) { - Sub sub = new Sub(); - } -} 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 -- cgit v1.2.1