diff options
author | Nao Pross <naopross@thearcway.org> | 2018-11-20 18:12:20 +0100 |
---|---|---|
committer | Nao Pross <naopross@thearcway.org> | 2018-11-20 18:14:27 +0100 |
commit | fadc7e8923859616b9283380776eb81e683f0129 (patch) | |
tree | b565c75a68fa15735f8985e8c48f82c0f035a836 /src | |
parent | Update Makefile (diff) | |
download | Subconscious-java-fadc7e8923859616b9283380776eb81e683f0129.tar.gz Subconscious-java-fadc7e8923859616b9283380776eb81e683f0129.zip |
Refractor MapScene
Diffstat (limited to '')
-rw-r--r-- | src/subconscious/graphics/BattleScene.java | 4 | ||||
-rw-r--r-- | src/subconscious/graphics/MapEditorScene.java | 3 | ||||
-rw-r--r-- | src/subconscious/graphics/MapScene.java | 282 | ||||
-rw-r--r-- | src/subconscious/graphics/Scene.java | 8 |
4 files changed, 159 insertions, 138 deletions
diff --git a/src/subconscious/graphics/BattleScene.java b/src/subconscious/graphics/BattleScene.java index 1b5e990..2c0efa4 100644 --- a/src/subconscious/graphics/BattleScene.java +++ b/src/subconscious/graphics/BattleScene.java @@ -30,7 +30,7 @@ import javax.swing.JPanel; import javax.swing.JButton; @SuppressWarnings("serial") -public class BattleScene extends MapScene { +public class BattleScene extends MapScene implements ActionListener { private enum Mode { NONE, ATTACK, MOVE @@ -199,7 +199,6 @@ public class BattleScene extends MapScene { @Override public void mouseClicked(MouseEvent e) { - int tileSize = this.maxSize/10; Point2D p = new Point2D.Double(e.getX(), e.getY()); try { @@ -263,7 +262,6 @@ public class BattleScene extends MapScene { this.realX = e.getX(); this.realY = e.getY(); - int tileSize = this.maxSize/10; Point2D p = new Point2D.Double(e.getX(), e.getY()); try { diff --git a/src/subconscious/graphics/MapEditorScene.java b/src/subconscious/graphics/MapEditorScene.java index 0f3674c..c4a9e11 100644 --- a/src/subconscious/graphics/MapEditorScene.java +++ b/src/subconscious/graphics/MapEditorScene.java @@ -35,7 +35,7 @@ import javax.swing.JCheckBox; import java.lang.Integer; @SuppressWarnings("serial") -public class MapEditorScene extends MapScene { +public class MapEditorScene extends MapScene implements ActionListener { private Tile.Type placingTile = Tile.Type.CLEAR; private Actor placingActor = new Actor("", 0, false, 0); private ArrayList<Object> actorFields = new ArrayList<>(); @@ -84,7 +84,6 @@ public class MapEditorScene extends MapScene { @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); diff --git a/src/subconscious/graphics/MapScene.java b/src/subconscious/graphics/MapScene.java index 0fade19..80b577c 100644 --- a/src/subconscious/graphics/MapScene.java +++ b/src/subconscious/graphics/MapScene.java @@ -9,15 +9,10 @@ import java.awt.Canvas; import java.awt.Point; import java.awt.Graphics2D; import java.awt.BasicStroke; -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; @@ -25,99 +20,50 @@ import java.awt.geom.AffineTransform; import java.awt.geom.Point2D; import java.awt.geom.NoninvertibleTransformException; -import javax.swing.JFrame; -import javax.swing.JPanel; -import javax.swing.JButton; - -import java.io.FileReader; -import java.io.BufferedReader; -import java.io.IOException; /* MapScene * Specialization of Scene to render a Map. */ @SuppressWarnings("serial") -public abstract class MapScene extends Scene implements ActionListener { +public abstract class MapScene extends Scene { protected Map map; - - protected Point pan; - protected Point mouse; - protected int panX = 0; - protected int panY = 0; + // used for rendering + protected int tileSize; + protected int shorterCanvasLenght; + protected AffineTransform tx = new AffineTransform(); + + // used for panning + protected Point pan = new Point(0, 0); + protected Point mouse = new Point(0, 0); + // used for zooming protected boolean zooming = false; protected boolean panning = false; protected double zoom = 1; - protected int mouseX; - protected int mouseY; - - protected int maxSize; - - protected AffineTransform tx = new AffineTransform(); - public MapScene(Game g) { super(g); - } - - public MapScene() { - super(); - } - - protected void renderTiles(Graphics2D g) { - } - protected void renderActors(Graphics2D g) { - - } - - @Override - protected void render() { + // this.map = g.getCurrentMap(); if (this.canvasSize.width < this.canvasSize.height) { - this.maxSize = this.canvasSize.width; + this.shorterCanvasLenght = this.canvasSize.width; } else { - this.maxSize = this.canvasSize.height; - } - - if (this.map == null) { - return; + this.shorterCanvasLenght = this.canvasSize.height; } - Graphics2D g = (Graphics2D) this.buffer.getDrawGraphics(); - - // clear - g.setColor(Palette.BLACK); - g.fillRect(0, 0, this.canvasSize.width, this.canvasSize.height); - - // zoom and pan - if (this.zooming) { - Point2D p1 = new Point2D.Double(this.mouseX, this.mouseY); - Point2D p2 = null; - try { - p2 = tx.inverseTransform(p1, null); - } catch (NoninvertibleTransformException ex) {} - - this.tx.setToIdentity(); - this.tx.translate(p1.getX(), p1.getY()); - this.tx.scale(this.zoom, this.zoom); - this.tx.translate(-p2.getX(), -p2.getY()); - - this.zooming = false; - } - - if (this.panning) { - this.tx.translate(this.panX/this.zoom, this.panY/this.zoom); - this.panning = false; - this.panX = 0; - this.panY = 0; - } - - g.transform(this.tx); + this.tileSize = this.shorterCanvasLenght / 10; + } - final int tileSize = this.maxSize / 10; + // This constructor is used ONLY for a MapEditorScene which does not use a + // game object, and when the MapEditor will be removed this constructor + // will also be deleted + @Deprecated + public MapScene() { } + + protected void renderTiles(Graphics2D g) { for (Tile tile : this.map.getGrid()) { switch (tile.getType()) { case CLEAR: @@ -133,61 +79,144 @@ public abstract class MapScene extends Scene implements ActionListener { break; } - // base color - g.fillRect(tileSize * tile.x, tileSize * tile.y, tileSize, tileSize); + // tile background + g.fillRect( + this.tileSize * tile.x, + this.tileSize * tile.y, + this.tileSize, + this.tileSize + ); // selection - if (tile.isSelected()) { - g.setColor(Palette.ORANGE_T); - g.fillRect(tileSize * tile.x, tileSize * tile.y, tileSize, tileSize); - } + // if (tile.isSelected()) { + // g.setColor(Palette.ORANGE_T); + // g.fillRect(tileSize * tile.x, tileSize * tile.y, tileSize, tileSize); + // } // TODO: set as class variables the storkes // under cursor - if (tile.cursorOnIt()) { - BasicStroke oldStroke = (BasicStroke) g.getStroke(); - - g.setStroke(new BasicStroke(10.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL)); - g.setColor(Palette.ORANGE); - g.drawRect(tileSize * tile.x, tileSize * tile.y, tileSize, tileSize); - g.setStroke(oldStroke); - } + // if (tile.cursorOnIt()) { + // BasicStroke oldStroke = (BasicStroke) g.getStroke(); + + // g.setStroke(new BasicStroke(10.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL)); + // g.setColor(Palette.ORANGE); + // g.drawRect(tileSize * tile.x, tileSize * tile.y, tileSize, tileSize); + // g.setStroke(oldStroke); + // } + // tile border g.setPaint(Palette.BLACK); - g.drawRect(tileSize * tile.x, tileSize * tile.y, tileSize, tileSize); + g.drawRect( + this.tileSize * tile.x, + this.tileSize * tile.y, + this.tileSize, + this.tileSize + ); } + // draw a border around the map g.setColor(Palette.ORANGE); - int maxBound = this.map.getSize()*tileSize; + + int maxBound = this.map.getSize() * tileSize; g.drawRect(0, 0, maxBound, maxBound); - + } + + protected void renderActors(Graphics2D g) { // Draw actors g.setFont(g.getFont().deriveFont(g.getFont().getSize()*2.0F)); - for (Actor actor: this.map.getActors()) { + + for (Actor actor : this.map.getActors()) { if (actor.isEnemy()) { g.setColor(Palette.RED); } else { g.setColor(Palette.BLUE); } - g.fillRect(tileSize * actor.getX() + tileSize/5, tileSize * actor.getY() + tileSize/5, - 3*tileSize/5, 3*tileSize/5); + + g.fillRect( + this.tileSize * actor.getX() + this.tileSize/5, + this.tileSize * actor.getY() + this.tileSize/5, + 3 * this.tileSize / 5, + 3 * this.tileSize / 5 + ); + + // name g.setColor(Palette.BLACK); - g.drawString(actor.getName(), tileSize*actor.getX() + tileSize/5, tileSize*actor.getY()-tileSize/20); + g.drawString( + actor.getName(), + this.tileSize * actor.getX() + this.tileSize/5, + this.tileSize * actor.getY() - this.tileSize/20 + ); + + // helth bar g.setColor(Palette.RED); - g.fillRect(tileSize*actor.getX() + tileSize/5, tileSize*actor.getY()-tileSize/20+tileSize/30, - (tileSize*4/5), tileSize/10); + g.fillRect( + this.tileSize * actor.getX() + this.tileSize/5, + this.tileSize * actor.getY() - tileSize/20 + tileSize/30, + this.tileSize * 4/5, + this.tileSize/10 + ); + g.setColor(Palette.DARKGREEN); - g.fillRect(tileSize*actor.getX() + tileSize/5, tileSize*actor.getY()-tileSize/20+tileSize/30, - (tileSize*4/5)*actor.getHP()/10, tileSize/10); + g.fillRect( + this.tileSize * actor.getX() + this.tileSize/5, + this.tileSize * actor.getY() - tileSize/20 + tileSize/30, + this.tileSize * 4/5 * actor.getHP()/10, tileSize/10 + ); + } + } + + @Override + protected void render() { + // TODO: remove, map should never be NULL + // this is an old piece of code used for MapEditor + if (this.map == null) { + return; + } + + Graphics2D g = (Graphics2D) this.buffer.getDrawGraphics(); + + // clear canvas + g.setColor(Palette.BLACK); + g.fillRect(0, 0, this.canvasSize.width, this.canvasSize.height); + + // zoom + if (this.zooming) { + Point2D p1 = new Point2D.Double(this.mouse.x, this.mouse.y); + Point2D p2 = null; + + try { + p2 = tx.inverseTransform(p1, null); + } catch (NoninvertibleTransformException ex) { + ex.printStackTrace(); + } + + this.tx.setToIdentity(); + this.tx.translate(p1.getX(), p1.getY()); + this.tx.scale(this.zoom, this.zoom); + this.tx.translate(-p2.getX(), -p2.getY()); + + this.zooming = false; } + // pan + if (this.panning) { + this.tx.translate(this.pan.x/this.zoom, this.pan.y/this.zoom); + this.panning = false; + this.pan.move(0, 0); + } + + g.transform(this.tx); + + this.renderTiles(g); + this.renderActors(g); + // Restore absolute coordinates - AffineTransform invertTx = null; try { - invertTx = this.tx.createInverse(); - } catch (NoninvertibleTransformException ex) {} + g.transform(this.tx.createInverse()); + } catch (NoninvertibleTransformException ex) { + ex.printStackTrace(); + } - g.transform(invertTx); this.absoluteRender(g); g.dispose(); @@ -198,59 +227,54 @@ public abstract class MapScene extends Scene implements ActionListener { @Override public void keyPressed(KeyEvent e) { + super.keyPressed(e); + int keyCode = e.getKeyCode(); switch (keyCode) { case KeyEvent.VK_UP: - this.panY += 10; + this.pan.y += 10; break; case KeyEvent.VK_DOWN: - this.panY -= 10; + this.pan.y -= 10; break; case KeyEvent.VK_LEFT: - this.panX += 10; + this.pan.x += 10; break; case KeyEvent.VK_RIGHT: - this.panX -= 10; + this.pan.x -= 10; break; } } @Override public void mousePressed(MouseEvent e) { - this.mouseX = e.getX(); - this.mouseY = e.getY(); - } + super.mousePressed(e); - @Override - public void mouseClicked(MouseEvent e) { + this.mouse.move(e.getX(), e.getY()); } @Override public void mouseDragged(MouseEvent e) { - int x = e.getX(); - int y = e.getY(); - this.panX += x - this.mouseX; - this.panY += y - this.mouseY; - this.mouseX = x; - this.mouseY = y; - this.panning = true; - } + super.mouseDragged(e); - @Override - public void mouseMoved(MouseEvent e) { + this.pan.translate( + e.getX() - this.mouse.x, + e.getY() - this.mouse.y + ); + this.mouse.move(e.getX(), e.getY()); + this.panning = true; } @Override public void mouseWheelMoved(MouseWheelEvent e) { - this.mouseX = e.getX(); - this.mouseY = e.getY(); + super.mouseWheelMoved(e); + + this.mouse.move(e.getX(), e.getY()); + if (this.zoom > 0.3 || e.getWheelRotation() < 0) { this.zoom -= ((double) e.getWheelRotation())/5; } this.zooming = true; } - - @Override - public void actionPerformed(ActionEvent e) {} } diff --git a/src/subconscious/graphics/Scene.java b/src/subconscious/graphics/Scene.java index 0f678ef..ecb2917 100644 --- a/src/subconscious/graphics/Scene.java +++ b/src/subconscious/graphics/Scene.java @@ -10,18 +10,18 @@ import java.awt.BorderLayout; import java.awt.event.KeyListener; import java.awt.event.KeyEvent; import java.awt.event.MouseListener; -import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; +import java.awt.event.MouseEvent; import java.awt.event.MouseWheelListener; import java.awt.event.MouseWheelEvent; +import java.awt.event.ComponentListener; +import java.awt.event.ComponentEvent; + import java.awt.image.BufferStrategy; import javax.swing.JFrame; import javax.swing.JPanel; -import java.awt.event.ComponentEvent; -import java.awt.event.ComponentListener; - /* Scene * Abstract model class for all scenes of the game. Each scene is in a different |