From d8111ef500cc17bb2610fa3e3b7cbca8d6061e53 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Tue, 25 Feb 2020 05:03:36 +0100 Subject: [PATCH] Add labels --- .../ynerant/leveleditor/game/GameFrame.java | 156 ++++++++++-------- .../ynerant/leveleditor/game/MobCancer.java | 2 +- 2 files changed, 91 insertions(+), 67 deletions(-) diff --git a/src/main/java/fr/ynerant/leveleditor/game/GameFrame.java b/src/main/java/fr/ynerant/leveleditor/game/GameFrame.java index f4a1505..96aaf20 100644 --- a/src/main/java/fr/ynerant/leveleditor/game/GameFrame.java +++ b/src/main/java/fr/ynerant/leveleditor/game/GameFrame.java @@ -8,67 +8,76 @@ import fr.ynerant.leveleditor.editor.CollidPanel; import javax.swing.*; import java.awt.*; -import java.awt.event.WindowEvent; -import java.awt.event.WindowListener; import java.util.List; import java.util.ArrayList; import java.util.Random; -public class GameFrame extends JFrame implements WindowListener { +public class GameFrame extends JFrame { private final Random RANDOM = new Random(); private final RawMap map; private int round = 0; - private int hp = 20; + private int hp = 5; private int reward = 20; private List mobs = new ArrayList<>(); private List towers = new ArrayList<>(); + private JLabel waveLabel; + private JLabel nbMobsLabel; + private JLabel hpLabel; + private JLabel rewardLabel; + private JLabel winLabel; + public GameFrame(RawMap map) { super("Jeu"); this.map = map; this.setSize(600, 600); this.setPreferredSize(getSize()); - this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setExtendedState(JFrame.MAXIMIZED_BOTH); this.setLocationRelativeTo(null); - this.addWindowListener(this); - this.setContentPane(new Grid()); + + JPanel root = new JPanel(); + root.setLayout(new BorderLayout()); + this.setContentPane(root); + + JPanel pane = new JPanel(); + pane.setLayout(new GridLayout(5, 1)); + root.add(pane, BorderLayout.SOUTH); + + Grid grid = new Grid(); + grid.setSize(map.getWidth(), map.getHeight()); + grid.setPreferredSize(grid.getSize()); + grid.setMinimumSize(grid.getSize()); + grid.setMaximumSize(grid.getSize()); + root.add(grid, BorderLayout.CENTER); + + waveLabel = new JLabel(); + pane.add(waveLabel); + + nbMobsLabel = new JLabel(); + pane.add(nbMobsLabel); + + hpLabel = new JLabel(); + pane.add(hpLabel); + + rewardLabel = new JLabel(); + pane.add(rewardLabel); + + winLabel = new JLabel(); + pane.add(winLabel); setVisible(true); new Thread(() -> { - while (true) { - if (mobs.isEmpty() && round < 4) { - ++round; - for (int i = 1; i <= RANDOM.nextInt(16) + 1; ++i) { - Mob mob = Mob.getRandomMob(); - mob.move(RANDOM.nextInt(getMap().getWidth() / 16), RANDOM.nextInt(getMap().getHeight() / 16)); - mobs.add(mob); - } - } + while (hp > 0 && round < 4) { + tick(); - for (Tower tower : towers) { - for (Mob mob : tower.filterDetectedMobs(mobs)) - mob.hit(); - } - - for (Mob mob : new ArrayList<>(mobs)) { - mob.tick(); - if (mob.getX() < 0 || mob.isDead()) { - mobs.remove(mob); - if (mob.getX() < 0) - --hp; - else - reward += mob.getReward(); - } - - try { - Thread.sleep(20L); - } catch (InterruptedException e) { - e.printStackTrace(); - } + try { + Thread.sleep(50L); + } catch (InterruptedException e) { + e.printStackTrace(); } } }).start(); @@ -80,7 +89,51 @@ public class GameFrame extends JFrame implements WindowListener { return map; } - private class Grid extends JPanel { + public void tick() { + if (mobs.isEmpty() && round < 4) { + ++round; + for (int i = 1; i <= RANDOM.nextInt(16) + 1; ++i) { + Mob mob = Mob.getRandomMob(); + mob.move(RANDOM.nextInt(getMap().getWidth() / 16), RANDOM.nextInt(getMap().getHeight() / 16)); + mobs.add(mob); + } + } + + if (round == 4 && mobs.isEmpty()) { + winLabel.setForeground(Color.green.darker()); + winLabel.setText("Vous avez gagné !"); + return; + } + + for (Tower tower : towers) { + for (Mob mob : tower.filterDetectedMobs(mobs)) + mob.hit(); + } + + for (Mob mob : new ArrayList<>(mobs)) { + mob.tick(); + if (mob.getX() < 0 || mob.isDead()) { + mobs.remove(mob); + if (mob.getX() < 0) { + --hp; + if (hp == 0) { + winLabel.setForeground(Color.red); + winLabel.setText("Vous avez perdu !"); + return; + } + } + else + reward += mob.getReward(); + } + } + + waveLabel.setText("Vague " + round); + nbMobsLabel.setText(mobs.size() + " mob" + (mobs.size() > 1 ? "s" : "") + " restant" + (mobs.size() > 1 ? "s" : "")); + hpLabel.setText("Points de vie : " + hp); + rewardLabel.setText("Butin : " + reward); + } + + private class Grid extends JComponent { @Override protected void paintComponent(Graphics _g) { Graphics2D g = (Graphics2D) _g; @@ -109,33 +162,4 @@ public class GameFrame extends JFrame implements WindowListener { repaint(); } } - - @Override - public void windowActivated(WindowEvent event) { - } - - @Override - public void windowClosed(WindowEvent event) { - } - - @Override - public void windowClosing(WindowEvent event) { - dispose(); - } - - @Override - public void windowDeactivated(WindowEvent event) { - } - - @Override - public void windowDeiconified(WindowEvent event) { - } - - @Override - public void windowIconified(WindowEvent event) { - } - - @Override - public void windowOpened(WindowEvent event) { - } } diff --git a/src/main/java/fr/ynerant/leveleditor/game/MobCancer.java b/src/main/java/fr/ynerant/leveleditor/game/MobCancer.java index 42954a5..fa46e8f 100644 --- a/src/main/java/fr/ynerant/leveleditor/game/MobCancer.java +++ b/src/main/java/fr/ynerant/leveleditor/game/MobCancer.java @@ -8,7 +8,7 @@ public class MobCancer extends Mob { @Override public long getSlowness() { - return 200; + return 100; } @Override