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 --- src/BattleScene.java | 322 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 322 insertions(+) create mode 100644 src/BattleScene.java (limited to 'src/BattleScene.java') 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; + } + + } +} -- cgit v1.2.1