summaryrefslogtreecommitdiffstats
path: root/src/MapScene.java
diff options
context:
space:
mode:
authormafaldo <mafaldo@heavyhammer.home>2018-11-18 22:29:05 +0100
committermafaldo <mafaldo@heavyhammer.home>2018-11-18 22:29:05 +0100
commite2bcaecea59bdf3a4092efdc4b9624b3f83f6fec (patch)
tree90e70bb3954173c25e63b65bbc64bd78f8c854be /src/MapScene.java
downloadSubconscious-java-e2bcaecea59bdf3a4092efdc4b9624b3f83f6fec.tar.gz
Subconscious-java-e2bcaecea59bdf3a4092efdc4b9624b3f83f6fec.zip
initial commit
Diffstat (limited to '')
-rw-r--r--src/MapScene.java229
1 files changed, 229 insertions, 0 deletions
diff --git a/src/MapScene.java b/src/MapScene.java
new file mode 100644
index 0000000..41ad546
--- /dev/null
+++ b/src/MapScene.java
@@ -0,0 +1,229 @@
+import java.awt.Canvas;
+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;
+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;
+import com.google.gson.Gson;
+
+public 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);
+
+ //this.map = new Map(new Dimension(10, 10));
+ }
+
+ @Override
+ protected void render() {
+ if (this.WIDTH < this.HEIGHT) {
+ this.maxSize = WIDTH;
+ } else {
+ this.maxSize = HEIGHT;
+ }
+ if (this.map != null) {
+ Graphics2D g = (Graphics2D) this.buffer.getDrawGraphics();
+ //clear
+ g.setColor(Palette.BLACK);
+ g.fillRect(0, 0, this.WIDTH, this.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);
+
+ //draw tiles
+ int tileSize = this.maxSize / 10;
+ for (Tile tile : this.map.getGrid()) {
+ switch (tile.getType()) {
+ case CLEAR:
+ continue;
+ case GRASS:
+ g.setColor(Palette.GREEN);
+ break;
+ case WATER:
+ g.setColor(Palette.BLUE);
+ break;
+ case MOUNTAIN:
+ g.setColor(Palette.BROWN);
+ break;
+ }
+
+ g.fillRect(
+ tileSize * tile.getX(), tileSize * tile.getY(),
+ tileSize, tileSize);
+
+ if (tile.isSelected()) {
+ g.setColor(Palette.ORANGE_T);
+ //g.fillRect(tileSize * tile.getX() + tileSize/4, tileSize * tile.getY() + tileSize/4,
+ // tileSize/2, tileSize/2);
+ g.fillRect(
+ tileSize * tile.getX(), tileSize * tile.getY(),
+ tileSize, tileSize);
+ }
+
+ //TODO set as class variables the storkes
+ 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.getX(), tileSize * tile.getY(),
+ tileSize, tileSize);
+ g.setStroke(oldStroke);
+ }
+
+
+ g.setPaint(Palette.BLACK);
+ g.drawRect(
+ tileSize * tile.getX(), tileSize * tile.getY(),
+ tileSize, tileSize);
+ }
+
+ g.setColor(Palette.ORANGE);
+ int maxBound = this.map.getSize()*tileSize;
+ g.drawRect(0, 0, maxBound, maxBound);
+
+ //draw actors
+ g.setFont(g.getFont().deriveFont(g.getFont().getSize()*2.0F));
+ 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.setColor(Palette.BLACK);
+ g.drawString(actor.getName(), tileSize*actor.getX() + tileSize/5, tileSize*actor.getY()-tileSize/20);
+ g.setColor(Palette.RED);
+ g.fillRect(tileSize*actor.getX() + tileSize/5, tileSize*actor.getY()-tileSize/20+tileSize/30,
+ (tileSize*4/5), 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);
+ }
+
+
+ AffineTransform invertTx = null;
+ try {
+ invertTx = this.tx.createInverse();
+ } catch (NoninvertibleTransformException ex) {}
+
+ g.transform(invertTx);
+ this.absoluteRender(g);
+
+ g.dispose();
+ this.buffer.show();
+ }
+ }
+
+ protected void absoluteRender(Graphics2D g) {
+
+ }
+
+ @Override
+ protected void update(int deltaTime) {
+ }
+
+ @Override
+ public void keyPressed(KeyEvent e) {
+ int keyCode = e.getKeyCode();
+ switch (keyCode) {
+ case KeyEvent.VK_UP:
+ this.panY += 10;
+ break;
+ case KeyEvent.VK_DOWN:
+ this.panY -= 10;
+ break;
+ case KeyEvent.VK_LEFT:
+ this.panX += 10;
+ break;
+ case KeyEvent.VK_RIGHT:
+ this.panX -= 10;
+ break;
+ }
+ }
+
+ @Override
+ public void mousePressed(MouseEvent e) {
+ this.mouseX = e.getX();
+ this.mouseY = e.getY();
+ }
+
+ @Override
+ public void mouseClicked(MouseEvent e) {
+ }
+
+ @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;
+ }
+
+ @Override
+ public void mouseMoved(MouseEvent e) {
+ }
+
+ @Override
+ public void mouseWheelMoved(MouseWheelEvent e) {
+ this.mouseX = e.getX();
+ this.mouseY = 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) {}
+}