summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNao Pross <naopross@thearcway.org>2018-12-03 09:32:50 +0100
committerNao Pross <naopross@thearcway.org>2018-12-03 17:34:37 +0100
commit927436f1cc7630e840928ad23a45a77051c060f1 (patch)
tree45fdf06edaa7157c38debad74f8f6cdfd696e563
parentStart impl of TurnInfo widget to show mission details (diff)
downloadSubconscious-java-927436f1cc7630e840928ad23a45a77051c060f1.tar.gz
Subconscious-java-927436f1cc7630e840928ad23a45a77051c060f1.zip
Replace old BattleScene code with new impl from WorldScene
-rw-r--r--src/subconscious/graphics/BattleScene.java334
-rw-r--r--src/subconscious/graphics/WorldScene.java2
-rw-r--r--src/subconscious/graphics/widget/ActorInfo.java4
-rw-r--r--src/subconscious/graphics/widget/TerrainInfo.java4
-rw-r--r--src/subconscious/graphics/widget/TurnInfo.java2
5 files changed, 43 insertions, 303 deletions
diff --git a/src/subconscious/graphics/BattleScene.java b/src/subconscious/graphics/BattleScene.java
index dbbae1e..c8dad2e 100644
--- a/src/subconscious/graphics/BattleScene.java
+++ b/src/subconscious/graphics/BattleScene.java
@@ -2,341 +2,71 @@ package subconscious.graphics;
import subconscious.Game;
import subconscious.Actor;
-import subconscious.MapLoader;
-import subconscious.Tile;
-import java.util.ArrayList;
-import java.util.Collections;
+import subconscious.graphics.widget.ActorInfo;
+import subconscious.graphics.widget.TerrainInfo;
-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.Point;
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.AffineTransform;
-import java.awt.geom.NoninvertibleTransformException;
-
-import javax.swing.JFrame;
-import javax.swing.JPanel;
-import javax.swing.JButton;
@SuppressWarnings("serial")
-public class BattleScene extends MapScene implements ActionListener {
-
- // TODO: move out of graphics and rename Turn or TurnState
- private enum Mode {
- NONE, ATTACK, MOVE
- };
-
- private AffineTransform tx = new AffineTransform();
-
- // 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(Game g, String uniqueName) {
- super(g, uniqueName);
-
- // 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());
-
- 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");
+public class BattleScene extends MapScene {
- moveButton.addActionListener(this);
- attackButton.addActionListener(this);
- nextButton.addActionListener(this);
- passButton.addActionListener(this);
+ protected ActorInfo actorInfoWidget;
+ protected TerrainInfo terrainInfoWidget;
- bottomPanel.add(moveButton);
- bottomPanel.add(attackButton);
- bottomPanel.add(nextButton);
- bottomPanel.add(passButton);
-
- this.add(bottomPanel, BorderLayout.PAGE_END);
+ public BattleScene(Game game, String label) {
+ super(game, label);
}
@Override
- protected void build() {
+ public void build() {
+ this.actorInfoWidget = new ActorInfo("actorinfo");
+ this.addWidget(this.actorInfoWidget);
+ this.terrainInfoWidget = new TerrainInfo("terraininfo");
+ this.addWidget(this.terrainInfoWidget);
}
@Override
protected void render(Graphics2D g) {
super.render(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.canvasSize.width-100*this.guiSize, this.canvasSize.height-100*this.guiSize,
- 100*this.guiSize, 100*this.guiSize);
- g.fillRect(0, this.canvasSize.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.canvasSize.width-90*this.guiSize, this.canvasSize.height-80*this.guiSize);
- break;
- case WATER:
- g.drawString("Water", this.canvasSize.width-90*this.guiSize, this.canvasSize.height-80*this.guiSize);
- break;
- case MOUNTAIN:
- g.drawString("Mountain", this.canvasSize.width-90*this.guiSize, this.canvasSize.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.canvasSize.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.canvasSize.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(long deltaNanoTime) {
- ArrayList<Actor> 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;
+ public void update(long deltaNanoTime) {
+
}
@Override
public void mouseClicked(MouseEvent e) {
-
- 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");
- }
+ super.mouseClicked(e);
}
@Override
public void mouseMoved(MouseEvent e) {
- this.realX = e.getX();
- this.realY = e.getY();
-
- Point2D p = new Point2D.Double(e.getX(), e.getY());
-
- try {
- p = this.tx.inverseTransform(p, null);
- } catch (NoninvertibleTransformException ex) {}
+ super.mouseMoved(e);
- 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<Tile> path = this.map.getPath(this.selectedActor, x, y);
- Collections.reverse(path);
- for (int i=0; i<this.selectedActor.getSkills().agility; i++) {
- path.get(i).setSelected(true);
- if (i == path.size()-1) {
- break;
- }
- }
- }
- this.previousX = x;
- this.previousY = y;
- }
- } catch (ArrayIndexOutOfBoundsException ex) {
- // System.out.println("no tile clicked");
- } catch (NullPointerException ext) {
- // System.out.println("map non existent");
+ Point tileUnderCursor = this.tileAtCoordinates(e.getPoint());
+ if (tileUnderCursor == null) {
+ this.terrainInfoWidget.unsetTile();
+ return;
}
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO: rename Ui elements to ex. btn-move, btn-pass, ...
- if ("move".equals(e.getActionCommand()) && this.selectedActor != null) {
- this.map.clearSelected();
- this.mode = Mode.MOVE;
- this.lastActor = this.selectedActor;
- this.actorClicked = true;
- } else if ("pass".equals(e.getActionCommand())) {
- this.map.clearSelected();
- this.aiPlay();
- } else if ("attack".equals(e.getActionCommand()) && this.selectedActor != null) {
- this.map.clearSelected();
- int range = this.selectedActor.getWeapon().getRange();
- int x = this.selectedActor.getX();
- int y = this.selectedActor.getY();
- for (int r=-range; r <= range; r++) {
- for (int c=-range; c <= range; c++) {
- if (x+r >=0 &&
- x+r < this.map.getSize() &&
- y+c >= 0 &&
- y+c < this.map.getSize()) {
- this.map.getTile(x+r, y+c).setSelected(true);
- }
- }
+ // System.out.println(this.map.getTile(tileUnderCursor.x, tileUnderCursor.y).type);
+
+ // update ActorInfo widget
+ for (Actor actor : this.map.getActors()) {
+ this.actorInfoWidget.unsetActor();
+ if (actor.getPoint().equals(tileUnderCursor)) {
+ this.actorInfoWidget.setActor(actor);
+ break;
}
- 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;
}
+ // update tileType widget
+ this.terrainInfoWidget.setTile(this.map.getTile(tileUnderCursor));
}
-}
+} \ No newline at end of file
diff --git a/src/subconscious/graphics/WorldScene.java b/src/subconscious/graphics/WorldScene.java
index 995efd5..e56e6c0 100644
--- a/src/subconscious/graphics/WorldScene.java
+++ b/src/subconscious/graphics/WorldScene.java
@@ -31,7 +31,7 @@ public class WorldScene extends MapScene {
}
@Override
- public void render(Graphics2D g) {
+ protected void render(Graphics2D g) {
super.render(g);
}
diff --git a/src/subconscious/graphics/widget/ActorInfo.java b/src/subconscious/graphics/widget/ActorInfo.java
index d381cd2..230bd19 100644
--- a/src/subconscious/graphics/widget/ActorInfo.java
+++ b/src/subconscious/graphics/widget/ActorInfo.java
@@ -56,6 +56,10 @@ public class ActorInfo extends Widget {
this.observingActor = actor;
}
+ public Actor getActor() {
+ return this.observingActor;
+ }
+
public synchronized void unsetActor() {
this.observingActor = null;
}
diff --git a/src/subconscious/graphics/widget/TerrainInfo.java b/src/subconscious/graphics/widget/TerrainInfo.java
index 9e1ab90..a5d2d1a 100644
--- a/src/subconscious/graphics/widget/TerrainInfo.java
+++ b/src/subconscious/graphics/widget/TerrainInfo.java
@@ -49,6 +49,10 @@ public class TerrainInfo extends Widget {
this.observingTile = tile;
}
+ public Tile getTile() {
+ return this.observingTile;
+ }
+
public void unsetTile() {
this.observingTile = null;
}
diff --git a/src/subconscious/graphics/widget/TurnInfo.java b/src/subconscious/graphics/widget/TurnInfo.java
index f3472ee..e0845b7 100644
--- a/src/subconscious/graphics/widget/TurnInfo.java
+++ b/src/subconscious/graphics/widget/TurnInfo.java
@@ -1,6 +1,8 @@
package subconscious.graphics.widget;
import subconscious.Game;
+import subconscious.graphics.Palette;
+import subconscious.graphics.Fonts;
import java.awt.Graphics2D;