summaryrefslogtreecommitdiffstats
path: root/src/Battle.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/Battle.java')
-rw-r--r--src/Battle.java304
1 files changed, 304 insertions, 0 deletions
diff --git a/src/Battle.java b/src/Battle.java
new file mode 100644
index 0000000..d7b63cc
--- /dev/null
+++ b/src/Battle.java
@@ -0,0 +1,304 @@
+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 Battle extends MapScene {
+
+ private enum Mode {
+ NONE, ATTACK, MOVE
+ };
+
+ private Sub main;
+ private int previousX = -1;
+ private int previousY = -1;
+ private Actor selectedActor;
+ private Actor lastActor;
+ private Mode mode;
+ private boolean actorClicked = false;
+ 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());
+
+ 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.getAgility()),
+ 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<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
+ this.main.backToMenu();
+ }
+ }
+
+ private void aiPlay() {
+ //TODO write AI
+ //controll 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 {
+ 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<Tile> path = this.map.getPath(this.selectedActor, x, y);
+ Collections.reverse(path);
+ for (int i=0; i<this.selectedActor.getAgility(); 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");
+ }
+ }
+
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ 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);
+ }
+ }
+ }
+ 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;
+ }
+
+ }
+}